at32f413_usart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. **************************************************************************
  3. * @file at32f413_usart.h
  4. * @version v2.0.5
  5. * @date 2022-05-20
  6. * @brief at32f413 usart header file
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. /* define to prevent recursive inclusion -------------------------------------*/
  27. #ifndef __AT32F413_USART_H
  28. #define __AT32F413_USART_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* includes ------------------------------------------------------------------*/
  33. #include "at32f413.h"
  34. /** @addtogroup AT32F413_periph_driver
  35. * @{
  36. */
  37. /** @addtogroup USART
  38. * @{
  39. */
  40. /** @defgroup USART_flags_definition
  41. * @brief usart flag
  42. * @{
  43. */
  44. #define USART_PERR_FLAG ((uint32_t)0x00000001) /*!< usart parity error flag */
  45. #define USART_FERR_FLAG ((uint32_t)0x00000002) /*!< usart framing error flag */
  46. #define USART_NERR_FLAG ((uint32_t)0x00000004) /*!< usart noise error flag */
  47. #define USART_ROERR_FLAG ((uint32_t)0x00000008) /*!< usart receiver overflow error flag */
  48. #define USART_IDLEF_FLAG ((uint32_t)0x00000010) /*!< usart idle flag */
  49. #define USART_RDBF_FLAG ((uint32_t)0x00000020) /*!< usart receive data buffer full flag */
  50. #define USART_TDC_FLAG ((uint32_t)0x00000040) /*!< usart transmit data complete flag */
  51. #define USART_TDBE_FLAG ((uint32_t)0x00000080) /*!< usart transmit data buffer empty flag */
  52. #define USART_BFF_FLAG ((uint32_t)0x00000100) /*!< usart break frame flag */
  53. #define USART_CTSCF_FLAG ((uint32_t)0x00000200) /*!< usart cts change flag */
  54. /**
  55. * @}
  56. */
  57. /** @defgroup USART_interrupts_definition
  58. * @brief usart interrupt
  59. * @{
  60. */
  61. #define USART_IDLE_INT MAKE_VALUE(0x0C,0x04) /*!< usart idle interrupt */
  62. #define USART_RDBF_INT MAKE_VALUE(0x0C,0x05) /*!< usart receive data buffer full interrupt */
  63. #define USART_TDC_INT MAKE_VALUE(0x0C,0x06) /*!< usart transmit data complete interrupt */
  64. #define USART_TDBE_INT MAKE_VALUE(0x0C,0x07) /*!< usart transmit data buffer empty interrupt */
  65. #define USART_PERR_INT MAKE_VALUE(0x0C,0x08) /*!< usart parity error interrupt */
  66. #define USART_BF_INT MAKE_VALUE(0x10,0x06) /*!< usart break frame interrupt */
  67. #define USART_ERR_INT MAKE_VALUE(0x14,0x00) /*!< usart error interrupt */
  68. #define USART_CTSCF_INT MAKE_VALUE(0x14,0x0A) /*!< usart cts change interrupt */
  69. /**
  70. * @}
  71. */
  72. /** @defgroup USART_exported_types
  73. * @{
  74. */
  75. /**
  76. * @brief usart parity selection type
  77. */
  78. typedef enum
  79. {
  80. USART_PARITY_NONE = 0x00, /*!< usart no parity */
  81. USART_PARITY_EVEN = 0x01, /*!< usart even parity */
  82. USART_PARITY_ODD = 0x02 /*!< usart odd parity */
  83. } usart_parity_selection_type;
  84. /**
  85. * @brief usart wakeup mode type
  86. */
  87. typedef enum
  88. {
  89. USART_WAKEUP_BY_IDLE_FRAME = 0x00, /*!< usart wakeup by idle frame */
  90. USART_WAKEUP_BY_MATCHING_ID = 0x01 /*!< usart wakeup by matching id */
  91. } usart_wakeup_mode_type;
  92. /**
  93. * @brief usart data bit num type
  94. */
  95. typedef enum
  96. {
  97. USART_DATA_8BITS = 0x00, /*!< usart data size is 8 bits */
  98. USART_DATA_9BITS = 0x01 /*!< usart data size is 9 bits */
  99. } usart_data_bit_num_type;
  100. /**
  101. * @brief usart break frame bit num type
  102. */
  103. typedef enum
  104. {
  105. USART_BREAK_10BITS = 0x00, /*!< usart lin mode berak frame detection 10 bits */
  106. USART_BREAK_11BITS = 0x01 /*!< usart lin mode berak frame detection 11 bits */
  107. } usart_break_bit_num_type;
  108. /**
  109. * @brief usart phase of the clock type
  110. */
  111. typedef enum
  112. {
  113. USART_CLOCK_PHASE_1EDGE = 0x00, /*!< usart data capture is done on the clock leading edge */
  114. USART_CLOCK_PHASE_2EDGE = 0x01 /*!< usart data capture is done on the clock trailing edge */
  115. } usart_clock_phase_type;
  116. /**
  117. * @brief usart polarity of the clock type
  118. */
  119. typedef enum
  120. {
  121. USART_CLOCK_POLARITY_LOW = 0x00, /*!< usart clock stay low level outside transmission window */
  122. USART_CLOCK_POLARITY_HIGH = 0x01 /*!< usart clock stay high level outside transmission window */
  123. } usart_clock_polarity_type;
  124. /**
  125. * @brief usart last bit clock pulse type
  126. */
  127. typedef enum
  128. {
  129. USART_CLOCK_LAST_BIT_NONE = 0x00, /*!< usart clock pulse of the last data bit is not outputted */
  130. USART_CLOCK_LAST_BIT_OUTPUT = 0x01 /*!< usart clock pulse of the last data bit is outputted */
  131. } usart_lbcp_type;
  132. /**
  133. * @brief usart stop bit num type
  134. */
  135. typedef enum
  136. {
  137. USART_STOP_1_BIT = 0x00, /*!< usart stop bits num is 1 */
  138. USART_STOP_0_5_BIT = 0x01, /*!< usart stop bits num is 0.5 */
  139. USART_STOP_2_BIT = 0x02, /*!< usart stop bits num is 2 */
  140. USART_STOP_1_5_BIT = 0x03 /*!< usart stop bits num is 1.5 */
  141. } usart_stop_bit_num_type;
  142. /**
  143. * @brief usart hardware flow control type
  144. */
  145. typedef enum
  146. {
  147. USART_HARDWARE_FLOW_NONE = 0x00, /*!< usart without hardware flow */
  148. USART_HARDWARE_FLOW_RTS = 0x01, /*!< usart hardware flow only rts */
  149. USART_HARDWARE_FLOW_CTS = 0x02, /*!< usart hardware flow only cts */
  150. USART_HARDWARE_FLOW_RTS_CTS = 0x03 /*!< usart hardware flow both rts and cts */
  151. } usart_hardware_flow_control_type;
  152. /**
  153. * @brief type define usart register all
  154. */
  155. typedef struct
  156. {
  157. /**
  158. * @brief usart sts register, offset:0x00
  159. */
  160. union
  161. {
  162. __IO uint32_t sts;
  163. struct
  164. {
  165. __IO uint32_t perr : 1; /* [0] */
  166. __IO uint32_t ferr : 1; /* [1] */
  167. __IO uint32_t nerr : 1; /* [2] */
  168. __IO uint32_t roerr : 1; /* [3] */
  169. __IO uint32_t idlef : 1; /* [4] */
  170. __IO uint32_t rdbf : 1; /* [5] */
  171. __IO uint32_t tdc : 1; /* [6] */
  172. __IO uint32_t tdbe : 1; /* [7] */
  173. __IO uint32_t bff : 1; /* [8] */
  174. __IO uint32_t ctscf : 1; /* [9] */
  175. __IO uint32_t reserved1 : 22;/* [31:10] */
  176. } sts_bit;
  177. };
  178. /**
  179. * @brief usart dt register, offset:0x04
  180. */
  181. union
  182. {
  183. __IO uint32_t dt;
  184. struct
  185. {
  186. __IO uint32_t dt : 9; /* [8:0] */
  187. __IO uint32_t reserved1 : 23;/* [31:9] */
  188. } dt_bit;
  189. };
  190. /**
  191. * @brief usart baudr register, offset:0x08
  192. */
  193. union
  194. {
  195. __IO uint32_t baudr;
  196. struct
  197. {
  198. __IO uint32_t div : 16;/* [15:0] */
  199. __IO uint32_t reserved1 : 16;/* [31:16] */
  200. } baudr_bit;
  201. };
  202. /**
  203. * @brief usart ctrl1 register, offset:0x0C
  204. */
  205. union
  206. {
  207. __IO uint32_t ctrl1;
  208. struct
  209. {
  210. __IO uint32_t sbf : 1; /* [0] */
  211. __IO uint32_t rm : 1; /* [1] */
  212. __IO uint32_t ren : 1; /* [2] */
  213. __IO uint32_t ten : 1; /* [3] */
  214. __IO uint32_t idleien : 1; /* [4] */
  215. __IO uint32_t rdbfien : 1; /* [5] */
  216. __IO uint32_t tdcien : 1; /* [6] */
  217. __IO uint32_t tdbeien : 1; /* [7] */
  218. __IO uint32_t perrien : 1; /* [8] */
  219. __IO uint32_t psel : 1; /* [9] */
  220. __IO uint32_t pen : 1; /* [10] */
  221. __IO uint32_t wum : 1; /* [11] */
  222. __IO uint32_t dbn : 1; /* [12] */
  223. __IO uint32_t uen : 1; /* [13] */
  224. __IO uint32_t reserved1 : 18;/* [31:14] */
  225. } ctrl1_bit;
  226. };
  227. /**
  228. * @brief usart ctrl2 register, offset:0x10
  229. */
  230. union
  231. {
  232. __IO uint32_t ctrl2;
  233. struct
  234. {
  235. __IO uint32_t id : 4; /* [3:0] */
  236. __IO uint32_t reserved1 : 1; /* [4] */
  237. __IO uint32_t bfbn : 1; /* [5] */
  238. __IO uint32_t bfien : 1; /* [6] */
  239. __IO uint32_t reserved2 : 1; /* [7] */
  240. __IO uint32_t lbcp : 1; /* [8] */
  241. __IO uint32_t clkpha : 1; /* [9] */
  242. __IO uint32_t clkpol : 1; /* [10] */
  243. __IO uint32_t clken : 1; /* [11] */
  244. __IO uint32_t stopbn : 2; /* [13:12] */
  245. __IO uint32_t linen : 1; /* [14] */
  246. __IO uint32_t reserved3 : 17;/* [31:15] */
  247. } ctrl2_bit;
  248. };
  249. /**
  250. * @brief usart ctrl3 register, offset:0x14
  251. */
  252. union
  253. {
  254. __IO uint32_t ctrl3;
  255. struct
  256. {
  257. __IO uint32_t errien : 1; /* [0] */
  258. __IO uint32_t irdaen : 1; /* [1] */
  259. __IO uint32_t irdalp : 1; /* [2] */
  260. __IO uint32_t slben : 1; /* [3] */
  261. __IO uint32_t scnacken : 1; /* [4] */
  262. __IO uint32_t scmen : 1; /* [5] */
  263. __IO uint32_t dmaren : 1; /* [6] */
  264. __IO uint32_t dmaten : 1; /* [7] */
  265. __IO uint32_t rtsen : 1; /* [8] */
  266. __IO uint32_t ctsen : 1; /* [9] */
  267. __IO uint32_t ctscfien : 1; /* [10] */
  268. __IO uint32_t reserved1 : 21;/* [31:11] */
  269. } ctrl3_bit;
  270. };
  271. /**
  272. * @brief usart gdiv register, offset:0x18
  273. */
  274. union
  275. {
  276. __IO uint32_t gdiv;
  277. struct
  278. {
  279. __IO uint32_t isdiv : 8; /* [7:0] */
  280. __IO uint32_t scgt : 8; /* [15:8] */
  281. __IO uint32_t reserved1 : 16;/* [31:16] */
  282. } gdiv_bit;
  283. };
  284. } usart_type;
  285. /**
  286. * @}
  287. */
  288. #define USART1 ((usart_type *) USART1_BASE)
  289. #define USART2 ((usart_type *) USART2_BASE)
  290. #define USART3 ((usart_type *) USART3_BASE)
  291. #define UART4 ((usart_type *) UART4_BASE)
  292. #define UART5 ((usart_type *) UART5_BASE)
  293. /** @defgroup USART_exported_functions
  294. * @{
  295. */
  296. void usart_reset(usart_type* usart_x);
  297. void usart_init(usart_type* usart_x, uint32_t baud_rate, usart_data_bit_num_type data_bit, usart_stop_bit_num_type stop_bit);
  298. void usart_parity_selection_config(usart_type* usart_x, usart_parity_selection_type parity);
  299. void usart_enable(usart_type* usart_x, confirm_state new_state);
  300. void usart_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  301. void usart_receiver_enable(usart_type* usart_x, confirm_state new_state);
  302. void usart_clock_config(usart_type* usart_x, usart_clock_polarity_type clk_pol, usart_clock_phase_type clk_pha, usart_lbcp_type clk_lb);
  303. void usart_clock_enable(usart_type* usart_x, confirm_state new_state);
  304. void usart_interrupt_enable(usart_type* usart_x, uint32_t usart_int, confirm_state new_state);
  305. void usart_dma_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  306. void usart_dma_receiver_enable(usart_type* usart_x, confirm_state new_state);
  307. void usart_wakeup_id_set(usart_type* usart_x, uint8_t usart_id);
  308. void usart_wakeup_mode_set(usart_type* usart_x, usart_wakeup_mode_type wakeup_mode);
  309. void usart_receiver_mute_enable(usart_type* usart_x, confirm_state new_state);
  310. void usart_break_bit_num_set(usart_type* usart_x, usart_break_bit_num_type break_bit);
  311. void usart_lin_mode_enable(usart_type* usart_x, confirm_state new_state);
  312. void usart_data_transmit(usart_type* usart_x, uint16_t data);
  313. uint16_t usart_data_receive(usart_type* usart_x);
  314. void usart_break_send(usart_type* usart_x);
  315. void usart_smartcard_guard_time_set(usart_type* usart_x, uint8_t guard_time_val);
  316. void usart_irda_smartcard_division_set(usart_type* usart_x, uint8_t div_val);
  317. void usart_smartcard_mode_enable(usart_type* usart_x, confirm_state new_state);
  318. void usart_smartcard_nack_set(usart_type* usart_x, confirm_state new_state);
  319. void usart_single_line_halfduplex_select(usart_type* usart_x, confirm_state new_state);
  320. void usart_irda_mode_enable(usart_type* usart_x, confirm_state new_state);
  321. void usart_irda_low_power_enable(usart_type* usart_x, confirm_state new_state);
  322. void usart_hardware_flow_control_set(usart_type* usart_x,usart_hardware_flow_control_type flow_state);
  323. flag_status usart_flag_get(usart_type* usart_x, uint32_t flag);
  324. void usart_flag_clear(usart_type* usart_x, uint32_t flag);
  325. /**
  326. * @}
  327. */
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */
  334. #ifdef __cplusplus
  335. }
  336. #endif
  337. #endif