AgriQL · Embedded · Flash drivers

Flash drivers

AgriQLEdge does not know your board's flash. You give it a partition and three functions. Addresses are relative to the start of the partition, and each function returns 0 on success.

int rd(void *ctx, uint32_t addr, uint8_t *buf, uint32_t len);          // read
int pr(void *ctx, uint32_t addr, const uint8_t *data, uint32_t len);   // program erased bytes
int er(void *ctx, uint32_t block);                                     // erase one block

agriql_flash_t flash = {
  nullptr,      // ctx, passed back to the functions
  1,            // program unit in bytes
  4096,         // erase block in bytes (a power of two, at least 256)
  64,           // erase blocks in the partition
  rd, pr, er
};
logbook.begin(flash, 4, true);  // 4 erase blocks per segment

program always receives an address and a length that are whole program units, over bytes that are erased. The library never asks to program a byte twice.

Geometry

ChipProgram unitErase block
ESP32-C3 / C6 / H21 (16 with flash encryption)4096
STM32L4, G4, WB, G082048
STM32F4, F71the sector size; use only the uniform 128 KB sectors (131072)
STM32H732131072
STM32U5168192
RP2040, RP23502564096
nRF52, nRF5344096
SAMD2164 (row write)256
SPI NOR (W25Q, MX25, IS25)14096

Choosing the segment size

  • A segment is the unit you forward to the gateway, and the unit lost when the log drops the oldest data.
  • The partition needs at least two segments. With 4 KB blocks and 4 blocks per segment, each segment holds 12 KB of readings (the first block holds the segment header).
  • Each frame of 16 readings has a 60-byte header, so the log uses about 3.7 times the bytes of the raw readings.

Examples in the library

  • Esp32Partition uses esp_partition_read, esp_partition_write and esp_partition_erase_range.
  • Stm32InternalFlash uses the STM32 HAL flash functions on an STM32L4.
  • AgriQLRamFlash in AgriQLEdge.h keeps the rules of NOR flash in RAM. It is useful for trying the library and for tests.