Skip to content

Commit ad538fb

Browse files
committed
Use runtime assert for hex; GD32 GPIO fixes
Replace an ill-formed constexpr static_assert in utils_hex.h with a runtime assert to avoid forcing a compile-time failure inside a constexpr path. Add a GD32H7XX-specific stub for Gd32GpioIntCfg that asserts "Not implemented" and restore the original interrupt configuration for other targets. Apply various formatting/comment cleanups and small template signature/layout tweaks in lib-gd32/include/gd32_gpio.h to improve readability. In lib-rdmsensor/src/json/json_config_rdmsensors.cpp update the copyright range and reformat code/brace style (no functional changes).
1 parent a01cafb commit ad538fb

3 files changed

Lines changed: 58 additions & 58 deletions

File tree

common/include/common/utils/utils_hex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ constexpr uint32_t FromHex(const char (&string)[N]) {
8585
for (size_t i = 0; i < N - 1; ++i) {
8686
const uint8_t kNibble = FromChar(string[i]);
8787
if constexpr (kNibble == 0xFF) {
88-
static_assert(false, "Invalid hex digit");
88+
assert(false && "Invalid hex digit");
8989
}
9090
result = (result << 4) | kNibble;
9191
}

lib-gd32/include/gd32_gpio.h

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ inline void Gd32GpioFsel(uint32_t gpio_periph, uint32_t pin, uint32_t fsel) {
124124
#endif
125125
}
126126

127-
#if !defined(GD32H7XX)
127+
#if defined(GD32H7XX)
128+
inline void Gd32GpioIntCfg([[maybe_unused]] uint32_t gpio, [[maybe_unused]] uint32_t trig_type) {
129+
assert(false && "Not implemented");
130+
}
131+
#else
128132
inline void Gd32GpioIntCfg(uint32_t gpio, uint32_t trig_type) {
129133
const uint32_t kLinex = BIT(GD32_GPIO_TO_NUMBER(gpio));
130134

@@ -244,7 +248,8 @@ inline void Gd32GpioSetPud(uint32_t gpio, uint32_t pud) {
244248
}
245249

246250
#if defined(GD32F4XX) || defined(GD32H7XX)
247-
template <uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin> inline void Gd32GpioModeSet() {
251+
template <uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin>
252+
inline void Gd32GpioModeSet() {
248253
static_assert(pin != 0, "pin cannot be zero");
249254
static_assert(pin == (1U << __builtin_ctz(pin)), "Only single pin values are allowed");
250255

@@ -267,7 +272,8 @@ template <uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t p
267272
GPIO_PUD(gpio_periph) = pupd;
268273
}
269274

270-
template <uint32_t gpio_periph, uint32_t alt_func_num, uint32_t pin> inline void Gd32GpioAfSet() {
275+
template <uint32_t gpio_periph, uint32_t alt_func_num, uint32_t pin>
276+
inline void Gd32GpioAfSet() {
271277
static_assert(pin != 0, "pin cannot be zero");
272278
static_assert(pin == (1U << __builtin_ctz(pin)), "Only single pin values are allowed");
273279

@@ -290,57 +296,58 @@ template <uint32_t gpio_periph, uint32_t alt_func_num, uint32_t pin> inline void
290296
GPIO_AFSEL1(gpio_periph) = afrh;
291297
}
292298
#else
293-
template <uint32_t gpio_periph, uint32_t mode, uint32_t pin, uint32_t speed = GPIO_OSPEED_50MHZ> inline void gd32_gpio_init() {
294-
/* GPIO mode configuration */
299+
template <uint32_t gpio_periph, uint32_t mode, uint32_t pin, uint32_t speed = GPIO_OSPEED_50MHZ>
300+
inline void gd32_gpio_init() {
301+
// GPIO mode configuration
295302
auto temp_mode = (mode & 0x0F);
296303

297-
/* GPIO speed configuration */
304+
// GPIO speed configuration
298305
if constexpr ((0x00U) != (mode & (0x10U))) {
299-
/* output mode max speed: 10MHz, 2MHz, 50MHz */
306+
// output mode max speed: 10MHz, 2MHz, 50MHz
300307
temp_mode |= speed;
301308
}
302309

303310
constexpr uint32_t kPinPos = 31U - __builtin_clz(pin);
304311

305312
if constexpr (kPinPos < 8U) {
306313
uint32_t reg = GPIO_CTL0(gpio_periph);
307-
/* clear the specified pin mode bits */
314+
// Clear the specified pin mode bits
308315
reg &= ~GPIO_MODE_MASK(kPinPos);
309-
/* set the specified pin mode bits */
316+
// Set the specified pin mode bits
310317
reg |= GPIO_MODE_SET(kPinPos, temp_mode);
311318

312-
/* set IPD or IPU */
319+
// Set IPD or IPU
313320
if constexpr (GPIO_MODE_IPD == mode) {
314-
/* reset the corresponding OCTL bit */
321+
// Reset the corresponding OCTL bit
315322
GPIO_BC(gpio_periph) = (1U << kPinPos);
316323
} else {
317-
/* set the corresponding OCTL bit */
324+
// Set the corresponding OCTL bit
318325
if constexpr (GPIO_MODE_IPU == mode) {
319326
GPIO_BOP(gpio_periph) = (1U << kPinPos);
320327
}
321328
}
322-
/* set GPIO_CTL0 register */
329+
// Set GPIO_CTL0 register */
323330
GPIO_CTL0(gpio_periph) = reg;
324331
} else {
325-
/* configure the eight high port pins with GPIO_CTL1 */
332+
// Configure the eight high port pins with GPIO_CTL1
326333
constexpr uint32_t kHighPinPos = kPinPos - 8U;
327334
uint32_t reg = GPIO_CTL1(gpio_periph);
328-
/* clear the specified pin mode bits */
335+
// Clear the specified pin mode bits */
329336
reg &= ~GPIO_MODE_MASK(kHighPinPos);
330-
/* set the specified pin mode bits */
337+
// Set the specified pin mode bits */
331338
reg |= GPIO_MODE_SET(kHighPinPos, temp_mode);
332339

333-
/* set IPD or IPU */
340+
// Set IPD or IPU
334341
if constexpr (GPIO_MODE_IPD == mode) {
335-
/* reset the corresponding OCTL bit */
342+
// Reset the corresponding OCTL bit
336343
GPIO_BC(gpio_periph) = (1U << kPinPos);
337344
} else {
338-
/* set the corresponding OCTL bit */
345+
// Set the corresponding OCTL bit
339346
if (GPIO_MODE_IPU == mode) {
340347
GPIO_BOP(gpio_periph) = (1U << kPinPos);
341348
}
342349
}
343-
/* set GPIO_CTL1 register */
350+
// set GPIO_CTL1 register
344351
GPIO_CTL1(gpio_periph) = reg;
345352
}
346353
}

lib-rdmsensor/src/json/json_config_rdmsensors.cpp

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json_config_rdmsensors.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
@@ -31,45 +31,38 @@
3131
#include "configstore.h"
3232
#include "common/utils/utils_hex.h"
3333

34-
namespace json::config
35-
{
36-
uint32_t GetRdmSensors(char* buffer, uint32_t length)
37-
{
38-
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
39-
char a[32];
40-
for (size_t types = 0; types < json::RdmSensorsParams::KeysSize(); types++)
41-
{
42-
a[0] = '[';
43-
uint32_t k = 1;
44-
for (uint32_t j = 0; j < common::store::rdm::sensors::kMaxDevices; j++)
45-
{
46-
if ((k + 3) > 31) break;
47-
const auto kType = ConfigStore::Instance().RdmSensorsIndexedGetType(j);
48-
if (kType == types)
49-
{
50-
const auto kAddress = ConfigStore::Instance().RdmSensorsIndexedGetAddress(j);
51-
if (kAddress != 0)
52-
{
53-
a[k++] = common::hex::ToCharLowercase(kAddress >> 4);
54-
a[k++] = common::hex::ToCharLowercase(kAddress);
55-
a[k++] = ',';
56-
}
57-
}
58-
}
59-
60-
if (a[k - 1] == ',') k -= 1;
61-
62-
a[k++] = ']';
63-
a[k] = '\0';
64-
65-
const auto& keys = json::RdmSensorsParams::Keys();
66-
doc[keys[types].GetName()] = a;
67-
}
34+
namespace json::config {
35+
uint32_t GetRdmSensors(char* buffer, uint32_t length) {
36+
return json::helpers::Serialize(buffer, length, [&](JsonDoc& doc) {
37+
char a[32];
38+
for (size_t types = 0; types < json::RdmSensorsParams::KeysSize(); types++) {
39+
a[0] = '[';
40+
uint32_t k = 1;
41+
for (uint32_t j = 0; j < common::store::rdm::sensors::kMaxDevices; j++) {
42+
if ((k + 3) > 31) break;
43+
const auto kType = ConfigStore::Instance().RdmSensorsIndexedGetType(j);
44+
if (kType == types) {
45+
const auto kAddress = ConfigStore::Instance().RdmSensorsIndexedGetAddress(j);
46+
if (kAddress != 0) {
47+
a[k++] = common::hex::ToCharLowercase(kAddress >> 4);
48+
a[k++] = common::hex::ToCharLowercase(kAddress);
49+
a[k++] = ',';
50+
}
51+
}
52+
}
53+
54+
if (a[k - 1] == ',') k -= 1;
55+
56+
a[k++] = ']';
57+
a[k] = '\0';
58+
59+
const auto& keys = json::RdmSensorsParams::Keys();
60+
doc[keys[types].GetName()] = a;
61+
}
6862
});
6963
}
7064

71-
void SetRdmSensors(const char* buffer, uint32_t buffer_size)
72-
{
65+
void SetRdmSensors(const char* buffer, uint32_t buffer_size) {
7366
::json::RdmSensorsParams rdmsensors_params;
7467
rdmsensors_params.Store(buffer, buffer_size);
7568
}

0 commit comments

Comments
 (0)