Skip to content

Commit fc7aa01

Browse files
committed
Tidy casts and formatting across drivers
Cleans up mixed formatting and explicit integer casts in config storage, firmware install, UART, and network code. Also updates config store to use the global UTC helper and refreshes a few debug messages and state names for consistency.
1 parent 6ed8c37 commit fc7aa01

9 files changed

Lines changed: 191 additions & 250 deletions

File tree

lib-configstore/include/configstore.h

Lines changed: 106 additions & 194 deletions
Large diffs are not rendered by default.

lib-flashcodeinstall/src/firmware.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file firmware.cpp
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -33,76 +33,75 @@
3333

3434
#include "firmware.h"
3535
#include "ubootheader.h"
36-
#include "firmware/debug/debug_debug.h"
36+
#include "firmware/debug/debug_debug.h"
3737

3838
namespace firmware {
3939
enum class State {
40-
IDLE, START, CONTINUE
40+
kIdle, kStart, kContinue
4141
};
4242

43-
static auto s_State = State::IDLE;
43+
static auto s_State = State::kIdle;
4444
static uint32_t s_nCRC;
4545

46-
bool firmware_install_start(const uint8_t *pBuffer, const uint32_t nBufferSize) {
46+
bool firmware_install_start(const uint8_t *buffer, uint32_t buffer_size) {
4747
DEBUG_ENTRY();
48-
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(pBuffer), nBufferSize);
48+
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(buffer), static_cast<unsigned>(buffer_size));
4949

50-
assert(s_State == State::IDLE);
51-
s_State = State::START;
50+
assert(s_State == State::kIdle);
51+
s_State = State::kStart;
5252

53-
assert(sizeof(struct TImageHeader) <= nBufferSize);
53+
assert(sizeof(struct TImageHeader) <= buffer_size);
5454

55-
UBootHeader uBootHeader(pBuffer);
56-
uBootHeader.Dump();
55+
UBootHeader uboot_header(buffer);
56+
uboot_header.Dump();
5757

58-
const auto isValid = uBootHeader.IsValid();
59-
DEBUG_PRINTF("Firmware is valid? %s", isValid ? "Yes" : "No");
58+
const auto kIsValid = uboot_header.IsValid();
59+
DEBUG_PRINTF("Firmware is valid? %s", kIsValid ? "Yes" : "No");
6060

61-
if (!isValid) {
61+
if (!kIsValid) {
6262
DEBUG_EXIT();
6363
return false;
6464
}
6565

66-
const uint32_t nFirmwareChunk = nBufferSize - sizeof(struct TImageHeader);
66+
const uint32_t kFirmwareChunk = buffer_size - sizeof(struct TImageHeader);
6767

68-
if (nFirmwareChunk > 0) {
69-
DEBUG_PRINTF("Firmware: Chunk = %u", nFirmwareChunk);
68+
if (kFirmwareChunk > 0) {
69+
DEBUG_PRINTF("Firmware: Chunk = %u", static_cast<unsigned>(kFirmwareChunk));
7070

71-
const auto *pFirmware = pBuffer + sizeof(struct TImageHeader);
71+
const auto *firmware = buffer + sizeof(struct TImageHeader);
7272

73-
s_nCRC = crc32(0, pFirmware, nFirmwareChunk);
73+
s_nCRC = crc32(0, firmware, kFirmwareChunk);
7474
}
7575

7676
DEBUG_EXIT();
7777
return true;
7878
}
7979

80-
bool firmware_install_continue(const uint8_t *pBuffer, const uint32_t nBufferSize) {
80+
bool firmware_install_continue(const uint8_t *buffer, uint32_t buffer_size) {
8181
DEBUG_ENTRY();
82-
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(pBuffer), nBufferSize);
82+
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(buffer), static_cast<unsigned>(buffer_size));
8383

84-
assert((s_State == State::START) || (s_State == State::CONTINUE));
85-
s_State = State::CONTINUE;
84+
assert((s_State == State::kStart) || (s_State == State::kContinue));
85+
s_State = State::kContinue;
8686

87-
s_nCRC = crc32(s_nCRC, pBuffer, nBufferSize);
87+
s_nCRC = crc32(s_nCRC, buffer, buffer_size);
8888

8989
DEBUG_EXIT();
9090
return true;
9191
}
9292

93-
bool firmware_install_end(const uint8_t *pBuffer, const uint32_t nBufferSize) {
93+
bool firmware_install_end(const uint8_t *buffer, uint32_t buffer_size) {
9494
DEBUG_ENTRY();
95-
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(pBuffer), nBufferSize);
95+
DEBUG_PRINTF("Firmware: Buffer = %p, Buffer size = %u", reinterpret_cast<const void *>(buffer), static_cast<unsigned>(buffer_size));
9696

97-
assert(s_State == State::CONTINUE);
98-
s_State = State::IDLE;
97+
assert(s_State == State::kContinue);
98+
s_State = State::kIdle;
9999

100-
s_nCRC = crc32(s_nCRC, pBuffer, nBufferSize);
100+
s_nCRC = crc32(s_nCRC, buffer, buffer_size);
101101

102-
DEBUG_PRINTF("CRC: %x", s_nCRC);
102+
DEBUG_PRINTF("CRC: %x", static_cast<unsigned>(s_nCRC));
103103

104104
DEBUG_EXIT();
105105
return true;
106106
}
107-
108107
} // namespace firmware

