hpm_uart_drv.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Copyright (c) 2021-2022-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_UART_DRV_H
  8. #define HPM_UART_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_uart_regs.h"
  11. #include "hpm_soc_feature.h"
  12. /**
  13. *
  14. * @brief UART driver APIs
  15. * @defgroup uart_interface UART driver APIs
  16. * @ingroup io_interfaces
  17. * @{
  18. */
  19. /**
  20. * @brief UART status
  21. */
  22. enum {
  23. status_uart_no_suitable_baudrate_parameter_found = MAKE_STATUS(status_group_uart, 1),
  24. };
  25. /* @brief Parity */
  26. typedef enum parity {
  27. parity_none = 0,
  28. parity_odd,
  29. parity_even,
  30. parity_always_1,
  31. parity_always_0,
  32. } parity_setting_t;
  33. /* @brief Stop bits */
  34. typedef enum num_of_stop_bits {
  35. stop_bits_1 = 0,
  36. stop_bits_1_5,
  37. stop_bits_2,
  38. } num_of_stop_bits_t;
  39. /* @brief Word length */
  40. typedef enum word_length {
  41. word_length_5_bits = 0,
  42. word_length_6_bits,
  43. word_length_7_bits,
  44. word_length_8_bits,
  45. } word_length_t;
  46. /* @brief UART fifo trigger levels */
  47. typedef enum uart_fifo_trg_lvl {
  48. #if defined(HPM_IP_FEATURE_UART_FINE_FIFO_THRLD) && (HPM_IP_FEATURE_UART_FINE_FIFO_THRLD == 1)
  49. uart_fifo_1_byte = 0,
  50. uart_fifo_2_bytes = 1,
  51. uart_fifo_3_bytes = 2,
  52. uart_fifo_4_bytes = 3,
  53. uart_fifo_5_bytes = 4,
  54. uart_fifo_6_bytes = 5,
  55. uart_fifo_7_bytes = 6,
  56. uart_fifo_8_bytes = 7,
  57. uart_fifo_9_bytes = 8,
  58. uart_fifo_10_bytes = 9,
  59. uart_fifo_11_bytes = 10,
  60. uart_fifo_12_bytes = 11,
  61. uart_fifo_13_bytes = 12,
  62. uart_fifo_14_bytes = 13,
  63. uart_fifo_15_bytes = 14,
  64. uart_fifo_16_bytes = 15,
  65. uart_rx_fifo_trg_not_empty = uart_fifo_1_byte,
  66. uart_rx_fifo_trg_gt_one_quarter = uart_fifo_4_bytes,
  67. uart_rx_fifo_trg_gt_half = uart_fifo_8_bytes,
  68. uart_rx_fifo_trg_gt_three_quarters = uart_fifo_12_bytes,
  69. uart_tx_fifo_trg_not_full = uart_fifo_16_bytes,
  70. uart_tx_fifo_trg_lt_three_quarters = uart_fifo_12_bytes,
  71. uart_tx_fifo_trg_lt_half = uart_fifo_8_bytes,
  72. uart_tx_fifo_trg_lt_one_quarter = uart_fifo_4_bytes,
  73. #else
  74. uart_rx_fifo_trg_not_empty = 0,
  75. uart_rx_fifo_trg_gt_one_quarter = 1,
  76. uart_rx_fifo_trg_gt_half = 2,
  77. uart_rx_fifo_trg_gt_three_quarters = 3,
  78. uart_tx_fifo_trg_not_full = 0,
  79. uart_tx_fifo_trg_lt_three_quarters = 1,
  80. uart_tx_fifo_trg_lt_half = 2,
  81. uart_tx_fifo_trg_lt_one_quarter = 3,
  82. #endif
  83. } uart_fifo_trg_lvl_t;
  84. /* @brief UART signals */
  85. typedef enum uart_signal {
  86. uart_signal_rts = UART_MCR_RTS_MASK,
  87. } uart_signal_t;
  88. /* @brief UART signal levels */
  89. typedef enum uart_signal_level {
  90. uart_signal_level_high,
  91. uart_signal_level_low,
  92. } uart_signal_level_t;
  93. /* @brief UART modem status */
  94. typedef enum uart_modem_stat {
  95. uart_modem_stat_cts = UART_MSR_CTS_MASK,
  96. uart_modem_stat_dcts_changed = UART_MSR_DCTS_MASK,
  97. } uart_modem_stat_t;
  98. /* @brief UART interrupt enable masks */
  99. typedef enum uart_intr_enable {
  100. uart_intr_rx_data_avail_or_timeout = UART_IER_ERBI_MASK,
  101. uart_intr_tx_slot_avail = UART_IER_ETHEI_MASK,
  102. uart_intr_rx_line_stat = UART_IER_ELSI_MASK,
  103. uart_intr_modem_stat = UART_IER_EMSI_MASK,
  104. #if defined(HPM_IP_FEATURE_UART_RX_IDLE_DETECT) && (HPM_IP_FEATURE_UART_RX_IDLE_DETECT == 1)
  105. uart_intr_rx_line_idle = UART_IER_ERXIDLE_MASK,
  106. #endif
  107. #if defined(HPM_IP_FEATURE_UART_9BIT_MODE) && (HPM_IP_FEATURE_UART_9BIT_MODE == 1)
  108. uart_intr_tx_line_idle = UART_IER_ETXIDLE_MASK,
  109. #endif
  110. #if defined(HPM_IP_FEATURE_UART_ADDR_MATCH) && (HPM_IP_FEATURE_UART_ADDR_MATCH == 1)
  111. uart_intr_addr_match = UART_IER_EADDRM_MASK,
  112. uart_intr_addr_match_and_rxidle = UART_IER_EADDRM_IDLE_MASK,
  113. uart_intr_addr_datalost = UART_IER_EDATLOST_MASK,
  114. #endif
  115. } uart_intr_enable_t;
  116. /* @brief UART interrupt IDs */
  117. typedef enum uart_intr_id {
  118. uart_intr_id_modem_stat = 0x0,
  119. uart_intr_id_tx_slot_avail = 0x2,
  120. uart_intr_id_rx_data_avail = 0x4,
  121. uart_intr_id_rx_line_stat = 0x6,
  122. uart_intr_id_rx_timeout = 0xc,
  123. } uart_intr_id_t;
  124. /* @brief UART status */
  125. typedef enum uart_stat {
  126. uart_stat_data_ready = UART_LSR_DR_MASK, /* rx data ready in fifo */
  127. uart_stat_overrun_error = UART_LSR_OE_MASK,
  128. uart_stat_parity_error = UART_LSR_PE_MASK,
  129. uart_stat_framing_error = UART_LSR_FE_MASK,
  130. uart_stat_line_break = UART_LSR_LBREAK_MASK,
  131. uart_stat_tx_slot_avail = UART_LSR_THRE_MASK,
  132. uart_stat_transmitter_empty = UART_LSR_TEMT_MASK,
  133. uart_stat_rx_fifo_error = UART_LSR_ERRF_MASK,
  134. } uart_stat_t;
  135. /**
  136. * @brief UART modem config
  137. */
  138. typedef struct uart_modem_config {
  139. bool auto_flow_ctrl_en; /**< Auto flow control enable flag */
  140. bool loop_back_en; /**< Loop back enable flag */
  141. bool set_rts_high; /**< Set signal RTS level high flag */
  142. } uart_modem_config_t;
  143. #if defined(HPM_IP_FEATURE_UART_RX_IDLE_DETECT) && (HPM_IP_FEATURE_UART_RX_IDLE_DETECT == 1)
  144. /**
  145. * @brief UART Idle detection conditions, suitable for RX and TX
  146. */
  147. typedef enum hpm_uart_rxline_idle_cond {
  148. uart_rxline_idle_cond_rxline_logic_one = 0, /**< Treat as idle if the RX Line high duration exceeds threshold */
  149. uart_rxline_idle_cond_state_machine_idle = 1 /**< Treat as idle if the RX state machine idle state duration exceeds threshold */
  150. } uart_rxline_idle_cond_t;
  151. /**
  152. * @brief UART Idle config, suitable for RX and TX
  153. */
  154. typedef struct hpm_uart_rxline_idle_detect_config {
  155. bool detect_enable; /**< RX Line Idle detection flag */
  156. bool detect_irq_enable; /**< Enable RX Line Idle detection interrupt */
  157. uart_rxline_idle_cond_t idle_cond; /**< RX Line Idle detection condition */
  158. uint8_t threshold; /**< UART RX Line Idle detection threshold, in terms of bits */
  159. } uart_rxline_idle_config_t;
  160. #endif
  161. /**
  162. * @brief UART config
  163. */
  164. typedef struct hpm_uart_config {
  165. uint32_t src_freq_in_hz; /**< Source clock frequency in Hz */
  166. uint32_t baudrate; /**< Baudrate */
  167. uint8_t num_of_stop_bits; /**< Number of stop bits */
  168. uint8_t word_length; /**< Word length */
  169. uint8_t parity; /**< Parity */
  170. uint8_t tx_fifo_level; /**< TX Fifo level */
  171. uint8_t rx_fifo_level; /**< RX Fifo level */
  172. bool dma_enable; /**< DMA Enable flag */
  173. bool fifo_enable; /**< Fifo Enable flag */
  174. uart_modem_config_t modem_config; /**< Modem config */
  175. #if defined(HPM_IP_FEATURE_UART_RX_IDLE_DETECT) && (HPM_IP_FEATURE_UART_RX_IDLE_DETECT == 1)
  176. uart_rxline_idle_config_t rxidle_config; /**< RX Idle configuration */
  177. #endif
  178. #if defined(HPM_IP_FEATURE_UART_9BIT_MODE) && (HPM_IP_FEATURE_UART_9BIT_MODE == 1)
  179. uart_rxline_idle_config_t txidle_config; /**< TX Idle configuration */
  180. #endif
  181. #if defined(HPM_IP_FEATURE_UART_RX_EN) && (HPM_IP_FEATURE_UART_RX_EN == 1)
  182. bool rx_enable; /**< RX Enable configuration */
  183. #endif
  184. } uart_config_t;
  185. #if defined(HPM_IP_FEATURE_UART_TRIG_MODE) && (HPM_IP_FEATURE_UART_TRIG_MODE == 1)
  186. typedef struct {
  187. uint16_t stop_bit_len;
  188. bool en_stop_bit_insert;
  189. bool hardware_trig;
  190. bool trig_mode;
  191. bool trig_clr_rxfifo;
  192. } uart_trig_config_t;
  193. #endif
  194. typedef struct {
  195. uint8_t tx_fifo_level; /**< TX Fifo level */
  196. uint8_t rx_fifo_level; /**< RX Fifo level */
  197. bool reset_tx_fifo; /**< reset tx Fifo */
  198. bool reset_rx_fifo; /**< reset rx Fifo */
  199. bool dma_enable; /**< DMA Enable flag */
  200. bool fifo_enable; /**< Fifo Enable flag */
  201. } uart_fifo_ctrl_t;
  202. #ifdef __cplusplus
  203. extern "C" {
  204. #endif
  205. /**
  206. * @brief Get fifo size
  207. *
  208. * @param [in] ptr UART base address
  209. * @retval size of Fifo
  210. */
  211. static inline uint8_t uart_get_fifo_size(UART_Type *ptr)
  212. {
  213. return 16 << ((ptr->CFG & UART_CFG_FIFOSIZE_MASK) >> UART_CFG_FIFOSIZE_SHIFT);
  214. }
  215. /**
  216. * @brief uart config fifo control
  217. *
  218. * @note fifo control register(FCR) is WO access, if support FCCR register, it is RW access.
  219. *
  220. * @param [in] ptr UART base address
  221. * @param [in] ctrl uart_fifo_ctrl_t
  222. */
  223. void uart_config_fifo_ctrl(UART_Type *ptr, uart_fifo_ctrl_t *ctrl);
  224. /**
  225. * @brief uart clear rx fifo by reading data
  226. *
  227. * @note read out all data in rx fifo, the uart_intr_rx_data_avail_or_timeout is cleared
  228. * when RBR register is read
  229. *
  230. * @param [in] ptr UART base address
  231. */
  232. static inline void uart_clear_rx_fifo(UART_Type *ptr)
  233. {
  234. while (ptr->LSR & UART_LSR_DR_MASK) {
  235. ptr->RBR;
  236. }
  237. }
  238. /**
  239. * @brief Reset TX Fifo
  240. *
  241. * @param [in] ptr UART base address
  242. */
  243. static inline void uart_reset_tx_fifo(UART_Type *ptr)
  244. {
  245. #if defined(HPM_IP_FEATURE_UART_FCRR) && (HPM_IP_FEATURE_UART_FCRR == 1)
  246. ptr->FCRR |= UART_FCRR_TFIFORST_MASK;
  247. #else
  248. ptr->FCR = UART_FCR_TFIFORST_MASK | (ptr->GPR);
  249. #endif
  250. }
  251. /**
  252. * @brief Reset RX Fifo
  253. *
  254. * @param [in] ptr UART base address
  255. */
  256. static inline void uart_reset_rx_fifo(UART_Type *ptr)
  257. {
  258. #if defined(HPM_IP_FEATURE_UART_FCRR) && (HPM_IP_FEATURE_UART_FCRR == 1)
  259. ptr->FCRR |= UART_FCRR_RFIFORST_MASK;
  260. #else
  261. ptr->FCR = UART_FCR_RFIFORST_MASK | (ptr->GPR);
  262. #endif
  263. }
  264. /**
  265. * @brief [in] Reset both TX and RX Fifo
  266. *
  267. * @param [in] ptr UART base address
  268. */
  269. static inline void uart_reset_all_fifo(UART_Type *ptr)
  270. {
  271. #if defined(HPM_IP_FEATURE_UART_FCRR) && (HPM_IP_FEATURE_UART_FCRR == 1)
  272. ptr->FCRR |= UART_FCRR_TFIFORST_MASK | UART_FCRR_RFIFORST_MASK;
  273. #else
  274. ptr->FCR = UART_FCR_RFIFORST_MASK | UART_FCR_TFIFORST_MASK | (ptr->GPR);
  275. #endif
  276. }
  277. /**
  278. * @brief Enable modem loopback
  279. *
  280. * @param [in] ptr UART base address
  281. */
  282. static inline void uart_modem_enable_loopback(UART_Type *ptr)
  283. {
  284. ptr->MCR |= UART_MCR_LOOP_MASK;
  285. }
  286. /**
  287. * @brief Disable modem loopback
  288. *
  289. * @param [in] ptr UART base address
  290. */
  291. static inline void uart_modem_disable_loopback(UART_Type *ptr)
  292. {
  293. ptr->MCR &= ~UART_MCR_LOOP_MASK;
  294. }
  295. /**
  296. * @brief Disable modem auto flow control
  297. *
  298. * @param [in] ptr UART base address
  299. */
  300. static inline void uart_modem_disable_auto_flow_control(UART_Type *ptr)
  301. {
  302. ptr->MCR &= ~UART_MCR_AFE_MASK;
  303. }
  304. /**
  305. * @brief Enable modem auto flow control
  306. *
  307. * @param [in] ptr UART base address
  308. */
  309. static inline void uart_modem_enable_auto_flow_control(UART_Type *ptr)
  310. {
  311. ptr->MCR |= UART_MCR_AFE_MASK;
  312. }
  313. /**
  314. * @brief Configure modem
  315. *
  316. * @param [in] ptr UART base address
  317. * @param config Pointer to modem config struct
  318. */
  319. static inline void uart_modem_config(UART_Type *ptr, uart_modem_config_t *config)
  320. {
  321. ptr->MCR = UART_MCR_AFE_SET(config->auto_flow_ctrl_en)
  322. | UART_MCR_LOOP_SET(config->loop_back_en)
  323. | UART_MCR_RTS_SET(!config->set_rts_high);
  324. }
  325. /**
  326. * @brief Get modem status
  327. *
  328. * @param [in] ptr UART base address
  329. * @retval Current modem status
  330. */
  331. static inline uint8_t uart_get_modem_status(UART_Type *ptr)
  332. {
  333. return ptr->MSR;
  334. }
  335. /**
  336. * @brief Write byte to TX
  337. *
  338. * @param ptr UART base address
  339. * @param c data to be sent
  340. */
  341. static inline void uart_write_byte(UART_Type *ptr, uint8_t c)
  342. {
  343. ptr->THR = UART_THR_THR_SET(c);
  344. }
  345. /**
  346. * @brief Read byte from RX
  347. *
  348. * @param ptr UART base address
  349. * @retval RX byte
  350. */
  351. static inline uint8_t uart_read_byte(UART_Type *ptr)
  352. {
  353. return (ptr->RBR & UART_RBR_RBR_MASK);
  354. }
  355. /**
  356. * @brief Check modem status with given mask
  357. *
  358. * @param [in] ptr UART base address
  359. * @param mask Status mask value to be checked against
  360. * @retval true if any bit in given mask is set
  361. * @retval false if none of any bit in given mask is set
  362. */
  363. static inline bool uart_check_modem_status(UART_Type *ptr, uart_modem_stat_t mask)
  364. {
  365. return ((ptr->MSR & mask) != 0U) ? true : false;
  366. }
  367. /**
  368. * @brief Disable IRQ with mask
  369. *
  370. * @param [in] ptr UART base address
  371. * @param irq_mask IRQ mask value to be disabled
  372. */
  373. static inline void uart_disable_irq(UART_Type *ptr, uart_intr_enable_t irq_mask)
  374. {
  375. ptr->IER &= ~irq_mask;
  376. }
  377. /**
  378. * @brief Enable IRQ with mask
  379. *
  380. * @param [in] ptr UART base address
  381. * @param irq_mask IRQ mask value to be enabled
  382. */
  383. static inline void uart_enable_irq(UART_Type *ptr, uart_intr_enable_t irq_mask)
  384. {
  385. ptr->IER |= irq_mask;
  386. }
  387. /**
  388. * @brief Get Enabled IRQ
  389. *
  390. * @param [in] ptr UART base address
  391. * @return enabled irq
  392. */
  393. static inline uint32_t uart_get_enabled_irq(UART_Type *ptr)
  394. {
  395. return ptr->IER;
  396. }
  397. /**
  398. * @brief Get interrupt identification
  399. *
  400. * @param [in] ptr UART base address
  401. * @retval interrupt id
  402. */
  403. static inline uint8_t uart_get_irq_id(UART_Type *ptr)
  404. {
  405. return (ptr->IIR & UART_IIR_INTRID_MASK);
  406. }
  407. #if defined(HPM_IP_FEATURE_UART_RX_IDLE_DETECT) && (HPM_IP_FEATURE_UART_RX_IDLE_DETECT == 1)
  408. /* if HPM_IP_FEATURE_UART_E00018_FIX = 1, the IIR2 register exists, should use IIR2 to get/clear rx idle status */
  409. #if !defined(HPM_IP_FEATURE_UART_E00018_FIX) || (HPM_IP_FEATURE_UART_E00018_FIX == 0)
  410. /**
  411. * @brief Determine whether UART RX Line is idle
  412. * @param [in] ptr UART base address
  413. * @retval false if uart RX line is not idle
  414. */
  415. static inline bool uart_is_rxline_idle(UART_Type *ptr)
  416. {
  417. return ((ptr->IIR & UART_IIR_RXIDLE_FLAG_MASK) != 0U) ? true : false;
  418. }
  419. /**
  420. * @brief Clear UART RX Line Idle Flag
  421. * @param [in] ptr UART base address
  422. */
  423. static inline void uart_clear_rxline_idle_flag(UART_Type *ptr)
  424. {
  425. ptr->IIR = UART_IIR_RXIDLE_FLAG_MASK; /* Write-1-Clear Logic */
  426. ptr->FCR = ptr->GPR;
  427. }
  428. #endif
  429. /**
  430. * @brief Enable UART RX Idle Line detection logic
  431. * @param [in] ptr UART base address
  432. */
  433. static inline void uart_enable_rxline_idle_detection(UART_Type *ptr)
  434. {
  435. ptr->IDLE_CFG |= UART_IDLE_CFG_RX_IDLE_EN_MASK;
  436. }
  437. /**
  438. * @brief Disable UART RX Idle Line detection logic
  439. *
  440. * @param [in] ptr UART base address
  441. */
  442. static inline void uart_disable_rxline_idle_detection(UART_Type *ptr)
  443. {
  444. ptr->IDLE_CFG &= ~UART_IDLE_CFG_RX_IDLE_EN_MASK;
  445. }
  446. /**
  447. * @brief Configure UART RX Line detection
  448. * @param [in] ptr UART base address
  449. * @param [in] rxidle_config RXLine IDLE detection configuration
  450. * @retval status_success if no error occurs
  451. */
  452. hpm_stat_t uart_init_rxline_idle_detection(UART_Type *ptr, uart_rxline_idle_config_t rxidle_config);
  453. #endif
  454. #if defined(HPM_IP_FEATURE_UART_E00018_FIX) && (HPM_IP_FEATURE_UART_E00018_FIX == 1)
  455. /**
  456. * @brief Determine whether UART TX Line is idle
  457. * @param [in] ptr UART base address
  458. * @retval false if uart TX line is not idle
  459. */
  460. static inline bool uart_is_txline_idle(UART_Type *ptr)
  461. {
  462. return ((ptr->IIR2 & UART_IIR2_TXIDLE_FLAG_MASK) != 0U) ? true : false;
  463. }
  464. /**
  465. * @brief Clear UART TX Line Idle Flag
  466. * @param [in] ptr UART base address
  467. */
  468. static inline void uart_clear_txline_idle_flag(UART_Type *ptr)
  469. {
  470. ptr->IIR2 = UART_IIR2_TXIDLE_FLAG_MASK; /* Write-1-Clear Logic */
  471. }
  472. /**
  473. * @brief Determine whether UART RX Line is idle
  474. * @param [in] ptr UART base address
  475. * @retval false if uart RX line is not idle
  476. */
  477. static inline bool uart_is_rxline_idle(UART_Type *ptr)
  478. {
  479. return ((ptr->IIR2 & UART_IIR2_RXIDLE_FLAG_MASK) != 0U) ? true : false;
  480. }
  481. /**
  482. * @brief Clear UART RX Line Idle Flag
  483. * @param [in] ptr UART base address
  484. */
  485. static inline void uart_clear_rxline_idle_flag(UART_Type *ptr)
  486. {
  487. ptr->IIR2 = UART_IIR2_RXIDLE_FLAG_MASK; /* Write-1-Clear Logic */
  488. }
  489. #endif
  490. #if defined(HPM_IP_FEATURE_UART_9BIT_MODE) && (HPM_IP_FEATURE_UART_9BIT_MODE == 1)
  491. /**
  492. * @brief Enable UART TX Idle Line detection logic
  493. * @param [in] ptr UART base address
  494. */
  495. static inline void uart_enable_txline_idle_detection(UART_Type *ptr)
  496. {
  497. ptr->IDLE_CFG |= UART_IDLE_CFG_TX_IDLE_EN_MASK;
  498. }
  499. /**
  500. * @brief Disable UART TX Idle Line detection logic
  501. *
  502. * @param [in] ptr UART base address
  503. */
  504. static inline void uart_disable_txline_idle_detection(UART_Type *ptr)
  505. {
  506. ptr->IDLE_CFG &= ~UART_IDLE_CFG_TX_IDLE_EN_MASK;
  507. }
  508. /**
  509. * @brief Configure UART TX Line detection
  510. * @param [in] ptr UART base address
  511. * @param [in] txidle_config TXLine IDLE detection configuration
  512. * @retval status_success if no error occurs
  513. */
  514. hpm_stat_t uart_init_txline_idle_detection(UART_Type *ptr, uart_rxline_idle_config_t txidle_config);
  515. #endif
  516. /**
  517. * @brief Get status
  518. *
  519. * @param [in] ptr UART base address
  520. * @retval current status
  521. */
  522. static inline uint32_t uart_get_status(UART_Type *ptr)
  523. {
  524. return ptr->LSR;
  525. }
  526. /**
  527. * @brief Check uart status according to the given status mask
  528. *
  529. * @note maybe clear other bits, such as PE/OE/LBREAK/ERRF bit. use uart_get_status API if you need to get these bits
  530. * @param [in] ptr UART base address
  531. * @param mask Status mask value to be checked against
  532. * @retval true if any bit in given mask is set
  533. * @retval false if none of any bit in given mask is set
  534. */
  535. static inline bool uart_check_status(UART_Type *ptr, uart_stat_t mask)
  536. {
  537. return ((ptr->LSR & mask) != 0U) ? true : false;
  538. }
  539. /**
  540. * @brief Get default config
  541. *
  542. * @param [in] ptr UART base address
  543. * @param config Pointer to the buffer to save default values
  544. */
  545. void uart_default_config(UART_Type *ptr, uart_config_t *config);
  546. /**
  547. * @brief Initialization
  548. *
  549. * @param [in] ptr UART base address
  550. * @param config Pointer to config struct
  551. * @retval status_success only if it succeeds
  552. */
  553. hpm_stat_t uart_init(UART_Type *ptr, uart_config_t *config);
  554. /**
  555. * @brief Send one byte after checking thresh hold status
  556. *
  557. * @param [in] ptr UART base address
  558. * @param c Byte to be sent
  559. * @retval status_success only if it succeeds
  560. */
  561. hpm_stat_t uart_send_byte(UART_Type *ptr, uint8_t c);
  562. /**
  563. * @brief Receive one byte after checking data ready status
  564. *
  565. * @param [in] ptr UART base address
  566. * @param c Pointer to buffer to save the byte received on UART
  567. * @retval status_success only if it succeeds
  568. */
  569. hpm_stat_t uart_receive_byte(UART_Type *ptr, uint8_t *c);
  570. /**
  571. * @brief Try to receive one byte without checking data ready status
  572. *
  573. * @param [in] ptr UART base address
  574. * @param c Pointer to buffer to save the byte received on UART
  575. * @retval status_success only if it succeeds
  576. */
  577. hpm_stat_t uart_try_receive_byte(UART_Type *ptr, uint8_t *c);
  578. /**
  579. * @brief Set uart signal output level
  580. *
  581. * @param [in] ptr UART base address
  582. * @param signal Target signal
  583. * @param level Target signal level
  584. */
  585. void uart_set_signal_level(UART_Type *ptr,
  586. uart_signal_t signal,
  587. uart_signal_level_t level);
  588. /**
  589. * @brief Flush sending buffer/fifo
  590. *
  591. * @param [in] ptr UART base address
  592. * @retval status_success only if it succeeds
  593. */
  594. hpm_stat_t uart_flush(UART_Type *ptr);
  595. /**
  596. * @brief Receive bytes blocking
  597. *
  598. * @param [in] ptr UART base address
  599. * @param buf Pointer to the buffer to save received data
  600. * @param size_in_byte Size in byte to be sent
  601. * @retval status_success only if it succeeds
  602. */
  603. hpm_stat_t uart_receive_data(UART_Type *ptr, uint8_t *buf, uint32_t size_in_byte);
  604. /**
  605. * @brief Send bytes blocking
  606. *
  607. * @param [in] ptr UART base address
  608. * @param buf Pointer to the buffer to be sent
  609. * @param size_in_byte Size in byte to be sent
  610. * @retval status_success only if it succeeds
  611. */
  612. hpm_stat_t uart_send_data(UART_Type *ptr, uint8_t *buf, uint32_t size_in_byte);
  613. /**
  614. * @brief Sets UART baudrate.
  615. *
  616. * This function configures the UART module baud rate. This function is used to update
  617. * the UART module baud rate after the UART module is initialized by the uart_init.
  618. *
  619. * @param ptr UART base address
  620. * @param baudrate UART baudrate to be set
  621. * @param src_clock_hz UART clock source frequency in Hz.
  622. * @retval status_uart_no_suitable_baudrate_parameter_found Baudrate is not supported in the current clock source
  623. * @retval status_success Set baudrate succeeded.
  624. */
  625. hpm_stat_t uart_set_baudrate(UART_Type *ptr, uint32_t baudrate, uint32_t src_clock_hz);
  626. #if defined(HPM_IP_FEATURE_UART_TRIG_MODE) && (HPM_IP_FEATURE_UART_TRIG_MODE == 1)
  627. /**
  628. * @brief uart configure transfer trigger mode
  629. *
  630. * This function can configure uart to send data in fifo after being triggered
  631. *
  632. * @param ptr UART base address
  633. * @param config uart_trig_config_t config
  634. */
  635. void uart_config_transfer_trig_mode(UART_Type *ptr, uart_trig_config_t *config);
  636. /**
  637. * @brief uart software trigger transmit
  638. *
  639. * This function immediately triggers the transfer, the transfer configed by uart_config_transfer_trig_mode()
  640. *
  641. * @param ptr UART base address
  642. */
  643. static inline void uart_software_trig_transfer(UART_Type *ptr)
  644. {
  645. ptr->MOTO_CFG &= ~UART_MOTO_CFG_HWTRG_EN_MASK;
  646. ptr->MOTO_CFG |= UART_MOTO_CFG_SWTRG_MASK;
  647. }
  648. /**
  649. * @brief uart enable hardware trigger mode
  650. *
  651. * This function enable hardware trigger the transfer, the transfer start when hardware event occured
  652. *
  653. * @param ptr UART base address
  654. * @param enable true for enable, false for disable
  655. */
  656. static inline void uart_enable_hardware_trig_transfer(UART_Type *ptr, bool enable)
  657. {
  658. if (enable) {
  659. ptr->MOTO_CFG |= UART_MOTO_CFG_HWTRG_EN_MASK;
  660. } else {
  661. ptr->MOTO_CFG &= ~UART_MOTO_CFG_HWTRG_EN_MASK;
  662. }
  663. }
  664. /**
  665. * @brief UART get data count in rx fifo
  666. *
  667. * @param ptr UART base address
  668. * @retval data count
  669. */
  670. static inline uint8_t uart_get_data_count_in_rx_fifo(UART_Type *ptr)
  671. {
  672. return UART_LSR_RFIFO_NUM_GET(ptr->LSR);
  673. }
  674. /**
  675. * @brief UART get data count in tx fifo
  676. *
  677. * @param ptr UART base address
  678. * @retval data count
  679. */
  680. static inline uint8_t uart_get_data_count_in_tx_fifo(UART_Type *ptr)
  681. {
  682. return UART_LSR_TFIFO_NUM_GET(ptr->LSR);
  683. }
  684. #endif
  685. #if defined(HPM_IP_FEATURE_UART_ADDR_MATCH) && (HPM_IP_FEATURE_UART_ADDR_MATCH == 1)
  686. /**
  687. * @brief uart enable 9bit transmit mode
  688. *
  689. * @param ptr UART base address
  690. * @param enable true for enable, false for disable
  691. */
  692. static inline void uart_enable_9bit_transmit_mode(UART_Type *ptr, bool enable)
  693. {
  694. if (enable) {
  695. ptr->ADDR_CFG |= UART_ADDR_CFG_TXEN_9BIT_MASK
  696. | UART_ADDR_CFG_RXEN_ADDR_MSB_MASK
  697. | UART_ADDR_CFG_RXEN_9BIT_MASK;
  698. } else {
  699. ptr->ADDR_CFG &= ~(UART_ADDR_CFG_TXEN_9BIT_MASK
  700. | UART_ADDR_CFG_RXEN_ADDR_MSB_MASK
  701. | UART_ADDR_CFG_RXEN_9BIT_MASK);
  702. }
  703. }
  704. /**
  705. * @brief uart enable address0 match
  706. *
  707. * @param ptr UART base address
  708. * @param addr address value
  709. */
  710. static inline void uart_enable_address0_match(UART_Type *ptr, uint8_t addr)
  711. {
  712. ptr->ADDR_CFG &= ~UART_ADDR_CFG_ADDR0_MASK;
  713. ptr->ADDR_CFG |= UART_ADDR_CFG_A0_EN_MASK | UART_ADDR_CFG_ADDR0_SET(addr);
  714. }
  715. /**
  716. * @brief uart enable address1 match
  717. *
  718. * @param ptr UART base address
  719. * @param addr address value
  720. */
  721. static inline void uart_enable_address1_match(UART_Type *ptr, uint8_t addr)
  722. {
  723. ptr->ADDR_CFG &= ~UART_ADDR_CFG_ADDR1_MASK;
  724. ptr->ADDR_CFG |= UART_ADDR_CFG_A1_EN_MASK | UART_ADDR_CFG_ADDR1_SET(addr);
  725. }
  726. /**
  727. * @brief uart disable address0 match
  728. *
  729. * @param ptr UART base address
  730. */
  731. static inline void uart_disable_address0_match(UART_Type *ptr)
  732. {
  733. ptr->ADDR_CFG &= ~UART_ADDR_CFG_A0_EN_MASK;
  734. }
  735. /**
  736. * @brief uart disable address1 match
  737. *
  738. * @param ptr UART base address
  739. */
  740. static inline void uart_disable_address1_match(UART_Type *ptr)
  741. {
  742. ptr->ADDR_CFG &= ~UART_ADDR_CFG_A1_EN_MASK;
  743. }
  744. /**
  745. * @brief uart disable address match(address0 and address1)
  746. *
  747. * @param ptr UART base address
  748. */
  749. static inline void uart_disable_address_match(UART_Type *ptr)
  750. {
  751. ptr->ADDR_CFG &= ~(UART_ADDR_CFG_A0_EN_MASK | UART_ADDR_CFG_A1_EN_MASK);
  752. }
  753. /**
  754. * @brief Determine whether address match for 9bit mode
  755. * @param [in] ptr UART base address
  756. * @retval false if uart address is not match
  757. */
  758. static inline bool uart_is_addr_match(UART_Type *ptr)
  759. {
  760. return ((ptr->IIR2 & UART_IIR2_ADDR_MATCH_MASK) != 0U) ? true : false;
  761. }
  762. /**
  763. * @brief Clear UART address match Flag
  764. * @param [in] ptr UART base address
  765. */
  766. static inline void uart_clear_addr_match_flag(UART_Type *ptr)
  767. {
  768. ptr->IIR2 = UART_IIR2_ADDR_MATCH_MASK; /* Write-1-Clear Logic */
  769. }
  770. /**
  771. * @brief Determine whether address match and rx idle for 9bit mode
  772. * @param [in] ptr UART base address
  773. * @retval false if uart address is not match and not rx idle
  774. */
  775. static inline bool uart_is_addr_match_and_rxidle(UART_Type *ptr)
  776. {
  777. return ((ptr->IIR2 & UART_IIR2_ADDR_MATCH_IDLE_MASK) != 0U) ? true : false;
  778. }
  779. /**
  780. * @brief Clear UART address match and rxidle Flag
  781. * @param [in] ptr UART base address
  782. */
  783. static inline void uart_clear_addr_match_and_rxidle_flag(UART_Type *ptr)
  784. {
  785. ptr->IIR2 = UART_IIR2_ADDR_MATCH_IDLE_MASK; /* Write-1-Clear Logic */
  786. }
  787. /**
  788. * @brief Determine whether data lost for 9bit mode
  789. * @param [in] ptr UART base address
  790. * @retval false if uart data is not lost
  791. */
  792. static inline bool uart_is_data_lost(UART_Type *ptr)
  793. {
  794. return ((ptr->IIR2 & UART_IIR2_DATA_LOST_MASK) != 0U) ? true : false;
  795. }
  796. /**
  797. * @brief Clear UART data lost Flag
  798. * @param [in] ptr UART base address
  799. */
  800. static inline void uart_clear_data_lost_flag(UART_Type *ptr)
  801. {
  802. ptr->IIR2 = UART_IIR2_DATA_LOST_MASK; /* Write-1-Clear Logic */
  803. }
  804. #endif
  805. /**
  806. * @brief Write RTS level for uart modem mode
  807. *
  808. * @param [in] ptr UART base address
  809. * @param high RTS set to high when it is set to true
  810. */
  811. static inline void uart_modem_write_rts_pin(UART_Type *ptr, uint8_t high)
  812. {
  813. if (high == true) {
  814. ptr->MCR &= ~UART_MCR_RTS_MASK;
  815. } else {
  816. ptr->MCR |= UART_MCR_RTS_MASK;
  817. }
  818. }
  819. #ifdef __cplusplus
  820. }
  821. #endif
  822. /**
  823. * @}
  824. */
  825. #endif /* HPM_UART_DRV_H */