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
關鍵詞:
stm32SST25V