Skip to content

Commit 3b39efb

Browse files
committed
Add stm32-code-gen-flash.
1 parent b40010f commit 3b39efb

File tree

2 files changed

+155
-0
lines changed
  • docs/code_gen/stm32
  • i18n/en/docusaurus-plugin-content-docs/current/code_gen/stm32

2 files changed

+155
-0
lines changed

docs/code_gen/stm32/flash.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
id: stm32-code-gen-flash
3+
title: Flash数据库
4+
sidebar_position: 1
5+
---
6+
7+
# Flash数据库
8+
9+
在上一步代码生成后,会出现`User/flash_map.hpp`,该文件记录了STM32的Flash地址映射表,格式如下:
10+
11+
```cpp
12+
#pragma once
13+
// Auto-generated Flash Layout Map
14+
// MCU: STM32F407IGH6
15+
16+
#include "main.h"
17+
18+
#include "stm32_flash.hpp"
19+
20+
constexpr LibXR::FlashSector FLASH_SECTORS[] = {
21+
{0x08000000, 0x00004000},
22+
{0x08004000, 0x00004000},
23+
{0x08008000, 0x00004000},
24+
{0x0800C000, 0x00004000},
25+
{0x08010000, 0x00010000},
26+
{0x08020000, 0x00020000},
27+
{0x08040000, 0x00020000},
28+
{0x08060000, 0x00020000},
29+
{0x08080000, 0x00020000},
30+
{0x080A0000, 0x00020000},
31+
{0x080C0000, 0x00020000},
32+
{0x080E0000, 0x00020000},
33+
};
34+
35+
constexpr size_t FLASH_SECTOR_NUMBER = sizeof(FLASH_SECTORS) / sizeof(LibXR::FlashSector);
36+
```
37+
38+
## 创建Flash对象
39+
40+
第一个模板参数是flash的总扇区数,第二个是数据库的起始扇区编号。FLASH_SECTOR_NUMBER - 1意味着取最后两个扇区。此对象提供了擦除和读写flash的接口。
41+
42+
```cpp
43+
// app_main.cpp
44+
/* User Code Begin 3 */
45+
STM32Flash<FLASH_SECTOR_NUMBER, FLASH_SECTOR_NUMBER - 1> flash(FLASH_SECTORS);
46+
```
47+
48+
## 创建数据库对象
49+
50+
对于STM32F1/F4等型号,使用DatabaseRaw。模板参数代表了flash的最小写入粒度。
51+
52+
```cpp
53+
LibXR::DatabaseRaw<4> database(flash);
54+
```
55+
56+
对于STM32G4/L4等flash不支持逆序写入的型号,请使用DatabaseRawSequential。第二个参数为可选,代表最大缓冲区大小。
57+
58+
```cpp
59+
LibXR::DatabaseRawSequential database(flash, 128);
60+
```
61+
62+
## 创建数据库键值
63+
64+
模板参数为键值存储的数据类型,第二个参数为键名,第三个参数为默认值。键值可自动转换成对应的类型,也可通过key.data_获取原始数据。
65+
66+
```cpp
67+
Database::Key<uint32_t> key1(database, "key1", 0);
68+
```
69+
70+
## 写入数据库
71+
72+
```cpp
73+
key1.set(key1.data_ + 1);
74+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)