lib-gd32/src/uart0/uart0.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <cstdint>
3030
#include <cstdio>
31+
#include <cstdarg>
3132

3233
#include "gd32_uart.h"
3334
#if defined(CONFIG_USART0_ENABLE_TX_DMA) || defined(CONFIG_USART0_ENABLE_RX_DMA)

lib-network/include/network_iface.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ void SetDomainName(const char* domainname);
4646
const char* DomainName();
4747

4848
#if defined(H3) || defined(GD32)
49-
[[nodiscard]] inline constexpr const char* InterfaceName() {
49+
[[nodiscard]] constexpr const char* InterfaceName() {
5050
return "eth0";
5151
}
5252
#else
5353
const char* InterfaceName();
5454
#endif
5555

5656
#if defined(H3) || defined(GD32)
57-
inline constexpr uint32_t InterfaceIndex() {
57+
constexpr uint32_t InterfaceIndex() {
5858
return 1;
5959
}
6060
#else
@@ -73,21 +73,25 @@ void SetAutoIp();
7373
bool AutoIp();
7474

7575
// DHCP
76-
inline constexpr bool IsDhcpCapable() {
76+
constexpr bool IsDhcpCapable() {
7777
return true;
7878
}
7979

80-
inline constexpr bool IsDhcpKnown() {
80+
constexpr bool IsDhcpKnown() {
8181
return true;
8282
}
8383

8484
void EnableDhcp();
8585
bool Dhcp();
8686

8787
inline char AddressingMode() {
88-
if (AutoIp()) return 'Z'; // Zeroconf
89-
if (IsDhcpKnown()) return Dhcp() ? 'D' : 'S'; // DHCP or Static
90-
return 'U'; // Unknown
88+
if (AutoIp()) {
89+
return 'Z'; // Zeroconf
90+
}
91+
if (IsDhcpKnown()) {
92+
return Dhcp() ? 'D' : 'S'; // DHCP or Static
93+
}
94+
return 'U'; // Unknown
9195
}
9296

9397
struct Counters {
@@ -99,7 +103,7 @@ struct Counters {
99103
} tx;
100104
};
101105

102-
void GetCounters(Counters& out);
106+
void GetCounters(Counters& counters);
103107
} // namespace network::iface
104108

105109
#endif // NETWORK_IFACE_H_

lib-network/src/apps/ntp/gd32/ptp/ntpclient.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace net::globals::ptp {
8787
extern uint32_t timestamp[2];
8888
} // namespace net::globals::ptp
8989

90-
#define _NTPFRAC_(x) (4294U * (x) + ((1981U * (x)) >> 11) + ((2911U * (x)) >> 28))
90+
#define _NTPFRAC_(x) ((4294U * (x)) + ((1981U * (x)) >> 11) + ((2911U * (x)) >> 28))
9191
#define NTPFRAC(x) _NTPFRAC_(x / 1000)
9292
// The reverse of the above, needed if we want to set our microsecond
9393
// clock (via clock_settime) based on the incoming time in NTP format.
@@ -130,7 +130,15 @@ static void Print([[maybe_unused]] const char* text, [[maybe_unused]] const stru
130130
#ifndef NDEBUG
131131
const auto kSeconds = static_cast<time_t>(timestamp->seconds - ntp::kJan1970);
132132
const auto* local_time = localtime(&kSeconds);
133-
printf("%s %02d:%02d:%02d.%06d %04d [%u][0x%.8x]\n", text, local_time->tm_hour, local_time->tm_min, local_time->tm_sec, USEC(timestamp->fraction), local_time->tm_year + 1900, timestamp->seconds, timestamp->fraction);
133+
printf("%s %02d:%02d:%02d.%06d %04d [%u][0x%.8x]\n",
134+
text,
135+
static_cast<int>(local_time->tm_hour),
136+
static_cast<int>(local_time->tm_min),
137+
static_cast<int>(local_time->tm_sec),
138+
static_cast<int>(USEC(timestamp->fraction)),
139+
local_time->tm_year + 1900,
140+
static_cast<int>(timestamp->seconds),
141+
static_cast<unsigned>(timestamp->fraction));
134142
#endif
135143
}
136144

@@ -237,9 +245,13 @@ static void Send() {
237245
network::udp::SendWithTimestamp(s_ntp_client.handle, reinterpret_cast<const uint8_t*>(&s_ntp_client.request), kRequestSize, s_ntp_client.server_ip, network::iana::Ports::kPortNtp);
238246

239247
#ifndef NDEBUG
240-
printf("Request: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n", __builtin_bswap32(s_ntp_client.request.origin_timestamp_s), __builtin_bswap32(s_ntp_client.request.origin_timestamp_f),
241-
__builtin_bswap32(s_ntp_client.request.receive_timestamp_s), __builtin_bswap32(s_ntp_client.request.receive_timestamp_f), __builtin_bswap32(s_ntp_client.request.transmit_timestamp_s),
242-
__builtin_bswap32(s_ntp_client.request.transmit_timestamp_f));
248+
printf("Request: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n",
249+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.origin_timestamp_s)), // NOLINT
250+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.origin_timestamp_f)), // NOLINT
251+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.receive_timestamp_s)), // NOLINT
252+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.receive_timestamp_f)), // NOLINT
253+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.transmit_timestamp_s)), // NOLINT
254+
static_cast<unsigned>(__builtin_bswap32(s_ntp_client.request.transmit_timestamp_f))); // NOLINT
243255
#endif
244256

