stm32驅(qū)動SST25VF程序 作者: 時間:2016-11-29 來源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對面交流海量資料庫查詢 收藏 void SST25V_WriteBytes(u8 Byte, u32 WriteAddr,u32 ByteLength) //以WriteAddr為起始地址,寫入ByteLength個數(shù)據(jù)Byte(寫入的是同一個數(shù)據(jù)Byte){while(ByteLength--) //判斷是否完成寫入NumByteToRead個字節(jié)數(shù)據(jù){SST25V_ByteWrite(Byte,WriteAddr); //向WriteAddr中寫入數(shù)據(jù)字節(jié)ByteWriteAddr++; //寫入地址自增1}}void SST25V_AAI_WriteBytes(u8 Byte, u32 Addr,u32 ByteLength)//以Addr為起始地址,用AAI模式,寫入ByteLength個數(shù)據(jù)Byte(寫入的是同一個數(shù)據(jù)Byte)ByteLength必須為偶數(shù){u32 Length = 0;Length = (ByteLength/2)-1; //AAI模式每次寫入兩字節(jié)數(shù)據(jù),因此ByteLength各字節(jié),分成Length次寫入SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //發(fā)送AAI模式寫入命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送24位的寫入數(shù)據(jù)地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(Byte); //寫入第一個字節(jié)數(shù)據(jù)SPI_Flash_SendByte(Byte); //寫入第二個字節(jié)數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成while(Length--) //判斷是否完成Length次寫入{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //發(fā)送AAI模式寫入命令SPI_Flash_SendByte(Byte); //寫入第一個字節(jié)數(shù)據(jù)SPI_Flash_SendByte(Byte); //寫入第二個字節(jié)數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成}SST25V_WriteDisable(); //寫入完成,退出AAI模式}void SST25V_AAI_BufferProgram(u8 *pBuffer,u32 Addr,u16 BufferLength)//用AAI模式將pBuffer中的BufferLength個字節(jié)數(shù)據(jù)寫入到以Addr為起始地址的區(qū)域,ByteLength必須為偶數(shù){u16 Length = 0;Length = (BufferLength/2)-1; //AAI模式每次寫入兩字節(jié)數(shù)據(jù),因此ByteLength各字節(jié),分成Length次寫入SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //發(fā)送AAI模式寫入數(shù)據(jù)命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送24位的寫入數(shù)據(jù)起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(*pBuffer); //寫入數(shù)據(jù)pBuffer++; //數(shù)據(jù)指針加1SPI_Flash_SendByte(*pBuffer); //寫入數(shù)據(jù)pBuffer++; //數(shù)據(jù)指針加1SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成while(Length--) //判斷是否完成Length次寫入{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //發(fā)送AAI模式寫入數(shù)據(jù)命令SPI_Flash_SendByte(*pBuffer); //寫入數(shù)據(jù)pBuffer++; //數(shù)據(jù)指針加1SPI_Flash_SendByte(*pBuffer); //寫入數(shù)據(jù)pBuffer++; //數(shù)據(jù)指針加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()函數(shù)配套使用{SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //發(fā)送AAI模式寫入數(shù)據(jù)命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送24位的寫入數(shù)據(jù)起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SPI_Flash_SendByte(Byte1); //寫入數(shù)據(jù)Byte1SPI_Flash_SendByte(Byte2); //寫入數(shù)據(jù)Byte2SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成}void SST25V_AAI_WordsProgram(u8 state,u8 Byte1, u8 Byte2) //在AAI模式下,繼續(xù)寫入Byte1和Byte2,必須和SST25V_AAI_WordProgram()函數(shù)配套使用,state==1退出AAI模式{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(AAI_WordProgram); //繼續(xù)發(fā)送AAI模式寫入數(shù)據(jù)命令SPI_Flash_SendByte(Byte1); //寫入數(shù)據(jù)Byte1SPI_Flash_SendByte(Byte2); //寫入數(shù)據(jù)Byte2SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成if(state==1) //state==1時,退出AAI模式{SST25V_WriteDisable();}}void SST25V_SectorErase_4KByte(u32 Addr) //擦除4KB數(shù)據(jù)空間,Addr是擦除起始地址{SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(SectorErace_4KB); //發(fā)送擦除4KB數(shù)據(jù)空間命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送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數(shù)據(jù)空間,Addr是擦除起始地址{SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(BlockErace_32KB); //發(fā)送擦除32KB數(shù)據(jù)空間命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送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數(shù)據(jù)空間,Addr是擦除起始地址{SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(BlockErace_64KB); //發(fā)送擦除64KB數(shù)據(jù)空間命令SPI_Flash_SendByte((Addr & 0xFF0000) >> 16); //發(fā)送24位的擦除起始地址SPI_Flash_SendByte((Addr & 0xFF00) >> 8);SPI_Flash_SendByte(Addr & 0xFF);SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待擦除操作完成}void SST25V_ChipErase(void) //擦除整個芯片數(shù)據(jù)空間{SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(ChipErace); //發(fā)送擦除整個芯片空間命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待擦除操作完成}u8 SST25V_ReadStatusRegister(void) //讀取狀態(tài)寄存器{u8 StatusRegister = 0;SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(ReadStatusRegister); //發(fā)送讀取狀態(tài)寄存器命令StatusRegister = SPI_Flash_ReceiveByte(); //接收狀態(tài)寄存器數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片return StatusRegister; //返回讀取到的狀態(tài)寄存器數(shù)據(jù)}void SST25V_WriteEnable(void) //允許對芯片寫入操作{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(WriteEnable); //發(fā)送允許寫入操作命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}void SST25V_WriteDisable(void) //禁止對芯片寫入操作{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(WriteDisable); //發(fā)送禁止寫入操作命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}void SST25V_EnableWriteStatusRegister(void) //允許改寫狀態(tài)寄存器{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(EnableWriteStatusRegister); //發(fā)送允許改寫狀態(tài)寄存器命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}void SST25V_WriteStatusRegister(u8 Byte) //改寫狀態(tài)寄存器,向狀態(tài)寄存器寫入數(shù)據(jù)Byte{SST25V_EnableWriteStatusRegister(); //允許改寫狀態(tài)寄存器SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(WriteStatusRegister); //發(fā)送改寫狀態(tài)寄存器命令SPI_Flash_SendByte(Byte); //向狀態(tài)寄存器寫入數(shù)據(jù)ByteSST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}void SST25V_WaitForWriteEnd(void) //等待寫入操作完成{u8 FLASH_Status = 0;SST25V_CS_LOW(); //片選端置低--選中芯片do{FLASH_Status = SST25V_ReadStatusRegister(); //獲取狀態(tài)寄存器值} while((FLASH_Status & 0x01) == SET); //判斷芯片是否處于忙碌狀態(tài),忙則繼續(xù)讀取狀態(tài)寄存器的值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); //發(fā)送讀取JEDEC ID命令Temp0 = SPI_Flash_ReceiveByte(); //接收第一字節(jié)數(shù)據(jù)Temp1 = SPI_Flash_ReceiveByte(); //接收第二字節(jié)數(shù)據(jù)Temp2 = SPI_Flash_ReceiveByte(); //接收第三字節(jié)數(shù)據(jù)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的狀態(tài)決定第一個字節(jié)是DeviceID還是ManufacturerID{u16 ManuID_DeviceID = 0; //存儲16位的ID數(shù)據(jù)u8 ManufacturerID = 0, DeviceID = 0;SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(ReadDeviceID); //發(fā)送讀取設(shè)備ID信息命令SPI_Flash_SendByte((ReadManu_DeviceID_Addr & 0xFF0000) >> 16); //發(fā)送24位的地址字節(jié),該地址為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位的數(shù)據(jù),高字節(jié)為ManufacturerID,低字節(jié)為DeviceIDSST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片return ManuID_DeviceID; //返回16位的ManuID_DeviceID,高字節(jié)為ManufacturerID,低字節(jié)為DeviceID}void SST25V_EBSY() //允許AAI模式期間,SO腳輸出忙狀態(tài){SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(EBSY); //發(fā)送允許AAI模式期間,SO腳輸出忙狀態(tài)命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}void SST25V_DBSY() //禁止AAI模式期間,SO腳輸出忙狀態(tài){SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(DBSY); //發(fā)送禁止AAI模式期間,SO腳輸出忙狀態(tài)命令SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片} 上一頁 1 2 3 下一頁
評論