Skip to content

Commit c8f6d74

Browse files
committed
Refactor flash/storage I/O to use std::span
Reworked config store, flashcode, flashcode install, SPI flash commands, and DMX scene read/write paths to use `std::span` instead of raw pointer+length pairs. This improves API safety and call-site clarity while preserving behavior. The update also cleans up SPI flash internals (namespacing, command constants, probe function names), adds readiness/operation timeout handling and watchdog feeds during long flash operations, and aligns node/display conditional includes/macros so RDM and node-type builds resolve the right dependencies.
1 parent 6525206 commit c8f6d74

26 files changed

Lines changed: 317 additions & 313 deletions

File tree

lib-clib/src/gd32/time_systick/time.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extern "C" {
4545
* 1970-01-01 00:00:00 +0000 (UTC).
4646
*/
4747

48-
int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz) {
48+
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
4949
assert(tv != 0);
5050

5151
const auto kCurrentSysTickMillis = gv_systick_millis;
@@ -77,7 +77,7 @@ int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz
7777
return 0;
7878
}
7979

80-
int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct timezone* tz) {
80+
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
8181
assert(tv != 0);
8282

8383
struct timeval g;

lib-clib/src/gd32/time_timer/time.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extern "C" {
110110
* 1970-01-01 00:00:00 +0000 (UTC).
111111
*/
112112

113-
int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz) {
113+
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
114114
assert(tv != nullptr);
115115

116116
#if __CORTEX_M == 7
@@ -127,7 +127,7 @@ int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz
127127
return 0;
128128
}
129129

130-
int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct timezone* tz) {
130+
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
131131
assert(tv != nullptr);
132132

133133
// Disable the timer interrupt to prevent it from triggering while we adjust the counter

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <cstdint>
3131
#include <cstdio>
32+
#include <span>
3233
#include <cassert>
3334

3435
#include "configstoredevice.h"
@@ -61,29 +62,30 @@ uint32_t StoreDevice::GetSectorSize() const {
6162
return kFlashSectorSize;
6263
}
6364

64-
bool StoreDevice::Read(__attribute__((unused)) uint32_t offset, __attribute__((unused)) uint32_t length, __attribute__((unused)) uint8_t* buffer, storedevice::Result& result) {
65+
bool StoreDevice::Read([[maybe_unused]] uint32_t offset, [[maybe_unused]] std::span<uint8_t> buffer, storedevice::Result& result) {
6566
CONFIGSTORE_DEBUG_ENTRY();
66-
assert((offset + length) <= BSRAM_SIZE);
67+
assert(offset <= BSRAM_SIZE);
68+
assert(buffer.size() <= (BSRAM_SIZE - offset));
6769

6870
result = storedevice::Result::kOk;
6971

7072
CONFIGSTORE_DEBUG_EXIT();
7173
return true;
7274
}
7375

74-
bool StoreDevice::Erase(__attribute__((unused)) uint32_t offset, __attribute__((unused)) uint32_t length, storedevice::Result& result) {
76+
bool StoreDevice::Erase([[maybe_unused]] uint32_t offset, [[maybe_unused]] uint32_t length, storedevice::Result& result) {
7577
CONFIGSTORE_DEBUG_ENTRY();
7678

7779
result = storedevice::Result::kOk;
78-
7980

8081
CONFIGSTORE_DEBUG_EXIT();
8182
return true;
8283
}
8384

84-
bool StoreDevice::Write(__attribute__((unused)) uint32_t offset, __attribute__((unused)) uint32_t length, __attribute__((unused)) const uint8_t* buffer, storedevice::Result& result) {
85+
bool StoreDevice::Write([[maybe_unused]] uint32_t offset, [[maybe_unused]] std::span<const uint8_t> buffer, storedevice::Result& result) {
8586
CONFIGSTORE_DEBUG_ENTRY();
86-
assert((offset + length) <= BSRAM_SIZE);
87+
assert(offset <= BSRAM_SIZE);
88+
assert(buffer.size() <= (BSRAM_SIZE - offset));
8789

8890
result = storedevice::Result::kOk;
8991

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#endif
2929

3030
#include <cstdint>
31+
#include <span>
3132

3233
#include "configstoredevice.h"
3334
#include "flashcode.h"
@@ -54,11 +55,11 @@ uint32_t StoreDevice::GetSectorSize() const {
5455
return FlashCode::GetSectorSize();
5556
}
5657

57-
bool StoreDevice::Read(uint32_t offset, uint32_t length, uint8_t* buffer, storedevice::Result& result) {
58+
bool StoreDevice::Read(uint32_t offset, std::span<uint8_t> buffer, storedevice::Result& result) {
5859
CONFIGSTORE_DEBUG_ENTRY();
5960

6061
flashcode::Result flashrom_result;
61-
const auto kState = FlashCode::Read(offset, length, buffer, flashrom_result);
62+
const auto kState = FlashCode::Read(offset, buffer, flashrom_result);
6263

6364
result = static_cast<storedevice::Result>(flashrom_result);
6465

@@ -78,11 +79,11 @@ bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& r
7879
return kState;
7980
}
8081

81-
bool StoreDevice::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, storedevice::Result& result) {
82+
bool StoreDevice::Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result) {
8283
CONFIGSTORE_DEBUG_ENTRY();
8384

8485
flashcode::Result flashrom_result;
85-
const auto kState = FlashCode::Write(offset, length, buffer, flashrom_result);
86+
const auto kState = FlashCode::Write(offset, buffer, flashrom_result);
8687

8788
result = static_cast<storedevice::Result>(flashrom_result);
8889

lib-configstore/device/i2c/storedevice.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ uint32_t StoreDevice::GetSectorSize() const {
7272
return storedevice::kFlashSectorSize;
7373
}
7474

75-
bool StoreDevice::Read(uint32_t offset, uint32_t length, uint8_t* buffer, storedevice::Result& result) {
75+
bool StoreDevice::Read(uint32_t offset, std::span<uint8_t> buffer, storedevice::Result& result) {
7676
CONFIGSTORE_DEBUG_ENTRY();
77-
assert((offset + length) <= storedevice::ROM_SIZE);
7877

79-
AT24C32::Read(offset, buffer, length);
78+
assert((offset + buffer.size()) <= storedevice::kRomSize);
79+
80+
AT24C32::Read(offset, buffer);
8081

8182
result = storedevice::Result::kOk;
8283

@@ -93,11 +94,12 @@ bool StoreDevice::Erase([[maybe_unused]] uint32_t offset, [[maybe_unused]] uint3
9394
return true;
9495
}
9596

96-
bool StoreDevice::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, storedevice::Result& result) {
97+
bool StoreDevice::Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result) {
9798
CONFIGSTORE_DEBUG_ENTRY();
98-
assert((offset + length) <= ROM_SIZE);
9999

100-
AT24C32::Write(offset, buffer, length);
100+
assert((offset + buffer.size()) <= storedevice::kRomSize);
101+
102+
AT24C32::Write(offset, buffer);
101103

102104
result = storedevice::Result::kOk;
103105

lib-configstore/device/spi/storedevice.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <cstdint>
3131
#include <cstdio>
32+
#include <span>
3233

3334
#include "configstoredevice.h"
3435
#include "spi/spi_flash.h"
@@ -40,11 +41,8 @@ StoreDevice::StoreDevice() {
4041
if (!spi::flash::Probe()) {
4142
puts("StoreDevice: No SPI flash chip.");
4243
} else {
43-
printf("StoreDevice: SPI flash %s sector size %u total %u bytes [%u kB]\n",
44-
spi::flash::Name(),
45-
static_cast<unsigned int>(spi::flash::SectorSize()),
46-
static_cast<unsigned int>(spi::flash::Size()),
47-
static_cast<unsigned int>(spi::flash::Size() / 1024U));
44+
printf("StoreDevice: SPI flash %s sector size %u total %u bytes [%u kB]\n", spi::flash::Name(), static_cast<unsigned int>(spi::flash::SectorSize()), static_cast<unsigned int>(spi::flash::Size()),
45+
static_cast<unsigned int>(spi::flash::Size() / 1024U));
4846
detected_ = true;
4947
}
5048

@@ -64,10 +62,10 @@ uint32_t StoreDevice::GetSectorSize() const {
6462
return spi::flash::SectorSize();
6563
}
6664

67-
bool StoreDevice::Read(uint32_t offset, uint32_t length, uint8_t* buffer, storedevice::Result& result) {
65+
bool StoreDevice::Read(uint32_t offset, std::span<uint8_t> buffer, storedevice::Result& result) {
6866
CONFIGSTORE_DEBUG_ENTRY();
6967

70-
result = spi::flash::cmd::Read(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
68+
result = spi::flash::cmd::Read(offset, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
7169

7270
CONFIGSTORE_DEBUG_PRINTF("result=%d", static_cast<int>(result));
7371
CONFIGSTORE_DEBUG_EXIT();
@@ -84,10 +82,10 @@ bool StoreDevice::Erase(uint32_t offset, uint32_t length, storedevice::Result& r
8482
return true;
8583
}
8684

87-
bool StoreDevice::Write(uint32_t offset, uint32_t length, const uint8_t* buffer, storedevice::Result& result) {
85+
bool StoreDevice::Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result) {
8886
CONFIGSTORE_DEBUG_ENTRY();
8987

90-
result = spi::flash::cmd::Write(offset, length, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
88+
result = spi::flash::cmd::Write(offset, buffer) ? storedevice::Result::kOk : storedevice::Result::kError;
9189

9290
CONFIGSTORE_DEBUG_PRINTF("result=%d", static_cast<int>(result));
9391
CONFIGSTORE_DEBUG_EXIT();

lib-configstore/include/configstore.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <cstdint>
3030
#include <cstring>
31+
#include <span>
3132
#include <cassert>
3233

3334
#include "configstoredevice.h"
@@ -92,7 +93,7 @@ class ConfigStore : StoreDevice {
9293
CONFIGSTORE_DEBUG_PRINTF("s_start_address=%p", reinterpret_cast<void*>(s_start_address));
9394

9495
storedevice::Result result;
95-
while (!StoreDevice::Read(s_start_address, kStoreSize, reinterpret_cast<uint8_t*>(&s_store), result)) {
96+
while (!StoreDevice::Read(s_start_address, std::span{s_store}, result)) {
9697
}
9798
assert(result == storedevice::Result::kOk);
9899
}
@@ -593,7 +594,7 @@ class ConfigStore : StoreDevice {
593594
break;
594595
case State::kWriting: {
595596
storedevice::Result result;
596-
if (StoreDevice::Write(s_start_address, sizeof(ConfigurationStore), reinterpret_cast<uint8_t*>(&s_store), result)) {
597+
if (StoreDevice::Write(s_start_address, std::span{s_store}.first<sizeof(ConfigurationStore)>(), result)) {
597598
s_state = State::kIdle;
598599
return false;
599600
}

lib-configstore/include/configstoredevice.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define CONFIGSTOREDEVICE_H_
2828

2929
#include <cstdint>
30+
#include <span>
3031

3132
namespace storedevice {
3233
enum class Result { kOk, kError };
@@ -49,9 +50,9 @@ class StoreDevice {
4950
[[nodiscard]] uint32_t GetSectorSize() const;
5051
[[nodiscard]] uint32_t GetSize() const;
5152

52-
bool Read(uint32_t offset, uint32_t length, uint8_t* buffer, storedevice::Result& result);
53+
bool Read(uint32_t offset, std::span<uint8_t> buffer, storedevice::Result& result);
5354
bool Erase(uint32_t offset, uint32_t length, storedevice::Result& result);
54-
bool Write(uint32_t offset, uint32_t length, const uint8_t* buffer, storedevice::Result& result);
55+
bool Write(uint32_t offset, std::span<const uint8_t> buffer, storedevice::Result& result);
5556

5657
private:
5758
bool detected_{false};

lib-flash/include/dummy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#define DUMMY_H_
33

44

5-
#endif // DUMMY_H_
5+
#endif // DUMMY_H_

0 commit comments

Comments
 (0)