245257
if (s_ntp_client.state.x > 0) {
@@ -273,10 +285,12 @@ static inline int32_t AbsInt32(int32_t x) {
273285
}
274286

275287
static void UpdatePtpTime() {
276-
int32_t diff_seconds1, diff_nano_seconds1;
288+
int32_t diff_seconds1;
289+
int32_t diff_nano_seconds1;
277290
Difference(s_ntp_client.t1, s_ntp_client.t2, diff_seconds1, diff_nano_seconds1);
278291

279-
int32_t diff_seconds2, diff_nano_seconds2;
292+
int32_t diff_seconds2;
293+
int32_t diff_nano_seconds2;
280294
Difference(s_ntp_client.t4, s_ntp_client.t3, diff_seconds2, diff_nano_seconds2);
281295

282296
const auto kOffsetSeconds = static_cast<int64_t>(diff_seconds1) + static_cast<int64_t>(diff_seconds2);
@@ -292,7 +306,7 @@ static void UpdatePtpTime() {
292306
gd32::ptp::ptptime ptp_get;
293307
Gd32PtpGetTime(&ptp_get);
294308

295-
s_ntp_client.request.reference_timestamp_s = __builtin_bswap32(static_cast<uint32_t>(ptp_get.tv_sec) + ntp::kJan1970);
309+
s_ntp_client.request.reference_timestamp_s = __builtin_bswap32(ptp_get.tv_sec + ntp::kJan1970);
296310
s_ntp_client.request.reference_timestamp_f = __builtin_bswap32(NTPFRAC(ptp_get.tv_nsec));
297311

298312
if ((ptp_offset.tv_sec == 0) && (ptp_offset.tv_nsec > -999999) && (ptp_offset.tv_nsec < 999999)) {
@@ -350,7 +364,13 @@ static void UpdatePtpTime() {
350364
sign = '-';
351365
}
352366

353-
printf(" %s : offset=%c%d.%09d delay=%d.%09d\n", s_ntp_client.state.mode == ntp::Modes::kBasic ? "Basic" : "Interleaved", sign, ptp_offset.tv_sec, ptp_offset.tv_nsec, ptp_delay.tv_sec, ptp_delay.tv_nsec);
367+
printf(" %s : offset=%c%d.%09d delay=%d.%09d\n",
368+
s_ntp_client.state.mode == ntp::Modes::kBasic ? "Basic" : "Interleaved",
369+
sign,
370+
static_cast<int>(ptp_offset.tv_sec),
371+
static_cast<int>(ptp_offset.tv_nsec),
372+
static_cast<int>(ptp_delay.tv_sec),
373+
static_cast<int>(ptp_delay.tv_nsec));
354374
#endif
355375
}
356376

@@ -369,8 +389,13 @@ static void UpdatePtpTime() {
369389
static void Process() {
370390
const auto* const kReply = s_ntp_client.reply;
371391
#ifndef NDEBUG
372-
printf("Response: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n", __builtin_bswap32(kReply->origin_timestamp_s), __builtin_bswap32(kReply->origin_timestamp_f), __builtin_bswap32(kReply->receive_timestamp_s),
373-
__builtin_bswap32(kReply->receive_timestamp_f), __builtin_bswap32(kReply->transmit_timestamp_s), __builtin_bswap32(kReply->transmit_timestamp_f));
392+
printf("Response: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n",
393+
static_cast<unsigned>(__builtin_bswap32(kReply->origin_timestamp_s)), // NOLINT
394+
static_cast<unsigned>(__builtin_bswap32(kReply->origin_timestamp_f)), // NOLINT
395+
static_cast<unsigned>(__builtin_bswap32(kReply->receive_timestamp_s)), // NOLINT
396+
static_cast<unsigned>(__builtin_bswap32(kReply->receive_timestamp_f)), // NOLINT
397+
static_cast<unsigned>(__builtin_bswap32(kReply->transmit_timestamp_s)), // NOLINT
398+
static_cast<unsigned>(__builtin_bswap32(kReply->transmit_timestamp_f))); // NOLINT
374399
#endif
375400
// If the origin timestamp is equal to the transmit timestamp, the response is in the basic mode.
376401
if ((kReply->origin_timestamp_s == s_ntp_client.request.transmit_timestamp_s) && (kReply->origin_timestamp_f == s_ntp_client.request.transmit_timestamp_f)) {

lib-network/src/core/ipv4/arp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ template <network::arp::EthSend S> static void Query(uint32_t destination_ip, vo
244244
network::memory::Allocator::Instance().Free(record_found->packet.p);
245245
}
246246

247-
printf("size=%u\n", size);
247+
printf("size=%u\n", static_cast<unsigned>(size));
248248
assert(size <= network::memory::kBlockSize);
249249
record_found->packet.p = network::memory::Allocator::Instance().Allocate();
250250
assert(record_found->packet.p != nullptr);

lib-network/src/emac/gd32/emac_debug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ void emac_debug_run() {
3939
#endif
4040

4141
if ((rxfifo_drop != 0) || (rxdma_drop != 0)) {
42-
printf("%u: RxFIFO: %u RxDMA: %u\n", ++s_counter, rxfifo_drop, rxdma_drop);
42+
printf("%u: RxFIFO: %u RxDMA: %u\n", static_cast<unsigned>(++s_counter), static_cast<unsigned>(rxfifo_drop), static_cast<unsigned>(rxdma_drop));
4343
}
4444
}

lib-network/src/emac/gd32/emac_eth.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ void Send(uint32_t length) {
359359

360360
// Transmits an Ethernet frame with data copying.
361361
void Send(void* buffer, uint32_t length) {
362-
DEBUG_PRINTF("%p -> %u", buffer, length);
362+
DEBUG_PRINTF("%p -> %u", buffer, static_cast<unsigned>(length));
363363

364364
assert(nullptr != buffer);
365365
assert(length <= ENET_MAX_FRAME_SIZE);

lib-network/src/emac/gd32/emac_phy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool Config(uint16_t address) {
6666

6767
const auto kAhbClk = rcu_clock_freq_get(CK_AHB);
6868

69-
DEBUG_PRINTF("kAhbClk=%u", kAhbClk);
69+
DEBUG_PRINTF("kAhbClk=%u", static_cast<unsigned>(kAhbClk));
7070

7171
#if defined GD32F10X_CL
7272
if (ENET_RANGE(kAhbClk, 20000000U, 35000000U)) {
@@ -163,7 +163,7 @@ bool Config(uint16_t address) {
163163
}
164164

165165
if (!(value & emac::mmi::BMCR_RESET)) {
166-
DEBUG_PRINTF("%u", millis() - kMillis);
166+
DEBUG_PRINTF("%u", static_cast<unsigned>(millis() - kMillis));
167167
DEBUG_EXIT();
168168
return true;
169169
}
@@ -175,7 +175,7 @@ bool Config(uint16_t address) {
175175
return false;
176176
}
177177

178-
DEBUG_PRINTF("%u", millis() - kMillis);
178+
DEBUG_PRINTF("%u", static_cast<unsigned>(millis() - kMillis));
179179
DEBUG_EXIT();
180180
return true;
181181
}

0 commit comments

Comments
 (0)