STM32学习笔记---SST25VF016BSPI实验
扫描二维码
随时随地手机看文章
继做了CAN实验后,继续进行第16个实验-SST25VF016BSPI实验,此实验利用SPI1来调试2M的FLASH-SST25VF016B,通过向SST写入特定的一个字符串后,然后再读出来,最后一一判断,如果没有差错的话,就证明是正确的了,否则就写入出现错误了。正确了就在串口终端上显示字符串信息,还有板子上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);
}
//+++++++++++++字节写入+++++++++++++++++++++++
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();
}
//+++++++++字节读++++++++++++++++++++++++++++
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);
}
//++++++++++连续读++++++++++++++++++++++++++++
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();
}
//+++++++++++++连续写入+++++++++++++++++++++++
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个字节的块
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页的数据
Delay(0xffff);
SST25_R_BLOCK(0, SST25_buffer,4096);//读出0页的数据
a=0;
for(i=0; i
if(SST25_buffer[i]==TxBuffer1[i]) a=1;//和原始数据进行比较, 以判别是否读写正常
else {a=0; i=TxBufferSize1;}
}
if(a==1) {
GPIO_SetBits(GPIOD, GPIO_Pin_3); //读写正确LED1 亮
USART_TX(USART1,&SST25_buffer[0]);
}
}
以下为结果演示: