Skip to content

Commit 21d48b7

Browse files
committed
Consolidate GD32 timers and driver cleanup
Merge the scattered timing pieces (`delayus`, `systick`, `timer5`, `timer6`, `uptime`, and `gd32_millis`) into a single `gd32_timers` interface with namespaced microsecond, millisecond, and uptime helpers. Update `timing.h`, GPIO utilities, and low-level headers to use the new API, while also adding module-specific GD32 debug macros and tightening internal linkage, naming, and conditional comments across the peripheral drivers. The soft UART RX path is also reworked around dedicated EXTI/timer handling as part of the cleanup.
1 parent bb86a26 commit 21d48b7

32 files changed

Lines changed: 1032 additions & 943 deletions

lib-gd32/device/enet/f/enet_gpio_config.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525

2626
#if defined(DEBUG_ENET)
2727
#undef NDEBUG
28-
#endif
28+
#endif // DEBUG_ENET
2929

3030
#include "gd32.h" // IWYU pragma: keep
3131
#include "firmware/debug/debug_debug.h"
3232

3333
// Needed for older GD32F firmware
3434
#if !defined(GPIO_OSPEED_MAX)
3535
#define GPIO_OSPEED_MAX GPIO_OSPEED_200MHZ
36-
#endif
36+
#endif // GPIO_OSPEED_MAX
3737

3838
void EnetGpioConfig() {
3939
DEBUG_ENTRY();
@@ -53,7 +53,7 @@ void EnetGpioConfig() {
5353
rcu_ckout0_config(RCU_CKOUT0SRC_CKPLL2);
5454
#else
5555
rcu_ckout0_config(RCU_CKOUT0SRC_CKPLL2, RCU_CKOUT0_DIV1);
56-
#endif
56+
#endif // GD32F10X_CL
5757
gpio_ethernet_phy_select(GPIO_ENET_PHY_RMII);
5858

5959
/* PA1: ETH_RMII_REF_CLK */
@@ -137,6 +137,6 @@ void EnetGpioConfig() {
137137
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_1);
138138
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_4);
139139
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_5);
140-
#endif
140+
#endif // defined(GD32F10X) || defined(GD32F20X)
141141
DEBUG_EXIT();
142142
}

lib-gd32/device/enet/h/enet_gpio_config.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#if defined(DEBUG_ENET)
2727
#undef NDEBUG
28-
#endif
28+
#endif // DEBUG_ENET
2929

3030
#include "gd32.h" // IWYU pragma: keep
3131
#include "firmware/debug/debug_debug.h"
@@ -55,27 +55,27 @@ void EnetGpioConfig() {
5555
#ifdef PHY_CLOCK_MCO
5656
/* output HXTAL clock (25MHz) on CKOUT0 pin(PA8) to clock the PHY */
5757
rcu_ckout0_config(RCU_CKOUT0SRC_HXTAL, RCU_CKOUT0_DIV1);
58-
#endif /* PHY_CLOCK_MCO */
58+
#endif // PHY_CLOCK_MCO
5959

6060
#ifdef USE_ENET0
6161
syscfg_enet_phy_interface_config(ENET0, SYSCFG_ENET_PHY_MII);
62-
#endif /* USE_ENET0 */
62+
#endif // USE_ENET0
6363
#ifdef USE_ENET1
6464
syscfg_enet_phy_interface_config(ENET1, SYSCFG_ENET_PHY_MII);
65-
#endif /* USE_ENET1 */
65+
#endif // USE_ENET1
6666

6767
#elif defined RMII_MODE
6868
/* choose DIV12 to get 50MHz from 600MHz on CKOUT0 pin (PA8) to clock the PHY */
6969
rcu_ckout0_config(RCU_CKOUT0SRC_PLL0P, RCU_CKOUT0_DIV12);
7070

7171
#ifdef USE_ENET0
7272
syscfg_enet_phy_interface_config(ENET0, SYSCFG_ENET_PHY_RMII);
73-
#endif /* USE_ENET0 */
73+
#endif // USE_ENET0
7474
#ifdef USE_ENET1
7575
syscfg_enet_phy_interface_config(ENET1, SYSCFG_ENET_PHY_RMII);
76-
#endif /* USE_ENET1 */
76+
#endif // USE_ENET1
7777

78-
#endif /* MII_MODE */
78+
#endif // MII_MODE
7979

8080
#ifdef USE_ENET0
8181
#ifdef MII_MODE
@@ -223,8 +223,8 @@ void EnetGpioConfig() {
223223
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_4);
224224
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_5);
225225

226-
#endif /* MII_MODE */
227-
#endif /* USE_ENET0 */
226+
#endif // MII_MODE
227+
#endif // USE_ENET0
228228

229229
#ifdef USE_ENET1
230230
#ifdef MII_MODE
@@ -368,7 +368,7 @@ void EnetGpioConfig() {
368368
gpio_af_set(GPIOG, GPIO_AF_6, GPIO_PIN_13);
369369
gpio_af_set(GPIOG, GPIO_AF_6, GPIO_PIN_14);
370370

371-
#endif /* MII_MODE */
372-
#endif /* USE_ENET1 */
371+
#endif // MII_MODE
372+
#endif // USE_ENET1
373373
DEBUG_EXIT();
374374
}

