Skip to content

Commit 6c6e673

Browse files
committed
Added support for SEGGER_OPEN_GetFlashInfo for runtime sector info
1 parent 11f1b2f commit 6c6e673

File tree

2 files changed

+104
-17
lines changed

2 files changed

+104
-17
lines changed

flash/flash_device.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
*/
4646
#define CUSTOM_VERIFY (false)
4747

48+
/**
49+
* @brief Allow changes in the sector arrangement on
50+
*
51+
*/
52+
#define RUNTIME_SECTORS (false)
53+
54+
4855
/**
4956
* @brief Device specific infomation
5057
*
@@ -76,7 +83,7 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice =
7683
// flash sectors
7784
{
7885
{0x00000400, 0x00000000},
79-
end_of_sectors
86+
device::end_of_sectors
8087
}
8188
};
8289

@@ -107,18 +114,24 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice =
107114
#define UNIFORM_ERASE_FUNC nullptr
108115
#endif
109116

117+
#if RUNTIME_SECTORS
118+
#define RUNTIME_SECTORS_FUNC SEGGER_OPEN_GetFlashInfo
119+
#else
120+
#define RUNTIME_SECTORS_FUNC nullptr
121+
#endif
122+
110123
/**
111124
* @brief array with all the functions for the segger software
112125
*
113126
*/
114127
extern "C" {
115128
// declaration for the OFL Api. If we initialize it here we get
116129
// a wrong name in the symbol table
117-
extern const uint32_t SEGGER_OFL_Api[13];
130+
extern const uint32_t SEGGER_OFL_Api[];
118131
}
119132

120133
// definition of OFL Api
121-
const uint32_t SEGGER_OFL_Api[13] __attribute__ ((section ("PrgCode"), __used__)) = {
134+
const uint32_t SEGGER_OFL_Api[] __attribute__ ((section ("PrgCode"), __used__)) = {
122135
reinterpret_cast<uint32_t>(FeedWatchdog),
123136
reinterpret_cast<uint32_t>(Init),
124137
reinterpret_cast<uint32_t>(UnInit),
@@ -132,6 +145,7 @@ const uint32_t SEGGER_OFL_Api[13] __attribute__ ((section ("PrgCode"), __used__)
132145
reinterpret_cast<uint32_t>(SEGGER_OPEN_Program),
133146
reinterpret_cast<uint32_t>(UNIFORM_ERASE_FUNC),
134147
reinterpret_cast<uint32_t>(nullptr), // SEGGER_OPEN_Start for turbo mode
148+
reinterpret_cast<uint32_t>(RUNTIME_SECTORS_FUNC),
135149
};
136150

137151
void __attribute__ ((noinline)) FeedWatchdog(void) {
@@ -236,4 +250,28 @@ int __attribute__ ((noinline)) SEGGER_OPEN_Program(uint32_t address, uint32_t si
236250

237251
return size;
238252
}
253+
#endif
254+
255+
#if RUNTIME_SECTORS
256+
int __attribute__ ((noinline, __used__)) SEGGER_OPEN_GetFlashInfo(flash_info *const info, uint32_t InfoAreaSize) {
257+
// set the sector count (max is 7)
258+
info->count = 7;
259+
260+
// set the flash data
261+
for (uint32_t i = 0; i < info->count; i++) {
262+
// update every sector
263+
info->sectors[i] = {
264+
// set the start offset for the current sector
265+
.offset = i * 0x100,
266+
267+
// set the sector size
268+
.size = 0x100,
269+
270+
// set the amount of sectors in the section
271+
.amount = 10,
272+
};
273+
}
274+
275+
return 0;
276+
}
239277
#endif

flash/flash_os.hpp

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ constexpr static uint16_t flash_drv_version = 0x101;
1010
// to allow more sectors in the flash device
1111
constexpr static uint32_t max_sectors = 4;
1212

13+
// max amount of sectors in the flash info. Should not be
14+
// modified as the j-link software only supports up to 7
15+
constexpr static uint32_t max_info_sectors = 7;
16+
17+
1318
/**
1419
* @brief Flash device types
1520
*
@@ -23,20 +28,22 @@ enum class device_type: uint8_t {
2328
external_spi = 5
2429
};
2530

26-
/**
27-
* @brief Information about a flash sector
28-
*
29-
*/
30-
struct flash_sector {
31-
// Sector size in bytes
32-
uint32_t size;
31+
namespace device {
32+
/**
33+
* @brief Information about a flash sector
34+
*
35+
*/
36+
struct flash_sector {
37+
// Sector size in bytes
38+
uint32_t size;
3339

34-
// Address offset on the base address
35-
uint32_t offset;
36-
};
40+
// Address offset on the base address
41+
uint32_t offset;
42+
};
3743

38-
// end of the sector list. Must be present at the end of the sector list
39-
constexpr static flash_sector end_of_sectors = {0xffffffff, 0xffffffff};
44+
// end of the sector list. Must be present at the end of the sector list
45+
constexpr static flash_sector end_of_sectors = {0xffffffff, 0xffffffff};
46+
}
4047

4148
/**
4249
* @brief Information about the flash device
@@ -74,7 +81,40 @@ struct flash_device {
7481
uint32_t erase_timeout;
7582

7683
// flash sector layout definition
77-
flash_sector sectors[max_sectors];
84+
device::flash_sector sectors[max_sectors];
85+
};
86+
87+
namespace info {
88+
/**
89+
* @brief Information about a flash sector
90+
*
91+
*/
92+
struct flash_sector {
93+
// Offset to the start of the sector
94+
uint32_t offset;
95+
96+
// Sector size in bytes
97+
uint32_t size;
98+
99+
// Amount of sectors
100+
uint32_t amount;
101+
};
102+
}
103+
104+
/**
105+
* @brief Information about the flash device when changing the
106+
* sectors at runtime
107+
*
108+
*/
109+
struct flash_info {
110+
// reserved bytes. (J-Link does not care what they are set to)
111+
uint32_t reserved[3];
112+
113+
// sector count
114+
uint32_t count;
115+
116+
// flash info sector layout
117+
info::flash_sector sectors[max_info_sectors];
78118
};
79119

80120
/**
@@ -84,7 +124,7 @@ struct flash_device {
84124
*/
85125
extern "C" {
86126
/**
87-
* @brief Keil / CMSIS API
127+
* @brief Keil / SEGGER API / CMSIS API
88128
*
89129
*/
90130

@@ -200,6 +240,15 @@ extern "C" {
200240
* @return int 0 = OK, 1 = Failed
201241
*/
202242
int SEGGER_OPEN_Erase(uint32_t SectorAddr, uint32_t SectorIndex, uint32_t NumSectors);
243+
244+
/**
245+
* @brief Get the runtime Flash Info
246+
*
247+
* @param pInfo
248+
* @param InfoAreaSize
249+
* @return int
250+
*/
251+
int SEGGER_OPEN_GetFlashInfo(flash_info *const info, uint32_t InfoAreaSize);
203252
}
204253

205254
#endif

0 commit comments

Comments
 (0)