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(); //系統(tǒng)啟動SST25V_Init(); //Flash芯片初始化SST25V_ChipErase(); //擦除整個芯片空間DataByte = SST25V_ByteRead(0x000000); //讀取0x000000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000010); //讀取0x000000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000100); //讀取0x000100地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x001000); //讀取0x001000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x010000); //讀取0x010000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x100000); //讀取0x100000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x600000); //讀取0x000000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xF00000); //讀取0x600000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFFFFF); //讀取0xFFFFFF地址空間的數(shù)據(jù)信息SST25V_AAI_WriteBytes(0x72, 0xFFF000,0x001000); //向以0xFFF000為起始地址的數(shù)據(jù)空間寫入0x001000個0x10--即向最后4KB空間寫入0x10DataByte = SST25V_ByteRead(0x000000); //讀取0x000000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000001); //讀取0x000001地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000002); //讀取0x000002地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFF000); //讀取0xFFF000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFF0EE); //讀取0xFFF0EE地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFFEEE); //讀取0xFFFEEE地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFFFFF); //讀取0xFFFFFF地址空間的數(shù)據(jù)信息SST25V_SectorErase_4KByte(0xFFF000); //擦除以0xFFF000為起始地址的4KB數(shù)據(jù)空間DataByte = SST25V_ByteRead(0x000000); //讀取0x000000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFF000); //讀取0xFFF000地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000001); //讀取0x000001地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0x000002); //讀取0x000002地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFEEEE); //讀取0xFFEEEE地址空間的數(shù)據(jù)信息DataByte = SST25V_ByteRead(0xFFFFFF); //讀取0xFFFFFF地址空間的數(shù)據(jù)信息//SST25V_BufferWrite(Tx_Buffer,0xFFF000,16); //將Tx_Buffer中的16個字節(jié)數(shù)據(jù)寫入以0xFFF000為起始地址的數(shù)據(jù)空間SST25V_AAI_BufferProgram(Tx_Buffer,0xFFF000,16); //采用AAI模式,將Tx_Buffer中的16個字節(jié)數(shù)據(jù)寫入以0xFFF000為起始地址的數(shù)據(jù)空間SST25V_BufferRead(Rx_Buffer,0xFFF000,16); //讀出0xFFF000為起始地址的16字節(jié)數(shù)據(jù),存放于Rx_Buffer中DataByte = SST25V_ByteRead(0xFFEEEE); //讀取0xFFEEEE地址空間的數(shù)據(jù)信息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的狀態(tài)決定是否允許寫狀態(tài)寄存器SST25V_HOLD_HIGH(); //保持HOLD#管腳置高SST25V_WriteStatusRegister(0x02); //給狀態(tài)寄存器寫入數(shù)據(jù)02SST25V_DBSY(); //禁止AAI模式時,SO腳輸出忙狀態(tài)}u8 SST25V_ByteRead(u32 ReadAddr) //從ReadAddr地址讀取一個字節(jié)數(shù)據(jù){u8 Temp = 0;SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(Read_Data); //發(fā)送讀取數(shù)據(jù)命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16); //發(fā)送24位的讀取數(shù)據(jù)地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);Temp = SPI_Flash_ReceiveByte(); //接收讀取到的數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片return Temp; //返回讀取到的數(shù)據(jù)}void SST25V_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)//從ReadAddr地址開始讀取NumByteToRead個字節(jié)數(shù)據(jù){SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(Read_Data); //發(fā)送讀取數(shù)據(jù)命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16); //發(fā)送24位的讀取數(shù)據(jù)起始地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);while(NumByteToRead--) //判斷是否完成讀取NumByteToRead個字節(jié)數(shù)據(jù){*pBuffer = SPI_Flash_ReceiveByte(); //接受讀取到的數(shù)據(jù),并存儲于pBuffer中pBuffer++; //pBuffer指針自增1}SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}u8 SST25V_HighSpeedByteRead(u32 ReadAddr) //從ReadAddr高速讀取一個字節(jié)數(shù)據(jù){u32 Temp = 0;SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(HighSpeedReadData); //發(fā)送快速讀取數(shù)據(jù)命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16); //發(fā)送24位的讀取數(shù)據(jù)地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);SPI_Flash_SendByte(Dummy_Byte); //發(fā)送虛擬字節(jié)數(shù)據(jù)Temp = SPI_Flash_ReceiveByte(); //接收讀取到的數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片return Temp; //返回讀取到的數(shù)據(jù)}void SST25V_HighSpeedBufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)//從ReadAddr開始高速讀取NumByteToRead個字節(jié)數(shù)據(jù),并存儲于pBuffer中{SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(HighSpeedReadData); //發(fā)送快速讀取數(shù)據(jù)命令SPI_Flash_SendByte((ReadAddr & 0xFF0000) >> 16); //發(fā)送24位的讀取數(shù)據(jù)起始地址SPI_Flash_SendByte((ReadAddr& 0xFF00) >> 8);SPI_Flash_SendByte(ReadAddr & 0xFF);SPI_Flash_SendByte(Dummy_Byte); //發(fā)送虛擬字節(jié)數(shù)據(jù)while(NumByteToRead--) //判斷是否完成讀取NumByteToRead個字節(jié)數(shù)據(jù){*pBuffer = SPI_Flash_ReceiveByte(); //接受讀取到的數(shù)據(jù),并存儲于pBuffer中pBuffer++; //pBuffer指針自增1}SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片}u8 SPI_Flash_SendByte(u8 byte) //SPI發(fā)送數(shù)據(jù){while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); //等待SPI1的Tx buffer為空SPI_SendData(SPI1, byte); //發(fā)送數(shù)據(jù)字節(jié)bytewhile(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); //等待SPI1完成數(shù)據(jù)接收return SPI_ReceiveData(SPI1); //返回讀取到的數(shù)據(jù)}u8 SPI_Flash_ReceiveByte(void) //SPI接收數(shù)據(jù){return (SPI_Flash_SendByte(Dummy_Byte)); //發(fā)送虛擬字節(jié),提供接收數(shù)據(jù)時鐘}void SST25V_ByteWrite(u8 Byte, u32 WriteAddr) //寫入一個數(shù)據(jù)字節(jié){SST25V_WriteEnable(); //允許寫入操作SST25V_CS_LOW(); //片選端置低--選中芯片SPI_Flash_SendByte(Byte_Program); //發(fā)送寫數(shù)據(jù)命令SPI_Flash_SendByte((WriteAddr & 0xFF0000) >> 16); //發(fā)送24位的寫入數(shù)據(jù)地址SPI_Flash_SendByte((WriteAddr & 0xFF00) >> 8);SPI_Flash_SendByte(WriteAddr & 0xFF);SPI_Flash_SendByte(Byte); //發(fā)送要寫入的數(shù)據(jù)SST25V_CS_HIGH(); //片選CS#管腳置高--不選中芯片SST25V_WaitForWriteEnd(); //等待寫操作完成}void SST25V_BufferWrite(u8 *pBuffer,u32 Addr,u16 BufferLength)//將pBuffer中的BufferLength個字節(jié)數(shù)據(jù)寫入到以Addr為起始地址的區(qū)域{while(BufferLength--) //判斷是否完成寫入NumByteToRead個字節(jié)數(shù)據(jù){SST25V_ByteWrite(*pBuffer,Addr); //把當前pBuffer對應的數(shù)據(jù),寫入當前Addr中pBuffer++; //數(shù)據(jù)指針自增1Addr++; //寫入地址自增1}}
評論