📄 spi_flash.c
字号:
/****************************************
* File Name : spi_flash.c
* Author :
* Date First Issued : 2009.4.20
* Description : AT26F004 驱动
* vertion : 1.0
* 注意 : 本驱动是为ST公司的ST103F系列Soc开发的,需要ST公司库的支持
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "spi_flash.h" //包含ST公司库的头文件
/* Private typedef -----------------------------------------------------------*/
//#define SPI_FLASH_PageSize 256
#define SeWRITE 0xAF /* Sequential Write to Memory instruction */
#define WRITE 0x02 /* Write to Memory instruction */
#define WRSR 0x01 /* Write Status Register instruction */
#define WREN 0x06 /* Write enable instruction */
#define UNPRO 0x39 /* 解除扇区保护*/
#define READ 0x03 /* Read from Memory instruction */
#define RDSR 0x05 /* Read Status Register instruction */
#define RDID 0x9F /* Read identification */
#define SE 0xD8 /* 64K Sector Erase instruction */
#define BE 0xC7 /* Bulk Erase instruction */
#define RDSPR 0x3C /* 读扇区保护寄存器*/
#define BUSY_READY 0x01 /* Device is ready or busy bit in Status Register*/
#define Dummy_Byte 0xA5
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : SPI_FLASH_Init
* Description : 初始化SPI2和GPIOB ,作为Flash应用
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI2 and GPIOB clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Configure SPI2 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB.12 as Output push-pull, used as Flash Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* SPI2 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
/*******************************************************************************
* Function Name : SPI_FLASH_SectorErase
* Description : 扇区擦除函数
* Input : SectorAddr: 要擦除的地址
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_SectorErase(u32 SectorAddr)
{
/* Send write enable instruction */
SPI_FLASH_WriteEnable();
/* Sector Erase */
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send Sector Erase instruction */
SPI_FLASH_SendByte(SE);
/* Send SectorAddr high nibble address byte */
SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
/* Send SectorAddr medium nibble address byte */
SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
/* Send SectorAddr low nibble address byte */
SPI_FLASH_SendByte(SectorAddr & 0xFF);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_BulkErase
* Description : 擦除整个FLASH
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_BulkErase(void)
{
/* Send write enable instruction */
SPI_FLASH_WriteEnable();
/* Bulk Erase */
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send Bulk Erase instruction */
SPI_FLASH_SendByte(BE);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_ByteWrite
* Description : 向FLASH中写一个单个字节
* Input : WriteAddr : FLASH's internal address to write to.
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_ByteWrite(u32 WriteAddr ,u8 i)
{
/* Enable the write access to the FLASH */
SPI_FLASH_WriteEnable();
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Write to Memory " instruction */
SPI_FLASH_SendByte(WRITE);
/* Send WriteAddr high nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SPI_FLASH_SendByte(WriteAddr & 0xFF);
SPI_FLASH_SendByte(i);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_SequentialWrite
* Description : 向FLASH中的写入多个数
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH,
* must be equal or less than "SPI_FLASH_PageSize" value.
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_SequentialWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
/* Enable the write access to the FLASH */
SPI_FLASH_WriteEnable();
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "sequtial Write to Memory " instruction */
SPI_FLASH_SendByte(SeWRITE);
/* Send WriteAddr high nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SPI_FLASH_SendByte(WriteAddr & 0xFF);
//写第一个字节
SPI_FLASH_SendByte(*pBuffer);
//要写字节数据的个数减一
NumByteToWrite--;
/* while there is data to be written on the FLASH */
while(NumByteToWrite--)
{
/* Wait the end of Flash writing */
SPI_FLASH_WaitForWriteEnd();
/* Send "sequtial Write to Memory " instruction */
SPI_FLASH_SendByte(SeWRITE);
/* Send the current byte */
SPI_FLASH_SendByte(*pBuffer);
/* Point on the next byte to be written */
pBuffer++;
}
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* Wait the end of Flash writing */
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_BufferWrite
* Description : Writes block of data to the FLASH. In this function, the
* number of WRITE cycles are reduced, using Page WRITE sequence.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH.
* Output : None
* Return : None
*******************************************************************************/
//void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
//{
// u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
//
// Addr = WriteAddr % SPI_FLASH_PageSize;
// count = SPI_FLASH_PageSize - Addr;
// NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
// NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
//
// if(Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned */
// {
// if(NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
// {
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, NumByteToWrite);
// }
// else /* NumByteToWrite > SPI_FLASH_PageSize */
// {
// while(NumOfPage--)
// {
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
// WriteAddr += SPI_FLASH_PageSize;
// pBuffer += SPI_FLASH_PageSize;
// }
//
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, NumOfSingle);
// }
// }
// else /* WriteAddr is not SPI_FLASH_PageSize aligned */
// {
// if(NumOfPage== 0) /* NumByteToWrite < SPI_FLASH_PageSize */
// {
// if(NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
// {
// temp = NumOfSingle - count;
//
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, count);
// WriteAddr += count;
// pBuffer += count;
//
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, temp);
// }
// else
// {
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, NumByteToWrite);
// }
// }
// else /* NumByteToWrite > SPI_FLASH_PageSize */
// {
// NumByteToWrite -= count;
// NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
// NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
//
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, count);
// WriteAddr += count;
// pBuffer += count;
//
// while(NumOfPage--)
// {
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
// WriteAddr += SPI_FLASH_PageSize;
// pBuffer += SPI_FLASH_PageSize;
// }
//
// if(NumOfSingle != 0)
// {
// SPI_FLASH_SequentialWrite(pBuffer, WriteAddr, NumOfSingle);
// }
// }
// }
//}
/*******************************************************************************
* Function Name : SPI_FLASH_BufferRead
* Description : Reads a block of data from the FLASH.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the FLASH.
* - ReadAddr : FLASH's internal address to read from.
* - NumByteToRead : number of bytes to read from the FLASH.
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -