at32f413_usart.h 14 KB

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