lib-gd32/device/enet/ptp/gd32_ptp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static FlagStatus EnetPtpflagstatusGet(uint32_t flag) {
6161

6262
return bitstatus;
6363
}
64-
#endif
64+
#endif // GD32H7XX
6565

6666
static void PtpStart(uint32_t init_sec, uint32_t init_subsec, [[maybe_unused]] uint32_t carry_cfg, uint32_t accuracy_cfg) {
6767
DEBUG_ENTRY();
@@ -72,7 +72,7 @@ static void PtpStart(uint32_t init_sec, uint32_t init_subsec, [[maybe_unused]] u
7272
enet_ptp_feature_enable(ENET_ALL_RX_TIMESTAMP | ENET_RXTX_TIMESTAMP);
7373
#else
7474
enet_ptp_feature_enable(ENET_RXTX_TIMESTAMP);
75-
#endif
75+
#endif // defined(GD32F4XX) || defined(GD32H7XX)
7676
enet_ptp_subsecond_increment_config(accuracy_cfg);
7777

7878
enet_ptp_timestamp_addend_config(carry_cfg);
@@ -109,7 +109,7 @@ void Gd32PtpStart() {
109109
auto* tm = localtime(&tv.tv_sec);
110110

111111
DEBUG_PRINTF("%.2d-%.2d-%.4d %.2d:%.2d:%.2d.%.6d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, static_cast<int>(tv.tv_usec));
112-
#endif
112+
#endif // NDEBUG
113113
DEBUG_EXIT();
114114
}
115115

@@ -123,7 +123,7 @@ void Gd32PtpGetTime(gd32::ptp::ptptime* ptp_time) {
123123
ptp_time->tv_nsec = systime.nanosecond;
124124
#else
125125
ptp_time->tv_nsec = gd32::PtpSubsecond2Nanosecond(systime.subsecond);
126-
#endif
126+
#endif // GD32F4XX
127127
}
128128

129129
void Gd32PtpSetTime(const gd32::ptp::ptptime* ptp_time) {
@@ -157,7 +157,7 @@ void Gd32PtpUpdateTime(const gd32::ptp::time_t* time) {
157157
const auto kAddend = ENET_PTP_TSADDEND(ENETx);
158158
#else
159159
const auto kAddend = ENET_PTP_TSADDEND;
160-
#endif
160+
#endif // GD32H7XX
161161

162162
enet_ptp_timestamp_update_config(sign, second, kSubSecond);
163163
enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_UPDATE);

lib-gd32/include/gd32.h

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,69 +26,28 @@
2626
#ifndef GD32_H_
2727
#define GD32_H_
2828

29-
#include <stdint.h>
30-
31-
struct HwTimersSeconds {
32-
#if !defined(CONFIG_NET_ENABLE_PTP)
33-
volatile uint32_t timeval;
34-
#endif
35-
volatile uint32_t uptime;
36-
};
37-
3829
#include "gd32xxxx.h" // IWYU pragma: keep
3930

4031
#if defined(GD32F30X)
4132
#define bkp_data_write bkp_write_data
4233
#define bkp_data_read bkp_read_data
43-
#endif
34+
#endif // GD32F30X
4435

4536
#if (defined(GD32F4XX) || defined(GD32H7XX)) && defined(__cplusplus)
4637
typedef enum { BKP_DATA_0, BKP_DATA_1 } bkp_data_register_enum;
4738
void bkp_data_write(bkp_data_register_enum register_number, uint16_t data);
4839
uint16_t bkp_data_read(bkp_data_register_enum register_number);
49-
#endif
40+
#endif // (defined(GD32F4XX) || defined(GD32H7XX)) && defined(__cplusplus)
5041

5142
#if !(defined(GD32F4XX) || defined(GD32H7XX))
5243
#define GPIO_INIT
53-
#endif
44+
#endif // !(defined(GD32F4XX) || defined(GD32H7XX))
5445

5546
#if defined(GD32H7XX)
5647
#define GPIO_OSPEED GPIO_OSPEED_60MHZ
5748
#else
5849
#define GPIO_OSPEED GPIO_OSPEED_50MHZ
59-
#endif
60-
61-
#ifdef __cplusplus
62-
constexpr uint32_t Gd32PortToGpio(uint32_t port, uint32_t pin) {
63-
return (port * 16U) + pin;
64-
}
65-
66-
constexpr uint8_t Gd32GpioToPort(uint32_t gpio) {
67-
return static_cast<uint8_t>(gpio / 16U);
68-
}
69-
70-
constexpr uint8_t Gd32GpioToNumber(uint32_t gpio) {
71-
return static_cast<uint8_t>(gpio % 16U);
72-
}
73-
74-
#define GD32_PORT_TO_GPIO(p, n) Gd32PortToGpio((p), (n))
75-
#define GD32_GPIO_TO_PORT(g) Gd32GpioToPort((g))
76-
#define GD32_GPIO_TO_NUMBER(g) Gd32GpioToNumber((g))
77-
#endif
78-
79-
typedef enum T_GD32_Port {
80-
GD32_GPIO_PORTA = 0,
81-
GD32_GPIO_PORTB,
82-
GD32_GPIO_PORTC,
83-
GD32_GPIO_PORTD,
84-
GD32_GPIO_PORTE,
85-
GD32_GPIO_PORTF,
86-
GD32_GPIO_PORTG,
87-
GD32_GPIO_PORTH,
88-
GD32_GPIO_PORTI,
89-
GD32_GPIO_PORTJ,
90-
GD32_GPIO_PORTK
91-
} GD32_Port_TypeDef;
50+
#endif // GD32H7XX
9251

9352
#include "gd32_board.h" // IWYU pragma: keep
9453

lib-gd32/include/gd32_debug.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,86 @@
2626
#ifndef GD32_DEBUG_H_
2727
#define GD32_DEBUG_H_
2828

29+
#include "firmware/debug/debug_debug.h"
30+
31+
#ifdef DEBUG_GD32_TIMERS
32+
#define GD32_TIMERS_DEBUG_ENTRY() DEBUG_ENTRY()
33+
#define GD32_TIMERS_DEBUG_EXIT() DEBUG_EXIT()
34+
#define GD32_TIMERS_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
35+
#define GD32_TIMERS_DEBUG_PUTS(...) DEBUG_PUTS(__VA_ARGS__)
36+
#else
37+
#define GD32_TIMERS_DEBUG_ENTRY() \
38+
do { \
39+
} while (false)
40+
#define GD32_TIMERS_DEBUG_EXIT() \
41+
do { \
42+
} while (false)
43+
#define GD32_TIMERS_DEBUG_PRINTF(...) \
44+
do { \
45+
} while (false)
46+
#define GD32_TIMERS_DEBUG_PUTS(...) \
47+
do { \
48+
} while (false)
49+
#endif // DEBUG_GD32_TIMERS
50+
51+
#ifdef DEBUG_GD32_PWM
52+
#define GD32_PWM_DEBUG_ENTRY() DEBUG_ENTRY()
53+
#define GD32_PWM_DEBUG_EXIT() DEBUG_EXIT()
54+
#define GD32_PWM_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
55+
#define GD32_PWM_DEBUG_PUTS(...) DEBUG_PUTS(__VA_ARGS__)
56+
#else
57+
#define GD32_PWM_DEBUG_ENTRY() \
58+
do { \
59+
} while (false)
60+
#define GD32_PWM_DEBUG_EXIT() \
61+
do { \
62+
} while (false)
63+
#define GD32_PWM_DEBUG_PRINTF(...) \
64+
do { \
65+
} while (false)
66+
#define GD32_PWM_DEBUG_PUTS(...) \
67+
do { \
68+
} while (false)
69+
#endif // DEBUG_GD32_PWM
70+
71+
#ifdef DEBUG_GD32_USB
72+
#define GD32_USB_DEBUG_ENTRY() DEBUG_ENTRY()
73+
#define GD32_USB_DEBUG_EXIT() DEBUG_EXIT()
74+
#define GD32_USB_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
75+
#define GD32_USB_DEBUG_PUTS(...) DEBUG_PUTS(__VA_ARGS__)
76+
#else
77+
#define GD32_USB_DEBUG_ENTRY() \
78+
do { \
79+
} while (false)
80+
#define GD32_USB_DEBUG_EXIT() \
81+
do { \
82+
} while (false)
83+
#define GD32_USB_DEBUG_PRINTF(...) \
84+
do { \
85+
} while (false)
86+
#define GD32_USB_DEBUG_PUTS(...) \
87+
do { \
88+
} while (false)
89+
#endif // DEBUG_GD32_USB
90+
91+
#ifdef DEBUG_GD32_TRNG
92+
#define GD32_TRNG_DEBUG_ENTRY() DEBUG_ENTRY()
93+
#define GD32_TRNG_DEBUG_EXIT() DEBUG_EXIT()
94+
#define GD32_TRNG_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
95+
#define GD32_TRNG_DEBUG_PUTS(...) DEBUG_PUTS(__VA_ARGS__)
96+
#else
97+
#define GD32_TRNG_DEBUG_ENTRY() \
98+
do { \
99+
} while (false)
100+
#define GD32_TRNG_DEBUG_EXIT() \
101+
do { \
102+
} while (false)
103+
#define GD32_TRNG_DEBUG_PRINTF(...) \
104+
do { \
105+
} while (false)
106+
#define GD32_TRNG_DEBUG_PUTS(...) \
107+
do { \
108+
} while (false)
109+
#endif // DEBUG_GD32_TRNG
110+
29111
#endif // GD32_DEBUG_H_

0 commit comments

Comments
 (0)