nrf_uart.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_UART_H__
  13. #define NRF_UART_H__
  14. #include "nrf.h"
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdbool.h>
  18. /**
  19. * @defgroup nrf_uart_hal UART HAL
  20. * @{
  21. * @ingroup nrf_uart
  22. *
  23. * @brief Hardware access layer for accessing the UART peripheral.
  24. */
  25. #define NRF_UART_PSEL_DISCONNECTED 0xFFFFFFFF
  26. /**
  27. * @enum nrf_uart_task_t
  28. * @brief UART tasks.
  29. */
  30. typedef enum
  31. {
  32. /*lint -save -e30 -esym(628,__INTADDR__)*/
  33. NRF_UART_TASK_STARTRX = offsetof(NRF_UART_Type, TASKS_STARTRX), /**< Task for starting reception. */
  34. NRF_UART_TASK_STOPRX = offsetof(NRF_UART_Type, TASKS_STOPRX), /**< Task for stopping reception. */
  35. NRF_UART_TASK_STARTTX = offsetof(NRF_UART_Type, TASKS_STARTTX), /**< Task for starting transmission. */
  36. NRF_UART_TASK_STOPTX = offsetof(NRF_UART_Type, TASKS_STOPTX), /**< Task for stopping transmission. */
  37. NRF_UART_TASK_SUSPEND = offsetof(NRF_UART_Type, TASKS_SUSPEND), /**< Task for suspending UART. */
  38. /*lint -restore*/
  39. } nrf_uart_task_t;
  40. /**
  41. * @enum nrf_uart_event_t
  42. * @brief UART events.
  43. */
  44. typedef enum
  45. {
  46. /*lint -save -e30*/
  47. NRF_UART_EVENT_CTS = offsetof(NRF_UART_Type, EVENTS_CTS), /**< Event from CTS line activation. */
  48. NRF_UART_EVENT_NCTS = offsetof(NRF_UART_Type, EVENTS_NCTS), /**< Event from CTS line deactivation. */
  49. NRF_UART_EVENT_RXDRDY = offsetof(NRF_UART_Type, EVENTS_RXDRDY),/**< Event from data ready in RXD. */
  50. NRF_UART_EVENT_TXDRDY = offsetof(NRF_UART_Type, EVENTS_TXDRDY),/**< Event from data sent from TXD. */
  51. NRF_UART_EVENT_ERROR = offsetof(NRF_UART_Type, EVENTS_ERROR), /**< Event from error detection. */
  52. NRF_UART_EVENT_RXTO = offsetof(NRF_UART_Type, EVENTS_RXTO) /**< Event from receiver timeout. */
  53. /*lint -restore*/
  54. } nrf_uart_event_t;
  55. /**
  56. * @enum nrf_uart_int_mask_t
  57. * @brief UART interrupts.
  58. */
  59. typedef enum
  60. {
  61. /*lint -save -e30*/
  62. NRF_UART_INT_MASK_CTS = UART_INTENCLR_CTS_Msk, /**< CTS line activation interrupt. */
  63. NRF_UART_INT_MASK_NCTS = UART_INTENCLR_NCTS_Msk, /**< CTS line deactivation interrupt. */
  64. NRF_UART_INT_MASK_RXDRDY = UART_INTENCLR_RXDRDY_Msk, /**< Data ready in RXD interrupt. */
  65. NRF_UART_INT_MASK_TXDRDY = UART_INTENCLR_TXDRDY_Msk, /**< Data sent from TXD interrupt. */
  66. NRF_UART_INT_MASK_ERROR = UART_INTENCLR_ERROR_Msk, /**< Error detection interrupt. */
  67. NRF_UART_INT_MASK_RXTO = UART_INTENCLR_RXTO_Msk /**< Receiver timeout interrupt. */
  68. /*lint -restore*/
  69. } nrf_uart_int_mask_t;
  70. /**
  71. * @enum nrf_uart_baudrate_t
  72. * @brief Baudrates supported by UART.
  73. */
  74. typedef enum
  75. {
  76. #ifdef NRF52
  77. NRF_UART_BAUDRATE_1200 = UARTE_BAUDRATE_BAUDRATE_Baud1200, /**< 1200 baud. */
  78. NRF_UART_BAUDRATE_2400 = UARTE_BAUDRATE_BAUDRATE_Baud2400, /**< 2400 baud. */
  79. NRF_UART_BAUDRATE_4800 = UARTE_BAUDRATE_BAUDRATE_Baud4800, /**< 4800 baud. */
  80. NRF_UART_BAUDRATE_9600 = UARTE_BAUDRATE_BAUDRATE_Baud9600, /**< 9600 baud. */
  81. NRF_UART_BAUDRATE_14400 = UARTE_BAUDRATE_BAUDRATE_Baud14400, /**< 14400 baud. */
  82. NRF_UART_BAUDRATE_19200 = UARTE_BAUDRATE_BAUDRATE_Baud19200, /**< 19200 baud. */
  83. NRF_UART_BAUDRATE_28800 = UARTE_BAUDRATE_BAUDRATE_Baud28800, /**< 28800 baud. */
  84. NRF_UART_BAUDRATE_38400 = UARTE_BAUDRATE_BAUDRATE_Baud38400, /**< 38400 baud. */
  85. NRF_UART_BAUDRATE_57600 = UARTE_BAUDRATE_BAUDRATE_Baud57600, /**< 57600 baud. */
  86. NRF_UART_BAUDRATE_76800 = UARTE_BAUDRATE_BAUDRATE_Baud76800, /**< 76800 baud. */
  87. NRF_UART_BAUDRATE_115200 = UARTE_BAUDRATE_BAUDRATE_Baud115200, /**< 115200 baud. */
  88. NRF_UART_BAUDRATE_230400 = UARTE_BAUDRATE_BAUDRATE_Baud230400, /**< 230400 baud. */
  89. NRF_UART_BAUDRATE_250000 = UARTE_BAUDRATE_BAUDRATE_Baud250000, /**< 250000 baud. */
  90. NRF_UART_BAUDRATE_460800 = UARTE_BAUDRATE_BAUDRATE_Baud460800, /**< 460800 baud. */
  91. NRF_UART_BAUDRATE_921600 = UARTE_BAUDRATE_BAUDRATE_Baud921600, /**< 921600 baud. */
  92. NRF_UART_BAUDRATE_1000000 = UARTE_BAUDRATE_BAUDRATE_Baud1M, /**< 1000000 baud. */
  93. #else
  94. NRF_UART_BAUDRATE_1200 = UART_BAUDRATE_BAUDRATE_Baud1200, /**< 1200 baud. */
  95. NRF_UART_BAUDRATE_2400 = UART_BAUDRATE_BAUDRATE_Baud2400, /**< 2400 baud. */
  96. NRF_UART_BAUDRATE_4800 = UART_BAUDRATE_BAUDRATE_Baud4800, /**< 4800 baud. */
  97. NRF_UART_BAUDRATE_9600 = UART_BAUDRATE_BAUDRATE_Baud9600, /**< 9600 baud. */
  98. NRF_UART_BAUDRATE_14400 = UART_BAUDRATE_BAUDRATE_Baud14400, /**< 14400 baud. */
  99. NRF_UART_BAUDRATE_19200 = UART_BAUDRATE_BAUDRATE_Baud19200, /**< 19200 baud. */
  100. NRF_UART_BAUDRATE_28800 = UART_BAUDRATE_BAUDRATE_Baud28800, /**< 28800 baud. */
  101. NRF_UART_BAUDRATE_38400 = UART_BAUDRATE_BAUDRATE_Baud38400, /**< 38400 baud. */
  102. NRF_UART_BAUDRATE_57600 = UART_BAUDRATE_BAUDRATE_Baud57600, /**< 57600 baud. */
  103. NRF_UART_BAUDRATE_76800 = UART_BAUDRATE_BAUDRATE_Baud76800, /**< 76800 baud. */
  104. NRF_UART_BAUDRATE_115200 = UART_BAUDRATE_BAUDRATE_Baud115200, /**< 115200 baud. */
  105. NRF_UART_BAUDRATE_230400 = UART_BAUDRATE_BAUDRATE_Baud230400, /**< 230400 baud. */
  106. NRF_UART_BAUDRATE_250000 = UART_BAUDRATE_BAUDRATE_Baud250000, /**< 250000 baud. */
  107. NRF_UART_BAUDRATE_460800 = UART_BAUDRATE_BAUDRATE_Baud460800, /**< 460800 baud. */
  108. NRF_UART_BAUDRATE_921600 = UART_BAUDRATE_BAUDRATE_Baud921600, /**< 921600 baud. */
  109. NRF_UART_BAUDRATE_1000000 = UART_BAUDRATE_BAUDRATE_Baud1M, /**< 1000000 baud. */
  110. #endif
  111. } nrf_uart_baudrate_t;
  112. /**
  113. * @enum nrf_uart_error_mask_t
  114. * @brief Types of UART error masks.
  115. */
  116. typedef enum
  117. {
  118. NRF_UART_ERROR_OVERRUN_MASK = UART_ERRORSRC_OVERRUN_Msk, /**< Overrun error. */
  119. NRF_UART_ERROR_PARITY_MASK = UART_ERRORSRC_PARITY_Msk, /**< Parity error. */
  120. NRF_UART_ERROR_FRAMING_MASK = UART_ERRORSRC_FRAMING_Msk, /**< Framing error. */
  121. NRF_UART_ERROR_BREAK_MASK = UART_ERRORSRC_BREAK_Msk, /**< Break error. */
  122. } nrf_uart_error_mask_t;
  123. /**
  124. * @enum nrf_uart_parity_t
  125. * @brief Types of UART parity modes.
  126. */
  127. typedef enum
  128. {
  129. NRF_UART_PARITY_EXCLUDED = UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos, /**< Parity excluded. */
  130. NRF_UART_PARITY_INCLUDED = UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos, /**< Parity included. */
  131. } nrf_uart_parity_t;
  132. /**
  133. * @enum nrf_uart_hwfc_t
  134. * @brief Types of UART flow control modes.
  135. */
  136. typedef enum
  137. {
  138. NRF_UART_HWFC_DISABLED = UART_CONFIG_HWFC_Disabled, /**< HW flow control disabled. */
  139. NRF_UART_HWFC_ENABLED = UART_CONFIG_HWFC_Enabled, /**< HW flow control enabled. */
  140. } nrf_uart_hwfc_t;
  141. /**
  142. * @brief Function for clearing a specific UART event.
  143. *
  144. * @param[in] p_reg UART instance.
  145. * @param[in] event Event to clear.
  146. */
  147. __STATIC_INLINE void nrf_uart_event_clear(NRF_UART_Type * p_reg, nrf_uart_event_t event);
  148. /**
  149. * @brief Function for checking the state of a specific UART event.
  150. *
  151. * @param[in] p_reg UART instance.
  152. * @param[in] event Event to check.
  153. *
  154. * @retval True if event is set, False otherwise.
  155. */
  156. __STATIC_INLINE bool nrf_uart_event_check(NRF_UART_Type * p_reg, nrf_uart_event_t event);
  157. /**
  158. * @brief Function for returning the address of a specific UART event register.
  159. *
  160. * @param[in] p_reg UART instance.
  161. * @param[in] event Desired event.
  162. *
  163. * @retval Address of specified event register.
  164. */
  165. __STATIC_INLINE uint32_t nrf_uart_event_address_get(NRF_UART_Type * p_reg,
  166. nrf_uart_event_t event);
  167. /**
  168. * @brief Function for enabling a specific interrupt.
  169. *
  170. * @param p_reg Instance.
  171. * @param int_mask Interrupts to enable.
  172. */
  173. __STATIC_INLINE void nrf_uart_int_enable(NRF_UART_Type * p_reg, uint32_t int_mask);
  174. /**
  175. * @brief Function for retrieving the state of a given interrupt.
  176. *
  177. * @param p_reg Instance.
  178. * @param int_mask Mask of interrupt to check.
  179. *
  180. * @retval true If the interrupt is enabled.
  181. * @retval false If the interrupt is not enabled.
  182. */
  183. __STATIC_INLINE bool nrf_uart_int_enable_check(NRF_UART_Type * p_reg, uint32_t int_mask);
  184. /**
  185. * @brief Function for disabling specific interrupts.
  186. *
  187. * @param p_reg Instance.
  188. * @param int_mask Interrupts to disable.
  189. */
  190. __STATIC_INLINE void nrf_uart_int_disable(NRF_UART_Type * p_reg, uint32_t int_mask);
  191. /**
  192. * @brief Function for getting error source mask. Function is clearing error source flags after reading.
  193. *
  194. * @param p_reg Instance.
  195. * @return Mask with error source flags.
  196. */
  197. __STATIC_INLINE uint32_t nrf_uart_errorsrc_get_and_clear(NRF_UART_Type * p_reg);
  198. /**
  199. * @brief Function for enabling UART.
  200. *
  201. * @param p_reg Instance.
  202. */
  203. __STATIC_INLINE void nrf_uart_enable(NRF_UART_Type * p_reg);
  204. /**
  205. * @brief Function for disabling UART.
  206. *
  207. * @param p_reg Instance.
  208. */
  209. __STATIC_INLINE void nrf_uart_disable(NRF_UART_Type * p_reg);
  210. /**
  211. * @brief Function for configuring TX/RX pins.
  212. *
  213. * @param p_reg Instance.
  214. * @param pseltxd TXD pin number.
  215. * @param pselrxd RXD pin number.
  216. */
  217. __STATIC_INLINE void nrf_uart_txrx_pins_set(NRF_UART_Type * p_reg, uint32_t pseltxd, uint32_t pselrxd);
  218. /**
  219. * @brief Function for disconnecting TX/RX pins.
  220. *
  221. * @param p_reg Instance.
  222. */
  223. __STATIC_INLINE void nrf_uart_txrx_pins_disconnect(NRF_UART_Type * p_reg);
  224. /**
  225. * @brief Function for getting TX pin.
  226. *
  227. * @param p_reg Instance.
  228. */
  229. __STATIC_INLINE uint32_t nrf_uart_tx_pin_get(NRF_UART_Type * p_reg);
  230. /**
  231. * @brief Function for getting RX pin.
  232. *
  233. * @param p_reg Instance.
  234. */
  235. __STATIC_INLINE uint32_t nrf_uart_rx_pin_get(NRF_UART_Type * p_reg);
  236. /**
  237. * @brief Function for getting RTS pin.
  238. *
  239. * @param p_reg Instance.
  240. */
  241. __STATIC_INLINE uint32_t nrf_uart_rts_pin_get(NRF_UART_Type * p_reg);
  242. /**
  243. * @brief Function for getting CTS pin.
  244. *
  245. * @param p_reg Instance.
  246. */
  247. __STATIC_INLINE uint32_t nrf_uart_cts_pin_get(NRF_UART_Type * p_reg);
  248. /**
  249. * @brief Function for configuring flow control pins.
  250. *
  251. * @param p_reg Instance.
  252. * @param pselrts RTS pin number.
  253. * @param pselcts CTS pin number.
  254. */
  255. __STATIC_INLINE void nrf_uart_hwfc_pins_set(NRF_UART_Type * p_reg,
  256. uint32_t pselrts,
  257. uint32_t pselcts);
  258. /**
  259. * @brief Function for disconnecting flow control pins.
  260. *
  261. * @param p_reg Instance.
  262. */
  263. __STATIC_INLINE void nrf_uart_hwfc_pins_disconnect(NRF_UART_Type * p_reg);
  264. /**
  265. * @brief Function for reading RX data.
  266. *
  267. * @param p_reg Instance.
  268. * @return Received byte.
  269. */
  270. __STATIC_INLINE uint8_t nrf_uart_rxd_get(NRF_UART_Type * p_reg);
  271. /**
  272. * @brief Function for setting Tx data.
  273. *
  274. * @param p_reg Instance.
  275. * @param txd Byte.
  276. */
  277. __STATIC_INLINE void nrf_uart_txd_set(NRF_UART_Type * p_reg, uint8_t txd);
  278. /**
  279. * @brief Function for starting an UART task.
  280. *
  281. * @param p_reg Instance.
  282. * @param task Task.
  283. */
  284. __STATIC_INLINE void nrf_uart_task_trigger(NRF_UART_Type * p_reg, nrf_uart_task_t task);
  285. /**
  286. * @brief Function for returning the address of a specific task register.
  287. *
  288. * @param p_reg Instance.
  289. * @param task Task.
  290. *
  291. * @return Task address.
  292. */
  293. __STATIC_INLINE uint32_t nrf_uart_task_address_get(NRF_UART_Type * p_reg, nrf_uart_task_t task);
  294. /**
  295. * @brief Function for configuring UART.
  296. *
  297. * @param p_reg Instance.
  298. * @param hwfc Hardware flow control. Enabled if true.
  299. * @param parity Parity. Included if true.
  300. */
  301. __STATIC_INLINE void nrf_uart_configure(NRF_UART_Type * p_reg,
  302. nrf_uart_parity_t parity,
  303. nrf_uart_hwfc_t hwfc);
  304. /**
  305. * @brief Function for setting UART baudrate.
  306. *
  307. * @param p_reg Instance.
  308. * @param baudrate Baudrate.
  309. */
  310. __STATIC_INLINE void nrf_uart_baudrate_set(NRF_UART_Type * p_reg, nrf_uart_baudrate_t baudrate);
  311. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  312. __STATIC_INLINE void nrf_uart_event_clear(NRF_UART_Type * p_reg, nrf_uart_event_t event)
  313. {
  314. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  315. }
  316. __STATIC_INLINE bool nrf_uart_event_check(NRF_UART_Type * p_reg, nrf_uart_event_t event)
  317. {
  318. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  319. }
  320. __STATIC_INLINE uint32_t nrf_uart_event_address_get(NRF_UART_Type * p_reg,
  321. nrf_uart_event_t event)
  322. {
  323. return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
  324. }
  325. __STATIC_INLINE void nrf_uart_int_enable(NRF_UART_Type * p_reg, uint32_t int_mask)
  326. {
  327. p_reg->INTENSET = int_mask;
  328. }
  329. __STATIC_INLINE bool nrf_uart_int_enable_check(NRF_UART_Type * p_reg, uint32_t int_mask)
  330. {
  331. return (bool)(p_reg->INTENSET & int_mask);
  332. }
  333. __STATIC_INLINE void nrf_uart_int_disable(NRF_UART_Type * p_reg, uint32_t int_mask)
  334. {
  335. p_reg->INTENCLR = int_mask;
  336. }
  337. __STATIC_INLINE uint32_t nrf_uart_errorsrc_get_and_clear(NRF_UART_Type * p_reg)
  338. {
  339. uint32_t errsrc_mask = p_reg->ERRORSRC;
  340. p_reg->ERRORSRC = errsrc_mask;
  341. return errsrc_mask;
  342. }
  343. __STATIC_INLINE void nrf_uart_enable(NRF_UART_Type * p_reg)
  344. {
  345. p_reg->ENABLE = UART_ENABLE_ENABLE_Enabled;
  346. }
  347. __STATIC_INLINE void nrf_uart_disable(NRF_UART_Type * p_reg)
  348. {
  349. p_reg->ENABLE = UART_ENABLE_ENABLE_Disabled;
  350. }
  351. __STATIC_INLINE void nrf_uart_txrx_pins_set(NRF_UART_Type * p_reg, uint32_t pseltxd, uint32_t pselrxd)
  352. {
  353. p_reg->PSELTXD = pseltxd;
  354. p_reg->PSELRXD = pselrxd;
  355. }
  356. __STATIC_INLINE void nrf_uart_txrx_pins_disconnect(NRF_UART_Type * p_reg)
  357. {
  358. nrf_uart_txrx_pins_set(p_reg, NRF_UART_PSEL_DISCONNECTED, NRF_UART_PSEL_DISCONNECTED);
  359. }
  360. __STATIC_INLINE uint32_t nrf_uart_tx_pin_get(NRF_UART_Type * p_reg)
  361. {
  362. return p_reg->PSELTXD;
  363. }
  364. __STATIC_INLINE uint32_t nrf_uart_rx_pin_get(NRF_UART_Type * p_reg)
  365. {
  366. return p_reg->PSELRXD;
  367. }
  368. __STATIC_INLINE uint32_t nrf_uart_rts_pin_get(NRF_UART_Type * p_reg)
  369. {
  370. return p_reg->PSELRTS;
  371. }
  372. __STATIC_INLINE uint32_t nrf_uart_cts_pin_get(NRF_UART_Type * p_reg)
  373. {
  374. return p_reg->PSELCTS;
  375. }
  376. __STATIC_INLINE void nrf_uart_hwfc_pins_set(NRF_UART_Type * p_reg, uint32_t pselrts, uint32_t pselcts)
  377. {
  378. p_reg->PSELRTS = pselrts;
  379. p_reg->PSELCTS = pselcts;
  380. }
  381. __STATIC_INLINE void nrf_uart_hwfc_pins_disconnect(NRF_UART_Type * p_reg)
  382. {
  383. nrf_uart_hwfc_pins_set(p_reg, NRF_UART_PSEL_DISCONNECTED, NRF_UART_PSEL_DISCONNECTED);
  384. }
  385. __STATIC_INLINE uint8_t nrf_uart_rxd_get(NRF_UART_Type * p_reg)
  386. {
  387. return p_reg->RXD;
  388. }
  389. __STATIC_INLINE void nrf_uart_txd_set(NRF_UART_Type * p_reg, uint8_t txd)
  390. {
  391. p_reg->TXD = txd;
  392. }
  393. __STATIC_INLINE void nrf_uart_task_trigger(NRF_UART_Type * p_reg, nrf_uart_task_t task)
  394. {
  395. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  396. }
  397. __STATIC_INLINE uint32_t nrf_uart_task_address_get(NRF_UART_Type * p_reg, nrf_uart_task_t task)
  398. {
  399. return (uint32_t)p_reg + (uint32_t)task;
  400. }
  401. __STATIC_INLINE void nrf_uart_configure(NRF_UART_Type * p_reg,
  402. nrf_uart_parity_t parity,
  403. nrf_uart_hwfc_t hwfc)
  404. {
  405. p_reg->CONFIG = (uint32_t)parity | (uint32_t)hwfc;
  406. }
  407. __STATIC_INLINE void nrf_uart_baudrate_set(NRF_UART_Type * p_reg, nrf_uart_baudrate_t baudrate)
  408. {
  409. p_reg->BAUDRATE = baudrate;
  410. }
  411. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  412. /** @} */
  413. #endif //NRF_UART_H__