Skip to content

Commit 430a32e

Browse files
committed
Namespace GD32 UART API and update callers
Refactors the low-level UART interface into the `gd32` namespace by renaming `Gd32*` functions/templates to `Uart*`, adding a typed `gd32::Uart` enum for peripheral IDs, and updating `uart.h` plus UART0 implementation to use the new API. Also adds the missing `extern HwTimersSeconds gv_seconds` declaration in `timing.h` to expose the shared seconds timer state.
1 parent 781a2f2 commit 430a32e

5 files changed

Lines changed: 71 additions & 50 deletions

File tree

lib-gd32/include/gd32_uart.h

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#include "gd32.h" // IWYU pragma: keep
3232

3333
#if !defined(GD32H7XX)
34-
#define USART_TDATA USART_DATA
35-
#define USART_RDATA USART_DATA
34+
#define USART_TDATA USART_DATA
35+
#define USART_RDATA USART_DATA
3636
#define USART_TDATA_TDATA USART_DATA_DATA
3737
#define USART_RDATA_TDATA USART_DATA_DATA
3838
#endif
@@ -45,70 +45,87 @@ inline constexpr uint32_t kUartParityOdd = 1;
4545
inline constexpr uint32_t kUartParityEven = 2;
4646
inline constexpr uint32_t kUartStop1Bit = 1;
4747
inline constexpr uint32_t kUartStop2Bits = 2;
48-
} // namespace gd32
4948

50-
void Gd32UartBegin(uint32_t usart_periph, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop_bits);
51-
void Gd32UartSetBaudrate(uint32_t usart_periph, uint32_t baudrate);
49+
enum class Uart : uint32_t {
50+
kUart0 = USART0,
51+
kUart1 = USART1,
52+
kUart2 = USART2,
53+
kUart3 = UART3,
54+
kUart4 = UART4,
55+
#if defined(USART5)
56+
kUart5 = USART5,
57+
#endif
58+
#if defined(UART6)
59+
kUart6 = UART6,
60+
#endif
61+
#if defined(UART7)
62+
kUart7 = UART7
63+
#endif
64+
};
65+
66+
void UartBegin(uint32_t usart_periph, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop_bits);
67+
void UartSetBaudrate(uint32_t usart_periph, uint32_t baudrate);
5268

53-
void Gd32UartTransmit(uint32_t usart_periph, const uint8_t* data, uint32_t length);
54-
void Gd32UartTransmitString(uint32_t usart_periph, const char* data);
69+
void UartTransmit(uint32_t usart_periph, const uint8_t* data, uint32_t length);
70+
void UartTransmitString(uint32_t usart_periph, const char* data);
5571

56-
inline uint32_t Gd32UartGetRxFifoLevel(__attribute__((unused)) uint32_t usart_periph) {
72+
inline uint32_t UartGetRxFifoLevel(__attribute__((unused)) uint32_t usart_periph) {
5773
return 1;
5874
}
5975

60-
inline uint8_t Gd32UartGetRxData(uint32_t usart_periph) {
76+
inline uint8_t UartGetRxData(uint32_t usart_periph) {
6177
return static_cast<uint8_t>(GET_BITS(USART_RDATA(usart_periph), 0U, 8U));
6278
}
6379

64-
template <usart_flag_enum flag> bool Gd32UsartFlagGet(uint32_t usart_periph) {
65-
return (0 != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag))));
80+
template <usart_flag_enum kFlag> bool UartFlagGet(uint32_t usart_periph) {
81+
return (0 != (USART_REG_VAL(usart_periph, kFlag) & BIT(USART_BIT_POS(kFlag))));
6682
}
6783

