at32f425_usart.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**
  2. **************************************************************************
  3. * @file at32f425_usart.h
  4. * @brief at32f425 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 __AT32F425_USART_H
  26. #define __AT32F425_USART_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* includes ------------------------------------------------------------------*/
  31. #include "at32f425.h"
  32. /** @addtogroup AT32F425_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_7BITS = 0x00, /*!< usart data size is 7 bits */
  96. USART_DATA_8BITS = 0x01, /*!< usart data size is 8 bits */
  97. USART_DATA_9BITS = 0x02 /*!< usart data size is 9 bits */
  98. } usart_data_bit_num_type;
  99. /**
  100. * @brief usart break frame bit num type
  101. */
  102. typedef enum
  103. {
  104. USART_BREAK_10BITS = 0x00, /*!< usart lin mode berak frame detection 10 bits */
  105. USART_BREAK_11BITS = 0x01 /*!< usart lin mode berak frame detection 11 bits */
  106. } usart_break_bit_num_type;
  107. /**
  108. * @brief usart phase of the clock type
  109. */
  110. typedef enum
  111. {
  112. USART_CLOCK_PHASE_1EDGE = 0x00, /*!< usart data capture is done on the clock leading edge */
  113. USART_CLOCK_PHASE_2EDGE = 0x01 /*!< usart data capture is done on the clock trailing edge */
  114. } usart_clock_phase_type;
  115. /**
  116. * @brief usart polarity of the clock type
  117. */
  118. typedef enum
  119. {
  120. USART_CLOCK_POLARITY_LOW = 0x00, /*!< usart clock stay low level outside transmission window */
  121. USART_CLOCK_POLARITY_HIGH = 0x01 /*!< usart clock stay high level outside transmission window */
  122. } usart_clock_polarity_type;
  123. /**
  124. * @brief usart last bit clock pulse type
  125. */
  126. typedef enum
  127. {
  128. USART_CLOCK_LAST_BIT_NONE = 0x00, /*!< usart clock pulse of the last data bit is not outputted */
  129. USART_CLOCK_LAST_BIT_OUTPUT = 0x01 /*!< usart clock pulse of the last data bit is outputted */
  130. } usart_lbcp_type;
  131. /**
  132. * @brief usart stop bit num type
  133. */
  134. typedef enum
  135. {
  136. USART_STOP_1_BIT = 0x00, /*!< usart stop bits num is 1 */
  137. USART_STOP_0_5_BIT = 0x01, /*!< usart stop bits num is 0.5 */
  138. USART_STOP_2_BIT = 0x02, /*!< usart stop bits num is 2 */
  139. USART_STOP_1_5_BIT = 0x03 /*!< usart stop bits num is 1.5 */
  140. } usart_stop_bit_num_type;
  141. /**
  142. * @brief usart hardware flow control type
  143. */
  144. typedef enum
  145. {
  146. USART_HARDWARE_FLOW_NONE = 0x00, /*!< usart without hardware flow */
  147. USART_HARDWARE_FLOW_RTS = 0x01, /*!< usart hardware flow only rts */
  148. USART_HARDWARE_FLOW_CTS = 0x02, /*!< usart hardware flow only cts */
  149. USART_HARDWARE_FLOW_RTS_CTS = 0x03 /*!< usart hardware flow both rts and cts */
  150. } usart_hardware_flow_control_type;
  151. /**
  152. * @brief usart identification bit num type
  153. */
  154. typedef enum
  155. {
  156. USART_ID_FIXED_4_BIT = 0x00, /*!< usart id bit num fixed 4 bits */
  157. USART_ID_RELATED_DATA_BIT = 0x01 /*!< usart id bit num related data bits */
  158. } usart_identification_bit_num_type;
  159. /**
  160. * @brief usart de polarity type
  161. */
  162. typedef enum
  163. {
  164. USART_DE_POLARITY_HIGH = 0x00, /*!< usart de polarity high */
  165. USART_DE_POLARITY_LOW = 0x01 /*!< usart de polarity low */
  166. } usart_de_polarity_type;
  167. /**
  168. * @brief type define usart register all
  169. */
  170. typedef struct
  171. {
  172. /**
  173. * @brief usart sts register, offset:0x00
  174. */
  175. union
  176. {
  177. __IO uint32_t sts;
  178. struct
  179. {
  180. __IO uint32_t perr : 1; /* [0] */
  181. __IO uint32_t ferr : 1; /* [1] */
  182. __IO uint32_t nerr : 1; /* [2] */
  183. __IO uint32_t roerr : 1; /* [3] */
  184. __IO uint32_t idlef : 1; /* [4] */
  185. __IO uint32_t rdbf : 1; /* [5] */
  186. __IO uint32_t tdc : 1; /* [6] */
  187. __IO uint32_t tdbe : 1; /* [7] */
  188. __IO uint32_t bff : 1; /* [8] */
  189. __IO uint32_t ctscf : 1; /* [9] */
  190. __IO uint32_t reserved1 : 22;/* [31:10] */
  191. } sts_bit;
  192. };
  193. /**
  194. * @brief usart dt register, offset:0x04
  195. */
  196. union
  197. {
  198. __IO uint32_t dt;
  199. struct
  200. {
  201. __IO uint32_t dt : 9; /* [8:0] */
  202. __IO uint32_t reserved1 : 23;/* [31:9] */
  203. } dt_bit;
  204. };
  205. /**
  206. * @brief usart baudr register, offset:0x08
  207. */
  208. union
  209. {
  210. __IO uint32_t baudr;
  211. struct
  212. {
  213. __IO uint32_t div : 16;/* [15:0] */
  214. __IO uint32_t reserved1 : 16;/* [31:16] */
  215. } baudr_bit;
  216. };
  217. /**
  218. * @brief usart ctrl1 register, offset:0x0C
  219. */
  220. union
  221. {
  222. __IO uint32_t ctrl1;
  223. struct
  224. {
  225. __IO uint32_t sbf : 1; /* [0] */
  226. __IO uint32_t rm : 1; /* [1] */
  227. __IO uint32_t ren : 1; /* [2] */
  228. __IO uint32_t ten : 1; /* [3] */
  229. __IO uint32_t idleien : 1; /* [4] */
  230. __IO uint32_t rdbfien : 1; /* [5] */
  231. __IO uint32_t tdcien : 1; /* [6] */
  232. __IO uint32_t tdbeien : 1; /* [7] */
  233. __IO uint32_t perrien : 1; /* [8] */
  234. __IO uint32_t psel : 1; /* [9] */
  235. __IO uint32_t pen : 1; /* [10] */
  236. __IO uint32_t wum : 1; /* [11] */
  237. __IO uint32_t dbn_l : 1; /* [12] */
  238. __IO uint32_t uen : 1; /* [13] */
  239. __IO uint32_t reserved1 : 2; /* [15:14] */
  240. __IO uint32_t tcdt : 5; /* [20:16] */
  241. __IO uint32_t tsdt : 5; /* [25:21] */
  242. __IO uint32_t reserved2 : 2; /* [27:26] */
  243. __IO uint32_t dbn_h : 1; /* [28] */
  244. __IO uint32_t reserved3 : 3; /* [31:29] */
  245. } ctrl1_bit;
  246. };
  247. /**
  248. * @brief usart ctrl2 register, offset:0x10
  249. */
  250. union
  251. {
  252. __IO uint32_t ctrl2;
  253. struct
  254. {
  255. __IO uint32_t id_l : 4; /* [3:0] */
  256. __IO uint32_t idbn : 1; /* [4] */
  257. __IO uint32_t bfbn : 1; /* [5] */
  258. __IO uint32_t bfien : 1; /* [6] */
  259. __IO uint32_t reserved1 : 1; /* [7] */
  260. __IO uint32_t lbcp : 1; /* [8] */
  261. __IO uint32_t clkpha : 1; /* [9] */
  262. __IO uint32_t clkpol : 1; /* [10] */
  263. __IO uint32_t clken : 1; /* [11] */
  264. __IO uint32_t stopbn : 2; /* [13:12] */
  265. __IO uint32_t linen : 1; /* [14] */
  266. __IO uint32_t trpswap : 1; /* [15] */
  267. __IO uint32_t reserved2 : 12;/* [27:16] */
  268. __IO uint32_t id_h : 4; /* [31:28] */
  269. } ctrl2_bit;
  270. };
  271. /**
  272. * @brief usart ctrl3 register, offset:0x14
  273. */
  274. union
  275. {
  276. __IO uint32_t ctrl3;
  277. struct
  278. {
  279. __IO uint32_t errien : 1; /* [0] */
  280. __IO uint32_t irdaen : 1; /* [1] */
  281. __IO uint32_t irdalp : 1; /* [2] */
  282. __IO uint32_t slben : 1; /* [3] */
  283. __IO uint32_t scnacken : 1; /* [4] */
  284. __IO uint32_t scmen : 1; /* [5] */
  285. __IO uint32_t dmaren : 1; /* [6] */
  286. __IO uint32_t dmaten : 1; /* [7] */
  287. __IO uint32_t rtsen : 1; /* [8] */
  288. __IO uint32_t ctsen : 1; /* [9] */
  289. __IO uint32_t ctscfien : 1; /* [10] */
  290. __IO uint32_t reserved1 : 3; /* [13:11] */
  291. __IO uint32_t rs485en : 1; /* [14] */
  292. __IO uint32_t dep : 1; /* [15] */
  293. __IO uint32_t reserved2 : 16;/* [31:16] */
  294. } ctrl3_bit;
  295. };
  296. /**
  297. * @brief usart gdiv register, offset:0x18
  298. */
  299. union
  300. {
  301. __IO uint32_t gdiv;
  302. struct
  303. {
  304. __IO uint32_t isdiv : 8; /* [7:0] */
  305. __IO uint32_t scgt : 8; /* [15:8] */
  306. __IO uint32_t reserved1 : 16;/* [31:16] */
  307. } gdiv_bit;
  308. };
  309. } usart_type;
  310. /**
  311. * @}
  312. */
  313. #define USART1 ((usart_type *) USART1_BASE)
  314. #define USART2 ((usart_type *) USART2_BASE)
  315. #define USART3 ((usart_type *) USART3_BASE)
  316. #define USART4 ((usart_type *) USART4_BASE)
  317. /** @defgroup USART_exported_functions
  318. * @{
  319. */
  320. void usart_reset(usart_type* usart_x);
  321. 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);
  322. void usart_parity_selection_config(usart_type* usart_x, usart_parity_selection_type parity);
  323. void usart_enable(usart_type* usart_x, confirm_state new_state);
  324. void usart_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  325. void usart_receiver_enable(usart_type* usart_x, confirm_state new_state);
  326. 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);
  327. void usart_clock_enable(usart_type* usart_x, confirm_state new_state);
  328. void usart_interrupt_enable(usart_type* usart_x, uint32_t usart_int, confirm_state new_state);
  329. void usart_dma_transmitter_enable(usart_type* usart_x, confirm_state new_state);
  330. void usart_dma_receiver_enable(usart_type* usart_x, confirm_state new_state);
  331. void usart_wakeup_id_set(usart_type* usart_x, uint8_t usart_id);
  332. void usart_wakeup_mode_set(usart_type* usart_x, usart_wakeup_mode_type wakeup_mode);
  333. void usart_receiver_mute_enable(usart_type* usart_x, confirm_state new_state);
  334. void usart_break_bit_num_set(usart_type* usart_x, usart_break_bit_num_type break_bit);
  335. void usart_lin_mode_enable(usart_type* usart_x, confirm_state new_state);
  336. void usart_data_transmit(usart_type* usart_x, uint16_t data);
  337. uint16_t usart_data_receive(usart_type* usart_x);
  338. void usart_break_send(usart_type* usart_x);
  339. void usart_smartcard_guard_time_set(usart_type* usart_x, uint8_t guard_time_val);
  340. void usart_irda_smartcard_division_set(usart_type* usart_x, uint8_t div_val);
  341. void usart_smartcard_mode_enable(usart_type* usart_x, confirm_state new_state);
  342. void usart_smartcard_nack_set(usart_type* usart_x, confirm_state new_state);
  343. void usart_single_line_halfduplex_select(usart_type* usart_x, confirm_state new_state);
  344. void usart_irda_mode_enable(usart_type* usart_x, confirm_state new_state);
  345. void usart_irda_low_power_enable(usart_type* usart_x, confirm_state new_state);
  346. void usart_hardware_flow_control_set(usart_type* usart_x,usart_hardware_flow_control_type flow_state);
  347. flag_status usart_flag_get(usart_type* usart_x, uint32_t flag);
  348. void usart_flag_clear(usart_type* usart_x, uint32_t flag);
  349. void usart_rs485_delay_time_config(usart_type* usart_x, uint8_t start_delay_time, uint8_t complete_delay_time);
  350. void usart_transmit_receive_pin_swap(usart_type* usart_x, confirm_state new_state);
  351. void usart_id_bit_num_set(usart_type* usart_x, usart_identification_bit_num_type id_bit_num);
  352. void usart_de_polarity_set(usart_type* usart_x, usart_de_polarity_type de_polarity);
  353. void usart_rs485_mode_enable(usart_type* usart_x, confirm_state new_state);
  354. /**
  355. * @}
  356. */
  357. /**
  358. * @}
  359. */
  360. /**
  361. * @}
  362. */
  363. #ifdef __cplusplus
  364. }
  365. #endif
  366. #endif