L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
flash_at25.c File Reference
#include "flash_at25.h"
#include "main.h"
#include <string.h>
Include dependency graph for flash_at25.c:

Go to the source code of this file.

Macros

#define CMD_READ_ID   0x9F
#define CMD_READ_DATA   0x03
#define CMD_FAST_READ   0x0B
#define CMD_PAGE_PROG   0x02
#define CMD_WRITE_ENABLE   0x06
#define CMD_WRITE_DISABLE   0x04
#define CMD_READ_STATUS   0x05
#define CMD_PAGE_ERASE   0x81
#define SR_WIP   0x01
#define SR_WEL   0x02
#define FLASH_TIMEOUT   10000

Functions

static void flash_Select (const flash_at25CS_t *s)
static void flash_Unselect (const flash_at25CS_t *s)
static HAL_StatusTypeDef flashNew_WaitReadyWithTimeout (const flash_at25CS_t *s)
 Helper function to wait for flash ready with timeout protection Optimized for frequent operations with proper timeout and CS handling.
static HAL_StatusTypeDef flashNew_WriteEnableVerified (const flash_at25CS_t *s)
 Helper function to enable write with verification Ensures write enable latch is properly set.
HAL_StatusTypeDef flash_at25_Init (flash_at25CS_t *s)
 Initialization for AT25EU0041A flash chip Performs proper device identification and verification Suitable for frequent operations with improved error checking.
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 operations Includes boundary checking and ready state verification.
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
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 verification Preserves existing data in flash sectors by reading, modifying, and writing back.

Variables

static uint8_t _buffer256 [256] = {}

Macro Definition Documentation

◆ CMD_FAST_READ

#define CMD_FAST_READ   0x0B

Definition at line 8 of file flash_at25.c.

Referenced by flash_at25_Read().

◆ CMD_PAGE_ERASE

#define CMD_PAGE_ERASE   0x81

Definition at line 13 of file flash_at25.c.

Referenced by flash_WritePage().

◆ CMD_PAGE_PROG

#define CMD_PAGE_PROG   0x02

Definition at line 9 of file flash_at25.c.

Referenced by flash_WritePage().

◆ CMD_READ_DATA

#define CMD_READ_DATA   0x03

Definition at line 7 of file flash_at25.c.

◆ CMD_READ_ID

#define CMD_READ_ID   0x9F

Definition at line 6 of file flash_at25.c.

Referenced by flash_at25_Init().

◆ CMD_READ_STATUS

#define CMD_READ_STATUS   0x05

Definition at line 12 of file flash_at25.c.

Referenced by flashNew_WaitReadyWithTimeout(), and flashNew_WriteEnableVerified().

◆ CMD_WRITE_DISABLE

#define CMD_WRITE_DISABLE   0x04

Definition at line 11 of file flash_at25.c.

◆ CMD_WRITE_ENABLE

#define CMD_WRITE_ENABLE   0x06

Definition at line 10 of file flash_at25.c.

Referenced by flashNew_WriteEnableVerified().

◆ FLASH_TIMEOUT

#define FLASH_TIMEOUT   10000

Definition at line 20 of file flash_at25.c.

Referenced by flashNew_WaitReadyWithTimeout().

◆ SR_WEL

#define SR_WEL   0x02

Definition at line 17 of file flash_at25.c.

Referenced by flashNew_WriteEnableVerified().

◆ SR_WIP

#define SR_WIP   0x01

Definition at line 16 of file flash_at25.c.

Referenced by flashNew_WaitReadyWithTimeout().

Function Documentation

◆ flash_at25_Init()

HAL_StatusTypeDef flash_at25_Init ( flash_at25CS_t * s)

Initialization for AT25EU0041A flash chip Performs proper device identification and verification Suitable for frequent operations with improved error checking.

Parameters
sPointer to flash configuration structure
Return values
HAL_OKif device identified correctly, HAL_ERROR otherwise

Definition at line 98 of file flash_at25.c.

References AT25_MANUFACTURER_ID, AT25_MEMSIZE, CMD_READ_ID, flash_Select(), flash_Unselect(), flash_at25CS_t::Is, flash_at25CS_t::Size, and flash_at25CS_t::Spi.

Referenced by flash_at25_Is(), and mems_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flash_at25_Is()

int8_t flash_at25_Is ( flash_at25CS_t * s,
int8_t tryInit )

  • check if flash chip is present or not

Parameters
tryInit- in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no

Definition at line 133 of file flash_at25.c.

References flash_at25_Init(), and flash_at25CS_t::Is.

Here is the call graph for this function:

◆ flash_at25_Read()

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 operations Includes boundary checking and ready state verification.

