微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32驱动SST25VF程序

stm32驱动SST25VF程序

时间:11-29 来源:互联网 点击:
main.c:
#include "stm32f10x_lib.h"#include "hw_conf.h"#include"SST25V.h"void delay(int d);u8 Tx_Buffer[] = {0x72,0x62,0x02,0x78,0x60,0x96,0x86,0x79,0x85,0x24,0x36,0x48,0x56,0x68,0x70,0x75,0x88,0x24};u8 Rx_Buffer[BufferSize];u8 DataByte=0;int main(void){#ifdef DEBUGdebug();#endifSetup_System();    //系统启动SST25V_Init();    //Flash芯片初始化SST25V_ChipErase();   //擦除整个芯片空间DataByte = SST25V_ByteRead(0x000000);             //读取0x000000地址空间的数据信息DataByte = SST25V_ByteRead(0x000010);             //读取0x000000地址空间的数据信息DataByte = SST25V_ByteRead(0x000100);             //读取0x000100地址空间的数据信息DataByte = SST25V_ByteRead(0x001000);             //读取0x001000地址空间的数据信息DataByte = SST25V_ByteRead(0x010000);             //读取0x010000地址空间的数据信息DataByte = SST25V_ByteRead(0x100000);             //读取0x100000地址空间的数据信息DataByte = SST25V_ByteRead(0x600000);             //读取0x000000地址空间的数据信息DataByte = SST25V_ByteRead(0xF00000);             //读取0x600000地址空间的数据信息DataByte = SST25V_ByteRead(0xFFFFFF);             //读取0xFFFFFF地址空间的数据信息SST25V_AAI_WriteBytes(0x72, 0xFFF000,0x001000);   //向以0xFFF000为起始地址的数据空间写入0x001000个0x10--即向最后4KB空间写入0x10DataByte = SST25V_ByteRead(0x000000);             //读取0x000000地址空间的数据信息DataByte = SST25V_ByteRead(0x000001);             //读取0x000001地址空间的数据信息DataByte = SST25V_ByteRead(0x000002);             //读取0x000002地址空间的数据信息DataByte = SST25V_ByteRead(0xFFF000);             //读取0xFFF000地址空间的数据信息DataByte = SST25V_ByteRead(0xFFF0EE);             //读取0xFFF0EE地址空间的数据信息DataByte = SST25V_ByteRead(0xFFFEEE);             //读取0xFFFEEE地址空间的数据信息DataByte = SST25V_ByteRead(0xFFFFFF);             //读取0xFFFFFF地址空间的数据信息SST25V_SectorErase_4KByte(0xFFF000);              //擦除以0xFFF000为起始地址的4KB数据空间DataByte = SST25V_ByteRead(0x000000);             //读取0x000000地址空间的数据信息DataByte = SST25V_ByteRead(0xFFF000);             //读取0xFFF000地址空间的数据信息DataByte = SST25V_ByteRead(0x000001);             //读取0x000001地址空间的数据信息DataByte = SST25V_ByteRead(0x000002);             //读取0x000002地址空间的数据信息DataByte = SST25V_ByteRead(0xFFEEEE);             //读取0xFFEEEE地址空间的数据信息DataByte = SST25V_ByteRead(0xFFFFFF);             //读取0xFFFFFF地址空间的数据信息//SST25V_BufferWrite(Tx_Buffer,0xFFF000,16);      //将Tx_Buffer中的16个字节数据写入以0xFFF000为起始地址的数据空间SST25V_AAI_BufferProgram(Tx_Buffer,0xFFF000,16);  //采用AAI模式,将Tx_Buffer中的16个字节数据写入以0xFFF000为起始地址的数据空间SST25V_BufferRead(Rx_Buffer,0xFFF000,16);         //读出0xFFF000为起始地址的16字节数据,存放于Rx_Buffer中DataByte = SST25V_ByteRead(0xFFEEEE);             //读取0xFFEEEE地址空间的数据信息while (1){if(Rx_Buffer[0]==0x72){GPIO_WriteBit(GPIOC,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6)));delay(100);}if(Rx_Buffer[1]==0x62){GPIO_WriteBit(GPIOC,GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_7)));delay(100);}if(Rx_Buffer[2]==0x02){GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)));delay(100);}if(Rx_Buffer[3]==0x78){GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));delay(100);}}}void delay(int d){int i = 0;for ( ;d;--d)for (i = 0;i<10000;i++);}SST25VF.c:#include "stm32f10x_lib.h"#include "SST25V.h"void SST25V_Init(void)   //Flash芯片初始化{SST25V_CS_HIGH();                   //片选CS#管脚置高--不选中芯片SST25V_WP_HIGH();                   //保护WP#管脚置高--BPL的状态决定是否允许写状态寄存器SST25V_HOLD_HIGH();                 //保持HOLD#管脚置高SST25V_WriteStatusRegister(0x02);   //给状态寄存器写入数据02SST25V_DBSY();                      //禁止AAI模式时,SO脚输出忙状态}u8 SST25V_ByteRead(u32 ReadAddr)     //从ReadAddr地址读取一个字节数据{u8 Temp = 0;SST25V_CS_LOW();                                  //片选端置低--选中芯片SPI_Flash_SendByte(Read_Data);                    //发送读取数据命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);  //发送24位的读取数据地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);Temp = SPI_Flash_ReceiveByte();                   //接收读取到的数据SST25V_CS_HIGH();                                 //片选CS#管脚置高--不选中芯片return Temp;                                      //返回读取到的数据}void SST25V_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)//从ReadAddr地址开始读取NumByteToRead个字节数据{SST25V_CS_LOW();                                  //片选端置低--选中芯片SPI_Flash_SendByte(Read_Data);                    //发送读取数据命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);  //发送24位的读取数据起始地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);while(NumByteToRead--)                  //判断是否完成读取NumByteToRead个字节数据{*pBuffer = SPI_Flash_ReceiveByte();   //接受读取到的数据,并存储于pBuffer中pBuffer++;                            //pBuffer指针自增1}SST25V_CS_HIGH();                       //片选CS#管脚置高--不选中芯片}u8 SST25V_HighSpeedByteRead(u32 ReadAddr)  //从ReadAddr高速读取一个字节数据{u32 Temp = 0;SST25V_CS_LOW();                                   //片选端置低--选中芯片SPI_Flash_SendByte(HighSpeedReadData);             //发送快速读取数据命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);   //发送24位的读取数据地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);SPI_Flash_SendByte(Dummy_Byte);                    //发送虚拟字节数据Temp = SPI_Flash_ReceiveByte();                    //接收读取到的数据SST25V_CS_HIGH();                                  //片选CS#管脚置高--不选中芯片return Temp;                                       //返回读取到的数据}void SST25V_HighSpeedBufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)//从ReadAddr开始高速读取NumByteToRead个字节数据,并存储于pBuffer中{SST25V_CS_LOW();                                   //片选端置低--选中芯片SPI_Flash_SendByte(HighSpeedReadData);             //发送快速读取数据命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16);   //发送24位的读取数据起始地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);SPI_Flash_SendByte(Dummy_Byte);                    //发送虚拟字节数据while(NumByteToRead--)                             //判断是否完成读取NumByteToRead个字节数据{*pBuffer = SPI_Flash_ReceiveByte();              //接受读取到的数据,并存储于pBuffer中pBuffer++;                                       //pBuffer指针自增1}SST25V_CS_HIGH();                                  //片选CS#管脚置高--不选中芯片}u8 SPI_Flash_SendByte(u8 byte)   //SPI发送数据{while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);     //等待SPI1的Tx buffer为空SPI_SendData(SPI1, byte);                                  //发送数据字节bytewhile(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);    //等待SPI1完成数据接收return SPI_ReceiveData(SPI1);                              //返回读取到的数据}u8 SPI_Flash_ReceiveByte(void)    //SPI接收数据{return (SPI_Flash_SendByte(Dummy_Byte));    //发送虚拟字节,提供接收数据时钟}void SST25V_ByteWrite(u8 Byte, u32 WriteAddr)   //写入一个数据字节{SST25V_WriteEnable();                                //允许写入操作SST25V_CS_LOW();                                     //片选端置低--选中芯片SPI_Flash_SendByte(Byte_Program);                    //发送写数据命令SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16);    //发送24位的写入数据地址SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);SPI_Flash_SendByte(WriteAddr & 0xFF);SPI_Flash_SendByte(Byte);                            //发送要写入的数据SST25V_CS_HIGH();                                    //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                            //等待写操作完成}void SST25V_BufferWrite(u8 *pBuffer,u32 Addr,u16 BufferLength)//将pBuffer中的BufferLength个字节数据写入到以Addr为起始地址的区域{while(BufferLength--)                   //判断是否完成写入NumByteToRead个字节数据{SST25V_ByteWrite(*pBuffer,Addr);     //把当前pBuffer对应的数据,写入当前Addr中pBuffer++;                           //数据指针自增1Addr++;                              //写入地址自增1}}void SST25V_WriteBytes(u8 Byte, u32 WriteAddr,u32 ByteLength)  //以WriteAddr为起始地址,写入ByteLength个数据Byte(写入的是同一个数据Byte){while(ByteLength--)                  //判断是否完成写入NumByteToRead个字节数据{SST25V_ByteWrite(Byte,WriteAddr);    //向WriteAddr中写入数据字节ByteWriteAddr++;                         //写入地址自增1}}void SST25V_AAI_WriteBytes(u8 Byte, u32 Addr,u32 ByteLength)//以Addr为起始地址,用AAI模式,写入ByteLength个数据Byte(写入的是同一个数据Byte)ByteLength必须为偶数{u32 Length = 0;Length = (ByteLength/2)-1;                       //AAI模式每次写入两字节数据,因此ByteLength各字节,分成Length次写入SST25V_WriteEnable();                            //允许写入操作SST25V_CS_LOW();                                 //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);             //发送AAI模式写入命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);     //发送24位的写入数据地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(Byte);                       //写入第一个字节数据SPI_Flash_SendByte(Byte);                       //写入第二个字节数据SST25V_CS_HIGH();                               //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                       //等待写操作完成while(Length--)                                 //判断是否完成Length次写入{SST25V_CS_LOW();                              //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);          //发送AAI模式写入命令SPI_Flash_SendByte(Byte);                     //写入第一个字节数据SPI_Flash_SendByte(Byte);                     //写入第二个字节数据SST25V_CS_HIGH();                             //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                     //等待写操作完成}SST25V_WriteDisable();                         //写入完成,退出AAI模式}void SST25V_AAI_BufferProgram(u8 *pBuffer,u32 Addr,u16 BufferLength)//用AAI模式将pBuffer中的BufferLength个字节数据写入到以Addr为起始地址的区域,ByteLength必须为偶数{u16 Length = 0;Length = (BufferLength/2)-1;                     //AAI模式每次写入两字节数据,因此ByteLength各字节,分成Length次写入SST25V_WriteEnable();                           //允许写入操作SST25V_CS_LOW();                                //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);            //发送AAI模式写入数据命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);    //发送24位的写入数据起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(*pBuffer);                  //写入数据pBuffer++;                                     //数据指针加1SPI_Flash_SendByte(*pBuffer);                  //写入数据pBuffer++;                                     //数据指针加1SST25V_CS_HIGH();                              //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                      //等待写操作完成while(Length--)      //判断是否完成Length次写入{SST25V_CS_LOW();                         //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);     //发送AAI模式写入数据命令SPI_Flash_SendByte(*pBuffer);            //写入数据pBuffer++;                               //数据指针加1SPI_Flash_SendByte(*pBuffer);            //写入数据pBuffer++;                               //数据指针加1SST25V_CS_HIGH();                        //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                //等待写操作完成}SST25V_WriteDisable();                     //写入完成,退出AAI模式}void SST25V_AAI_WordProgram(u8 Byte1, u8 Byte2, u32 Addr)//在AAI模式下,以Addr为起始地址,分别写入Byte1和Byte2,必须和SST25V_AAI_WordsProgram()函数配套使用{SST25V_WriteEnable();                              //允许写入操作SST25V_CS_LOW();                                   //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);               //发送AAI模式写入数据命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);       //发送24位的写入数据起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(Byte1);                        //写入数据Byte1SPI_Flash_SendByte(Byte2);                        //写入数据Byte2SST25V_CS_HIGH();                                 //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                         //等待写操作完成}void SST25V_AAI_WordsProgram(u8 state,u8 Byte1, u8 Byte2) //在AAI模式下,继续写入Byte1和Byte2,必须和SST25V_AAI_WordProgram()函数配套使用,state==1退出AAI模式{SST25V_CS_LOW();                        //片选端置低--选中芯片SPI_Flash_SendByte(AAI_WordProgram);    //继续发送AAI模式写入数据命令SPI_Flash_SendByte(Byte1);              //写入数据Byte1SPI_Flash_SendByte(Byte2);              //写入数据Byte2SST25V_CS_HIGH();                       //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();               //等待写操作完成if(state==1)                            //state==1时,退出AAI模式{SST25V_WriteDisable();}}void SST25V_SectorErase_4KByte(u32 Addr)    //擦除4KB数据空间,Addr是擦除起始地址{SST25V_WriteEnable();                           //允许写入操作SST25V_CS_LOW();                                //片选端置低--选中芯片SPI_Flash_SendByte(SectorErace_4KB);            //发送擦除4KB数据空间命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);    //发送24位的擦除起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SST25V_CS_HIGH();                               //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                       //等待擦除操作完成}void SST25V_BlockErase_32KByte(u32 Addr)         //擦除32KB数据空间,Addr是擦除起始地址{SST25V_WriteEnable();                          //允许写入操作SST25V_CS_LOW();                               //片选端置低--选中芯片SPI_Flash_SendByte(BlockErace_32KB);           //发送擦除32KB数据空间命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);   //发送24位的擦除起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SST25V_CS_HIGH();                              //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                      //等待擦除操作完成}void SST25V_BlockErase_64KByte(u32 Addr)         //擦除64KB数据空间,Addr是擦除起始地址{SST25V_WriteEnable();                          //允许写入操作SST25V_CS_LOW();                               //片选端置低--选中芯片SPI_Flash_SendByte(BlockErace_64KB);           //发送擦除64KB数据空间命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16);   //发送24位的擦除起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SST25V_CS_HIGH();                              //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();                      //等待擦除操作完成}void SST25V_ChipErase(void)            //擦除整个芯片数据空间{SST25V_WriteEnable();                //允许写入操作SST25V_CS_LOW();                     //片选端置低--选中芯片SPI_Flash_SendByte(ChipErace);       //发送擦除整个芯片空间命令SST25V_CS_HIGH();                    //片选CS#管脚置高--不选中芯片SST25V_WaitForWriteEnd();            //等待擦除操作完成}u8 SST25V_ReadStatusRegister(void)    //读取状态寄存器{u8 StatusRegister = 0;SST25V_CS_LOW();                            //片选端置低--选中芯片SPI_Flash_SendByte(ReadStatusRegister);     //发送读取状态寄存器命令StatusRegister = SPI_Flash_ReceiveByte();   //接收状态寄存器数据SST25V_CS_HIGH();                           //片选CS#管脚置高--不选中芯片return StatusRegister;                      //返回读取到的状态寄存器数据}void SST25V_WriteEnable(void)          //允许对芯片写入操作{SST25V_CS_LOW();                     //片选端置低--选中芯片SPI_Flash_SendByte(WriteEnable);     //发送允许写入操作命令SST25V_CS_HIGH();                    //片选CS#管脚置高--不选中芯片}void SST25V_WriteDisable(void)         //禁止对芯片写入操作{SST25V_CS_LOW();                     //片选端置低--选中芯片SPI_Flash_SendByte(WriteDisable);    //发送禁止写入操作命令SST25V_CS_HIGH();                    //片选CS#管脚置高--不选中芯片}void SST25V_EnableWriteStatusRegister(void)        //允许改写状态寄存器{SST25V_CS_LOW();                                 //片选端置低--选中芯片SPI_Flash_SendByte(EnableWriteStatusRegister);   //发送允许改写状态寄存器命令SST25V_CS_HIGH();                                //片选CS#管脚置高--不选中芯片}void SST25V_WriteStatusRegister(u8 Byte)      //改写状态寄存器,向状态寄存器写入数据Byte{SST25V_EnableWriteStatusRegister();         //允许改写状态寄存器SST25V_CS_LOW();                            //片选端置低--选中芯片SPI_Flash_SendByte(WriteStatusRegister);    //发送改写状态寄存器命令SPI_Flash_SendByte(Byte);                   //向状态寄存器写入数据ByteSST25V_CS_HIGH();                           //片选CS#管脚置高--不选中芯片}void SST25V_WaitForWriteEnd(void)     //等待写入操作完成{u8 FLASH_Status = 0;SST25V_CS_LOW();                                 //片选端置低--选中芯片do{FLASH_Status = SST25V_ReadStatusRegister();    //获取状态寄存器值} while((FLASH_Status & 0x01) == SET);           //判断芯片是否处于忙碌状态,忙则继续读取状态寄存器的值SST25V_CS_HIGH();                                //片选CS#管脚置高--不选中芯片}u32 SST25V_ReadJedecID(void)       //读取JEDEC ID{u32 JEDECID = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;SST25V_CS_LOW();                                   //片选端置低--选中芯片SPI_Flash_SendByte(ReadJedec_ID);                  //发送读取JEDEC ID命令Temp0 = SPI_Flash_ReceiveByte();                   //接收第一字节数据Temp1 = SPI_Flash_ReceiveByte();                   //接收第二字节数据Temp2 = SPI_Flash_ReceiveByte();                   //接收第三字节数据SST25V_CS_HIGH();                                  //片选CS#管脚置高--不选中芯片JEDECID = (Temp0 < 16) | (Temp1 < 8) | Temp2;    //还原JEDEC IDreturn JEDECID;                                    //返回JEDEC ID}u16 SST25V_ReadManuID_DeviceID(u32 ReadManu_DeviceID_Addr) //读取ManufacturerID和DeviceID,ReadManu_DeviceID_Addr的状态决定第一个字节是DeviceID还是ManufacturerID{u16 ManuID_DeviceID = 0;              //存储16位的ID数据u8 ManufacturerID = 0,  DeviceID = 0;SST25V_CS_LOW();                      //片选端置低--选中芯片SPI_Flash_SendByte(ReadDeviceID);     //发送读取设备ID信息命令SPI_Flash_SendByte((ReadManu_DeviceID_Addr & 0xFF0000) >> 16);   //发送24位的地址字节,该地址为0和1,分别决定先输出的是ManufacturerID还是DeviceIDSPI_Flash_SendByte((ReadManu_DeviceID_Addr & 0xFF00) >> 8);SPI_Flash_SendByte(ReadManu_DeviceID_Addr & 0xFF);if(ReadManu_DeviceID_Addr==1)   //先输出DeviceID{DeviceID = SPI_Flash_ReceiveByte();         //接收DeviceIDManufacturerID = SPI_Flash_ReceiveByte();   //接收ManufacturerID}else   //先输出ManufacturerID{ManufacturerID = SPI_Flash_ReceiveByte();   //接收ManufacturerIDDeviceID = SPI_Flash_ReceiveByte();         //接收DeviceID}ManuID_DeviceID = ((ManufacturerID<8) | DeviceID);   //保存为16位的数据,高字节为ManufacturerID,低字节为DeviceIDSST25V_CS_HIGH();                                     //片选CS#管脚置高--不选中芯片return ManuID_DeviceID;                               //返回16位的ManuID_DeviceID,高字节为ManufacturerID,低字节为DeviceID}void SST25V_EBSY()    //允许AAI模式期间,SO脚输出忙状态{SST25V_CS_LOW();            //片选端置低--选中芯片SPI_Flash_SendByte(EBSY);   //发送允许AAI模式期间,SO脚输出忙状态命令SST25V_CS_HIGH();           //片选CS#管脚置高--不选中芯片}void SST25V_DBSY()            //禁止AAI模式期间,SO脚输出忙状态{SST25V_CS_LOW();            //片选端置低--选中芯片SPI_Flash_SendByte(DBSY);   //发送禁止AAI模式期间,SO脚输出忙状态命令SST25V_CS_HIGH();           //片选CS#管脚置高--不选中芯片}SST25VF.h:#include "stm32f10x_lib.h"#ifndef __SST25V_H#define __SST25V_H#define BufferSize                    (countof(Tx_Buffer)-1)#define countof(a)                    (sizeof(a) / sizeof(*(a)))#define SST25V_PageSize               256#define Dummy_Byte                    0xA5#define SST25V_CS_LOW()               GPIO_ResetBits(GPIOA, GPIO_Pin_4)#define SST25V_CS_HIGH()              GPIO_SetBits(GPIOA, GPIO_Pin_4)#define SST25V_WP_LOW()               GPIO_ResetBits(GPIOC, GPIO_Pin_0)#define SST25V_WP_HIGH()              GPIO_SetBits(GPIOC, GPIO_Pin_0)#define SST25V_HOLD_LOW()             GPIO_ResetBits(GPIOC, GPIO_Pin_1)#define SST25V_HOLD_HIGH()            GPIO_SetBits(GPIOC, GPIO_Pin_1)#define Read_Data                     0x03       //读取存储器数据#define HighSpeedReadData             0x0B       //快速读取存储器数据#define SectorErace_4KB               0x20       //扇区擦除#define BlockErace_32KB               0x52       //32KB块擦除#define BlockErace_64KB               0xD8       //64KB块擦除#define ChipErace                     0xC7       //片擦除#define Byte_Program                  0x02       //页面编程--写数据#define AAI_WordProgram               0xAD#define ReadStatusRegister            0x05       //读状态寄存器#define EnableWriteStatusRegister     0x50#define WriteStatusRegister           0x01       //写状态寄存器#define WriteEnable                   0x06       //写使能,设置状态寄存器#define WriteDisable                  0x04       //写禁止#define ReadDeviceID                  0xAB       //获取设备ID信息#define ReadJedec_ID                  0x9F       //JEDEC的ID信息#define EBSY                          0X70       //允许AAI模式期间,SO脚输出忙状态命令#define DBSY                          0X80       //禁止AAI模式期间,SO脚输出忙状态命令void SST25V_Init(void);   //Flash芯片初始化u8 SST25V_ByteRead(u32 ReadAddr);   //从ReadAddr地址读取一个字节数据void SST25V_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);  //从ReadAddr地址开始读取NumByteToRead个字节数据u8 SST25V_HighSpeedByteRead(u32 ReadAddr);  //从ReadAddr高速读取一个字节数据void SST25V_HighSpeedBufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead);//从ReadAddr开始高速读取NumByteToRead个字节数据,并存储于pBuffer中u8 SPI_Flash_SendByte(u8 byte);   //SPI发送数据u8 SPI_Flash_ReceiveByte(void);    //SPI接收数据void SST25V_ByteWrite(u8 Byte, u32 WriteAddr);   //写入一个数据字节void SST25V_BufferWrite(u8 *pBuffer,u32 Addr,u16 BufferLength);//将pBuffer中的BufferLength个字节数据写入到以Addr为起始地址的区域void SST25V_WriteBytes(u8 Byte, u32 WriteAddr,u32 ByteLength);  //以WriteAddr为起始地址,写入ByteLength个数据Byte(写入的是同一个数据Byte)void SST25V_AAI_WriteBytes(u8 Byte, u32 Addr,u32 ByteLength);  //以Addr为起始地址,用AAI模式,写入ByteLength个数据Byte(写入的是同一个数据Byte)ByteLength必须为偶数void SST25V_AAI_BufferProgram(u8 *pBuffer,u32 Addr,u16 BufferLength);  //用AAI模式将pBuffer中的BufferLength个字节数据写入到以Addr为起始地址的区域,ByteLength必须为偶数void SST25V_AAI_WordProgram(u8 Byte1, u8 Byte2, u32 Addr); //在AAI模式下,以Addr为起始地址,分别写入Byte1和Byte2,必须和SST25V_AAI_WordsProgram()函数配套使用void SST25V_AAI_WordsProgram(u8 state,u8 Byte1, u8 Byte2); //在AAI模式下,继续写入Byte1和Byte2,必须和SST25V_AAI_WordProgram()函数配套使用,state==1退出AAI模式void SST25V_SectorErase_4KByte(u32 Addr);    //擦除4KB数据空间,Addr是擦除起始地址void SST25V_BlockErase_32KByte(u32 Addr);         //擦除32KB数据空间,Addr是擦除起始地址void SST25V_BlockErase_64KByte(u32 Addr);         //擦除64KB数据空间,Addr是擦除起始地址void SST25V_ChipErase(void);            //擦除整个芯片数据空间u8 SST25V_ReadStatusRegister(void);    //读取状态寄存器void SST25V_WriteEnable(void);          //允许对芯片写入操作void SST25V_WriteDisable(void);         //禁止对芯片写入操作void SST25V_EnableWriteStatusRegister(void);        //允许改写状态寄存器void SST25V_WriteStatusRegister(u8 Byte);      //改写状态寄存器,向状态寄存器写入数据Bytevoid SST25V_WaitForWriteEnd(void);     //等待写入操作完成u32 SST25V_ReadJedecID(void);       //读取JEDEC IDu16 SST25V_ReadManuID_DeviceID(u32 ReadManu_DeviceID_Addr); //读取ManufacturerID和DeviceID,ReadManu_DeviceID_Addr的状态决定第一个字节是DeviceID还是ManufacturerIDvoid SST25V_EBSY();    //允许AAI模式期间,SO脚输出忙状态void SST25V_DBSY();            //禁止AAI模式期间,SO脚输出忙状态#endifhw_conf.c:#include"stm32f10x_lib.h"#include "hw_conf.h"ErrorStatus HSEStartUpStatus;void RCC_Configuration(void){RCC_DeInit();RCC_HSEConfig(RCC_HSE_ON);HSEStartUpStatus = RCC_WaitForHSEStartUp();if(HSEStartUpStatus == SUCCESS){FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);FLASH_SetLatency(FLASH_Latency_2);RCC_HCLKConfig(RCC_SYSCLK_Div1);RCC_PCLK2Config(RCC_HCLK_Div1);RCC_PCLK1Config(RCC_HCLK_Div2);RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);RCC_PLLCmd(ENABLE);while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);while(RCC_GetSYSCLKSource() != 0x08){}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);}void GPIO_Configuration(void){//PC4567分别接LED4,3,2,1,以便调试观察GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6 | GPIO_Pin_7;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);//PA5--SPI1_SCK    PA6--SPI1--MISO    PA7--SPI1--MOSIGPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;  //Configure SPI1 pins: NSS, SCK, MISO and MOSIGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);//PA4--SPI1_NSSGPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;  //Configure PA.4 as Output push-pull, used as Flash Chip selectGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);}void SPI_configuration()  //SPI1 configuration{SPI_InitTypeDef  SPI_InitStructure;SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;   //SPI设置为双线双向全双工SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                        //设置为主 SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;                    //SPI发送接收 8 位帧结构SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;                          //时钟悬空高SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;                         //数据捕获于第二个时钟沿SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                            //内部 NSS 信号有 SSI位控制SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;   //波特率预分频值为 4SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                   //数据传输从 MSB 位开始SPI_InitStructure.SPI_CRCPolynomial = 7;                             //定义了用于 CRC值计算的多项式 7SPI_Init(SPI1, &SPI_InitStructure);SPI_Cmd(SPI1, ENABLE);                                               //Enable SPI1}void NVIC_Configuration(void){#ifdef  VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);#elseNVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  #endif}void Setup_System(void){RCC_Configuration();GPIO_Configuration();SPI_configuration();NVIC_Configuration();}hw_conf.h:#ifndef   _hw_conf_H_#define   _hw_conf_H_extern void Setup_System(void);#endif

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top