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;
103 uint8_t waitForWakeup = 0;
104 uint32_t timeout = 2000;
118 ret = HAL_SPI_Transmit(s->
Spi, &cmd, 1, 100);
121 ret = HAL_SPI_Receive(s->
Spi,
id, 3, 100);
139 }
while (waitForWakeup &&
HAL_GetTick() - start_tick < timeout);
147 if (!s->
Is && tryInit)
154 HAL_StatusTypeDef ret = HAL_ERROR;
157 if (s->
Is > 0 && buffer != NULL && size > 0 && addr + size <= s->Size)
164 cmd[1] = (addr >> 16) & 0xFF;
165 cmd[2] = (addr >> 8) & 0xFF;
166 cmd[3] = addr & 0xFF;
176 if ((ret = HAL_SPI_Transmit(s->
Spi, cmd, 5, HAL_MAX_DELAY)) != HAL_OK)
178 ret = HAL_SPI_Receive(s->
Spi, buffer, size, HAL_MAX_DELAY);
193 HAL_StatusTypeDef status = HAL_ERROR;
199 writeLog(
"flash_WritePage incorrect length:%d", (
int)size);
204 uint32_t pageAddr = addr >> 8;
211 cmd[1] = (pageAddr >> 16) & 0xFF;
212 cmd[2] = (pageAddr >> 8) & 0xFF;
213 cmd[3] = pageAddr & 0xFF;
217 if ((status = HAL_SPI_Transmit(s->
Spi, cmd, 4, HAL_MAX_DELAY)) != HAL_OK)
222 memcpy(
_buffer256 + (addr - pageAddr), data, size);
230 if ((status = HAL_SPI_Transmit(s->
Spi, cmd, 4, HAL_MAX_DELAY)) != HAL_OK)
245 HAL_StatusTypeDef ret = HAL_ERROR;
246 uint32_t pAddr = addr;
247 uint32_t bytesLeft = size;
248 const uint8_t *pBuffer = data;
251 while (bytesLeft > 0)
254 uint32_t pageOffset = pAddr % 256;
255 uint32_t maxWrite = 256 - pageOffset;
256 uint32_t currentWriteLen = (bytesLeft < maxWrite) ? bytesLeft : maxWrite;
262 pAddr += currentWriteLen;
263 pBuffer += currentWriteLen;
264 bytesLeft -= currentWriteLen;
270 uint8_t sectorBuffer[512] = {};
274 writeLog(
"flashNew_Write: read check failed:%d", (
int) ret);
277 if (memcmp(sectorBuffer, data, size) != 0)
279 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)
This function is executed in case of error occurrence.
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
void Power3v3_On()
Cumulative turn the all peripheries to ON state - pull-up the _3V3_Enb_Pin pin.
void Power3v3_Off()
Cumulative turn the all peripheries to OFF state - pull-down the _3V3_Enb_Pin pin.
Configuration and state descriptor for one AT25EU0041A flash chip. Pass a pointer to this struct to e...
uint32_t HAL_GetTick(void)