From 6c6e6739f5eee6d3606709aa03e679a7fe69e75a Mon Sep 17 00:00:00 2001 From: itzandroidtab Date: Mon, 9 Oct 2023 23:57:10 +0200 Subject: [PATCH] Added support for SEGGER_OPEN_GetFlashInfo for runtime sector info --- flash/flash_device.cpp | 44 ++++++++++++++++++++++-- flash/flash_os.hpp | 77 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 104 insertions(+), 17 deletions(-) diff --git a/flash/flash_device.cpp b/flash/flash_device.cpp index 2e0fa7a..cbf2404 100644 --- a/flash/flash_device.cpp +++ b/flash/flash_device.cpp @@ -45,6 +45,13 @@ */ #define CUSTOM_VERIFY (false) +/** + * @brief Allow changes in the sector arrangement on + * + */ +#define RUNTIME_SECTORS (false) + + /** * @brief Device specific infomation * @@ -76,7 +83,7 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice = // flash sectors { {0x00000400, 0x00000000}, - end_of_sectors + device::end_of_sectors } }; @@ -107,6 +114,12 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice = #define UNIFORM_ERASE_FUNC nullptr #endif +#if RUNTIME_SECTORS + #define RUNTIME_SECTORS_FUNC SEGGER_OPEN_GetFlashInfo +#else + #define RUNTIME_SECTORS_FUNC nullptr +#endif + /** * @brief array with all the functions for the segger software * @@ -114,11 +127,11 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice = extern "C" { // declaration for the OFL Api. If we initialize it here we get // a wrong name in the symbol table - extern const uint32_t SEGGER_OFL_Api[13]; + extern const uint32_t SEGGER_OFL_Api[]; } // definition of OFL Api -const uint32_t SEGGER_OFL_Api[13] __attribute__ ((section ("PrgCode"), __used__)) = { +const uint32_t SEGGER_OFL_Api[] __attribute__ ((section ("PrgCode"), __used__)) = { reinterpret_cast(FeedWatchdog), reinterpret_cast(Init), reinterpret_cast(UnInit), @@ -132,6 +145,7 @@ const uint32_t SEGGER_OFL_Api[13] __attribute__ ((section ("PrgCode"), __used__) reinterpret_cast(SEGGER_OPEN_Program), reinterpret_cast(UNIFORM_ERASE_FUNC), reinterpret_cast(nullptr), // SEGGER_OPEN_Start for turbo mode + reinterpret_cast(RUNTIME_SECTORS_FUNC), }; void __attribute__ ((noinline)) FeedWatchdog(void) { @@ -236,4 +250,28 @@ int __attribute__ ((noinline)) SEGGER_OPEN_Program(uint32_t address, uint32_t si return size; } +#endif + +#if RUNTIME_SECTORS + int __attribute__ ((noinline, __used__)) SEGGER_OPEN_GetFlashInfo(flash_info *const info, uint32_t InfoAreaSize) { + // set the sector count (max is 7) + info->count = 7; + + // set the flash data + for (uint32_t i = 0; i < info->count; i++) { + // update every sector + info->sectors[i] = { + // set the start offset for the current sector + .offset = i * 0x100, + + // set the sector size + .size = 0x100, + + // set the amount of sectors in the section + .amount = 10, + }; + } + + return 0; + } #endif \ No newline at end of file diff --git a/flash/flash_os.hpp b/flash/flash_os.hpp index caa362d..5411f73 100644 --- a/flash/flash_os.hpp +++ b/flash/flash_os.hpp @@ -10,6 +10,11 @@ constexpr static uint16_t flash_drv_version = 0x101; // to allow more sectors in the flash device constexpr static uint32_t max_sectors = 4; +// max amount of sectors in the flash info. Should not be +// modified as the j-link software only supports up to 7 +constexpr static uint32_t max_info_sectors = 7; + + /** * @brief Flash device types * @@ -23,20 +28,22 @@ enum class device_type: uint8_t { external_spi = 5 }; -/** - * @brief Information about a flash sector - * - */ -struct flash_sector { - // Sector size in bytes - uint32_t size; +namespace device { + /** + * @brief Information about a flash sector + * + */ + struct flash_sector { + // Sector size in bytes + uint32_t size; - // Address offset on the base address - uint32_t offset; -}; + // Address offset on the base address + uint32_t offset; + }; -// end of the sector list. Must be present at the end of the sector list -constexpr static flash_sector end_of_sectors = {0xffffffff, 0xffffffff}; + // end of the sector list. Must be present at the end of the sector list + constexpr static flash_sector end_of_sectors = {0xffffffff, 0xffffffff}; +} /** * @brief Information about the flash device @@ -74,7 +81,40 @@ struct flash_device { uint32_t erase_timeout; // flash sector layout definition - flash_sector sectors[max_sectors]; + device::flash_sector sectors[max_sectors]; +}; + +namespace info { + /** + * @brief Information about a flash sector + * + */ + struct flash_sector { + // Offset to the start of the sector + uint32_t offset; + + // Sector size in bytes + uint32_t size; + + // Amount of sectors + uint32_t amount; + }; +} + +/** + * @brief Information about the flash device when changing the + * sectors at runtime + * + */ +struct flash_info { + // reserved bytes. (J-Link does not care what they are set to) + uint32_t reserved[3]; + + // sector count + uint32_t count; + + // flash info sector layout + info::flash_sector sectors[max_info_sectors]; }; /** @@ -84,7 +124,7 @@ struct flash_device { */ extern "C" { /** - * @brief Keil / CMSIS API + * @brief Keil / SEGGER API / CMSIS API * */ @@ -200,6 +240,15 @@ extern "C" { * @return int 0 = OK, 1 = Failed */ int SEGGER_OPEN_Erase(uint32_t SectorAddr, uint32_t SectorIndex, uint32_t NumSectors); + + /** + * @brief Get the runtime Flash Info + * + * @param pInfo + * @param InfoAreaSize + * @return int + */ + int SEGGER_OPEN_GetFlashInfo(flash_info *const info, uint32_t InfoAreaSize); } #endif \ No newline at end of file