Skip to content

Commit 16c7844

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 bec3bbc commit 16c7844

50 files changed

Lines changed: 562 additions & 489 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/include/json/json_jsondoc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class JsonDoc {
9595
}
9696

9797
void WriteField(const char* key, uint32_t value) {
98+
assert(key != nullptr);
99+
98100
if (!first_) {
99101
Write(",");
100102
}
@@ -121,6 +123,8 @@ class JsonDoc {
121123
}
122124

123125
void Write(const char* string) {
126+
assert(string != nullptr);
127+
124128
if (pos_ >= max_len_) {
125129
return;
126130
}

lib-artnet/include/json/artnetparamsconst.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
#include "common/utils/utils_hash.h"
2929
#include "json/json_key.h"
3030
#include "dmxnode_outputtype.h"
31-
#if defined(DMXNODE_OUTPUT_DMX)
32-
#include "dmx.h"
33-
#endif
3431

3532
namespace json {
3633
struct ArtNetParamsConst {
@@ -40,9 +37,9 @@ struct ArtNetParamsConst {
4037
#if defined(RDM_CONTROLLER) || defined(RDM_RESPONDER)
4138
static constexpr auto kEnableRdm = json::MakeSimpleKey("enable_rdm");
4239

43-
static constexpr json::PortKey kRdmEnablePortA{"rdm_enable_port_a", 17, Fnv1a32("rdm_enable_port_a", 17)};
40+
static constexpr json::PortKey kRdmEnablePortA{.name="rdm_enable_port_a", .length=17, .hash=Fnv1a32("rdm_enable_port_a", 17)};
4441
#if (DMX_MAX_PORTS > 1)
45-
static constexpr json::PortKey kRdmEnablePortB{"rdm_enable_port_b", 17, Fnv1a32("rdm_enable_port_b", 17)};
42+
static constexpr json::PortKey kRdmEnablePortB{.name="rdm_enable_port_b", .length=17, .hash=Fnv1a32("rdm_enable_port_b", 17)};
4643
#endif
4744
#if (DMX_MAX_PORTS > 2)
4845
static constexpr json::PortKey kRdmEnablePortC{"rdm_enable_port_c", 17, Fnv1a32("rdm_enable_port_c", 17)};
@@ -66,9 +63,9 @@ struct ArtNetParamsConst {
6663
#endif
6764

6865
#if defined(DMX_MAX_PORTS)
69-
static constexpr json::PortKey kDestinationIpPortA{"destination_ip_port_a", 21, Fnv1a32("destination_ip_port_a", 21)};
66+
static constexpr json::PortKey kDestinationIpPortA{.name="destination_ip_port_a", .length=21, .hash=Fnv1a32("destination_ip_port_a", 21)};
7067
#if (DMX_MAX_PORTS > 1)
71-
static constexpr json::PortKey kDestinationIpPortB{"destination_ip_port_b", 21, Fnv1a32("destination_ip_port_b", 21)};
68+
static constexpr json::PortKey kDestinationIpPortB{.name="destination_ip_port_b", .length=21, .hash=Fnv1a32("destination_ip_port_b", 21)};
7269
#endif
7370
#if (DMX_MAX_PORTS > 2)
7471
static constexpr json::PortKey kDestinationIpPortC{"destination_ip_port_c", 21, Fnv1a32("destination_ip_port_c", 21)};
@@ -90,9 +87,9 @@ struct ArtNetParamsConst {
9087
#endif
9188
};
9289

93-
static constexpr json::PortKey kProtocolPortA{"protocol_port_a", 15, Fnv1a32("protocol_port_a", 15)};
90+
static constexpr json::PortKey kProtocolPortA{.name="protocol_port_a", .length=15, .hash=Fnv1a32("protocol_port_a", 15)};
9491
#if (DMX_MAX_PORTS > 1)
95-
static constexpr json::PortKey kProtocolPortB{"protocol_port_b", 15, Fnv1a32("protocol_port_b", 15)};
92+
static constexpr json::PortKey kProtocolPortB{.name="protocol_port_b", .length=15, .hash=Fnv1a32("protocol_port_b", 15)};
9693
#endif
9794
#if (DMX_MAX_PORTS > 2)
9895
static constexpr json::PortKey kProtocolPortC{"protocol_port_c", 15, Fnv1a32("protocol_port_c", 15)};
@@ -114,9 +111,9 @@ struct ArtNetParamsConst {
114111
#endif
115112
};
116113

117-
static constexpr json::PortKey kBgDiscoveryPortA{"bg_discovery_port_a", 19, Fnv1a32("bg_discovery_port_a", 19)};
114+
static constexpr json::PortKey kBgDiscoveryPortA{.name="bg_discovery_port_a", .length=19, .hash=Fnv1a32("bg_discovery_port_a", 19)};
118115
#if (DMX_MAX_PORTS > 1)
119-
static constexpr json::PortKey kBgDiscoveryPortB{"bg_discovery_port_b", 19, Fnv1a32("bg_discovery_port_b", 19)};
116+
static constexpr json::PortKey kBgDiscoveryPortB{.name="bg_discovery_port_b", .length=19, .hash=Fnv1a32("bg_discovery_port_b", 19)};
120117
#endif
121118
#if (DMX_MAX_PORTS > 2)
122119
static constexpr json::PortKey kBgDiscoveryPortC{"bg_discovery_port_c", 19, Fnv1a32("bg_discovery_port_c", 19)};

lib-artnet/src/node/failsafe/artnetnodefailsafe.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include <cstdint>
27+
#include <span>
2728

2829
#include "artnetnode.h"
2930
#include "dmxnode.h"
@@ -38,7 +39,7 @@ void ArtNetNode::FailSafeRecord() {
3839

3940
for (uint32_t port_index = 0; port_index < dmxnode::kMaxPorts; port_index++) {
4041
if (node_.port[port_index].direction == dmxnode::Direction::kOutput) {
41-
dmxnode::scenes::Write(port_index, dmxnode::Data::Backup(port_index));
42+
dmxnode::scenes::Write(port_index, std::span<const uint8_t>{dmxnode::Data::Backup(port_index), dmxnode::kUniverseSize});
4243
}
4344
}
4445

@@ -54,7 +55,7 @@ void ArtNetNode::FailSafePlayback() {
5455

5556
for (uint32_t port_index = 0; port_index < dmxnode::kMaxPorts; port_index++) {
5657
if (node_.port[port_index].direction == dmxnode::Direction::kOutput) {
57-
dmxnode::scenes::Read(port_index, const_cast<uint8_t*>(dmxnode::Data::Backup(port_index)));
58+
dmxnode::scenes::Read(port_index, std::span<uint8_t>{const_cast<uint8_t*>(dmxnode::Data::Backup(port_index)), dmxnode::kUniverseSize});
5859
dmxnode::DataOutput(dmxnode_output_type_, port_index);
5960

6061
if (!output_port_[port_index].is_transmitting) {

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
}

0 commit comments

Comments
 (0)