Parameters
sPointer to flash configuration structure
addrStarting address to read from (24-bit address, 0x000000 to 0x07FFFF for 512KB)
bufferPointer to destination buffer
sizeNumber of bytes to read
Return values
HAL_OKon success, HAL_ERROR on failure, HAL_TIMEOUT on timeout

Definition at line 140 of file flash_at25.c.

References CMD_FAST_READ, flash_Select(), flash_Unselect(), flashNew_WaitReadyWithTimeout(), flash_at25CS_t::Is, and flash_at25CS_t::Spi.

Referenced by flash_at25_Write(), flash_WritePage(), mems_Check(), mems_GetLastData(), and mems_ReadMainBlock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flash_at25_Write()

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 verification Preserves existing data in flash sectors by reading, modifying, and writing back.

This function implements a read-modify-write approach:

  1. Calculates which 256 Page needs to be erased based on addr & size
  2. For each affected Page: reads it, resets bytes to 0xFF where new data will be written
  3. Erases the sector and writes back the modified sector data
  4. Then writes the new data to the prepared sectors

Handles sector boundaries automatically - if data crosses sector boundary, both sectors are processed

Includes boundary checking, write enable verification, and proper timeout handling

Parameters
sPointer to flash configuration structure
addrStarting address to write to (24-bit address, 0x000000 to 0x07FFFF for 512KB)
dataPointer to source data buffer
sizeNumber of bytes to write
Return values
HAL_OKon success, HAL_ERROR on failure, HAL_TIMEOUT on timeout

Definition at line 229 of file flash_at25.c.

References Error_Handler(), flash_at25_Read(), flash_WritePage(), and writeLog().

Referenced by mems_AddData(), mems_Reset(), and mems_WriteMainBlock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flash_Select()

void flash_Select ( const flash_at25CS_t * s)
static

Definition at line 27 of file flash_at25.c.

References flash_at25CS_t::CSPin, and flash_at25CS_t::CSPort.

Referenced by flash_at25_Init(), flash_at25_Read(), flash_WritePage(), flashNew_WaitReadyWithTimeout(), and flashNew_WriteEnableVerified().

Here is the caller graph for this function:

◆ flash_Unselect()

void flash_Unselect ( const flash_at25CS_t * s)
static

Definition at line 32 of file flash_at25.c.

References flash_at25CS_t::CSPin, and flash_at25CS_t::CSPort.

Referenced by flash_at25_Init(), flash_at25_Read(), flash_WritePage(), flashNew_WaitReadyWithTimeout(), and flashNew_WriteEnableVerified().

Here is the caller graph for this function:

◆ flash_WritePage()

HAL_StatusTypeDef flash_WritePage ( const flash_at25CS_t * s,
uint32_t addr,
const uint8_t * data,
uint16_t size )
static

write data to 256 bounder with erase of page

Definition at line 177 of file flash_at25.c.

References _buffer256, CMD_PAGE_ERASE, CMD_PAGE_PROG, Error_Handler(), flash_at25_Read(), flash_Select(), flash_Unselect(), flashNew_WaitReadyWithTimeout(), flashNew_WriteEnableVerified(), flash_at25CS_t::Is, flash_at25CS_t::Spi, and writeLog().

Referenced by flash_at25_Write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flashNew_WaitReadyWithTimeout()

HAL_StatusTypeDef flashNew_WaitReadyWithTimeout ( const flash_at25CS_t * s)
static

Helper function to wait for flash ready with timeout protection Optimized for frequent operations with proper timeout and CS handling.

Parameters
sPointer to flash configuration structure
Return values
HAL_OKif ready, HAL_TIMEOUT if timeout, HAL_ERROR on communication error

Definition at line 43 of file flash_at25.c.

References CMD_READ_STATUS, flash_Select(), FLASH_TIMEOUT, flash_Unselect(), flash_at25CS_t::Spi, and SR_WIP.

Referenced by flash_at25_Read(), and flash_WritePage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flashNew_WriteEnableVerified()

HAL_StatusTypeDef flashNew_WriteEnableVerified ( const flash_at25CS_t * s)
static

Helper function to enable write with verification Ensures write enable latch is properly set.

Parameters
sPointer to flash configuration structure
Return values
HAL_OKif write enabled, HAL_ERROR otherwise

Definition at line 70 of file flash_at25.c.

References CMD_READ_STATUS, CMD_WRITE_ENABLE, flash_Select(), flash_Unselect(), flash_at25CS_t::Spi, and SR_WEL.

Referenced by flash_WritePage().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _buffer256

uint8_t _buffer256[256] = {}
static

Definition at line 25 of file flash_at25.c.

Referenced by flash_WritePage().