L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
flash_at25.h
Go to the documentation of this file.
1/*
2 * flash12.h
3 * module for reading and writing to FLASH12 click - chip AT25EU0041A (4Mbit / 512KB)
4 * gemini: https://gemini.google.com/share/fee2e604904f
5 * Created on: 24. 12. 2025
6 * Author: Milan
7 *
8 * Module handles writing without regard to 256bytes boundary
9 * Flash is activated via CS to GND. Consumption is 2mA
10 * StandBy mode is CS to VCC, consumption 14mcA
11 */
12
13#ifndef INC_FLASH12_H_
14#define INC_FLASH12_H_
15
16/*
17 * MT 24.12.2025
18 */
19
20#include "stm32wlxx_hal.h"
21
22// Device Info for AT25EU0041A
23#define AT25_MANUFACTURER_ID 0x1F
24#define AT25_MEMSIZE (1<<19) // 512kB alias 4Mb
25
26#pragma pack(1)
27/**
28 * @brief Configuration and state descriptor for one AT25EU0041A flash chip.
29 * Pass a pointer to this struct to every flash_at25_XXX function.
30 * Populate the IN fields before calling flash_at25_Init(); the OUT
31 * fields are filled by the initialisation routine.
32 */
33typedef struct
34{
35 SPI_HandleTypeDef *Spi; /**< IN SPI handle used to communicate with the chip */
36 GPIO_TypeDef* CSPort; /**< IN GPIO port of the chip-select (CS/NSS) pin */
37 uint16_t CSPin; /**< IN GPIO pin number of the chip-select line */
38 int8_t Is; /**< OUT Set to 1 by flash_at25_Init() if the chip was identified, 0 otherwise */
39 uint32_t Size : 24; /**< OUT Total usable memory in bytes (e.g. 524288 for the 4 Mbit / 512 KB variant) */
41#pragma pack()
42
43/**
44 * @brief - check if flash chip is present or not
45 * @param tryInit - in case sensor is not yet initialized, 1 - attempt to initialize again, 0 - no
46 */
47int8_t flash_at25_Is(flash_at25CS_t *s, int8_t tryInit);
48
49/**
50 * @brief Initialization for AT25EU0041A flash chip
51 * Performs proper device identification and verification
52 * Suitable for frequent operations with improved error checking
53 * @param s Pointer to flash configuration structure
54 * @retval HAL_OK if device identified correctly, HAL_ERROR otherwise
55 */
56HAL_StatusTypeDef flash_at25_Init(flash_at25CS_t *s);
57
58/**
59 * @brief Fast read from flash memory
60 * Uses fast read command (0x0B) for improved performance in frequent read operations
61 * Includes boundary checking and ready state verification
62 * @param s Pointer to flash configuration structure
63 * @param addr Starting address to read from (24-bit address, 0x000000 to 0x07FFFF for 512KB)
64 * @param buffer Pointer to destination buffer
65 * @param size Number of bytes to read
66 * @retval HAL_OK on success, HAL_ERROR on failure, HAL_TIMEOUT on timeout
67 */
68HAL_StatusTypeDef flash_at25_Read(const flash_at25CS_t *s, uint32_t addr, void *buffer, uint16_t size);
69
70/**
71 * @brief Write to flash memory with sector preservation
72 * Handles page-aligned writes efficiently with proper verification
73 * Preserves existing data in flash sectors by reading, modifying, and writing back
74 *
75 * This function implements a read-modify-write approach:
76 * 1. Calculates which 256 Page needs to be erased based on addr & size
77 * 2. For each affected Page: reads it, resets bytes to 0xFF where new data will be written
78 * 3. Erases the sector and writes back the modified sector data
79 * 4. Then writes the new data to the prepared sectors
80 *
81 * Handles sector boundaries automatically - if data crosses sector boundary, both sectors are processed
82 *
83 * Includes boundary checking, write enable verification, and proper timeout handling
84 * @param s Pointer to flash configuration structure
85 * @param addr Starting address to write to (24-bit address, 0x000000 to 0x07FFFF for 512KB)
86 * @param data Pointer to source data buffer
87 * @param size Number of bytes to write
88 * @retval HAL_OK on success, HAL_ERROR on failure, HAL_TIMEOUT on timeout
89 */
90HAL_StatusTypeDef flash_at25_Write(const flash_at25CS_t *s, uint32_t addr, const void *data, uint16_t size);
91
92
93#endif /* INC_FLASH12_H_ */
int8_t flash_at25_Is(flash_at25CS_t *s, int8_t tryInit)
check if flash chip is present or not
Definition flash_at25.c:133
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...
Definition flash_at25.c:140
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...
Definition flash_at25.c:229
HAL_StatusTypeDef flash_at25_Init(flash_at25CS_t *s)
Initialization for AT25EU0041A flash chip Performs proper device identification and verification Suit...
Definition flash_at25.c:98
Configuration and state descriptor for one AT25EU0041A flash chip. Pass a pointer to this struct to e...
Definition flash_at25.h:34
uint16_t CSPin
Definition flash_at25.h:37
SPI_HandleTypeDef * Spi
Definition flash_at25.h:35
GPIO_TypeDef * CSPort
Definition flash_at25.h:36
uint32_t Size
Definition flash_at25.h:39