STM32學(xué)習(xí)筆記---SST25VF016BSPI實驗
繼做了CAN實驗后,繼續(xù)進(jìn)行第16個實驗-SST25VF016BSPI實驗,此實驗利用SPI1來調(diào)試2M的FLASH-SST25VF016B,通過向SST寫入特定的一個字符串后,然后再讀出來,最后一一判斷,如果沒有差錯的話,就證明是正確的了,否則就寫入出現(xiàn)錯誤了。正確了就在串口終端上顯示字符串信息,還有板子上LED3亮。
//++++++++++++寫使能 +++++++++++++++++
void wen(void)
{
Select_Flash();
SPI_Flash_SendByte(0x06);
NotSelect_Flash();
}
//+++++ +++++++ 寫禁止 ++++++++++++++++++
void wdis(void)
{
Select_Flash();
SPI_Flash_SendByte(0x04);
NotSelect_Flash();
wip();
}
//+++++++++++++++++++++++++++++++
void wsr(void)
{
Select_Flash();
SPI_Flash_SendByte(0x50);
NotSelect_Flash();
//Delay(0xff);
Select_Flash();
SPI_Flash_SendByte(0x01);
SPI_Flash_SendByte(0x00);
NotSelect_Flash();
wip();
}
//++++++++++ 忙檢測 +++++++++
void wip(void)
{
unsigned char a=1;
while((a&0x01)==1) a=rdsr();
}
//++++++++++++++++++++++++++++++++
unsigned char rdsr(void)
{
unsigned char busy;
Select_Flash();
SPI_Flash_SendByte(0x05);
busy = SPI_Flash_ReadByte();
NotSelect_Flash();
return(busy);
}
//+++++++++++++字節(jié)寫入+++++++++++++++++++++++
void bw1(unsigned long a1,unsigned char a2)
{
wsr();
wen();
Select_Flash();
SPI_Flash_SendByte(0x02);
SPI_Flash_SendByte((a1&0xffffff)>>16);
SPI_Flash_SendByte((a1&0xffff)>>8);
SPI_Flash_SendByte(a1&0xff);
SPI_Flash_SendByte(a2);
NotSelect_Flash();
wip();
wdis();
}
//+++++++++字節(jié)讀++++++++++++++++++++++++++++
unsigned char br1(unsigned long a1)
{
unsigned char i;
Select_Flash();
SPI_Flash_SendByte(0x03);
SPI_Flash_SendByte((a1&0xffffff)>>16);
SPI_Flash_SendByte((a1&0xffff)>>8);
SPI_Flash_SendByte(a1&0xff);
i=SPI_Flash_ReadByte();
NotSelect_Flash();
return(i);
}
//++++++++++連續(xù)讀++++++++++++++++++++++++++++
void r_sect(unsigned long a)
{
unsigned int i;
Select_Flash();
SPI_Flash_SendByte(0x03);
SPI_Flash_SendByte((a&0xffffff)>>16);
SPI_Flash_SendByte((a&0xffff)>>8);
SPI_Flash_SendByte(a&0xff);
for(i=0; i<816; i++){
SST25_buffer[i]=SPI_Flash_ReadByte();
}
NotSelect_Flash();
}
//+++++++++++++連續(xù)寫入+++++++++++++++++++++++
void w_sect(unsigned long a1)
{
unsigned int i=0,a2;
wsr();
wen();
Select_Flash();
SPI_Flash_SendByte(0xad);
SPI_Flash_SendByte((a1&0xffffff)>>16);
SPI_Flash_SendByte((a1&0xffff)>>8);
SPI_Flash_SendByte(a1&0xff);
SPI_Flash_SendByte(SST25_buffer[0]);
SPI_Flash_SendByte(SST25_buffer[1]);
NotSelect_Flash();
i=2;
while(i<4096){
a2=130;
while(a2>0) a2--;
Select_Flash();
SPI_Flash_SendByte(0xad);
SPI_Flash_SendByte(SST25_buffer[i++]);
SPI_Flash_SendByte(SST25_buffer[i++]);
NotSelect_Flash();
}
a2=130;
while(a2>0) a2--;
wdis();
Select_Flash();
wip();
}
//++++++++++塊讀++++++++++++++++++++++++++++
void SST25_R_BLOCK(uint32_t addr, u8 *readbuff, uint16_t BlockSize)
{
unsigned int i=0;
Select_Flash();
SPI_Flash_SendByte(0x03);
SPI_Flash_SendByte((addr&0xffffff)>>16);
SPI_Flash_SendByte((addr&0xffff)>>8);
SPI_Flash_SendByte(addr&0xff);
while(i
}
NotSelect_Flash();
}
//+++++++++++++塊寫入+++++++++++++++++++++++
void SST25_W_BLOCK(uint32_t addr, u8 *readbuff, uint16_t BlockSize)
{
unsigned int i=0,a2;
sect_clr(addr);//清除addr開始的4096個字節(jié)的塊
wsr();
wen();
Select_Flash();
SPI_Flash_SendByte(0xad);
SPI_Flash_SendByte((addr&0xffffff)>>16);
SPI_Flash_SendByte((addr&0xffff)>>8);
SPI_Flash_SendByte(addr&0xff);
SPI_Flash_SendByte(readbuff[0]);
SPI_Flash_SendByte(readbuff[1]);
NotSelect_Flash();
i=2;
while(i
while(a2>0) a2--;
Select_Flash();
SPI_Flash_SendByte(0xad);
SPI_Flash_SendByte(readbuff[i++]);
SPI_Flash_SendByte(readbuff[i++]);
NotSelect_Flash();
}
a2=130;
while(a2>0) a2--;
wdis();
Select_Flash();
wip();
}
//++++++++++++++++++++++++++++++++
void sect_clr(unsigned long a1)
{
//wen();
wsr();
wen();
Select_Flash();
SPI_Flash_SendByte(0x20);
SPI_Flash_SendByte((a1&0xffffff)>>16);//addh
SPI_Flash_SendByte((a1&0xffff)>>8);//addl
SPI_Flash_SendByte(a1&0xff);//wtt
NotSelect_Flash();
wip();
}
//========讀器件廠家信息
void FlashReadID(void)
{
Select_Flash();
SPI_Flash_SendByte(0x90);
SPI_Flash_SendByte(0x00);
SPI_Flash_SendByte(0x00);
SPI_Flash_SendByte(0x00);
fac_id= SPI_Flash_ReadByte();//BFH: SST
dev_id= SPI_Flash_ReadByte();//41H:SST25VF016B
NotSelect_Flash();
}
**************************
//測試SST25VF016B-SPI
void TEST_SST25VF016BSPI(void)
{
uint8_t a=0;
uint16_t i=0;
for(i=0; i
SST25_W_BLOCK(0, SST25_buffer,4096);//寫入0頁的數(shù)據(jù)
Delay(0xffff);
SST25_R_BLOCK(0, SST25_buffer,4096);//讀出0頁的數(shù)據(jù)
a=0;
for(i=0; i
if(SST25_buffer[i]==TxBuffer1[i]) a=1;//和原始數(shù)據(jù)進(jìn)行比較, 以判別是否讀寫正常
else {a=0; i=TxBufferSize1;}
}
if(a==1) {
GPIO_SetBits(GPIOD, GPIO_Pin_3); //讀寫正確LED1 亮
USART_TX(USART1,&SST25_buffer[0]);
}
}
以下為結(jié)果演示: