6#define CMD_READ_ID 0x9F
7#define CMD_READ_DATA 0x03
8#define CMD_FAST_READ 0x0B
9#define CMD_PAGE_PROG 0x02
10#define CMD_WRITE_ENABLE 0x06
11#define CMD_WRITE_DISABLE 0x04
12#define CMD_READ_STATUS 0x05
13#define CMD_PAGE_ERASE 0x81
20#define FLASH_TIMEOUT 10000
29 HAL_GPIO_WritePin(s->
CSPort, s->
CSPin, GPIO_PIN_RESET);
34 HAL_GPIO_WritePin(s->
CSPort, s->
CSPin, GPIO_PIN_SET);
45 HAL_StatusTypeDef ret;
54 if ((ret = HAL_SPI_Transmit(s->
Spi, &cmd, 1, HAL_MAX_DELAY)) != HAL_OK)
56 if ((ret = HAL_SPI_Receive(s->
Spi, &status, 1, HAL_MAX_DELAY)) != HAL_OK)
59 }
while ((status &
SR_WIP) && timeout--);
72 HAL_StatusTypeDef ret = HAL_ERROR;
80 if ((ret = HAL_SPI_Transmit(s->
Spi, &cmd, 1, HAL_MAX_DELAY)) != HAL_OK)
87 if ((ret = HAL_SPI_Transmit(s->
Spi, &cmd, 1, HAL_MAX_DELAY)) != HAL_OK)
89 if ((ret = HAL_SPI_Receive(s->
Spi, &status, 1, HAL_MAX_DELAY)) != HAL_OK)
92 }
while (ret == HAL_OK && !(status &
SR_WEL));
102 HAL_StatusTypeDef ret;
110 ret = HAL_SPI_Transmit(s->
Spi, &cmd, 1, 100);
113 ret = HAL_SPI_Receive(s->
Spi,
id, 3, 100);
135 if (!s->
Is && tryInit)
142 HAL_StatusTypeDef ret = HAL_ERROR;
145 if (s->
Is > 0 && buffer != NULL && size > 0 && addr + size <= s->Size)
152 cmd[1] = (addr >> 16) & 0xFF;
153 cmd[2] = (addr >> 8) & 0xFF;
154 cmd[3] = addr & 0xFF;
163 if ((ret = HAL_SPI_Transmit(s->
Spi, cmd, 5, HAL_MAX_DELAY)) != HAL_OK)
165 ret = HAL_SPI_Receive(s->
Spi, buffer, size, HAL_MAX_DELAY);
179 HAL_StatusTypeDef status = HAL_ERROR;
185 writeLog(
"flash_WritePage incorrect length:%d", (
int)size);
190 uint32_t pageAddr = addr >> 8;
197 cmd[1] = (pageAddr >> 16) & 0xFF;
198 cmd[2] = (pageAddr >> 8) & 0xFF;
199 cmd[3] = pageAddr & 0xFF;
203 if ((status = HAL_SPI_Transmit(s->
Spi, cmd, 4, HAL_MAX_DELAY)) != HAL_OK)
208 memcpy(
_buffer256 + (addr - pageAddr), data, size);
216 if ((status = HAL_SPI_Transmit(s->
Spi, cmd, 4, HAL_MAX_DELAY)) != HAL_OK)
231 HAL_StatusTypeDef ret = HAL_ERROR;
232 uint32_t pAddr = addr;
233 uint32_t bytesLeft = size;
234 const uint8_t *pBuffer = data;
236 while (bytesLeft > 0)
239 uint32_t pageOffset = pAddr % 256;
240 uint32_t maxWrite = 256 - pageOffset;
241 uint32_t currentWriteLen = (bytesLeft < maxWrite) ? bytesLeft : maxWrite;
247 pAddr += currentWriteLen;
248 pBuffer += currentWriteLen;
249 bytesLeft -= currentWriteLen;
254 uint8_t sectorBuffer[512] = {};
258 writeLog(
"flashNew_Write: read check failed:%d", (
int) ret);
261 if (memcmp(sectorBuffer, data, size) != 0)
263 writeLog(
"flashNew_Write: compare failed!");
int8_t flash_at25_Is(flash_at25CS_t *s, int8_t tryInit)
check if flash chip is present or not
HAL_StatusTypeDef flash_at25_Read(const flash_at25CS_t *s, uint32_t addr, void *buffer, uint16_t size)
Fast read from flash memory Uses fast read command (0x0B) for improved performance in frequent read o...
HAL_StatusTypeDef flash_at25_Write(const flash_at25CS_t *s, uint32_t addr, const void *data, uint16_t size)
Write to flash memory with sector preservation Handles page-aligned writes efficiently with proper ve...
HAL_StatusTypeDef flash_at25_Init(flash_at25CS_t *s)
Initialization for AT25EU0041A flash chip Performs proper device identification and verification Suit...
static HAL_StatusTypeDef flash_WritePage(const flash_at25CS_t *s, uint32_t addr, const uint8_t *data, uint16_t size)
write data to 256 bounder with erase of page
static void flash_Select(const flash_at25CS_t *s)
static void flash_Unselect(const flash_at25CS_t *s)
static HAL_StatusTypeDef flashNew_WriteEnableVerified(const flash_at25CS_t *s)
Helper function to enable write with verification Ensures write enable latch is properly set.
static HAL_StatusTypeDef flashNew_WaitReadyWithTimeout(const flash_at25CS_t *s)
Helper function to wait for flash ready with timeout protection Optimized for frequent operations wit...
static uint8_t _buffer256[256]
#define AT25_MANUFACTURER_ID
: Header for main.c file. This file contains the common defines of the application.
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
Configuration and state descriptor for one AT25EU0041A flash chip. Pass a pointer to this struct to e...