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;
4545inline constexpr uint32_t kUartParityEven = 2 ;
4646inline constexpr uint32_t kUartStop1Bit = 1 ;
4747inline 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_
0 commit comments