gd32f30x_usart.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*!
  2. \file gd32f30x_usart.h
  3. \brief definitions for the USART
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef GD32F30X_USART_H
  33. #define GD32F30X_USART_H
  34. #include "gd32f30x.h"
  35. /* USARTx(x=0,1,2)/UARTx(x=3,4) definitions */
  36. #define USART1 USART_BASE /*!< USART1 base address */
  37. #define USART2 (USART_BASE+0x00000400U) /*!< USART2 base address */
  38. #define UART3 (USART_BASE+0x00000800U) /*!< UART3 base address */
  39. #define UART4 (USART_BASE+0x00000C00U) /*!< UART4 base address */
  40. #define USART0 (USART_BASE+0x0000F400U) /*!< USART0 base address */
  41. /* registers definitions */
  42. #define USART_STAT0(usartx) REG32((usartx) + 0x00000000U) /*!< USART status register 0 */
  43. #define USART_DATA(usartx) REG32((usartx) + 0x00000004U) /*!< USART data register */
  44. #define USART_BAUD(usartx) REG32((usartx) + 0x00000008U) /*!< USART baud rate register */
  45. #define USART_CTL0(usartx) REG32((usartx) + 0x0000000CU) /*!< USART control register 0 */
  46. #define USART_CTL1(usartx) REG32((usartx) + 0x00000010U) /*!< USART control register 1 */
  47. #define USART_CTL2(usartx) REG32((usartx) + 0x00000014U) /*!< USART control register 2 */
  48. #define USART_GP(usartx) REG32((usartx) + 0x00000018U) /*!< USART guard time and prescaler register */
  49. #define USART_CTL3(usartx) REG32((usartx) + 0x00000080U) /*!< USART control register 3 */
  50. #define USART_RT(usartx) REG32((usartx) + 0x00000084U) /*!< USART receiver timeout register */
  51. #define USART_STAT1(usartx) REG32((usartx) + 0x00000088U) /*!< USART status register 1 */
  52. /* bits definitions */
  53. /* USARTx_STAT0 */
  54. #define USART_STAT0_PERR BIT(0) /*!< parity error flag */
  55. #define USART_STAT0_FERR BIT(1) /*!< frame error flag */
  56. #define USART_STAT0_NERR BIT(2) /*!< noise error flag */
  57. #define USART_STAT0_ORERR BIT(3) /*!< overrun error */
  58. #define USART_STAT0_IDLEF BIT(4) /*!< IDLE frame detected flag */
  59. #define USART_STAT0_RBNE BIT(5) /*!< read data buffer not empty */
  60. #define USART_STAT0_TC BIT(6) /*!< transmission complete */
  61. #define USART_STAT0_TBE BIT(7) /*!< transmit data buffer empty */
  62. #define USART_STAT0_LBDF BIT(8) /*!< LIN break detected flag */
  63. #define USART_STAT0_CTSF BIT(9) /*!< CTS change flag */
  64. /* USARTx_DATA */
  65. #define USART_DATA_DATA BITS(0,8) /*!< transmit or read data value */
  66. /* USARTx_BAUD */
  67. #define USART_BAUD_FRADIV BITS(0,3) /*!< fraction part of baud-rate divider */
  68. #define USART_BAUD_INTDIV BITS(4,15) /*!< integer part of baud-rate divider */
  69. /* USARTx_CTL0 */
  70. #define USART_CTL0_SBKCMD BIT(0) /*!< send break command */
  71. #define USART_CTL0_RWU BIT(1) /*!< receiver wakeup from mute mode */
  72. #define USART_CTL0_REN BIT(2) /*!< enable receiver */
  73. #define USART_CTL0_TEN BIT(3) /*!< enable transmitter */
  74. #define USART_CTL0_IDLEIE BIT(4) /*!< enable idle line detected interrupt */
  75. #define USART_CTL0_RBNEIE BIT(5) /*!< enable read data buffer not empty interrupt and overrun error interrupt */
  76. #define USART_CTL0_TCIE BIT(6) /*!< enable transmission complete interrupt */
  77. #define USART_CTL0_TBEIE BIT(7) /*!< enable transmitter buffer empty interrupt */
  78. #define USART_CTL0_PERRIE BIT(8) /*!< enable parity error interrupt */
  79. #define USART_CTL0_PM BIT(9) /*!< parity mode */
  80. #define USART_CTL0_PCEN BIT(10) /*!< enable parity check function */
  81. #define USART_CTL0_WM BIT(11) /*!< wakeup method in mute mode */
  82. #define USART_CTL0_WL BIT(12) /*!< word length */
  83. #define USART_CTL0_UEN BIT(13) /*!< enable USART */
  84. /* USARTx_CTL1 */
  85. #define USART_CTL1_ADDR BITS(0,3) /*!< address of USART */
  86. #define USART_CTL1_LBLEN BIT(5) /*!< LIN break frame length */
  87. #define USART_CTL1_LBDIE BIT(6) /*!< eanble LIN break detected interrupt */
  88. #define USART_CTL1_CLEN BIT(8) /*!< CK length */
  89. #define USART_CTL1_CPH BIT(9) /*!< CK phase */
  90. #define USART_CTL1_CPL BIT(10) /*!< CK polarity */
  91. #define USART_CTL1_CKEN BIT(11) /*!< enable CK pin */
  92. #define USART_CTL1_STB BITS(12,13) /*!< STOP bits length */
  93. #define USART_CTL1_LMEN BIT(14) /*!< enable LIN mode */
  94. /* USARTx_CTL2 */
  95. #define USART_CTL2_ERRIE BIT(0) /*!< enable error interrupt */
  96. #define USART_CTL2_IREN BIT(1) /*!< enable IrDA mode */
  97. #define USART_CTL2_IRLP BIT(2) /*!< IrDA low-power */
  98. #define USART_CTL2_HDEN BIT(3) /*!< enable half-duplex */
  99. #define USART_CTL2_NKEN BIT(4) /*!< mode NACK enable in smartcard */
  100. #define USART_CTL2_SCEN BIT(5) /*!< senable martcard mode */
  101. #define USART_CTL2_DENR BIT(6) /*!< enable DMA request for reception */
  102. #define USART_CTL2_DENT BIT(7) /*!< enable DMA request for transmission */
  103. #define USART_CTL2_RTSEN BIT(8) /*!< enable RTS */
  104. #define USART_CTL2_CTSEN BIT(9) /*!< enable CTS */
  105. #define USART_CTL2_CTSIE BIT(10) /*!< enable CTS interrupt */
  106. /* USARTx_GP */
  107. #define USART_GP_PSC BITS(0,7) /*!< prescaler value for dividing the system clock */
  108. #define USART_GP_GUAT BITS(8,15) /*!< guard time value in smartcard mode */
  109. /* USARTx_CTL3 */
  110. #define USART_CTL3_RTEN BIT(0) /*!< enable receiver timeout */
  111. #define USART_CTL3_SCRTNUM BITS(1,3) /*!< smartcard auto-retry number */
  112. #define USART_CTL3_RTIE BIT(4) /*!< interrupt enable bit of receive timeout event */
  113. #define USART_CTL3_EBIE BIT(5) /*!< interrupt enable bit of end of block event */
  114. #define USART_CTL3_RINV BIT(8) /*!< RX pin level inversion */
  115. #define USART_CTL3_TINV BIT(9) /*!< TX pin level inversion */
  116. #define USART_CTL3_DINV BIT(10) /*!< data bit level inversion */
  117. #define USART_CTL3_MSBF BIT(11) /*!< most significant bit first */
  118. /* USARTx_RT */
  119. #define USART_RT_RT BITS(0,23) /*!< receiver timeout threshold */
  120. #define USART_RT_BL BITS(24,31) /*!< block length */
  121. /* USARTx_STAT1 */
  122. #define USART_STAT1_RTF BIT(11) /*!< receiver timeout flag */
  123. #define USART_STAT1_EBF BIT(12) /*!< end of block flag */
  124. #define USART_STAT1_BSY BIT(16) /*!< busy flag */
  125. /* constants definitions */
  126. /* define the USART bit position and its register index offset */
  127. #define USART_REGIDX_BIT(regidx, bitpos) (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos))
  128. #define USART_REG_VAL(usartx, offset) (REG32((usartx) + (((uint32_t)(offset) & 0xFFFFU) >> 6)))
  129. #define USART_BIT_POS(val) ((uint32_t)(val) & 0x1FU)
  130. #define USART_REGIDX_BIT2(regidx, bitpos, regidx2, bitpos2) (((uint32_t)(regidx2) << 22) | (uint32_t)((bitpos2) << 16)\
  131. | (((uint32_t)(regidx) << 6) | (uint32_t)(bitpos)))
  132. #define USART_REG_VAL2(usartx, offset) (REG32((usartx) + ((uint32_t)(offset) >> 22)))
  133. #define USART_BIT_POS2(val) (((uint32_t)(val) & 0x1F0000U) >> 16)
  134. /* register offset */
  135. #define USART_STAT0_REG_OFFSET ((uint32_t)0x00000000U) /*!< STAT0 register offset */
  136. #define USART_STAT1_REG_OFFSET ((uint32_t)0x00000088U) /*!< STAT1 register offset */
  137. #define USART_CTL0_REG_OFFSET ((uint32_t)0x0000000CU) /*!< CTL0 register offset */
  138. #define USART_CTL1_REG_OFFSET ((uint32_t)0x00000010U) /*!< CTL1 register offset */
  139. #define USART_CTL2_REG_OFFSET ((uint32_t)0x00000014U) /*!< CTL2 register offset */
  140. #define USART_CTL3_REG_OFFSET ((uint32_t)0x00000080U) /*!< CTL3 register offset */
  141. /* USART flags */
  142. typedef enum
  143. {
  144. /* flags in STAT0 register */
  145. USART_FLAG_CTS = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 9U), /*!< CTS change flag */
  146. USART_FLAG_LBD = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 8U), /*!< LIN break detected flag */
  147. USART_FLAG_TBE = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 7U), /*!< transmit data buffer empty */
  148. USART_FLAG_TC = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 6U), /*!< transmission complete */
  149. USART_FLAG_RBNE = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 5U), /*!< read data buffer not empty */
  150. USART_FLAG_IDLE = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 4U), /*!< IDLE frame detected flag */
  151. USART_FLAG_ORERR = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 3U), /*!< overrun error */
  152. USART_FLAG_NERR = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 2U), /*!< noise error flag */
  153. USART_FLAG_FERR = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 1U), /*!< frame error flag */
  154. USART_FLAG_PERR = USART_REGIDX_BIT(USART_STAT0_REG_OFFSET, 0U), /*!< parity error flag */
  155. /* flags in STAT1 register */
  156. USART_FLAG_BSY = USART_REGIDX_BIT(USART_STAT1_REG_OFFSET, 16U), /*!< busy flag */
  157. USART_FLAG_EB = USART_REGIDX_BIT(USART_STAT1_REG_OFFSET, 12U), /*!< end of block flag */
  158. USART_FLAG_RT = USART_REGIDX_BIT(USART_STAT1_REG_OFFSET, 11U) /*!< receiver timeout flag */
  159. }usart_flag_enum;
  160. /* USART interrupt flags */
  161. typedef enum
  162. {
  163. /* interrupt flags in CTL0 register */
  164. USART_INT_FLAG_PERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 8U, USART_STAT0_REG_OFFSET, 0U), /*!< parity error interrupt and flag */
  165. USART_INT_FLAG_TBE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 7U, USART_STAT0_REG_OFFSET, 7U), /*!< transmitter buffer empty interrupt and flag */
  166. USART_INT_FLAG_TC = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 6U, USART_STAT0_REG_OFFSET, 6U), /*!< transmission complete interrupt and flag */
  167. USART_INT_FLAG_RBNE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT0_REG_OFFSET, 5U), /*!< read data buffer not empty interrupt and flag */
  168. USART_INT_FLAG_RBNE_ORERR = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 5U, USART_STAT0_REG_OFFSET, 3U), /*!< read data buffer not empty interrupt and overrun error flag */
  169. USART_INT_FLAG_IDLE = USART_REGIDX_BIT2(USART_CTL0_REG_OFFSET, 4U, USART_STAT0_REG_OFFSET, 4U), /*!< IDLE line detected interrupt and flag */
  170. /* interrupt flags in CTL1 register */
  171. USART_INT_FLAG_LBD = USART_REGIDX_BIT2(USART_CTL1_REG_OFFSET, 6U, USART_STAT0_REG_OFFSET, 8U), /*!< LIN break detected interrupt and flag */
  172. /* interrupt flags in CTL2 register */
  173. USART_INT_FLAG_CTS = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 10U, USART_STAT0_REG_OFFSET, 9U), /*!< CTS interrupt and flag */
  174. USART_INT_FLAG_ERR_ORERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT0_REG_OFFSET, 3U), /*!< error interrupt and overrun error */
  175. USART_INT_FLAG_ERR_NERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT0_REG_OFFSET, 2U), /*!< error interrupt and noise error flag */
  176. USART_INT_FLAG_ERR_FERR = USART_REGIDX_BIT2(USART_CTL2_REG_OFFSET, 0U, USART_STAT0_REG_OFFSET, 1U), /*!< error interrupt and frame error flag */
  177. /* interrupt flags in CTL3 register */
  178. USART_INT_FLAG_EB = USART_REGIDX_BIT2(USART_CTL3_REG_OFFSET, 5U, USART_STAT1_REG_OFFSET, 12U), /*!< interrupt enable bit of end of block event and flag */
  179. USART_INT_FLAG_RT = USART_REGIDX_BIT2(USART_CTL3_REG_OFFSET, 4U, USART_STAT1_REG_OFFSET, 11U) /*!< interrupt enable bit of receive timeout event and flag */
  180. }usart_interrupt_flag_enum;
  181. /* USART interrupt enable or disable */
  182. typedef enum
  183. {
  184. /* interrupt in CTL0 register */
  185. USART_INT_PERR = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 8U), /*!< parity error interrupt */
  186. USART_INT_TBE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 7U), /*!< transmitter buffer empty interrupt */
  187. USART_INT_TC = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 6U), /*!< transmission complete interrupt */
  188. USART_INT_RBNE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 5U), /*!< read data buffer not empty interrupt and overrun error interrupt */
  189. USART_INT_IDLE = USART_REGIDX_BIT(USART_CTL0_REG_OFFSET, 4U), /*!< IDLE line detected interrupt */
  190. /* interrupt in CTL1 register */
  191. USART_INT_LBD = USART_REGIDX_BIT(USART_CTL1_REG_OFFSET, 6U), /*!< LIN break detected interrupt */
  192. /* interrupt in CTL2 register */
  193. USART_INT_CTS = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 10U), /*!< CTS interrupt */
  194. USART_INT_ERR = USART_REGIDX_BIT(USART_CTL2_REG_OFFSET, 0U), /*!< error interrupt */
  195. /* interrupt in CTL3 register */
  196. USART_INT_EB = USART_REGIDX_BIT(USART_CTL3_REG_OFFSET, 5U), /*!< end of block interrupt */
  197. USART_INT_RT = USART_REGIDX_BIT(USART_CTL3_REG_OFFSET, 4U) /*!< receive timeout interrupt */
  198. }usart_interrupt_enum;
  199. /* configure USART invert */
  200. typedef enum
  201. {
  202. /* data bit level inversion */
  203. USART_DINV_ENABLE, /*!< data bit level inversion */
  204. USART_DINV_DISABLE, /*!< data bit level not inversion */
  205. /* TX pin level inversion */
  206. USART_TXPIN_ENABLE, /*!< TX pin level inversion */
  207. USART_TXPIN_DISABLE, /*!< TX pin level not inversion */
  208. /* RX pin level inversion */
  209. USART_RXPIN_ENABLE, /*!< RX pin level inversion */
  210. USART_RXPIN_DISABLE /*!< RX pin level not inversion */
  211. }usart_invert_enum;
  212. /* configure USART receiver */
  213. #define CTL0_REN(regval) (BIT(2) & ((uint32_t)(regval) << 2))
  214. #define USART_RECEIVE_ENABLE CTL0_REN(1) /*!< enable receiver */
  215. #define USART_RECEIVE_DISABLE CTL0_REN(0) /*!< disable receiver */
  216. /* configure USART transmitter */
  217. #define CTL0_TEN(regval) (BIT(3) & ((uint32_t)(regval) << 3))
  218. #define USART_TRANSMIT_ENABLE CTL0_TEN(1) /*!< enable transmitter */
  219. #define USART_TRANSMIT_DISABLE CTL0_TEN(0) /*!< disable transmitter */
  220. /* USART parity bits definitions */
  221. #define CTL0_PM(regval) (BITS(9,10) & ((uint32_t)(regval) << 9))
  222. #define USART_PM_NONE CTL0_PM(0) /*!< no parity */
  223. #define USART_PM_EVEN CTL0_PM(2) /*!< even parity */
  224. #define USART_PM_ODD CTL0_PM(3) /*!< odd parity */
  225. /* USART wakeup method in mute mode */
  226. #define CTL0_WM(regval) (BIT(11) & ((uint32_t)(regval) << 11))
  227. #define USART_WM_IDLE CTL0_WM(0) /*!< idle line */
  228. #define USART_WM_ADDR CTL0_WM(1) /*!< address match */
  229. /* USART word length definitions */
  230. #define CTL0_WL(regval) (BIT(12) & ((uint32_t)(regval) << 12))
  231. #define USART_WL_8BIT CTL0_WL(0) /*!< 8 bits */
  232. #define USART_WL_9BIT CTL0_WL(1) /*!< 9 bits */
  233. /* USART stop bits definitions */
  234. #define CTL1_STB(regval) (BITS(12,13) & ((uint32_t)(regval) << 12))
  235. #define USART_STB_1BIT CTL1_STB(0) /*!< 1 bit */
  236. #define USART_STB_0_5BIT CTL1_STB(1) /*!< 0.5 bit */
  237. #define USART_STB_2BIT CTL1_STB(2) /*!< 2 bits */
  238. #define USART_STB_1_5BIT CTL1_STB(3) /*!< 1.5 bits */
  239. /* USART LIN break frame length */
  240. #define CTL1_LBLEN(regval) (BIT(5) & ((uint32_t)(regval) << 5))
  241. #define USART_LBLEN_10B CTL1_LBLEN(0) /*!< 10 bits */
  242. #define USART_LBLEN_11B CTL1_LBLEN(1) /*!< 11 bits */
  243. /* USART CK length */
  244. #define CTL1_CLEN(regval) (BIT(8) & ((uint32_t)(regval) << 8))
  245. #define USART_CLEN_NONE CTL1_CLEN(0) /*!< there are 7 CK pulses for an 8 bit frame and 8 CK pulses for a 9 bit frame */
  246. #define USART_CLEN_EN CTL1_CLEN(1) /*!< there are 8 CK pulses for an 8 bit frame and 9 CK pulses for a 9 bit frame */
  247. /* USART clock phase */
  248. #define CTL1_CPH(regval) (BIT(9) & ((uint32_t)(regval) << 9))
  249. #define USART_CPH_1CK CTL1_CPH(0) /*!< first clock transition is the first data capture edge */
  250. #define USART_CPH_2CK CTL1_CPH(1) /*!< second clock transition is the first data capture edge */
  251. /* USART clock polarity */
  252. #define CTL1_CPL(regval) (BIT(10) & ((uint32_t)(regval) << 10))
  253. #define USART_CPL_LOW CTL1_CPL(0) /*!< steady low value on CK pin */
  254. #define USART_CPL_HIGH CTL1_CPL(1) /*!< steady high value on CK pin */
  255. /* configure USART DMA request for reception */
  256. #define CTL2_DENR(regval) (BIT(6) & ((uint32_t)(regval) << 6))
  257. #define USART_RECEIVE_DMA_ENABLE CTL2_DENR(1) /*!< enable DAM request for reception */
  258. #define USART_RECEIVE_DMA_DISABLE CTL2_DENR(0) /*!< disable DAM request for reception */
  259. /* configure USART DMA request for transmission */
  260. #define CTL2_DENT(regval) (BIT(7) & ((uint32_t)(regval) << 7))
  261. #define USART_TRANSMIT_DMA_ENABLE CTL2_DENT(1) /*!< enable DAM request for transmission */
  262. #define USART_TRANSMIT_DMA_DISABLE CTL2_DENT(0) /*!< disable DAM request for transmission */
  263. /* configure USART RTS */
  264. #define CLT2_RTSEN(regval) (BIT(8) & ((uint32_t)(regval) << 8))
  265. #define USART_RTS_ENABLE CLT2_RTSEN(1) /*!< enable RTS */
  266. #define USART_RTS_DISABLE CLT2_RTSEN(0) /*!< disable RTS */
  267. /* configure USART CTS */
  268. #define CLT2_CTSEN(regval) (BIT(9) & ((uint32_t)(regval) << 9))
  269. #define USART_CTS_ENABLE CLT2_CTSEN(1) /*!< enable CTS */
  270. #define USART_CTS_DISABLE CLT2_CTSEN(0) /*!< disable CTS */
  271. /* enable USART IrDA low-power */
  272. #define CTL2_IRLP(regval) (BIT(2) & ((uint32_t)(regval) << 2))
  273. #define USART_IRLP_LOW CTL2_IRLP(1) /*!< low-power */
  274. #define USART_IRLP_NORMAL CTL2_IRLP(0) /*!< normal */
  275. /* USART data is transmitted/received with the LSB/MSB first */
  276. #define CTL3_MSBF(regval) (BIT(11) & ((uint32_t)(regval) << 11))
  277. #define USART_MSBF_LSB CTL3_MSBF(0) /*!< LSB first */
  278. #define USART_MSBF_MSB CTL3_MSBF(1) /*!< MSB first */
  279. /* function declarations */
  280. /* initialization functions */
  281. /* reset USART */
  282. void usart_deinit(uint32_t usart_periph);
  283. /* configure USART baud rate value */
  284. void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval);
  285. /* configure USART parity function */
  286. void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg);
  287. /* configure USART word length */
  288. void usart_word_length_set(uint32_t usart_periph, uint32_t wlen);
  289. /* configure USART stop bit length */
  290. void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen);
  291. /* enable USART */
  292. void usart_enable(uint32_t usart_periph);
  293. /* disable USART */
  294. void usart_disable(uint32_t usart_periph);
  295. /* configure USART transmitter */
  296. void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig);
  297. /* configure USART receiver */
  298. void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig);
  299. /* USART normal mode communication */
  300. /* data is transmitted/received with the LSB/MSB first */
  301. void usart_data_first_config(uint32_t usart_periph, uint32_t msbf);
  302. /* configure USART inverted */
  303. void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara);
  304. /* enable receiver timeout */
  305. void usart_receiver_timeout_enable(uint32_t usart_periph);
  306. /* disable receiver timeout */
  307. void usart_receiver_timeout_disable(uint32_t usart_periph);
  308. /* configure receiver timeout threshold */
  309. void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout);
  310. /* USART transmit data function */
  311. void usart_data_transmit(uint32_t usart_periph, uint16_t data);
  312. /* USART receive data function */
  313. uint16_t usart_data_receive(uint32_t usart_periph);
  314. /* multi-processor communication */
  315. /* configure address of the USART */
  316. void usart_address_config(uint32_t usart_periph, uint8_t addr);
  317. /* enable mute mode */
  318. void usart_mute_mode_enable(uint32_t usart_periph);
  319. /* disable mute mode */
  320. void usart_mute_mode_disable(uint32_t usart_periph);
  321. /* configure wakeup method in mute mode */
  322. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod);
  323. /* LIN mode communication */
  324. /* enable LIN mode */
  325. void usart_lin_mode_enable(uint32_t usart_periph);
  326. /* disable LIN mode */
  327. void usart_lin_mode_disable(uint32_t usart_periph);
  328. /* LIN break detection length */
  329. void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen);
  330. /* send break frame */
  331. void usart_send_break(uint32_t usart_periph);
  332. /* half-duplex communication */
  333. /* enable half-duplex mode */
  334. void usart_halfduplex_enable(uint32_t usart_periph);
  335. /* disable half-duplex mode */
  336. void usart_halfduplex_disable(uint32_t usart_periph);
  337. /* synchronous communication */
  338. /* enable CK pin in synchronous mode */
  339. void usart_synchronous_clock_enable(uint32_t usart_periph);
  340. /* disable CK pin in synchronous mode */
  341. void usart_synchronous_clock_disable(uint32_t usart_periph);
  342. /* configure USART synchronous mode parameters */
  343. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl);
  344. /* smartcard communication */
  345. /* configure guard time value in smartcard mode */
  346. void usart_guard_time_config(uint32_t usart_periph,uint8_t guat);
  347. /* enable smartcard mode */
  348. void usart_smartcard_mode_enable(uint32_t usart_periph);
  349. /* disable smartcard mode */
  350. void usart_smartcard_mode_disable(uint32_t usart_periph);
  351. /* enable NACK in smartcard mode */
  352. void usart_smartcard_mode_nack_enable(uint32_t usart_periph);
  353. /* disable NACK in smartcard mode */
  354. void usart_smartcard_mode_nack_disable(uint32_t usart_periph);
  355. /* configure smartcard auto-retry number */
  356. void usart_smartcard_autoretry_config(uint32_t usart_periph, uint8_t scrtnum);
  357. /* configure block length */
  358. void usart_block_length_config(uint32_t usart_periph, uint8_t bl);
  359. /* IrDA communication */
  360. /* enable IrDA mode */
  361. void usart_irda_mode_enable(uint32_t usart_periph);
  362. /* disable IrDA mode */
  363. void usart_irda_mode_disable(uint32_t usart_periph);
  364. /* configure the peripheral clock prescaler */
  365. void usart_prescaler_config(uint32_t usart_periph, uint8_t psc);
  366. /* configure IrDA low-power */
  367. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp);
  368. /* hardware flow communication */
  369. /* configure hardware flow control RTS */
  370. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig);
  371. /* configure hardware flow control CTS */
  372. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig);
  373. /* DMA communication */
  374. /* configure USART DMA reception */
  375. void usart_dma_receive_config(uint32_t usart_periph, uint8_t dmacmd);
  376. /* configure USART DMA transmission */
  377. void usart_dma_transmit_config(uint32_t usart_periph, uint8_t dmacmd);
  378. /* flag & interrupt functions */
  379. /* get flag in STAT0/STAT1 register */
  380. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag);
  381. /* clear flag in STAT0/STAT1 register */
  382. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag);
  383. /* enable USART interrupt */
  384. void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt);
  385. /* disable USART interrupt */
  386. void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt);
  387. /* get USART interrupt and flag status */
  388. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag);
  389. /* clear interrupt flag in STAT0/STAT1 register */
  390. void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum int_flag);
  391. #endif /* GD32F30x_USART_H */