spi flash为M25P64时,给出详细代码
时间: 2024-02-29 20:51:29 浏览: 71
好的,以下是一个基于STM32的SPI Flash擦除函数示例,假设使用的是M25P64 SPI Flash。这里使用的是HAL库,你需要在你的项目中添加HAL库的支持。
```c
#include "main.h"
#include "spi.h"
#define SPI_FLASH_CS_LOW() HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_RESET)
#define SPI_FLASH_CS_HIGH() HAL_GPIO_WritePin(SPI_FLASH_CS_GPIO_Port, SPI_FLASH_CS_Pin, GPIO_PIN_SET)
#define CMD_WRITE_ENABLE 0x06
#define CMD_WRITE_DISABLE 0x04
#define CMD_READ_STATUS 0x05
#define CMD_WRITE_STATUS 0x01
#define CMD_FAST_READ 0x0B
#define CMD_PAGE_PROGRAM 0x02
#define CMD_SECTOR_ERASE 0x20
#define CMD_BLOCK_ERASE 0xD8
#define CMD_CHIP_ERASE 0xC7
#define STATUS_BUSY 0x01
#define STATUS_WEL 0x02
void SPI_Flash_WriteEnable(void)
{
SPI_FLASH_CS_LOW();
HAL_SPI_Transmit(&hspi1, &CMD_WRITE_ENABLE, 1, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
}
void SPI_Flash_WriteDisable(void)
{
SPI_FLASH_CS_LOW();
HAL_SPI_Transmit(&hspi1, &CMD_WRITE_DISABLE, 1, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
}
uint8_t SPI_Flash_ReadStatus(void)
{
uint8_t status;
SPI_FLASH_CS_LOW();
HAL_SPI_Transmit(&hspi1, &CMD_READ_STATUS, 1, HAL_MAX_DELAY);
HAL_SPI_Receive(&hspi1, &status, 1, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
return status;
}
void SPI_Flash_WriteStatus(uint8_t status)
{
SPI_Flash_WriteEnable();
SPI_FLASH_CS_LOW();
HAL_SPI_Transmit(&hspi1, &CMD_WRITE_STATUS, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&hspi1, &status, 1, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
while(SPI_Flash_ReadStatus() & STATUS_BUSY);
}
void SPI_Flash_FastRead(uint32_t address, uint8_t *data, uint32_t size)
{
uint8_t cmd[] = {CMD_FAST_READ, (uint8_t)(address >> 16), (uint8_t)(address >> 8), (uint8_t)address, 0x00};
SPI_FLASH_CS_LOW();
HAL_SPI_Transmit(&hspi1, cmd, sizeof(cmd), HAL_MAX_DELAY);
HAL_SPI_Receive(&hspi1, data, size, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
}
void SPI_Flash_PageProgram(uint32_t address, uint8_t *data, uint32_t size)
{
SPI_Flash_WriteEnable();
SPI_FLASH_CS_LOW();
uint8_t cmd[] = {CMD_PAGE_PROGRAM, (uint8_t)(address >> 16), (uint8_t)(address >> 8), (uint8_t)address};
HAL_SPI_Transmit(&hspi1, cmd, sizeof(cmd), HAL_MAX_DELAY);
HAL_SPI_Transmit(&hspi1, data, size, HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
while(SPI_Flash_ReadStatus() & STATUS_BUSY);
}
void SPI_Flash_SectorErase(uint32_t address)
{
SPI_Flash_WriteEnable();
SPI_FLASH_CS_LOW();
uint8_t cmd[] = {CMD_SECTOR_ERASE, (uint8_t)(address >> 16), (uint8_t)(address >> 8), (uint8_t)address};
HAL_SPI_Transmit(&hspi1, cmd, sizeof(cmd), HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
while(SPI_Flash_ReadStatus() & STATUS_BUSY);
}
void SPI_Flash_BlockErase(uint32_t address)
{
SPI_Flash_WriteEnable();
SPI_FLASH_CS_LOW();
uint8_t cmd[] = {CMD_BLOCK_ERASE, (uint8_t)(address >> 16), (uint8_t)(address >> 8), (uint8_t)address};
HAL_SPI_Transmit(&hspi1, cmd, sizeof(cmd), HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
while(SPI_Flash_ReadStatus() & STATUS_BUSY);
}
void SPI_Flash_ChipErase(void)
{
SPI_Flash_WriteEnable();
SPI_FLASH_CS_LOW();
uint8_t cmd[] = {CMD_CHIP_ERASE};
HAL_SPI_Transmit(&hspi1, cmd, sizeof(cmd), HAL_MAX_DELAY);
SPI_FLASH_CS_HIGH();
while(SPI_Flash_ReadStatus() & STATUS_BUSY);
}
void SPI_Flash_EraseAll(void)
{
SPI_Flash_ChipErase();
}
void SPI_Flash_Init(void)
{
SPI_Flash_WriteStatus(0x00);
}
void SPI_Flash_EraseOnStartup(void)
{
SPI_Flash_EraseAll();
}
int main(void)
{
HAL_Init();
MX_SPI1_Init();
SPI_Flash_Init();
SPI_Flash_EraseOnStartup();
while(1)
{
// Your code here
}
}
```
在上述代码中,`SPI_Flash_EraseOnStartup()`函数会在每次设备上电时调用,以擦除SPI Flash存储器中的所有内容。请注意,这里使用的是芯片擦除命令(`CMD_CHIP_ERASE`),这会将整个芯片擦除。如果你想使用其他擦除方式,可以使用`SPI_Flash_SectorErase()`或`SPI_Flash_BlockErase()`函数。
当然,你需要根据你的具体硬件和软件实现来修改和适配以上代码。
阅读全文