68-
template <usart_flag_enum flag> void Gd32UsartFlagClear(uint32_t usart_periph) {
84+
template <usart_flag_enum kFlag> void UartFlagClear(uint32_t usart_periph) {
6985
#if defined(GD32F10X) || defined(GD32F30X) || defined(GD32F20X)
70-
USART_REG_VAL(usart_periph, flag) = ~BIT(USART_BIT_POS(flag));
86+
USART_REG_VAL(usart_periph, kFlag) = ~BIT(USART_BIT_POS(kFlag));
7187
#elif defined(GD32F4XX)
72-
USART_REG_VAL(usart_periph, flag) &= ~BIT(USART_BIT_POS(flag));
88+
USART_REG_VAL(usart_periph, kFlag) &= ~BIT(USART_BIT_POS(kFlag));
7389
#elif defined(GD32H7XX)
74-
if constexpr (USART_FLAG_AM1 == flag) {
90+
if constexpr (USART_FLAG_AM1 == kFlag) {
7591
USART_INTC(usart_periph) |= USART_INTC_AMC1;
76-
} else if constexpr (USART_FLAG_EPERR == flag) {
92+
} else if constexpr (USART_FLAG_EPERR == kFlag) {
7793
USART_CHC(usart_periph) &= (uint32_t)(~USART_CHC_EPERR);
78-
} else if constexpr (USART_FLAG_TFE == flag) {
94+
} else if constexpr (USART_FLAG_TFE == kFlag) {
7995
USART_FCS(usart_periph) |= USART_FCS_TFEC;
8096
} else {
81-
USART_INTC(usart_periph) |= BIT(USART_BIT_POS(flag));
97+
USART_INTC(usart_periph) |= BIT(USART_BIT_POS(kFlag));
8298
}
8399
#else
84100
#error
85101
#endif
86102
}
87103

88-
template <uint32_t interrupt> void Gd32UsartInterruptEnable(uint32_t usart_periph) {
89-
USART_REG_VAL(usart_periph, interrupt) |= BIT(USART_BIT_POS(interrupt));
104+
template <uint32_t kInterrupt> void UartInterruptEnable(uint32_t usart_periph) {
105+
USART_REG_VAL(usart_periph, kInterrupt) |= BIT(USART_BIT_POS(kInterrupt));
90106
}
91107

92-
template <uint32_t interrupt> void Gd32UsartInterruptDisable(uint32_t usart_periph) {
93-
USART_REG_VAL(usart_periph, interrupt) &= ~BIT(USART_BIT_POS(interrupt));
108+
template <uint32_t kInterrupt> void UartInterruptDisable(uint32_t usart_periph) {
109+
USART_REG_VAL(usart_periph, kInterrupt) &= ~BIT(USART_BIT_POS(kInterrupt));
94110
}
95111

96-
template <usart_interrupt_flag_enum flag> void Gd32UsartInterruptFlagClear(uint32_t usart_periph) {
112+
template <usart_interrupt_flag_enum kFlag> void UartInterruptFlagClear(uint32_t usart_periph) {
97113
#if defined(GD32F10X) || defined(GD32F30X) || defined(GD32F20X)
98-
USART_REG_VAL2(usart_periph, flag) = ~BIT(USART_BIT_POS2(flag));
114+
USART_REG_VAL2(usart_periph, kFlag) = ~BIT(USART_BIT_POS2(kFlag));
99115
#elif defined(GD32F4XX)
100-
USART_REG_VAL2(usart_periph, flag) &= ~BIT(USART_BIT_POS2(flag));
116+
USART_REG_VAL2(usart_periph, kFlag) &= ~BIT(USART_BIT_POS2(kFlag));
101117
#elif defined(GD32H7XX)
102-
if constexpr (USART_INT_FLAG_TFE == flag) {
118+
if constexpr (USART_INT_FLAG_TFE == kFlag) {
103119
USART_FCS(usart_periph) |= USART_FCS_TFEC;
104-
} else if constexpr (USART_INT_FLAG_RFF == flag) {
120+
} else if constexpr (USART_INT_FLAG_RFF == kFlag) {
105121
USART_FCS(usart_periph) &= (~USART_FCS_RFFIF);
106122
} else {
107-
USART_INTC(usart_periph) |= BIT(USART_BIT_POS2(flag));
123+
USART_INTC(usart_periph) |= BIT(USART_BIT_POS2(kFlag));
108124
}
109125
#else
110126
#error
111127
#endif
112128
}
129+
} // namespace gd32
113130

114131
#endif // GD32_UART_H_

lib-gd32/include/timing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <cstdint>
3030

31+
extern struct HwTimersSeconds gv_seconds;
32+
3133
#if defined(CONFIG_HAL_USE_SYSTICK)
3234
extern volatile uint32_t gv_nSysTickMillis;
3335
#elif defined(USE_FREE_RTOS)

lib-gd32/include/uart.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,38 @@
2929
#include "gd32_uart.h"
3030

3131
namespace uart {
32-
inline constexpr auto BITS_8 = gd32::kUartBits8;
33-
inline constexpr auto BITS_9 = gd32::kUartBits9;
32+
inline constexpr auto BITS_8 = gd32::kUartBits8;
33+
inline constexpr auto BITS_9 = gd32::kUartBits9;
3434

35-
inline constexpr auto PARITY_NONE = gd32::kUartParityNone;
36-
inline constexpr auto PARITY_ODD = gd32::kUartParityOdd;
37-
inline constexpr auto PARITY_EVEN = gd32::kUartParityEven;
35+
inline constexpr auto PARITY_NONE = gd32::kUartParityNone;
36+
inline constexpr auto PARITY_ODD = gd32::kUartParityOdd;
37+
inline constexpr auto PARITY_EVEN = gd32::kUartParityEven;
3838

39-
inline constexpr auto STOP_1BIT = gd32::kUartStop1Bit;
40-
inline constexpr auto STOP_2BITS = gd32::kUartStop2Bits;
39+
inline constexpr auto STOP_1BIT = gd32::kUartStop1Bit;
40+
inline constexpr auto STOP_2BITS = gd32::kUartStop2Bits;
4141

4242
inline void Begin(uint32_t uart_base, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop_bits) {
43-
Gd32UartBegin(uart_base, baudrate, bits, parity, stop_bits);
43+
gd32::UartBegin(uart_base, baudrate, bits, parity, stop_bits);
4444
}
4545

4646
inline void SetBaudrate(uint32_t uart_base, uint32_t baudrate) {
47-
Gd32UartSetBaudrate(uart_base, baudrate);
47+
gd32::UartSetBaudrate(uart_base, baudrate);
4848
}
4949

5050
inline void Transmit(uint32_t uart_base, const uint8_t* data, uint32_t length) {
51-
Gd32UartTransmit(uart_base, data, length);
51+
gd32::UartTransmit(uart_base, data, length);
5252
}
5353

5454
inline void TransmitString(uint32_t uart_base, const char* data) {
55-
Gd32UartTransmitString(uart_base, data);
55+
gd32::UartTransmitString(uart_base, data);
5656
}
5757

5858
inline uint32_t GetRxFifoLevel(uint32_t uart_base) {
59-
return Gd32UartGetRxFifoLevel(uart_base);
59+
return gd32::UartGetRxFifoLevel(uart_base);
6060
}
6161

6262
inline uint8_t GetRxData(uint32_t uart_base) {
63-
return Gd32UartGetRxData(uart_base);
63+
return gd32::UartGetRxData(uart_base);
6464
}
6565
} // namespace uart
6666

lib-gd32/src/gd32_uart.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ static void GpioConfig(uint32_t usart_periph) {
258258
}
259259
#endif
260260

261-
void Gd32UartBegin(uint32_t usart_periph, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop_bits) {
261+
namespace gd32 {
262+
void UartBegin(uint32_t usart_periph, uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop_bits) {
262263
RcuConfig(usart_periph);
263264
GpioConfig(usart_periph);
264265

@@ -291,7 +292,7 @@ void Gd32UartBegin(uint32_t usart_periph, uint32_t baudrate, uint32_t bits, uint
291292
USART_CTL0(usart_periph) |= USART_CTL0_UEN;
292293
}
293294

294-
void Gd32UartSetBaudrate(uint32_t usart_periph, uint32_t baudrate) {
295+
void UartSetBaudrate(uint32_t usart_periph, uint32_t baudrate) {
295296
assert(baudrate != 0);
296297

297298
USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
@@ -301,7 +302,7 @@ void Gd32UartSetBaudrate(uint32_t usart_periph, uint32_t baudrate) {
301302
USART_CTL0(usart_periph) |= USART_CTL0_UEN;
302303
}
303304

304-
void Gd32UartTransmit(uint32_t usart_periph, const uint8_t* data, uint32_t length) {
305+
void UartTransmit(uint32_t usart_periph, const uint8_t* data, uint32_t length) {
305306
if (data == nullptr) [[unlikely]] {
306307
return;
307308
}
@@ -316,7 +317,7 @@ void Gd32UartTransmit(uint32_t usart_periph, const uint8_t* data, uint32_t lengt
316317
}
317318
}
318319

319-
void Gd32UartTransmitString(uint32_t usart_periph, const char* data) {
320+
void UartTransmitString(uint32_t usart_periph, const char* data) {
320321
if (data == nullptr) [[unlikely]] {
321322
return;
322323
}
@@ -330,3 +331,4 @@ void Gd32UartTransmitString(uint32_t usart_periph, const char* data) {
330331
#endif
331332
}
332333
}
334+
} // namespace gd32

lib-gd32/src/uart0/uart0.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extern "C" void USART0_IRQHandler() {
7272
#endif
7373

7474
void Init() {
75-
Gd32UartBegin(USART0, 115200U, gd32::kUartBits8, gd32::kUartParityNone, gd32::kUartStop1Bit);
75+
gd32::UartBegin(USART0, 115200U, gd32::kUartBits8, gd32::kUartParityNone, gd32::kUartStop1Bit);
7676
#if defined(CONFIG_USART0_ENABLE_TX_DMA) || defined(CONFIG_USART0_ENABLE_RX_DMA)
7777
// DMA
7878
#if defined(GD32H7XX)
@@ -160,7 +160,7 @@ void WriteDma(const void* data, uint32_t size) {
160160
assert(data != nullptr);
161161
assert(size <= DMA_CHXCNT_CNT);
162162

163-
while (!Gd32UsartFlagGet<USART_FLAG_TBE>(USART0));
163+
while (!gd32::UartFlagGet<USART_FLAG_TBE>(USART0));
164164

165165
auto dma_chctl = DMA_CHCTL(USART0_DMAx, USART0_TX_DMA_CHx);
166166
dma_chctl &= ~DMA_CHXCTL_CHEN;
@@ -175,11 +175,11 @@ void WriteDma(const void* data, uint32_t size) {
175175

176176
void PutChar(int c) {
177177
if (c == '\n') {
178-
while (!Gd32UsartFlagGet<USART_FLAG_TBE>(USART0));
178+
while (!gd32::UartFlagGet<USART_FLAG_TBE>(USART0));
179179
USART_TDATA(USART0) = static_cast<uint16_t>(USART_TDATA_TDATA & static_cast<uint8_t>('\r'));
180180
}
181181

182-
while (!Gd32UsartFlagGet<USART_FLAG_TBE>(USART0));
182+
while (!gd32::UartFlagGet<USART_FLAG_TBE>(USART0));
183183
USART_TDATA(USART0) = static_cast<uint16_t>(USART_TDATA_TDATA & static_cast<uint8_t>(c));
184184
}
185185

@@ -230,7 +230,7 @@ int GetChar() {
230230
}
231231
#else
232232
int GetChar() {
233-
if (__builtin_expect((!Gd32UsartFlagGet<USART_FLAG_RBNE>(USART0)), 1)) {
233+
if (__builtin_expect((!gd32::UartFlagGet<USART_FLAG_RBNE>(USART0)), 1)) {
234234
return EOF;
235235
}
236236

0 commit comments

Comments
 (0)