Skip to content

Commit

Permalink
Added support for SEGGER_OPEN_GetFlashInfo for runtime sector info
Browse files Browse the repository at this point in the history
  • Loading branch information
itzandroidtab committed Oct 9, 2023
1 parent 11f1b2f commit 6c6e673
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
44 changes: 41 additions & 3 deletions flash/flash_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
*/
#define CUSTOM_VERIFY (false)

/**
* @brief Allow changes in the sector arrangement on
*
*/
#define RUNTIME_SECTORS (false)


/**
* @brief Device specific infomation
*
Expand Down Expand Up @@ -76,7 +83,7 @@ const __attribute__ ((section("DevDscr"), __used__)) flash_device FlashDevice =
// flash sectors
{
{0x00000400, 0x00000000},
end_of_sectors
device::end_of_sectors
}
};

Expand Down Expand Up @@ -107,18 +114,24 @@ 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
*
*/
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<uint32_t>(FeedWatchdog),
reinterpret_cast<uint32_t>(Init),
reinterpret_cast<uint32_t>(UnInit),
Expand All @@ -132,6 +145,7 @@ const uint32_t SEGGER_OFL_Api[13] __attribute__ ((section ("PrgCode"), __used__)
reinterpret_cast<uint32_t>(SEGGER_OPEN_Program),
reinterpret_cast<uint32_t>(UNIFORM_ERASE_FUNC),
reinterpret_cast<uint32_t>(nullptr), // SEGGER_OPEN_Start for turbo mode
reinterpret_cast<uint32_t>(RUNTIME_SECTORS_FUNC),
};

void __attribute__ ((noinline)) FeedWatchdog(void) {
Expand Down Expand Up @@ -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
77 changes: 63 additions & 14 deletions flash/flash_os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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];
};

/**
Expand All @@ -84,7 +124,7 @@ struct flash_device {
*/
extern "C" {
/**
* @brief Keil / CMSIS API
* @brief Keil / SEGGER API / CMSIS API
*
*/

Expand Down Expand Up @@ -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

0 comments on commit 6c6e673

Please sign in to comment.