Skip to content

Commit 7e6e9d7

Browse files
committed
Refactor GD32 timing and peripheral helpers
Consolidates low-level timing into a new `gd32_timers` module and switches board init to `gd32::timers::Start()`, replacing scattered systick/timer/delay/millis implementations. The change removes legacy timer/systick/delay files, updates timing and RTC users to the new API, and adds `gd32_gpio_macros.h` to centralize GPIO port/pin conversion types and macros. It also aligns several drivers with newer namespaced interfaces (for example SPI flash), unifies debug macro usage, and applies broad header/format consistency cleanup.
1 parent 735d01f commit 7e6e9d7

54 files changed

Lines changed: 1347 additions & 1292 deletions

Some content is hidden

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

lib-board/src/gd32/board_init.cpp

Lines changed: 20 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
#include <cstddef>
27-
2826
#if !defined(_TIME_STAMP_DAY_)
2927
#define _TIME_STAMP_DAY_ 0
3028
#endif
@@ -39,6 +37,11 @@
3937
#error
4038
#endif
4139

40+
#if (defined(GD32F4XX) || defined(GD32H7XX)) && !defined(MCU_HAVE_GPIO_TG)
41+
#error
42+
#endif
43+
44+
#include <cstddef>
4245
#include <cstring>
4346
#include <cstdio>
4447
#include <cassert>
@@ -65,31 +68,15 @@
6568
#include "board_statusled.h"
6669
#include "panelled.h"
6770
#include "logic_analyzer.h"
71+
#include "gd32_timers.h"
6872

69-
#if defined(CONFIG_HAL_USE_SYSTICK)
70-
void SystickConfig();
71-
#endif
72-
73-
void UdelayInit();
7473
void Gd32AdcInit();
7574

7675
#if defined(GD32H7XX)
7776
void CacheEnable();
7877
void MpuConfig();
7978
#endif
8079

