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
| Chip | Program unit | Erase block |
|---|---|---|
| ESP32-C3 / C6 / H2 | 1 (16 with flash encryption) | 4096 |
| STM32L4, G4, WB, G0 | 8 | 2048 |
| STM32F4, F7 | 1 | the sector size; use only the uniform 128 KB sectors (131072) |
| STM32H7 | 32 | 131072 |
| STM32U5 | 16 | 8192 |
| RP2040, RP2350 | 256 | 4096 |
| nRF52, nRF53 | 4 | 4096 |
| SAMD21 | 64 (row write) | 256 |
| SPI NOR (W25Q, MX25, IS25) | 1 | 4096 |
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
Esp32Partitionusesesp_partition_read,esp_partition_writeandesp_partition_erase_range.Stm32InternalFlashuses the STM32 HAL flash functions on an STM32L4.AgriQLRamFlashinAgriQLEdge.hkeeps the rules of NOR flash in RAM. It is useful for trying the library and for tests.