at32f423_usart.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. **************************************************************************
  3. * @file at32f423_usart.h
  4. * @brief at32f423 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 __AT32F423_USART_H
  26. #define __AT32F423_USART_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* includes ------------------------------------------------------------------*/
  31. #include "at32f423.h"
  32. /** @addtogroup AT32F423_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. #define USART_RTODF_FLAG ((uint32_t)0x00000800) /*!< usart receiver time out detection flag */
  53. #define USART_OCCUPY_FLAG ((uint32_t)0x00010000) /*!< usart receiver occupy flag */
  54. #define USART_CMDF_FLAG ((uint32_t)0x00020000) /*!< usart character match detection flag */
  55. #define USART_LPWUF_FLAG ((uint32_t)0x00100000) /*!< usart low power wake up flag */
  56. #define USART_TXON_FLAG ((uint32_t)0x00200000) /*!< usart transmitter turned on flag */
  57. #define USART_RXON_FLAG ((uint32_t)0x00400000) /*!< usart receiver turned on flag */
  58. /**
  59. * @}
  60. */
  61. /** @defgroup USART_interrupts_definition
  62. * @brief usart interrupt
  63. * @{
  64. */
  65. #define USART_IDLE_INT MAKE_VALUE(0x0C,0x04) /*!< usart idle interrupt */
  66. #define USART_RDBF_INT MAKE_VALUE(0x0C,0x05) /*!< usart receive data buffer full interrupt */
  67. #define USART_TDC_INT MAKE_VALUE(0x0C,0x06) /*!< usart transmit data complete interrupt */
  68. #define USART_TDBE_INT MAKE_VALUE(0x0C,0x07) /*!< usart transmit data buffer empty interrupt */
  69. #define USART_PERR_INT MAKE_VALUE(0x0C,0x08) /*!< usart parity error interrupt */
  70. #define USART_BF_INT MAKE_VALUE(0x10,0x06) /*!< usart break frame interrupt */
  71. #define USART_ERR_INT MAKE_VALUE(0x14,0x00) /*!< usart error interrupt */
  72. #define USART_CTSCF_INT MAKE_VALUE(0x14,0x0A) /*!< usart cts change interrupt */
  73. #define USART_LPWUF_INT MAKE_VALUE(0x14,0x0D) /*!< usart low power wakeup flag interrupt */
  74. #define USART_RTOD_INT MAKE_VALUE(0x0C,0x1A) /*!< usart receiver time out detection flag interrupt */
  75. #define USART_CMD_INT MAKE_VALUE(0x0C,0x0E) /*!< usart character match detection flag interrupt */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup USART_exported_types
  80. * @{
  81. */
  82. /**
  83. * @brief usart parity selection type
  84. */
  85. typedef enum
  86. {
  87. USART_PARITY_NONE = 0x00, /*!< usart no parity */
  88. USART_PARITY_EVEN = 0x01, /*!< usart even parity */
  89. USART_PARITY_ODD = 0x02 /*!< usart odd parity */
  90. } usart_parity_selection_type;
  91. /**
  92. * @brief usart wakeup mode type
  93. */
  94. typedef enum
  95. {
  96. USART_WAKEUP_BY_IDLE_FRAME = 0x00, /*!< usart wakeup by idle frame */
  97. USART_WAKEUP_BY_MATCHING_ID = 0x01 /*!< usart wakeup by matching id */
  98. } usart_wakeup_mode_type;
  99. /**
  100. * @brief usart data bit num type
  101. */
  102. typedef enum
  103. {
  104. USART_DATA_8BITS = 0x00, /*!< usart data size is 8 bits */
  105. USART_DATA_9BITS = 0x01, /*!< usart data size is 9 bits */
  106. USART_DATA_7BITS = 0x02 /*!< usart data size is 7 bits */
  107. } usart_data_bit_num_type;
  108. /**
  109. * @brief usart break frame bit num type
  110. */
  111. typedef enum
  112. {
  113. USART_BREAK_10BITS = 0x00, /*!< usart lin mode berak frame detection 10 bits */
  114. USART_BREAK_11BITS = 0x01 /*!< usart lin mode berak frame detection 11 bits */
  115. } usart_break_bit_num_type;
  116. /**
  117. * @brief usart phase of the clock type
  118. */
  119. typedef enum
  120. {
  121. USART_CLOCK_PHASE_1EDGE = 0x00, /*!< usart data capture is done on the clock leading edge */
  122. USART_CLOCK_PHASE_2EDGE = 0x01 /*!< usart data capture is done on the clock trailing edge */
  123. } usart_clock_phase_type;
  124. /**
  125. * @brief usart polarity of the clock type
  126. */
  127. typedef enum
  128. {
  129. USART_CLOCK_POLARITY_LOW = 0x00, /*!< usart clock stay low level outside transmission window */
  130. USART_CLOCK_POLARITY_HIGH = 0x01 /*!< usart clock stay high level outside transmission window */
  131. } usart_clock_polarity_type;
  132. /**
  133. * @brief usart last bit clock pulse type
  134. */
  135. typedef enum
  136. {
  137. USART_CLOCK_LAST_BIT_NONE = 0x00, /*!< usart clock pulse of the last data bit is not outputted */
  138. USART_CLOCK_LAST_BIT_OUTPUT = 0x01 /*!< usart clock pulse of the last data bit is outputted */
  139. } usart_lbcp_type;
  140. /**
  141. * @brief usart stop bit num type
  142. */
  143. typedef enum
  144. {
  145. USART_STOP_1_BIT = 0x00, /*!< usart stop bits num is 1 */
  146. USART_STOP_0_5_BIT = 0x01, /*!< usart stop bits num is 0.5 */
  147. USART_STOP_2_BIT = 0x02, /*!< usart stop bits num is 2 */
  148. USART_STOP_1_5_BIT = 0x03 /*!< usart stop bits num is 1.5 */
  149. } usart_stop_bit_num_type;
  150. /**
  151. * @brief usart hardware flow control type
  152. */
  153. typedef enum
  154. {
  155. USART_HARDWARE_FLOW_NONE = 0x00, /*!< usart without hardware flow */
  156. USART_HARDWARE_FLOW_RTS = 0x01, /*!< usart hardware flow only rts */
  157. USART_HARDWARE_FLOW_CTS = 0x02, /*!< usart hardware flow only cts */
  158. USART_HARDWARE_FLOW_RTS_CTS = 0x03 /*!< usart hardware flow both rts and cts */
  159. } usart_hardware_flow_control_type;
  160. /**
  161. * @brief usart identification bit num type
  162. */
  163. typedef enum
  164. {
  165. USART_ID_FIXED_4_BIT = 0x00, /*!< usart id bit num fixed 4 bits */
  166. USART_ID_RELATED_DATA_BIT = 0x01 /*!< usart id bit num related data bits */
  167. } usart_identification_bit_num_type;
  168. /**
  169. * @brief usart de polarity type
  170. */
  171. typedef enum
  172. {
  173. USART_DE_POLARITY_HIGH = 0x00, /*!< usart de polarity high */
  174. USART_DE_POLARITY_LOW = 0x01 /*!< usart de polarity low */
  175. } usart_de_polarity_type;
  176. /**
  177. * @brief usart wakeup method type
  178. */
  179. typedef enum
  180. {
  181. USART_WAKEUP_METHOD_ID = 0x00, /*!< usart low power wakeup method id match */
  182. USART_WAKEUP_METHOD_START = 0x02, /*!< usart low power wakeup method start bit */
  183. USART_WAKEUP_METHOD_RDBF = 0x03, /*!< usart low power wakeup method receive data buffer full */
  184. } usart_wakeup_method_type;
  185. /**
  186. * @brief type define usart register all
  187. */
  188. typedef struct
  189. {
  190. /**
  191. * @brief usart sts register, offset:0x00
  192. */
  193. union
  194. {
  195. __IO uint32_t sts;
  196. struct
  197. {
  198. __IO uint32_t perr : 1; /* [0] */
  199. __IO uint32_t ferr : 1; /* [1] */
  200. __IO uint32_t nerr : 1; /* [2] */
  201. __IO uint32_t roerr : 1; /* [3] */
  202. __IO uint32_t idlef : 1; /* [4] */
  203. __IO uint32_t rdbf : 1; /* [5] */
  204. __IO uint32_t tdc : 1; /* [6] */
  205. __IO uint32_t tdbe : 1; /* [7] */
  206. __IO uint32_t bff : 1; /* [8] */
  207. __IO uint32_t ctscf : 1; /* [9] */
  208. __IO uint32_t reserved1 : 1; /* [10] */
  209. __IO uint32_t rtodf : 1; /* [11] */
  210. __IO uint32_t reserved2 : 4; /* [12:15] */
  211. __IO uint32_t occupy : 1; /* [16] */
  212. __IO uint32_t cmdf : 1; /* [17] */
  213. __IO uint32_t reserved3 : 2; /* [18:19] */
  214. __IO uint32_t lpwuf : 1; /* [20] */
  215. __IO uint32_t txon : 1; /* [21] */
  216. __IO uint32_t rxon : 1; /* [22] */
  217. __IO uint32_t reserved4 : 9; /* [31:23] */
  218. } sts_bit;
  219. };
  220. /**
  221. * @brief usart dt register, offset:0x04
  222. */
  223. union
  224. {
  225. __IO uint32_t dt;
  226. struct
  227. {
  228. __IO uint32_t dt : 9; /* [8:0] */
  229. __IO uint32_t reserved1 : 23;/* [31:9] */
  230. } dt_bit;
  231. };
  232. /**
  233. * @brief usart baudr register, offset:0x08
  234. */
  235. union
  236. {
  237. __IO uint32_t baudr;
  238. struct
  239. {
  240. __IO uint32_t div : 16;/* [15:0] */
  241. __IO uint32_t reserved1 : 16;/* [31:16] */
  242. } baudr_bit;
  243. };
  244. /**
  245. * @brief usart ctrl1 register, offset:0x0C
  246. */
  247. union
  248. {
  249. __IO uint32_t ctrl1;
  250. struct
  251. {
  252. __IO uint32_t sbf : 1; /* [0] */
  253. __IO uint32_t rm : 1; /* [1] */
  254. __IO uint32_t ren : 1; /* [2] */
  255. __IO uint32_t ten : 1; /* [3] */
  256. __IO uint32_t idleien : 1; /* [4] */
  257. __IO uint32_t rdbfien : 1; /* [5] */
  258. __IO uint32_t tdcien : 1; /* [6] */
  259. __IO uint32_t tdbeien : 1; /* [7] */
  260. __IO uint32_t perrien : 1; /* [8] */
  261. __IO uint32_t psel : 1; /* [9] */
  262. __IO uint32_t pen : 1; /* [10] */
  263. __IO uint32_t wum : 1; /* [11] */
  264. __IO uint32_t dbn0 : 1; /* [12] */
  265. __IO uint32_t uen : 1; /* [13] */
  266. __IO uint32_t cmdie : 1; /* [14] */
  267. __IO uint32_t reserved1 : 1; /* [15] */
  268. __IO uint32_t tcdt : 5; /* [20:16] */
  269. __IO uint32_t tsdt : 5; /* [25:21] */
  270. __IO uint32_t retodie : 1; /* [26] */
  271. __IO uint32_t rtoden : 1; /* [27] */
  272. __IO uint32_t dbn1 : 1; /* [28] */
  273. __IO uint32_t reserved2 : 3; /* [31:29] */
  274. } ctrl1_bit;
  275. };
  276. /**
  277. * @brief usart ctrl2 register, offset:0x10
  278. */
  279. union
  280. {
  281. __IO uint32_t ctrl2;
  282. struct
  283. {
  284. __IO uint32_t idl : 4; /* [3:0] */
  285. __IO uint32_t idbn : 1; /* [4] */
  286. __IO uint32_t bfbn : 1; /* [5] */
  287. __IO uint32_t bfien : 1; /* [6] */
  288. __IO uint32_t reserved1 : 1; /* [7] */
  289. __IO uint32_t lbcp : 1; /* [8] */
  290. __IO uint32_t clkpha : 1; /* [9] */
  291. __IO uint32_t clkpol : 1; /* [10] */
  292. __IO uint32_t clken : 1; /* [11] */
  293. __IO uint32_t stopbn : 2; /* [13:12] */
  294. __IO uint32_t linen : 1; /* [14] */
  295. __IO uint32_t trpswap : 1; /* [15] */
  296. __IO uint32_t rxrev : 1; /* [16] */
  297. __IO uint32_t txrev : 1; /* [17] */
  298. __IO uint32_t dtrev : 1; /* [18] */
  299. __IO uint32_t mtf : 1; /* [19] */
  300. __IO uint32_t reserved2 : 8; /* [27:20] */
  301. __IO uint32_t idh : 4; /* [31:28] */
  302. } ctrl2_bit;
  303. };
  304. /**
  305. * @brief usart ctrl3 register, offset:0x14
  306. */
  307. union
  308. {
  309. __IO uint32_t ctrl3;
  310. struct
  311. {
  312. __IO uint32_t errien : 1; /* [0] */
  313. __IO uint32_t irdaen : 1; /* [1] */
  314. __IO uint32_t irdalp : 1; /* [2] */
  315. __IO uint32_t slben : 1; /* [3] */
  316. __IO uint32_t scnacken : 1; /* [4] */
  317. __IO uint32_t scmen : 1; /* [5] */
  318. __IO uint32_t dmaren : 1; /* [6] */
  319. __IO uint32_t dmaten : 1; /* [7] */
  320. __IO uint32_t rtsen : 1; /* [8] */
  321. __IO uint32_t ctsen : 1; /* [9] */
  322. __IO uint32_t ctscfien : 1; /* [10] */
  323. __IO uint32_t smusen : 1; /* [11] */
  324. __IO uint32_t reserved1 : 1; /* [12] */
  325. __IO uint32_t lpwufie : 1; /* [13] */
  326. __IO uint32_t rs485en : 1; /* [14] */
  327. __IO uint32_t dep : 1; /* [15] */
  328. __IO uint32_t lpwum : 2; /* [17:16] */
  329. __IO uint32_t reserved2 : 14;/* [31:18] */
  330. } ctrl3_bit;
  331. };
  332. /**
  333. * @brief usart gdiv register, offset:0x18
  334. */
  335. union
  336. {
  337. __IO uint32_t gdiv;
  338. struct
  339. {
  340. __IO uint32_t isdiv : 8; /* [7:0] */
  341. __IO uint32_t scgt : 8; /* [15:8] */
  342. __IO uint32_t reserved1 : 16;/* [31:16] */
  343. } gdiv_bit;
  344. };
  345. /**
  346. * @brief usart rtov register, offset:0x1C
  347. */
  348. union
  349. {
  350. __IO uint32_t rtov;
  351. struct
  352. {
  353. __IO uint32_t rtov : 24;/* [23:0] */
  354. __IO uint32_t reserved1 : 8; /* [31:24] */
  355. } rtov_bit;
  356. };
  357. /**
  358. * @brief usart ifc register, offset:0x20
  359. */
  360. union
  361. {
  362. __IO uint32_t ifc;
  363. struct
  364. {
  365. __IO uint32_t reserved1 : 11;/* [10:0] */
  366. __IO uint32_t lpwufc : 1; /* [11] */
  367. __IO uint32_t reserved2 : 5; /* [16:12] */
  368. __IO uint32_t cmdfc : 1; /* [17] */
  369. __IO uint32_t reserved3 : 2; /* [19:18] */
  370. __IO uint32_t rtodfc : 1; /* [20] */
  371. __IO uint32_t reserved4 : 11;/* [31:21] */
  372. } ifc_bit;
  373. };
  374. } usart_type;
  375. /**
  376. * @}
  377. */
  378. #define USART1 ((usart_type *) USART1_BASE)
  379. #define USART2 ((usart_type *) USART2_BASE)
  380. #define USART3 ((usart_type *) USART3_BASE)
  381. #define USART4 ((usart_type *) USART4_BASE)
  382. #define USART5 ((usart_type *) USART5_BASE)
  383. #define USART6 ((usart_type *) USART6_BASE)
  384. #define USART7 ((usart_type *) USART7_BASE)
  385. #if defined (AT32F423Kx) || defined (AT32F423Tx) || defined (AT32F423Cx) || \
  386. defined (AT32F423Rx) || defined (AT32F423Vx)
  387. #define USART8 ((usart_type *) USART8_BASE)
  388. #endif
  389. /** @defgroup USART_exported_functions
  390. * @{
  391. */
  392. void usart_reset(usart_type* usart_x);
  393. 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);
  394. void usart_parity_selection_config(usart_type* usart_x, usart_parity_selection_type parity);
  395. void usart_enable(usart_type* usart_x, confirm_state new_state);
  396. void usart_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  397. void usart_receiver_enable(usart_type* usart_x, confirm_state new_state);
  398. 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);
  399. void usart_clock_enable(usart_type* usart_x, confirm_state new_state);
  400. void usart_interrupt_enable(usart_type* usart_x, uint32_t usart_int, confirm_state new_state);
  401. void usart_dma_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  402. void usart_dma_receiver_enable(usart_type* usart_x, confirm_state new_state);
  403. void usart_wakeup_id_set(usart_type* usart_x, uint8_t usart_id);
  404. void usart_wakeup_mode_set(usart_type* usart_x, usart_wakeup_mode_type wakeup_mode);
  405. void usart_receiver_mute_enable(usart_type* usart_x, confirm_state new_state);
  406. void usart_break_bit_num_set(usart_type* usart_x, usart_break_bit_num_type break_bit);
  407. void usart_lin_mode_enable(usart_type* usart_x, confirm_state new_state);
  408. void usart_data_transmit(usart_type* usart_x, uint16_t data);
  409. uint16_t usart_data_receive(usart_type* usart_x);
  410. void usart_break_send(usart_type* usart_x);
  411. void usart_smartcard_guard_time_set(usart_type* usart_x, uint8_t guard_time_val);
  412. void usart_irda_smartcard_division_set(usart_type* usart_x, uint8_t div_val);
  413. void usart_smartcard_mode_enable(usart_type* usart_x, confirm_state new_state);
  414. void usart_smartcard_nack_set(usart_type* usart_x, confirm_state new_state);
  415. void usart_single_line_halfduplex_select(usart_type* usart_x, confirm_state new_state);
  416. void usart_irda_mode_enable(usart_type* usart_x, confirm_state new_state);
  417. void usart_irda_low_power_enable(usart_type* usart_x, confirm_state new_state);
  418. void usart_hardware_flow_control_set(usart_type* usart_x,usart_hardware_flow_control_type flow_state);
  419. flag_status usart_flag_get(usart_type* usart_x, uint32_t flag);
  420. void usart_flag_clear(usart_type* usart_x, uint32_t flag);
  421. void usart_rs485_delay_time_config(usart_type* usart_x, uint8_t start_delay_time, uint8_t complete_delay_time);
  422. void usart_transmit_receive_pin_swap(usart_type* usart_x, confirm_state new_state);
  423. void usart_id_bit_num_set(usart_type* usart_x, usart_identification_bit_num_type id_bit_num);
  424. void usart_de_polarity_set(usart_type* usart_x, usart_de_polarity_type de_polarity);
  425. void usart_rs485_mode_enable(usart_type* usart_x, confirm_state new_state);
  426. void usart_low_power_wakeup_set(usart_type* usart_x, usart_wakeup_method_type wakeup_method);
  427. void usart_deep_sleep_mode_enable(usart_type* usart_x, confirm_state new_state);
  428. void usart_msb_transmit_first_enable(usart_type* usart_x, confirm_state new_state);
  429. void usart_dt_polarity_reverse(usart_type* usart_x, confirm_state new_state);
  430. void usart_transmit_pin_polarity_reverse(usart_type* usart_x, confirm_state new_state);
  431. void usart_receive_pin_polarity_reverse(usart_type* usart_x, confirm_state new_state);
  432. void usart_receiver_timeout_detection_enable(usart_type* usart_x, confirm_state new_state);
  433. void usart_receiver_timeout_value_set(usart_type* usart_x, uint32_t time);
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. /**
  441. * @}
  442. */
  443. #ifdef __cplusplus
  444. }
  445. #endif
  446. #endif