gd32f3x0_usart.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*!
  2. \file gd32f3x0_usart.h
  3. \brief definitions for the USART
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #ifndef GD32F3X0_USART_H
  10. #define GD32F3X0_USART_H
  11. #include "gd32f3x0.h"
  12. /* USARTx(x=0,1) definitions */
  13. #define USART0 (USART_BASE + 0x0000F400U)
  14. #define USART1 USART_BASE
  15. /* registers definitions */
  16. #define USART_CTL0(usartx) REG32((usartx) + 0x00U) /*!< USART control register 0 */
  17. #define USART_CTL1(usartx) REG32((usartx) + 0x04U) /*!< USART control register 1 */
  18. #define USART_CTL2(usartx) REG32((usartx) + 0x08U) /*!< USART control register 2 */
  19. #define USART_BAUD(usartx) REG32((usartx) + 0x0CU) /*!< USART baud rate register */
  20. #define USART_GP(usartx) REG32((usartx) + 0x10U) /*!< USART guard time and prescaler register */
  21. #define USART_RT(usartx) REG32((usartx) + 0x14U) /*!< USART receiver timeout register */
  22. #define USART_CMD(usartx) REG32((usartx) + 0x18U) /*!< USART command register */
  23. #define USART_STAT(usartx) REG32((usartx) + 0x1CU) /*!< USART status register */
  24. #define USART_INTC(usartx) REG32((usartx) + 0x20U) /*!< USART status clear register */
  25. #define USART_RDATA(usartx) REG32((usartx) + 0x24U) /*!< USART receive data register */
  26. #define USART_TDATA(usartx) REG32((usartx) + 0x28U) /*!< USART transmit data register */
  27. #define USART_RFCS(usartx) REG32((usartx) + 0xD0U) /*!< USART receive FIFO control and status register */
  28. /* bits definitions */
  29. /* USARTx_CTL0 */
  30. #define USART_CTL0_UEN BIT(0) /*!< USART enable */
  31. #define USART_CTL0_UESM BIT(1) /*!< USART enable in deep-sleep mode */
  32. #define USART_CTL0_REN BIT(2) /*!< receiver enable */
  33. #define USART_CTL0_TEN BIT(3) /*!< transmitter enable */
  34. #define USART_CTL0_IDLEIE BIT(4) /*!< idle line detected interrupt enable */
  35. #define USART_CTL0_RBNEIE BIT(5) /*!< read data buffer not empty interrupt and overrun error interrupt enable */
  36. #define USART_CTL0_TCIE BIT(6) /*!< transmission complete interrupt enable */
  37. #define USART_CTL0_TBEIE BIT(7) /*!< transmitter register empty interrupt enable */
  38. #define USART_CTL0_PERRIE BIT(8) /*!< parity error interrupt enable */
  39. #define USART_CTL0_PM BIT(9) /*!< parity mode */
  40. #define USART_CTL0_PCEN BIT(10) /*!< parity control enable */
  41. #define USART_CTL0_WM BIT(11) /*!< wakeup method in mute mode */
  42. #define USART_CTL0_WL BIT(12) /*!< word length */
  43. #define USART_CTL0_MEN BIT(13) /*!< mute mode enable */
  44. #define USART_CTL0_AMIE BIT(14) /*!< address match interrupt enable */
  45. #define USART_CTL0_OVSMOD BIT(15) /*!< oversample mode */
  46. #define USART_CTL0_DED BITS(16,20) /*!< driver enable deassertion time */
  47. #define USART_CTL0_DEA BITS(21,25) /*!< driver enable assertion time */
  48. #define USART_CTL0_RTIE BIT(26) /*!< receiver timeout interrupt enable */
  49. #define USART_CTL0_EBIE BIT(27) /*!< end of block interrupt enable */
  50. /* USARTx_CTL1 */
  51. #define USART_CTL1_ADDM BIT(4) /*!< address detection mode */
  52. #define USART_CTL1_LBLEN BIT(5) /*!< LIN break frame length */
  53. #define USART_CTL1_LBDIE BIT(6) /*!< LIN break detection interrupt enable */
  54. #define USART_CTL1_CLEN BIT(8) /*!< last bit clock pulse */
  55. #define USART_CTL1_CPH BIT(9) /*!< clock phase */
  56. #define USART_CTL1_CPL BIT(10) /*!< clock polarity */
  57. #define USART_CTL1_CKEN BIT(11) /*!< ck pin enable */
  58. #define USART_CTL1_STB BITS(12,13) /*!< stop bits length */
  59. #define USART_CTL1_LMEN BIT(14) /*!< LIN mode enable */
  60. #define USART_CTL1_STRP BIT(15) /*!< swap TX/RX pins */
  61. #define USART_CTL1_RINV BIT(16) /*!< RX pin level inversion */
  62. #define USART_CTL1_TINV BIT(17) /*!< TX pin level inversion */
  63. #define USART_CTL1_DINV BIT(18) /*!< data bit level inversion */
  64. #define USART_CTL1_MSBF BIT(19) /*!< most significant bit first */
  65. #define USART_CTL1_ABDEN BIT(20) /*!< auto baud rate enable */
  66. #define USART_CTL1_ABDM BITS(21,22) /*!< auto baud rate mode */
  67. #define USART_CTL1_RTEN BIT(23) /*!< receiver timeout enable */
  68. #define USART_CTL1_ADDR BITS(24,31) /*!< address of the USART terminal */
  69. /* USARTx_CTL2 */
  70. #define USART_CTL2_ERRIE BIT(0) /*!< error interrupt enable in multibuffer communication */
  71. #define USART_CTL2_IREN BIT(1) /*!< IrDA mode enable */
  72. #define USART_CTL2_IRLP BIT(2) /*!< IrDA low-power */
  73. #define USART_CTL2_HDEN BIT(3) /*!< half-duplex enable */
  74. #define USART_CTL2_NKEN BIT(4) /*!< NACK enable in smartcard mode */
  75. #define USART_CTL2_SCEN BIT(5) /*!< smartcard mode enable */
  76. #define USART_CTL2_DENR BIT(6) /*!< DMA enable for reception */
  77. #define USART_CTL2_DENT BIT(7) /*!< DMA enable for transmission */
  78. #define USART_CTL2_RTSEN BIT(8) /*!< RTS enable */
  79. #define USART_CTL2_CTSEN BIT(9) /*!< CTS enable */
  80. #define USART_CTL2_CTSIE BIT(10) /*!< CTS interrupt enable */
  81. #define USART_CTL2_OSB BIT(11) /*!< one sample bit mode */
  82. #define USART_CTL2_OVRD BIT(12) /*!< overrun disable */
  83. #define USART_CTL2_DDRE BIT(13) /*!< disable DMA on reception error */
  84. #define USART_CTL2_DEM BIT(14) /*!< driver enable mode */
  85. #define USART_CTL2_DEP BIT(15) /*!< driver enable polarity mode */
  86. #define USART_CTL2_SCRTNUM BITS(17,19) /*!< smartcard auto-retry number */
  87. #define USART_CTL2_WUM BITS(20,21) /*!< wakeup mode from deep-sleep mode */
  88. #define USART_CTL2_WUIE BIT(22) /*!< wakeup from deep-sleep mode interrupt enable */
  89. /* USARTx_BAUD */
  90. #define USART_BAUD_FRADIV BITS(0,3) /*!< fraction of baud-rate divider */
  91. #define USART_BAUD_INTDIV BITS(4,15) /*!< integer of baud-rate divider */
  92. /* USARTx_GP */
  93. #define USART_GP_PSC BITS(0,7) /*!< prescaler value for dividing the system clock */
  94. #define USART_GP_GUAT BITS(8,15) /*!< guard time value in smartcard mode */
  95. /* USARTx_RT */
  96. #define USART_RT_RT BITS(0,23) /*!< receiver timeout threshold */
  97. #define USART_RT_BL BITS(24,31) /*!< block length */
  98. /* USARTx_CMD */
  99. #define USART_CMD_ABDCMD BIT(0) /*!< auto baudrate detection command */
  100. #define USART_CMD_SBKCMD BIT(1) /*!< send break command */
  101. #define USART_CMD_MMCMD BIT(2) /*!< mute mode command */
  102. #define USART_CMD_RXFCMD BIT(3) /*!< receive data flush command */
  103. #define USART_CMD_TXFCMD BIT(4) /*!< transmit data flush request */
  104. /* USARTx_STAT */
  105. #define USART_STAT_PERR BIT(0) /*!< parity error flag */
  106. #define USART_STAT_FERR BIT(1) /*!< frame error flag */
  107. #define USART_STAT_NERR BIT(2) /*!< noise error flag */
  108. #define USART_STAT_ORERR BIT(3) /*!< overrun error */
  109. #define USART_STAT_IDLEF BIT(4) /*!< idle line detected flag */
  110. #define USART_STAT_RBNE BIT(5) /*!< read data buffer not empty */
  111. #define USART_STAT_TC BIT(6) /*!< transmission completed */
  112. #define USART_STAT_TBE BIT(7) /*!< transmit data register empty */
  113. #define USART_STAT_LBDF BIT(8) /*!< LIN break detected flag */
  114. #define USART_STAT_CTSF BIT(9) /*!< CTS change flag */
  115. #define USART_STAT_CTS BIT(10) /*!< CTS level */
  116. #define USART_STAT_RTF BIT(11) /*!< receiver timeout flag */
  117. #define USART_STAT_EBF BIT(12) /*!< end of block flag */
  118. #define USART_STAT_ABDE BIT(14) /*!< auto baudrate detection error */
  119. #define USART_STAT_ABDF BIT(15) /*!< auto baudrate detection flag */
  120. #define USART_STAT_BSY BIT(16) /*!< busy flag */
  121. #define USART_STAT_AMF BIT(17) /*!< address match flag */
  122. #define USART_STAT_SBF BIT(18) /*!< send break flag */
  123. #define USART_STAT_RWU BIT(19) /*!< receiver wakeup from mute mode */
  124. #define USART_STAT_WUF BIT(20) /*!< wakeup from deep-sleep mode flag */
  125. #define USART_STAT_TEA BIT(21) /*!< transmit enable acknowledge flag */
  126. #define USART_STAT_REA BIT(22) /*!< receive enable acknowledge flag */
  127. /* USARTx_INTC */
  128. #define USART_INTC_PEC BIT(0) /*!< parity error clear */
  129. #define USART_INTC_FEC BIT(1) /*!< frame error flag clear */
  130. #define USART_INTC_NEC BIT(2) /*!< noise detected clear */
  131. #define USART_INTC_OREC BIT(3) /*!< overrun error clear */
  132. #define USART_INTC_IDLEC BIT(4) /*!< idle line detected clear */
  133. #define USART_INTC_TCC BIT(6) /*!< transmission complete clear */
  134. #define USART_INTC_LBDC BIT(8) /*!< LIN break detected clear */
  135. #define USART_INTC_CTSC BIT(9) /*!< CTS change clear */
  136. #define USART_INTC_RTC BIT(11) /*!< receiver timeout clear */
  137. #define USART_INTC_EBC BIT(12) /*!< end of timeout clear */
  138. #define USART_INTC_AMC BIT(17) /*!< address match clear */
  139. #define USART_INTC_WUC BIT(20) /*!< wakeup from deep-sleep mode clear */
  140. /* USARTx_RDATA */
  141. #define USART_RDATA_RDATA BITS(0,8) /*!< receive data value */
  142. /* USARTx_TDATA */
  143. #define USART_TDATA_TDATA BITS(0,8) /*!< transmit data value */
  144. /* USARTx_RFCS */
  145. #define USART_RFCS_ELNACK BIT(0) /*!< early NACK */
  146. #define USART_RFCS_RFEN BIT(8) /*!< receive FIFO enable */
  147. #define USART_RFCS_RFFIE BIT(9) /*!< receive FIFO full interrupt enable */
  148. #define USART_RFCS_RFE BIT(10) /*!< receive FIFO empty flag */
  149. #define USART_RFCS_RFF BIT(11) /*!< receive FIFO full flag */
  150. #define USART_RFCS_RFCNT BITS(12,14) /*!< receive FIFO counter number */
  151. #define USART_RFCS_RFFINT BIT(15) /*!< receive FIFO full interrupt flag */
  152. /* constants definitions */
  153. /* define the USART bit position and its register index offset */
  154. #define USART_REGIDX_BIT(regidx, bitpos) (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
  155. #define USART_REG_VAL(usartx, offset) (REG32((usartx) + (((uint32_t)(offset) & 0xFFFFU) >> 6)))
  156. #define USART_BIT_POS(val) ((uint32_t)(val) & 0x1FU)
  157. #define USART_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2) (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
  158. | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
  159. #define USART_REG_VAL2(usartx, offset) (REG32((usartx) + ((uint32_t)(offset) >> 22)))
  160. #define USART_BIT_POS2(val) (((uint32_t)(val) & 0x1F0000U) >> 16)
  161. /* register offset */
  162. #define USART_CTL0_REG_OFFSET 0x00U /*!< CTL0 register offset */
  163. #define USART_CTL1_REG_OFFSET 0x04U /*!< CTL1 register offset */
  164. #define USART_CTL2_REG_OFFSET 0x08U /*!< CTL2 register offset */
  165. #define USART_STAT_REG_OFFSET 0x1CU /*!< STAT register offset */
  166. #define USART_RFCS_REG_OFFSET 0xD0U /*!< RFCS register offset */
  167. /* USART flags */
  168. typedef enum{
  169. /* flags in STAT register */
  170. USART_FLAG_REA = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 22U), /*!< receive enable acknowledge flag */
  171. USART_FLAG_TEA = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 21U), /*!< transmit enable acknowledge flag */
  172. USART_FLAG_WU = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 20U), /*!< wakeup from Deep-sleep mode flag */
  173. USART_FLAG_RWU = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 19U), /*!< receiver wakeup from mute mode */
  174. USART_FLAG_SB = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 18U), /*!< send break flag */
  175. USART_FLAG_AM = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 17U), /*!< ADDR match flag */
  176. USART_FLAG_BSY = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 16U), /*!< busy flag */
  177. USART_FLAG_ABD = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 15U), /*!< auto baudrate detection flag */
  178. USART_FLAG_ABDE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 14U), /*!< auto baudrate detection error */
  179. USART_FLAG_EB = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 12U), /*!< end of block flag */
  180. USART_FLAG_RT = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 11U), /*!< receiver timeout flag */
  181. USART_FLAG_CTS = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 10U), /*!< CTS level */
  182. USART_FLAG_CTSF = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 9U), /*!< CTS change flag */
  183. USART_FLAG_LBD = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 8U), /*!< LIN break detected flag */
  184. USART_FLAG_TBE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 7U), /*!< transmit data buffer empty */
  185. USART_FLAG_TC = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 6U), /*!< transmission complete */
  186. USART_FLAG_RBNE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 5U), /*!< read data buffer not empty */
  187. USART_FLAG_IDLE = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 4U), /*!< IDLE line detected flag */
  188. USART_FLAG_ORERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 3U), /*!< overrun error */
  189. USART_FLAG_NERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 2U), /*!< noise error flag */
  190. USART_FLAG_FERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 1U), /*!< frame error flag */
  191. USART_FLAG_PERR = USART_REGIDX_BIT(USART_STAT_REG_OFFSET, 0U), /*!< parity error flag */
  192. /* flags in RFCS register */
  193. USART_FLAG_RFFINT = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 15U), /*!< receive FIFO full interrupt flag */
  194. USART_FLAG_RFF = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 11U), /*!< receive FIFO full flag */
  195. USART_FLAG_RFE = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 10U), /*!< receive FIFO empty flag */
  196. }usart_flag_enum;
  197. /* USART interrupt flags */
  198. typedef enum
  199. {
  200. /* interrupt flags in CTL0 register */
  201. USART_INT_FLAG_EB = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 27U, USART_STAT_REG_OFFSET, 12U), /*!< end of block interrupt and flag */
  202. USART_INT_FLAG_RT = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 26U, USART_STAT_REG_OFFSET, 11U), /*!< receiver timeout interrupt and flag */
  203. USART_INT_FLAG_AM = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 14U, USART_STAT_REG_OFFSET, 17U), /*!< address match interrupt and flag */
  204. USART_INT_FLAG_PERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 8U, USART_STAT_REG_OFFSET, 0U), /*!< parity error interrupt and flag */
  205. USART_INT_FLAG_TBE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 7U, USART_STAT_REG_OFFSET, 7U), /*!< transmitter buffer empty interrupt and flag */
  206. USART_INT_FLAG_TC = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 6U, USART_STAT_REG_OFFSET, 6U), /*!< transmission complete interrupt and flag */
  207. USART_INT_FLAG_RBNE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT_REG_OFFSET, 5U), /*!< read data buffer not empty interrupt and flag */
  208. USART_INT_FLAG_RBNE_ORERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT_REG_OFFSET, 3U), /*!< read data buffer not empty interrupt and overrun error flag */
  209. USART_INT_FLAG_IDLE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 4U, USART_STAT_REG_OFFSET, 4U), /*!< IDLE line detected interrupt and flag */
  210. /* interrupt flags in CTL1 register */
  211. USART_INT_FLAG_LBD = USART_REGIDX_BIT2(USART_CTL1_REG_OFFSET, 6U, USART_STAT_REG_OFFSET, 8U), /*!< LIN break detected interrupt and flag */
  212. /* interrupt flags in CTL2 register */
  213. USART_INT_FLAG_WU = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 22U, USART_STAT_REG_OFFSET, 20U), /*!< wakeup from deep-sleep mode interrupt and flag */
  214. USART_INT_FLAG_CTS = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 10U, USART_STAT_REG_OFFSET, 9U), /*!< CTS interrupt and flag */
  215. USART_INT_FLAG_ERR_NERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 2U), /*!< error interrupt and noise error flag */
  216. USART_INT_FLAG_ERR_ORERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 3U), /*!< error interrupt and overrun error */
  217. USART_INT_FLAG_ERR_FERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT_REG_OFFSET, 1U), /*!< error interrupt and frame error flag */
  218. /* interrupt flags in RFCS register */
  219. USART_INT_FLAG_RFF = USART_REGIDX_BIT2(USART_RFCS_REG_OFFSET, 9U, USART_RFCS_REG_OFFSET, 15U), /*!< receive FIFO full interrupt and flag */
  220. }usart_interrupt_flag_enum;
  221. /* USART interrupt enable or disable */
  222. typedef enum
  223. {
  224. /* interrupt in CTL0 register */
  225. USART_INT_EB = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 27U), /*!< end of block interrupt */
  226. USART_INT_RT = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 26U), /*!< receiver timeout interrupt */
  227. USART_INT_AM = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 14U), /*!< address match interrupt */
  228. USART_INT_PERR = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 8U), /*!< parity error interrupt */
  229. USART_INT_TBE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 7U), /*!< transmitter buffer empty interrupt */
  230. USART_INT_TC = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 6U), /*!< transmission complete interrupt */
  231. USART_INT_RBNE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 5U), /*!< read data buffer not empty interrupt and overrun error interrupt */
  232. USART_INT_IDLE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 4U), /*!< IDLE line detected interrupt */
  233. /* interrupt in CTL1 register */
  234. USART_INT_LBD = USART_REGIDX_BIT(USART_CTL1_REG_OFFSET, 6U), /*!< LIN break detected interrupt */
  235. /* interrupt in CTL2 register */
  236. USART_INT_WU = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 22U), /*!< wakeup from deep-sleep mode interrupt */
  237. USART_INT_CTS = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 10U), /*!< CTS interrupt */
  238. USART_INT_ERR = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 0U), /*!< error interrupt */
  239. /* interrupt in RFCS register */
  240. USART_INT_RFF = USART_REGIDX_BIT(USART_RFCS_REG_OFFSET, 9U), /*!< receive FIFO full interrupt */
  241. }usart_interrupt_enum;
  242. /* USART invert configure */
  243. typedef enum {
  244. /* data bit level inversion */
  245. USART_DINV_ENABLE, /*!< data bit level inversion */
  246. USART_DINV_DISABLE, /*!< data bit level not inversion */
  247. /* TX pin level inversion */
  248. USART_TXPIN_ENABLE, /*!< TX pin level inversion */
  249. USART_TXPIN_DISABLE, /*!< TX pin level not inversion */
  250. /* RX pin level inversion */
  251. USART_RXPIN_ENABLE, /*!< RX pin level inversion */
  252. USART_RXPIN_DISABLE, /*!< RX pin level not inversion */
  253. /* swap TX/RX pins */
  254. USART_SWAP_ENABLE, /*!< swap TX/RX pins */
  255. USART_SWAP_DISABLE, /*!< not swap TX/RX pins */
  256. }usart_invert_enum;
  257. /* USART receiver configure */
  258. #define CTL0_REN(regval) (BIT(2) & ((uint32_t)(regval) << 2))
  259. #define USART_RECEIVE_ENABLE CTL0_REN(1) /*!< enable receiver */
  260. #define USART_RECEIVE_DISABLE CTL0_REN(0) /*!< disable receiver */
  261. /* USART transmitter configure */
  262. #define CTL0_TEN(regval) (BIT(3) & ((uint32_t)(regval) << 3))
  263. #define USART_TRANSMIT_ENABLE CTL0_TEN(1) /*!< enable transmitter */
  264. #define USART_TRANSMIT_DISABLE CTL0_TEN(0) /*!< disable transmitter */
  265. /* USART parity bits definitions */
  266. #define CTL0_PM(regval) (BITS(9,10) & ((uint32_t)(regval) << 9))
  267. #define USART_PM_NONE CTL0_PM(0) /*!< no parity */
  268. #define USART_PM_EVEN CTL0_PM(2) /*!< even parity */
  269. #define USART_PM_ODD CTL0_PM(3) /*!< odd parity */
  270. /* USART wakeup method in mute mode */
  271. #define CTL0_WM(regval) (BIT(11) & ((uint32_t)(regval) << 11))
  272. #define USART_WM_IDLE CTL0_WM(0) /*!< idle line */
  273. #define USART_WM_ADDR CTL0_WM(1) /*!< address match */
  274. /* USART word length definitions */
  275. #define CTL0_WL(regval) (BIT(12) & ((uint32_t)(regval) << 12))
  276. #define USART_WL_8BIT CTL0_WL(0) /*!< 8 bits */
  277. #define USART_WL_9BIT CTL0_WL(1) /*!< 9 bits */
  278. /* USART oversample mode */
  279. #define CTL0_OVSMOD(regval) (BIT(15) & ((uint32_t)(regval) << 15))
  280. #define USART_OVSMOD_8 CTL0_OVSMOD(1) /*!< oversampling by 8 */
  281. #define USART_OVSMOD_16 CTL0_OVSMOD(0) /*!< oversampling by 16 */
  282. /* USART address detection mode */
  283. #define CTL1_ADDM(regval) (BIT(4) & ((uint32_t)(regval) << 4))
  284. #define USART_ADDM_4BIT CTL1_ADDM(0) /*!< 4-bit address detection */
  285. #define USART_ADDM_FULLBIT CTL1_ADDM(1) /*!< full-bit address detection */
  286. /* USART LIN break frame length */
  287. #define CTL1_LBLEN(regval) (BIT(5) & ((uint32_t)(regval) << 5))
  288. #define USART_LBLEN_10B CTL1_LBLEN(0) /*!< 10 bits break detection */
  289. #define USART_LBLEN_11B CTL1_LBLEN(1) /*!< 11 bits break detection */
  290. /* USART last bit clock pulse */
  291. #define CTL1_CLEN(regval) (BIT(8) & ((uint32_t)(regval) << 8))
  292. #define USART_CLEN_NONE CTL1_CLEN(0) /*!< clock pulse of the last data bit (MSB) is not output to the CK pin */
  293. #define USART_CLEN_EN CTL1_CLEN(1) /*!< clock pulse of the last data bit (MSB) is output to the CK pin */
  294. /* USART clock phase */
  295. #define CTL1_CPH(regval) (BIT(9) & ((uint32_t)(regval) << 9))
  296. #define USART_CPH_1CK CTL1_CPH(0) /*!< first clock transition is the first data capture edge */
  297. #define USART_CPH_2CK CTL1_CPH(1) /*!< second clock transition is the first data capture edge */
  298. /* USART clock polarity */
  299. #define CTL1_CPL(regval) (BIT(10) & ((uint32_t)(regval) << 10))
  300. #define USART_CPL_LOW CTL1_CPL(0) /*!< steady low value on CK pin */
  301. #define USART_CPL_HIGH CTL1_CPL(1) /*!< steady high value on CK pin */
  302. /* USART stop bits definitions */
  303. #define CTL1_STB(regval) (BITS(12,13) & ((uint32_t)(regval) << 12))
  304. #define USART_STB_1BIT CTL1_STB(0) /*!< 1 bit */
  305. #define USART_STB_0_5BIT CTL1_STB(1) /*!< 0.5 bit */
  306. #define USART_STB_2BIT CTL1_STB(2) /*!< 2 bits */
  307. #define USART_STB_1_5BIT CTL1_STB(3) /*!< 1.5 bits */
  308. /* USART data is transmitted/received with the LSB/MSB first */
  309. #define CTL1_MSBF(regval) (BIT(19) & ((uint32_t)(regval) << 19))
  310. #define USART_MSBF_LSB CTL1_MSBF(0) /*!< LSB first */
  311. #define USART_MSBF_MSB CTL1_MSBF(1) /*!< MSB first */
  312. /* USART auto baud rate detection mode bits definitions */
  313. #define CTL1_ABDM(regval) (BITS(21,22) & ((uint32_t)(regval) << 21))
  314. #define USART_ABDM_FTOR CTL1_ABDM(0) /*!< falling edge to rising edge measurement */
  315. #define USART_ABDM_FTOF CTL1_ABDM(1) /*!< falling edge to falling edge measurement */
  316. /* USART IrDA low-power enable */
  317. #define CTL2_IRLP(regval) (BIT(2) & ((uint32_t)(regval) << 2))
  318. #define USART_IRLP_LOW CTL2_IRLP(1) /*!< low-power */
  319. #define USART_IRLP_NORMAL CTL2_IRLP(0) /*!< normal */
  320. /* DMA enable for reception */
  321. #define CTL2_DENR(regval) (BIT(6) & ((uint32_t)(regval) << 6))
  322. #define USART_DENR_ENABLE CTL2_DENR(1) /*!< enable for reception */
  323. #define USART_DENR_DISABLE CTL2_DENR(0) /*!< disable for reception */
  324. /* DMA enable for transmission */
  325. #define CTL2_DENT(regval) (BIT(7) & ((uint32_t)(regval) << 7))
  326. #define USART_DENT_ENABLE CTL2_DENT(1) /*!< enable for transmission */
  327. #define USART_DENT_DISABLE CTL2_DENT(0) /*!< disable for transmission */
  328. /* USART RTS hardware flow control configure */
  329. #define CTL2_RTSEN(regval) (BIT(8) & ((uint32_t)(regval) << 8))
  330. #define USART_RTS_ENABLE CTL2_RTSEN(1) /*!< RTS hardware flow control enabled */
  331. #define USART_RTS_DISABLE CTL2_RTSEN(0) /*!< RTS hardware flow control disabled */
  332. /* USART CTS hardware flow control configure */
  333. #define CTL2_CTSEN(regval) (BIT(9) & ((uint32_t)(regval) << 9))
  334. #define USART_CTS_ENABLE CTL2_CTSEN(1) /*!< CTS hardware flow control enabled */
  335. #define USART_CTS_DISABLE CTL2_CTSEN(0) /*!< CTS hardware flow control disabled */
  336. /* USART one sample bit method configure */
  337. #define CTL2_OSB(regval) (BIT(11) & ((uint32_t)(regval) << 11))
  338. #define USART_OSB_1BIT CTL2_OSB(1) /*!< 1 sample bit */
  339. #define USART_OSB_3BIT CTL2_OSB(0) /*!< 3 sample bits */
  340. /* USART driver enable polarity mode */
  341. #define CTL2_DEP(regval) (BIT(15) & ((uint32_t)(regval) << 15))
  342. #define USART_DEP_HIGH CTL2_DEP(0) /*!< DE signal is active high */
  343. #define USART_DEP_LOW CTL2_DEP(1) /*!< DE signal is active low */
  344. /* USART wakeup mode from deep-sleep mode */
  345. #define CTL2_WUM(regval) (BITS(20,21) & ((uint32_t)(regval) << 20))
  346. #define USART_WUM_ADDR CTL2_WUM(0) /*!< WUF active on address match */
  347. #define USART_WUM_STARTB CTL2_WUM(2) /*!< WUF active on start bit */
  348. #define USART_WUM_RBNE CTL2_WUM(3) /*!< WUF active on RBNE */
  349. /* function declarations */
  350. /* initialization functions */
  351. /* reset USART */
  352. void usart_deinit(uint32_t usart_periph);
  353. /* configure USART baud rate value */
  354. void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval);
  355. /* configure USART parity function */
  356. void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg);
  357. /* configure USART word length */
  358. void usart_word_length_set(uint32_t usart_periph, uint32_t wlen);
  359. /* configure USART stop bit length */
  360. void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen);
  361. /* USART normal mode communication */
  362. /* enable USART */
  363. void usart_enable(uint32_t usart_periph);
  364. /* disable USART */
  365. void usart_disable(uint32_t usart_periph);
  366. /* configure USART transmitter */
  367. void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig);
  368. /* configure USART receiver */
  369. void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig);
  370. /* USART transmit data function */
  371. void usart_data_transmit(uint32_t usart_periph, uint32_t data);
  372. /* USART receive data function */
  373. uint16_t usart_data_receive(uint32_t usart_periph);
  374. /* data is transmitted/received with the LSB/MSB first */
  375. void usart_data_first_config(uint32_t usart_periph, uint32_t msbf);
  376. /* configure USART inverted */
  377. void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara);
  378. /* overrun function is enabled */
  379. void usart_overrun_enable(uint32_t usart_periph);
  380. /* overrun function is disabled */
  381. void usart_overrun_disable(uint32_t usart_periph);
  382. /* configure the USART oversample mode */
  383. void usart_oversample_config(uint32_t usart_periph, uint32_t oversamp);
  384. /* sample bit method configure */
  385. void usart_sample_bit_config(uint32_t usart_periph, uint32_t osb);
  386. /* auto baud rate detection */
  387. /* auto baud rate detection enable */
  388. void usart_autobaud_detection_enable(uint32_t usart_periph);
  389. /* auto baud rate detection disable */
  390. void usart_autobaud_detection_disable(uint32_t usart_periph);
  391. /* auto baud rate detection mode configure */
  392. void usart_autobaud_detection_mode_config(uint32_t usart_periph, uint32_t abdmod);
  393. /* multi-processor communication */
  394. /* enable mute mode */
  395. void usart_mute_mode_enable(uint32_t usart_periph);
  396. /* disable mute mode */
  397. void usart_mute_mode_disable(uint32_t usart_periph);
  398. /* configure wakeup method in mute mode */
  399. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod);
  400. /* address detection mode configure */
  401. void usart_address_detection_mode_config(uint32_t usart_periph, uint32_t addmod);
  402. /* configure address of the USART */
  403. void usart_address_config(uint32_t usart_periph, uint8_t addr);
  404. /* enable receiver timeout */
  405. void usart_receiver_timeout_enable(uint32_t usart_periph);
  406. /* disable receiver timeout */
  407. void usart_receiver_timeout_disable(uint32_t usart_periph);
  408. /* configure receiver timeout threshold */
  409. void usart_receiver_timeout_config(uint32_t usart_periph, uint32_t rtimeout);
  410. /* LIN mode communication */
  411. /* LIN mode enable */
  412. void usart_lin_mode_enable(uint32_t usart_periph);
  413. /* LIN mode disable */
  414. void usart_lin_mode_disable(uint32_t usart_periph);
  415. /* LIN break detection length */
  416. void usart_lin_break_dection_length_config(uint32_t usart_periph, uint32_t lblen);
  417. /* half-duplex communication */
  418. /* half-duplex enable */
  419. void usart_halfduplex_enable(uint32_t usart_periph);
  420. /* half-duplex disable */
  421. void usart_halfduplex_disable(uint32_t usart_periph);
  422. /* synchronous communication */
  423. /* clock enable */
  424. void usart_clock_enable(uint32_t usart_periph);
  425. /* clock disable*/
  426. void usart_clock_disable(uint32_t usart_periph);
  427. /* configure USART synchronous mode parameters */
  428. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl);
  429. /* smartcard communication */
  430. /* smartcard mode enable */
  431. void usart_smartcard_mode_enable(uint32_t usart_periph);
  432. /* smartcard mode disable */
  433. void usart_smartcard_mode_disable(uint32_t usart_periph);
  434. /* NACK enable in smartcard mode */
  435. void usart_smartcard_mode_nack_enable(uint32_t usart_periph);
  436. /* NACK disable in smartcard mode */
  437. void usart_smartcard_mode_nack_disable(uint32_t usart_periph);
  438. /* guard time value configure in smartcard mode */
  439. void usart_guard_time_config(uint32_t usart_periph, uint32_t guat);
  440. /* block length configure */
  441. void usart_block_length_config(uint32_t usart_periph, uint32_t bl);
  442. /* smartcard auto-retry number configure */
  443. void usart_smartcard_autoretry_config(uint32_t usart_periph, uint32_t scrtnum);
  444. /* early NACK enable */
  445. void usart_smartcard_mode_early_nack_enable(uint32_t usart_periph);
  446. /* early NACK disable */
  447. void usart_smartcard_mode_early_nack_disable(uint32_t usart_periph);
  448. /* IrDA communication */
  449. /* enable IrDA mode */
  450. void usart_irda_mode_enable(uint32_t usart_periph);
  451. /* disable IrDA mode */
  452. void usart_irda_mode_disable(uint32_t usart_periph);
  453. /* configure IrDA low-power */
  454. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp);
  455. /* configure the peripheral clock prescaler */
  456. void usart_prescaler_config(uint32_t usart_periph, uint32_t psc);
  457. /* hardware flow communication */
  458. /* configure hardware flow control RTS */
  459. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig);
  460. /* configure hardware flow control CTS */
  461. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig);
  462. /* RS485 driver enable */
  463. void usart_rs485_driver_enable(uint32_t usart_periph);
  464. /* RS485 driver disable */
  465. void usart_rs485_driver_disable(uint32_t usart_periph);
  466. /* driver enable assertion time configure */
  467. void usart_driver_assertime_config(uint32_t usart_periph, uint32_t deatime);
  468. /* driver enable de-assertion time configure */
  469. void usart_driver_deassertime_config(uint32_t usart_periph, uint32_t dedtime);
  470. /* configure driver enable polarity mode */
  471. void usart_depolarity_config(uint32_t usart_periph, uint32_t dep);
  472. /* USART DMA */
  473. /* configure USART DMA for reception */
  474. void usart_dma_receive_config(uint32_t usart_periph, uint32_t dmacmd);
  475. /* configure USART DMA for transmission */
  476. void usart_dma_transmit_config(uint32_t usart_periph, uint32_t dmacmd);
  477. /* disable DMA on reception error */
  478. void usart_reception_error_dma_disable(uint32_t usart_periph);
  479. /* enable DMA on reception error */
  480. void usart_reception_error_dma_enable(uint32_t usart_periph);
  481. /* USART be able to wake up the mcu from deep-sleep mode */
  482. void usart_wakeup_enable(uint32_t usart_periph);
  483. /* USART be not able to wake up the mcu from deep-sleep mode */
  484. void usart_wakeup_disable(uint32_t usart_periph);
  485. /* wakeup mode from deep-sleep mode */
  486. void usart_wakeup_mode_config(uint32_t usart_periph, uint32_t wum);
  487. /* USART receive FIFO */
  488. /* receive FIFO enable */
  489. void usart_receive_fifo_enable(uint32_t usart_periph);
  490. /* receive FIFO disable */
  491. void usart_receive_fifo_disable(uint32_t usart_periph);
  492. /* read receive FIFO counter number */
  493. uint8_t usart_receive_fifo_counter_number(uint32_t usart_periph);
  494. /* get flag in STAT/RFCS register */
  495. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag);
  496. /* clear USART status */
  497. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag);
  498. /* enable USART interrupt */
  499. void usart_interrupt_enable(uint32_t usart_periph, uint32_t inttype);
  500. /* disable USART interrupt */
  501. void usart_interrupt_disable(uint32_t usart_periph, uint32_t inttype);
  502. /* enable USART command */
  503. void usart_command_enable(uint32_t usart_periph, uint32_t cmdtype);
  504. /* get USART interrupt and flag status */
  505. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, uint32_t int_flag);
  506. /* clear USART interrupt flag */
  507. void usart_interrupt_flag_clear(uint32_t usart_periph, uint32_t flag);
  508. #endif /* GD32F3X0_USART_H */