Skip to content

Commit a916912

Browse files
committed
Fix FMC flash bounds and logging
Corrected the GD32 FMC end-of-flash address calculation to avoid an off-by-one bounds issue in flash reads. Added targeted debug tracing around the flash read/write paths and cleaned up redundant debug entry/exit calls. The config-store state log now reports the idle state after a successful flash write, keeping the diagnostics and flash boundary checks aligned with the actual device layout.
1 parent 71ab57d commit a916912

4 files changed

Lines changed: 11 additions & 12 deletions

File tree

lib-configstore/device/gd32/rom/storedevice.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,9 @@ bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& r
8080
}
8181

8282
bool StoreDevice::Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result) {
83-
CONFIGSTORE_DEBUG_ENTRY();
84-
8583
flashcode::Result flashrom_result;
8684
const auto kState = FlashCode::Write(offset, buffer, flashrom_result);
8785

8886
result = static_cast<storedevice::Result>(flashrom_result);
89-
90-
CONFIGSTORE_DEBUG_EXIT();
9187
return kState;
9288
}

lib-configstore/include/configstore.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,12 @@ class ConfigStore : StoreDevice {
518518
}
519519

520520
static void Timer([[maybe_unused]] TimerHandle_t timer_handle) {
521-
CONFIGSTORE_DEBUG_ENTRY();
522-
523521
if (!Instance().Commit()) {
524522
Instance().TimerStop();
525523

526524
CONFIGSTORE_DEBUG_EXIT();
527525
return;
528526
}
529-
530-
CONFIGSTORE_DEBUG_EXIT();
531527
}
532528

533529
void TimerStart() {
@@ -561,8 +557,6 @@ class ConfigStore : StoreDevice {
561557
}
562558

563559
bool Flash() {
564-
CONFIGSTORE_DEBUG_PUTS(kStateNames[static_cast<unsigned int>(s_state)]);
565-
566560
if (__builtin_expect((s_state == State::kIdle), 1)) {
567561
return false;
568562
}
@@ -596,6 +590,7 @@ class ConfigStore : StoreDevice {
596590
storedevice::Result result;
597591
if (StoreDevice::Write(s_start_address, std::span{s_store}.first<sizeof(ConfigurationStore)>(), result)) {
598592
s_state = State::kIdle;
593+
CONFIGSTORE_DEBUG_PUTS(kStateNames[static_cast<unsigned int>(s_state)]);
599594
return false;
600595
}
601596
assert(result == storedevice::Result::kOk);

lib-flashcode/src/gd32/flashcode.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ uint32_t FlashCode::GetSectorSize() const {
6262
}
6363

6464
bool FlashCode::Read(uint32_t offset, std::span<uint8_t> buffer, flashcode::Result& result) {
65+
FLASHCODE_DEBUG_ENTRY();
66+
FLASHCODE_DEBUG_PRINTF("offset=%u", static_cast<unsigned>(offset));
67+
6568
const auto kStatus = gd32::fmc::Read(offset, buffer); // Blocking
6669
result = kStatus ? flashcode::Result::kOk : flashcode::Result::kError;
70+
71+
FLASHCODE_DEBUG_EXIT();
6772
return true;
6873
}
6974

lib-gd32/src/f/fmc/gd32_fmc.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ constexpr auto kBanK1FlashPage = 4 * k1KiB;
5151
constexpr uint32_t kStartAddress = FLASH_BASE;
5252
constexpr uint32_t kBank0StartAddress = kStartAddress;
5353
constexpr uint32_t kBank1StartAddress = 0x08080000;
54-
const uint32_t kEndAddress = (kStartAddress + (FMC_SIZE * k1KiB) - 1);
54+
const uint32_t kEndAddress = (kStartAddress + (FMC_SIZE * k1KiB));
5555

5656
enum class State { kIdle, kEraseBusy, kEraseProgram, kWriteBusy, kWriteProgram };
5757

@@ -124,9 +124,12 @@ bool Read(uint32_t offset, std::span<uint8_t> buffer) {
124124
GD32_FMC_DEBUG_ENTRY();
125125

126126
const auto kAddress = offset + FLASH_BASE;
127+
128+
GD32_FMC_DEBUG_PRINTF("kStartAddress=%p, kAddress=%p, kEndAddress=%p", reinterpret_cast<void*>(kStartAddress), reinterpret_cast<void*>(kAddress), reinterpret_cast<void*>(kEndAddress));
127129

128130
if (buffer.empty() || ((buffer.size() % sizeof(uint32_t)) != 0) || (kAddress < kStartAddress) || (kAddress >= kEndAddress) || (buffer.size() > (kEndAddress - kAddress))) {
129-
return false;
131+
GD32_FMC_DEBUG_EXIT();
132+
return false;
130133
}
131134

132135
assert((reinterpret_cast<uintptr_t>(buffer.data()) % alignof(uint32_t)) == 0);

0 commit comments

Comments
 (0)