81-
void Timer5Config();
82-
void Timer6Config();
83-
#if !defined(CONFIG_NET_ENABLE_PTP)
84-
#if defined(CONFIG_TIME_USE_TIMER)
85-
#if defined(GD32H7XX)
86-
void Timer16Config();
87-
#else
88-
void Timer7Config();
89-
#endif
90-
#endif
91-
#endif
92-
9380
#if !defined(DISABLE_RTC)
9481
#include "hwclock.h"
9582
static HwClock hw_clock;
@@ -116,7 +103,7 @@ void Init() {
116103
uart0::Init();
117104
#endif
118105
// From here we console output
119-
#if defined (BOARD_DEBUG)
106+
#if defined(BOARD_DEBUG)
120107
putchar('\n');
121108
#endif
122109

@@ -128,7 +115,7 @@ void Init() {
128115
// Clear section .dmx
129116
const auto kSize = static_cast<size_t>(&_edmx - &_sdmx);
130117
memset(&_sdmx, 0, kSize);
131-
#if defined (BOARD_DEBUG)
118+
#if defined(BOARD_DEBUG)
132119
printf("Cleared .dmx at %p, size %u\n", &_sdmx, kSize);
133120
#endif
134121
}
@@ -138,7 +125,7 @@ void Init() {
138125
// Clear section .lightset
139126
const auto kSize = static_cast<size_t>(&_elightset - &_slightset);
140127
memset(&_slightset, 0, kSize);
141-
#if defined (BOARD_DEBUG)
128+
#if defined(BOARD_DEBUG)
142129
printf("Cleared .lightset at %p, size %u\n", &_slightset, kSize);
143130
#endif
144131
}
@@ -147,7 +134,7 @@ void Init() {
147134
// Clear section .network
148135
const auto kSize = static_cast<size_t>(&_enetwork - &_snetwork);
149136
memset(&_snetwork, 0, kSize);
150-
#if defined (BOARD_DEBUG)
137+
#if defined(BOARD_DEBUG)
151138
printf("Cleared .network at %p, size %u\n", &_snetwork, kSize);
152139
#endif
153140
}
@@ -156,7 +143,7 @@ void Init() {
156143
// Clear section .pixel
157144
const auto kSize = static_cast<size_t>(&_epixel - &_spixel);
158145
memset(&_spixel, 0, kSize);
159-
#if defined (BOARD_DEBUG)
146+
#if defined(BOARD_DEBUG)
160147
printf("Cleared .pixel at %p, size %u\n", &_spixel, kSize);
161148
#endif
162149
}
@@ -168,76 +155,35 @@ void Init() {
168155
// clear section .network
169156
const auto kSize = static_cast<size_t>(&_enetwork - &_snetwork);
170157
memset(&_snetwork, 0, kSize);
171-
#if defined (BOARD_DEBUG)
158+
#if defined(BOARD_DEBUG)
172159
printf("Cleared .network at %p, size %u\n", &_snetwork, kSize);
173160
#endif
174161
}
175162
#endif
176163
#endif
177164

178-
#if defined (BOARD_DEBUG)
165+
#if defined(BOARD_DEBUG)
179166
// Show the AHB and APBx busses frequency
180167
const auto kSys = rcu_clock_freq_get(CK_SYS);
181168
const auto kAhb = rcu_clock_freq_get(CK_AHB);
182169
const auto kApb1 = rcu_clock_freq_get(CK_APB1);
183170
const auto kApb2 = rcu_clock_freq_get(CK_APB2);
184-
printf("CK_SYS=%u\nCK_AHB=%u\nCK_APB1=%u\nCK_APB2=%u\n", kSys, kAhb, kApb1, kApb2);
171+
printf("CK_SYS=%u\nCK_AHB=%u\nCK_APB1=%u\nCK_APB2=%u\n", static_cast<unsigned>(kSys), static_cast<unsigned>(kAhb), static_cast<unsigned>(kApb1), static_cast<unsigned>(kApb2));
185172
assert(kSys == MCU_CLOCK_FREQ);
186173
assert(kAhb == AHB_CLOCK_FREQ);
187174
assert(kApb1 == APB1_CLOCK_FREQ);
188175
assert(kApb2 == APB2_CLOCK_FREQ);
189176
#if defined(GD32H7XX)
190177
const auto kApb3 = rcu_clock_freq_get(CK_APB3);
191178
const auto kApb4 = rcu_clock_freq_get(CK_APB4);
192-
printf("nCK_APB3=%u\nCK_APB4=%u\n", nAPB3, nAPB4);
179+
printf("nCK_APB3=%u\nCK_APB4=%u\n", static_cast<unsigned>(nAPB3), static_cast<unsigned>(nAPB4));
193180
assert(kApb3 == APB3_CLOCK_FREQ);
194181
assert(kApb4 == APB4_CLOCK_FREQ);
195182
#endif
196183
#endif
197184

198-
/*
199-
* Setup the TIMERx
200-
*/
201-
202-
#if defined(GD32H7XX)
203-
#elif defined(GD32F4XX)
204-
/*
205-
* AHB = SYSCLK = 240 MHz (GD32F470), others = 200 MHz
206-
* APB1 = AHB / 4 = 50 MHz => APB1PSC = 0b101
207-
* APB2 = AHB / 2 = 100 MHz => APB2PSC = 0b100
208-
*/
209-
210-
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
211-
212-
/*
213-
* If APB1PSC/APB2PSC in RCU_CFG0 register is 0b0xx(CK_APBx = CK_AHB),
214-
* 0b100(CK_APBx = CK_AHB/2), or 0b101(CK_APBx = CK_AHB/4), the TIMER
215-
* clock is equal to CK_AHB(CK_TIMERx = CK_AHB).
216-
*/
217-
218-
/*
219-
* TIMER in APB1 domain: CK_TIMERx = AHB = 200 MHz => 240 MHz (GD32F470).
220-
* TIMER in APB2 domain: CK_TIMERx = AHB = 200 MHz => 240 MHz (GD32F470).
221-
*/
222-
#else
223-
#endif
224-
225-
Timer5Config();
226-
Timer6Config();
227-
#if defined(CONFIG_HAL_USE_SYSTICK)
228-
SystickConfig();
229-
#endif
230-
#if !defined(CONFIG_NET_ENABLE_PTP)
231-
#if defined(CONFIG_TIME_USE_TIMER)
232-
#if defined(GD32H7XX)
233-
Timer16Config();
234-
#else
235-
Timer7Config();
236-
#endif
237-
#endif
238-
#endif
185+
gd32::timers::Start();
239186

240-
UdelayInit();
241187
Gd32AdcInit();
242188
Gd32I2cBegin();
243189
#if defined(CONFIG_ENABLE_I2C1)
@@ -261,16 +207,7 @@ void Init() {
261207
#endif
262208
bkp_data_write(BKP_DATA_1, 0x0);
263209

264-
#if defined(CONFIG_HAVE_CRC32_HW)
265-
rcu_periph_clock_enable(RCU_CRC);
266-
crc_data_register_reset();
267-
#endif
268-
269-
/*
270-
* Initialize status led, 74hc595 and panel led
271-
*/
272-
273-
#if !defined(CONFIG_LEDBLINK_USE_PANELLED)
210+
// Initialize status led
274211
rcu_periph_clock_enable(LED_BLINK_GPIO_CLK);
275212
#if defined(GPIO_INIT)
276213
gpio_init(LED_BLINK_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_BLINK_PIN);
@@ -279,7 +216,6 @@ void Init() {
279216
gpio_output_options_set(LED_BLINK_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED, LED_BLINK_PIN);
280217
#endif
281218
GPIO_BOP(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
282-
#endif
283219

284220
#if defined(PANELLED_595_CS_GPIOx)
285221
rcu_periph_clock_enable(PANELLED_595_CS_RCU_GPIOx);
@@ -301,17 +237,19 @@ void Init() {
301237
logic_analyzer::Init();
302238

303239
#if !defined(CONFIG_NET_ENABLE_PTP)
240+
#if defined(CONFIG_TIME_USE_TIMER) || defined(CONFIG_TIME_USE_SYSTICK)
304241
struct tm tmbuf;
305242
memset(&tmbuf, 0, sizeof(struct tm));
306243
tmbuf.tm_mday = _TIME_STAMP_DAY_; // The day of the month, in the range 1 to 31.
307244
tmbuf.tm_mon = _TIME_STAMP_MONTH_ - 1; // The number of months since January, in the range 0 to 11.
308245
tmbuf.tm_year = _TIME_STAMP_YEAR_ - 1900; // The number of years since 1900.
309246

310247
const auto kSeconds = mktime(&tmbuf);
311-
const struct timeval kTv = {kSeconds, 0};
248+
const struct timeval kTv = {.tv_sec = kSeconds, .tv_usec = 0};
312249

313250
settimeofday(&kTv, nullptr);
314251
#endif
252+
#endif
315253

316254
#if !defined(DISABLE_RTC)
317255
HwClock::Get()->RtcProbe();

lib-board/src/gd32/board_statusled.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file board_statusled.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
@@ -26,82 +26,70 @@
2626
#include <cstdint>
2727

2828
#include "board_statusled.h"
29+
#include "common/utils/utils_units.h"
2930
#include "softwaretimers.h"
3031
#include "board_debug.h"
3132
#include "gd32.h" // IWYU pragma: keep
3233

33-
static TimerHandle_t s_timer_id = kTimerIdNone;
34+
namespace {
35+
TimerHandle_t s_timer_id = kTimerIdNone;
3436

35-
#if !defined(HAL_HAVE_PORT_BIT_TOGGLE)
36-
static int32_t s_toggle_led = 1;
37+
#if !defined(MCU_HAVE_GPIO_TG)
38+
int32_t s_toggle_led = 1;
3739
#endif
3840

39-
static void Ledblink([[maybe_unused]] TimerHandle_t handle) {
40-
#if defined(HAL_HAVE_PORT_BIT_TOGGLE)
41+
void Ledblink([[maybe_unused]] TimerHandle_t handle) {
42+
#if defined(MCU_HAVE_GPIO_TG)
4143
GPIO_TG(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
4244
#else
4345
s_toggle_led = -s_toggle_led;
4446

4547
if (s_toggle_led > 0) {
46-
#if defined(CONFIG_LEDBLINK_USE_PANELLED)
47-
hal::PanelLedOn(panelled::ACTIVITY);
48-
#else
4948
GPIO_BOP(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
50-
#endif
5149
} else {
52-
#if defined(CONFIG_LEDBLINK_USE_PANELLED)
53-
hal::PanelLedOff(panelled::ACTIVITY);
54-
#else
5550
GPIO_BC(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
56-
#endif
5751
}
5852
#endif
5953
}
54+
} // namespace
6055

6156
namespace board::statusled {
6257
void SetFrequency(uint32_t frequency_hz) {
6358
BOARD_DEBUG_ENTRY();
6459
BOARD_DEBUG_PRINTF("s_timer_id=%d, frequency_hz=%u", static_cast<int>(s_timer_id), static_cast<unsigned>(frequency_hz));
6560

6661
if (s_timer_id == kTimerIdNone) {
67-
s_timer_id = SoftwareTimerAdd((1000U / frequency_hz), Ledblink);
62+
s_timer_id = SoftwareTimerAdd((common::units::kMsPerSecond / frequency_hz), Ledblink);
6863
BOARD_DEBUG_EXIT();
6964
return;
7065
}
7166

7267
switch (frequency_hz) {
7368
case 0:
7469
SoftwareTimerDelete(s_timer_id);
75-
#if defined(CONFIG_LEDBLINK_USE_PANELLED)
76-
hal::PanelLedOff(panelled::ACTIVITY);
77-
#else
70+
7871
GPIO_BC(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
79-
#endif
8072
break;
8173
#if !defined(CONFIG_HAL_USE_MINIMUM)
8274
case 1:
83-
SoftwareTimerChange(s_timer_id, (1000U / 1));
75+
SoftwareTimerChange(s_timer_id, (common::units::kMsPerSecond / 1));
8476
break;
8577
case 3:
86-
SoftwareTimerChange(s_timer_id, (1000U / 3));
78+
SoftwareTimerChange(s_timer_id, (common::units::kMsPerSecond / 3));
8779
break;
8880
case 5:
89-
SoftwareTimerChange(s_timer_id, (1000U / 5));
81+
SoftwareTimerChange(s_timer_id, (common::units::kMsPerSecond / 5));
9082
break;
9183
case 8:
92-
SoftwareTimerChange(s_timer_id, (1000U / 8));
84+
SoftwareTimerChange(s_timer_id, (common::units::kMsPerSecond / 8));
9385
break;
9486
#endif
9587
case 255:
9688
SoftwareTimerDelete(s_timer_id);
97-
#if defined(CONFIG_LEDBLINK_USE_PANELLED)
98-
hal::PanelLedOn(panelled::ACTIVITY);
99-
#else
10089
GPIO_BOP(LED_BLINK_GPIO_PORT) = LED_BLINK_PIN;
101-
#endif
10290
break;
10391
default:
104-
SoftwareTimerChange(s_timer_id, (1000U / frequency_hz));
92+
SoftwareTimerChange(s_timer_id, (common::units::kMsPerSecond / frequency_hz));
10593
break;
10694
}
10795

lib-dmxnode/src/scenes/spi/scenes.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ static bool CheckHaveFlash() {
3939
DMXNODE_DEBUG_PRINTF("s_hasFlash=%d", s_has_flash);
4040

4141
if (!s_has_flash) {
42-
if (!spi_flash_probe()) {
42+
if (!spi::flash::Probe()) {
4343
DMXNODE_DEBUG_EXIT();
4444
return false;
4545
}
4646

47-
const auto kEraseSize = spi_flash_get_sector_size();
47+
const auto kEraseSize = spi::flash::SectorSize();
4848
assert(kEraseSize <= dmxnode::scenes::kBytesNeeded);
4949
const auto kPages = 1 + (dmxnode::scenes::kBytesNeeded / kEraseSize);
5050

5151
DMXNODE_DEBUG_PRINTF("Bytes needed=%u, nEraseSize=%u, nPages=%u", dmxnode::scenes::kBytesNeeded, kEraseSize, kPages);
5252

53-
assert(((kPages + 1) * kEraseSize) <= spi_flash_get_size());
53+
assert(((kPages + 1) * kEraseSize) <= spi::flash::get_size());
5454

55-
s_offset_base = spi_flash_get_size() - ((kPages + 1) * kEraseSize);
55+
s_offset_base = spi::flash::Size() - ((kPages + 1) * kEraseSize);
5656

5757
DMXNODE_DEBUG_PRINTF("nOffsetBase=%p", s_offset_base);
5858
}
@@ -70,7 +70,7 @@ void WriteStart() {
7070
return;
7171
}
7272

73-
s_has_flash = spi_flash_cmd_erase(s_offset_base, spi_flash_get_sector_size());
73+
s_has_flash = spi::flash::cmd::Erase(s_offset_base, spi::flash::SectorSize());
7474

7575
DMXNODE_DEBUG_PRINTF("s_hasFlash=%d", s_has_flash);
7676
DMXNODE_DEBUG_EXIT();
@@ -90,7 +90,7 @@ void Write(uint32_t port_index, const uint8_t* data) {
9090

9191
DMXNODE_DEBUG_PRINTF("s_offset_base=%p, kOffset=%p", s_offset_base, kOffset);
9292

93-
spi_flash_cmd_write_multi(kOffset, dmxnode::kUniverseSize, data);
93+
spi::flash::cmd::Write(kOffset, dmxnode::kUniverseSize, data);
9494

9595
DMXNODE_DEBUG_EXIT();
9696
}
@@ -131,7 +131,7 @@ void Read(uint32_t port_index, uint8_t* data) {
131131

132132
DMXNODE_DEBUG_PRINTF("s_offset_base=%p, kOffset=%u", reinterpret_cast<void*>(s_offset_base), static_cast<unsigned>(kOffset));
133133

134-
spi_flash_cmd_read_fast(kOffset, dmxnode::kUniverseSize, data);
134+
spi::flash::cmd::Read(kOffset, dmxnode::kUniverseSize, data);
135135

136136
DMXNODE_DEBUG_EXIT();
137137
}

0 commit comments

Comments
 (0)