|
| 1 | +--- |
| 2 | +id: stm32-code-gen-flash |
| 3 | +title: Flash Database |
| 4 | +sidebar_position: 1 |
| 5 | +--- |
| 6 | + |
| 7 | +# Flash Database |
| 8 | + |
| 9 | +After code generation, a file named `User/flash_map.hpp` will be created. This file defines the STM32 flash sector layout, with a format similar to: |
| 10 | + |
| 11 | +```cpp |
| 12 | +#pragma once |
| 13 | +// Auto-generated Flash Layout Map |
| 14 | +// MCU: STM32F407IGH6 |
| 15 | + |
| 16 | +#include "main.h" |
| 17 | +#include "stm32_flash.hpp" |
| 18 | + |
| 19 | +constexpr LibXR::FlashSector FLASH_SECTORS[] = { |
| 20 | + {0x08000000, 0x00004000}, |
| 21 | + {0x08004000, 0x00004000}, |
| 22 | + {0x08008000, 0x00004000}, |
| 23 | + {0x0800C000, 0x00004000}, |
| 24 | + {0x08010000, 0x00010000}, |
| 25 | + {0x08020000, 0x00020000}, |
| 26 | + {0x08040000, 0x00020000}, |
| 27 | + {0x08060000, 0x00020000}, |
| 28 | + {0x08080000, 0x00020000}, |
| 29 | + {0x080A0000, 0x00020000}, |
| 30 | + {0x080C0000, 0x00020000}, |
| 31 | + {0x080E0000, 0x00020000}, |
| 32 | +}; |
| 33 | + |
| 34 | +constexpr size_t FLASH_SECTOR_NUMBER = sizeof(FLASH_SECTORS) / sizeof(LibXR::FlashSector); |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Creating a Flash Object |
| 40 | + |
| 41 | +The first template parameter specifies the total number of sectors; the second specifies the starting sector for use. `FLASH_SECTOR_NUMBER - 1` means using the last two sectors. This object provides interfaces for flash erasing and read/write operations. |
| 42 | + |
| 43 | +```cpp |
| 44 | +// app_main.cpp |
| 45 | +/* User Code Begin 3 */ |
| 46 | +STM32Flash<FLASH_SECTOR_NUMBER, FLASH_SECTOR_NUMBER - 1> flash(FLASH_SECTORS); |
| 47 | +``` |
| 48 | +
|
| 49 | +--- |
| 50 | +
|
| 51 | +## Creating a Database Object |
| 52 | +
|
| 53 | +For STM32F1/F4 series devices, use `DatabaseRaw`. The template parameter indicates the flash write granularity in bytes. |
| 54 | +
|
| 55 | +```cpp |
| 56 | +LibXR::DatabaseRaw<4> database(flash); |
| 57 | +``` |
| 58 | + |
| 59 | +For chips like STM32G4/L4 that do not support reverse overwrite in flash, use `DatabaseRawSequential`. The second parameter (optional) specifies the maximum buffer size. |
| 60 | + |
| 61 | +```cpp |
| 62 | +LibXR::DatabaseRawSequential database(flash, 128); |
| 63 | +``` |
| 64 | +
|
| 65 | +--- |
| 66 | +
|
| 67 | +## Creating a Database Key |
| 68 | +
|
| 69 | +The first template argument is the data type stored in the key. The second is the key name, and the third is the default value. Keys can be implicitly cast to their value type or accessed via `key.data_`. |
| 70 | +
|
| 71 | +```cpp |
| 72 | +Database::Key<uint32_t> key1(database, "key1", 0); |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## Writing to the Database |
| 78 | + |
| 79 | +```cpp |
| 80 | +key1.set(key1.data_ + 1); |
| 81 | +``` |
0 commit comments