nrf_drv_uart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. #include "nrf_drv_uart.h"
  13. #include "nrf_assert.h"
  14. #include "nordic_common.h"
  15. #include "nrf_drv_common.h"
  16. #include "nrf_gpio.h"
  17. #include "app_util_platform.h"
  18. // This set of macros makes it possible to exclude parts of code, when one type
  19. // of supported peripherals is not used.
  20. #ifdef NRF51
  21. #define UART_IN_USE
  22. #elif defined(NRF52)
  23. #if (UART_EASY_DMA_SUPPORT == 1)
  24. #define UARTE_IN_USE
  25. #endif
  26. #if (UART_LEGACY_SUPPORT == 1)
  27. #define UART_IN_USE
  28. #endif
  29. #endif
  30. #if (defined(UARTE_IN_USE) && defined(UART_IN_USE))
  31. // UARTE and UART combined
  32. #define CODE_FOR_UARTE(code) if (m_cb.use_easy_dma) { code }
  33. #define CODE_FOR_UART(code) else { code }
  34. #elif (defined(UARTE_IN_USE) && !defined(UART_IN_USE))
  35. // UARTE only
  36. #define CODE_FOR_UARTE(code) { code }
  37. #define CODE_FOR_UART(code)
  38. #elif (!defined(UARTE_IN_USE) && defined(UART_IN_USE))
  39. // UART only
  40. #define CODE_FOR_UARTE(code)
  41. #define CODE_FOR_UART(code) { code }
  42. #else
  43. #error "Wrong configuration."
  44. #endif
  45. #ifndef IS_EASY_DMA_RAM_ADDRESS
  46. #define IS_EASY_DMA_RAM_ADDRESS(addr) (((uint32_t)addr & 0xFFFF0000) == 0x20000000)
  47. #endif
  48. #define TX_COUNTER_ABORT_REQ_VALUE 256
  49. typedef struct
  50. {
  51. void * p_context;
  52. nrf_uart_event_handler_t handler;
  53. uint8_t const * p_tx_buffer;
  54. uint8_t * p_rx_buffer;
  55. uint8_t * p_rx_secondary_buffer;
  56. volatile uint16_t tx_counter;
  57. uint8_t tx_buffer_length;
  58. uint8_t rx_buffer_length;
  59. uint8_t rx_secondary_buffer_length;
  60. volatile uint8_t rx_counter;
  61. bool rx_enabled;
  62. nrf_drv_state_t state;
  63. #if (defined(UARTE_IN_USE) && defined(UART_IN_USE))
  64. bool use_easy_dma;
  65. #endif
  66. } uart_control_block_t;
  67. static uart_control_block_t m_cb;
  68. static const nrf_drv_uart_config_t m_default_config = NRF_DRV_UART_DEFAULT_CONFIG;
  69. __STATIC_INLINE void apply_config(nrf_drv_uart_config_t const * p_config)
  70. {
  71. nrf_gpio_pin_set(p_config->pseltxd);
  72. nrf_gpio_cfg_output(p_config->pseltxd);
  73. nrf_gpio_cfg_input(p_config->pselrxd, NRF_GPIO_PIN_NOPULL);
  74. CODE_FOR_UARTE
  75. (
  76. nrf_uarte_baudrate_set(NRF_UARTE0, (nrf_uarte_baudrate_t)p_config->baudrate);
  77. nrf_uarte_configure(NRF_UARTE0, (nrf_uarte_parity_t)p_config->parity,
  78. (nrf_uarte_hwfc_t)p_config->hwfc);
  79. nrf_uarte_txrx_pins_set(NRF_UARTE0, p_config->pseltxd, p_config->pselrxd);
  80. if (p_config->hwfc == NRF_UART_HWFC_ENABLED)
  81. {
  82. nrf_gpio_cfg_input(p_config->pselcts, NRF_GPIO_PIN_NOPULL);
  83. nrf_gpio_pin_set(p_config->pselrts);
  84. nrf_gpio_cfg_output(p_config->pselrts);
  85. nrf_uarte_hwfc_pins_set(NRF_UARTE0, p_config->pselrts, p_config->pselcts);
  86. }
  87. )
  88. CODE_FOR_UART
  89. (
  90. nrf_uart_baudrate_set(NRF_UART0, p_config->baudrate);
  91. nrf_uart_configure(NRF_UART0, p_config->parity, p_config->hwfc);
  92. nrf_uart_txrx_pins_set(NRF_UART0, p_config->pseltxd, p_config->pselrxd);
  93. if (p_config->hwfc == NRF_UART_HWFC_ENABLED)
  94. {
  95. nrf_gpio_cfg_input(p_config->pselcts, NRF_GPIO_PIN_NOPULL);
  96. nrf_gpio_pin_set(p_config->pselrts);
  97. nrf_gpio_cfg_output(p_config->pselrts);
  98. nrf_uart_hwfc_pins_set(NRF_UART0, p_config->pselrts, p_config->pselcts);
  99. }
  100. )
  101. }
  102. __STATIC_INLINE void interrupts_enable(uint8_t interrupt_priority)
  103. {
  104. CODE_FOR_UARTE
  105. (
  106. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDRX);
  107. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDTX);
  108. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ERROR);
  109. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_RXTO);
  110. nrf_uarte_int_enable(NRF_UARTE0, NRF_UARTE_INT_ENDRX_MASK |
  111. NRF_UARTE_INT_ENDTX_MASK |
  112. NRF_UARTE_INT_ERROR_MASK |
  113. NRF_UARTE_INT_RXTO_MASK);
  114. )
  115. CODE_FOR_UART
  116. (
  117. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
  118. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXTO);
  119. nrf_uart_int_enable(NRF_UART0, NRF_UART_INT_MASK_TXDRDY |
  120. NRF_UART_INT_MASK_RXTO);
  121. )
  122. nrf_drv_common_irq_enable(UART0_IRQn, interrupt_priority);
  123. }
  124. __STATIC_INLINE void interrupts_disable(void)
  125. {
  126. CODE_FOR_UARTE
  127. (
  128. nrf_uarte_int_disable(NRF_UARTE0, NRF_UARTE_INT_ENDRX_MASK |
  129. NRF_UARTE_INT_ENDTX_MASK |
  130. NRF_UARTE_INT_ERROR_MASK |
  131. NRF_UARTE_INT_RXTO_MASK);
  132. )
  133. CODE_FOR_UART
  134. (
  135. nrf_uart_int_disable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY |
  136. NRF_UART_INT_MASK_TXDRDY |
  137. NRF_UART_INT_MASK_ERROR |
  138. NRF_UART_INT_MASK_RXTO);
  139. )
  140. nrf_drv_common_irq_disable(UART0_IRQn);
  141. }
  142. __STATIC_INLINE void pins_to_default(void)
  143. {
  144. /* Reset pins to default states */
  145. uint32_t txd;
  146. uint32_t rxd;
  147. uint32_t rts;
  148. uint32_t cts;
  149. CODE_FOR_UARTE
  150. (
  151. txd = nrf_uarte_tx_pin_get(NRF_UARTE0);
  152. rxd = nrf_uarte_rx_pin_get(NRF_UARTE0);
  153. rts = nrf_uarte_rts_pin_get(NRF_UARTE0);
  154. cts = nrf_uarte_cts_pin_get(NRF_UARTE0);
  155. nrf_uarte_txrx_pins_disconnect(NRF_UARTE0);
  156. nrf_uarte_hwfc_pins_disconnect(NRF_UARTE0);
  157. )
  158. CODE_FOR_UART
  159. (
  160. txd = nrf_uart_tx_pin_get(NRF_UART0);
  161. rxd = nrf_uart_rx_pin_get(NRF_UART0);
  162. rts = nrf_uart_rts_pin_get(NRF_UART0);
  163. cts = nrf_uart_cts_pin_get(NRF_UART0);
  164. nrf_uart_txrx_pins_disconnect(NRF_UART0);
  165. nrf_uart_hwfc_pins_disconnect(NRF_UART0);
  166. )
  167. nrf_gpio_cfg_default(txd);
  168. nrf_gpio_cfg_default(rxd);
  169. if (cts != NRF_UART_PSEL_DISCONNECTED)
  170. {
  171. nrf_gpio_cfg_default(cts);
  172. }
  173. if (rts != NRF_UART_PSEL_DISCONNECTED)
  174. {
  175. nrf_gpio_cfg_default(rts);
  176. }
  177. }
  178. __STATIC_INLINE void uart_enable(void)
  179. {
  180. CODE_FOR_UARTE(nrf_uarte_enable(NRF_UARTE0);)
  181. CODE_FOR_UART(nrf_uart_enable(NRF_UART0););
  182. }
  183. __STATIC_INLINE void uart_disable(void)
  184. {
  185. CODE_FOR_UARTE(nrf_uarte_disable(NRF_UARTE0);)
  186. CODE_FOR_UART(nrf_uart_disable(NRF_UART0););
  187. }
  188. ret_code_t nrf_drv_uart_init(nrf_drv_uart_config_t const * p_config,
  189. nrf_uart_event_handler_t event_handler)
  190. {
  191. if (m_cb.state != NRF_DRV_STATE_UNINITIALIZED)
  192. {
  193. return NRF_ERROR_INVALID_STATE;
  194. }
  195. if (p_config == NULL)
  196. {
  197. p_config = &m_default_config;
  198. }
  199. #if (defined(UARTE_IN_USE) && defined(UART_IN_USE))
  200. m_cb.use_easy_dma = p_config->use_easy_dma;
  201. #endif
  202. apply_config(p_config);
  203. m_cb.handler = event_handler;
  204. m_cb.p_context = p_config->p_context;
  205. if (m_cb.handler)
  206. {
  207. interrupts_enable(p_config->interrupt_priority);
  208. }
  209. uart_enable();
  210. m_cb.rx_buffer_length = 0;
  211. m_cb.rx_secondary_buffer_length = 0;
  212. m_cb.tx_buffer_length = 0;
  213. m_cb.state = NRF_DRV_STATE_INITIALIZED;
  214. m_cb.rx_enabled = false;
  215. return NRF_SUCCESS;
  216. }
  217. void nrf_drv_uart_uninit(void)
  218. {
  219. uart_disable();
  220. if (m_cb.handler)
  221. {
  222. interrupts_disable();
  223. }
  224. pins_to_default();
  225. m_cb.state = NRF_DRV_STATE_UNINITIALIZED;
  226. m_cb.handler = NULL;
  227. }
  228. #if defined(UART_IN_USE)
  229. __STATIC_INLINE void tx_byte(void)
  230. {
  231. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
  232. uint8_t txd = m_cb.p_tx_buffer[m_cb.tx_counter];
  233. m_cb.tx_counter++;
  234. nrf_uart_txd_set(NRF_UART0, txd);
  235. }
  236. __STATIC_INLINE ret_code_t nrf_drv_uart_tx_for_uart()
  237. {
  238. ret_code_t err_code = NRF_SUCCESS;
  239. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
  240. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTTX);
  241. tx_byte();
  242. if (m_cb.handler == NULL)
  243. {
  244. while (m_cb.tx_counter < (uint16_t) m_cb.tx_buffer_length)
  245. {
  246. while (!nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_TXDRDY) &&
  247. m_cb.tx_counter != TX_COUNTER_ABORT_REQ_VALUE)
  248. {
  249. }
  250. if (m_cb.tx_counter != TX_COUNTER_ABORT_REQ_VALUE)
  251. {
  252. tx_byte();
  253. }
  254. }
  255. if (m_cb.tx_counter == TX_COUNTER_ABORT_REQ_VALUE)
  256. {
  257. err_code = NRF_ERROR_FORBIDDEN;
  258. }
  259. else
  260. {
  261. while (!nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_TXDRDY))
  262. {
  263. }
  264. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPTX);
  265. }
  266. m_cb.tx_buffer_length = 0;
  267. }
  268. return err_code;
  269. }
  270. #endif
  271. #if defined(UARTE_IN_USE)
  272. __STATIC_INLINE ret_code_t nrf_drv_uart_tx_for_uarte()
  273. {
  274. ret_code_t err_code = NRF_SUCCESS;
  275. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDTX);
  276. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_TXSTOPPED);
  277. nrf_uarte_tx_buffer_set(NRF_UARTE0, m_cb.p_tx_buffer, m_cb.tx_buffer_length);
  278. nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STARTTX);
  279. if (m_cb.handler == NULL)
  280. {
  281. bool endtx;
  282. bool txstopped;
  283. do
  284. {
  285. endtx = nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ENDTX);
  286. txstopped = nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_TXSTOPPED);
  287. }
  288. while ((!endtx) && (!txstopped));
  289. if (txstopped)
  290. {
  291. err_code = NRF_ERROR_FORBIDDEN;
  292. }
  293. m_cb.tx_buffer_length = 0;
  294. }
  295. return err_code;
  296. }
  297. #endif
  298. ret_code_t nrf_drv_uart_tx(uint8_t const * const p_data, uint8_t length)
  299. {
  300. ASSERT(m_cb.state == NRF_DRV_STATE_INITIALIZED);
  301. ASSERT(length>0);
  302. ASSERT(p_data);
  303. CODE_FOR_UARTE
  304. (
  305. // EasyDMA requires that transfer buffers are placed in DataRAM,
  306. // signal error if the are not.
  307. if (!IS_EASY_DMA_RAM_ADDRESS(p_data))
  308. {
  309. return NRF_ERROR_INVALID_ADDR;
  310. }
  311. )
  312. if (nrf_drv_uart_tx_in_progress())
  313. {
  314. return NRF_ERROR_BUSY;
  315. }
  316. m_cb.tx_buffer_length = length;
  317. m_cb.p_tx_buffer = p_data;
  318. m_cb.tx_counter = 0;
  319. CODE_FOR_UARTE
  320. (
  321. return nrf_drv_uart_tx_for_uarte();
  322. )
  323. CODE_FOR_UART
  324. (
  325. return nrf_drv_uart_tx_for_uart();
  326. )
  327. }
  328. bool nrf_drv_uart_tx_in_progress(void)
  329. {
  330. return (m_cb.tx_buffer_length != 0);
  331. }
  332. #if defined(UART_IN_USE)
  333. __STATIC_INLINE void rx_enable(void)
  334. {
  335. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_ERROR);
  336. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY);
  337. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTRX);
  338. }
  339. __STATIC_INLINE void rx_byte(void)
  340. {
  341. if (!m_cb.rx_buffer_length)
  342. {
  343. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY);
  344. // Byte received when buffer is not set - data lost.
  345. (void) nrf_uart_rxd_get(NRF_UART0);
  346. return;
  347. }
  348. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY);
  349. m_cb.p_rx_buffer[m_cb.rx_counter] = nrf_uart_rxd_get(NRF_UART0);
  350. m_cb.rx_counter++;
  351. }
  352. __STATIC_INLINE ret_code_t nrf_drv_uart_rx_for_uart(uint8_t * p_data, uint8_t length, bool second_buffer)
  353. {
  354. if ((!m_cb.rx_enabled) && (!second_buffer))
  355. {
  356. rx_enable();
  357. }
  358. if (m_cb.handler == NULL)
  359. {
  360. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXTO);
  361. bool rxrdy;
  362. bool rxto;
  363. bool error;
  364. do
  365. {
  366. do
  367. {
  368. error = nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_ERROR);
  369. rxrdy = nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_RXDRDY);
  370. rxto = nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_RXTO);
  371. } while ((!rxrdy) && (!rxto) && (!error));
  372. if (error || rxto)
  373. {
  374. break;
  375. }
  376. rx_byte();
  377. } while (m_cb.rx_buffer_length > m_cb.rx_counter);
  378. m_cb.rx_buffer_length = 0;
  379. if (error)
  380. {
  381. return NRF_ERROR_INTERNAL;
  382. }
  383. if (rxto)
  384. {
  385. return NRF_ERROR_FORBIDDEN;
  386. }
  387. if (m_cb.rx_enabled)
  388. {
  389. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTRX);
  390. }
  391. else
  392. {
  393. // Skip stopping RX if driver is forced to be enabled.
  394. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
  395. }
  396. }
  397. else
  398. {
  399. nrf_uart_int_enable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  400. }
  401. return NRF_SUCCESS;
  402. }
  403. #endif
  404. #if defined(UARTE_IN_USE)
  405. __STATIC_INLINE ret_code_t nrf_drv_uart_rx_for_uarte(uint8_t * p_data, uint8_t length, bool second_buffer)
  406. {
  407. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDRX);
  408. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_RXTO);
  409. nrf_uarte_rx_buffer_set(NRF_UARTE0, p_data, length);
  410. if (!second_buffer)
  411. {
  412. nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STARTRX);
  413. }
  414. else
  415. {
  416. nrf_uarte_shorts_enable(NRF_UARTE0, NRF_UARTE_SHORT_ENDRX_STARTRX);
  417. }
  418. if (m_cb.handler == NULL)
  419. {
  420. bool endrx;
  421. bool rxto;
  422. bool error;
  423. do {
  424. endrx = nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ENDRX);
  425. rxto = nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_RXTO);
  426. error = nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ERROR);
  427. }while ((!endrx) && (!rxto) && (!error));
  428. m_cb.rx_buffer_length = 0;
  429. if (error)
  430. {
  431. return NRF_ERROR_INTERNAL;
  432. }
  433. if (rxto)
  434. {
  435. return NRF_ERROR_FORBIDDEN;
  436. }
  437. }
  438. else
  439. {
  440. nrf_uarte_int_enable(NRF_UARTE0, NRF_UARTE_INT_ERROR_MASK | NRF_UARTE_INT_ENDRX_MASK);
  441. }
  442. return NRF_SUCCESS;
  443. }
  444. #endif
  445. ret_code_t nrf_drv_uart_rx(uint8_t * p_data, uint8_t length)
  446. {
  447. ASSERT(m_cb.state == NRF_DRV_STATE_INITIALIZED);
  448. ASSERT(length>0);
  449. CODE_FOR_UARTE
  450. (
  451. // EasyDMA requires that transfer buffers are placed in DataRAM,
  452. // signal error if the are not.
  453. if (!IS_EASY_DMA_RAM_ADDRESS(p_data))
  454. {
  455. return NRF_ERROR_INVALID_ADDR;
  456. }
  457. )
  458. bool second_buffer = false;
  459. if (m_cb.handler)
  460. {
  461. CODE_FOR_UARTE
  462. (
  463. nrf_uarte_int_disable(NRF_UARTE0, NRF_UARTE_INT_ERROR_MASK | NRF_UARTE_INT_ENDRX_MASK);
  464. )
  465. CODE_FOR_UART
  466. (
  467. nrf_uart_int_disable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  468. )
  469. }
  470. if (m_cb.rx_buffer_length != 0)
  471. {
  472. if (m_cb.rx_secondary_buffer_length != 0)
  473. {
  474. if (m_cb.handler)
  475. {
  476. CODE_FOR_UARTE
  477. (
  478. nrf_uarte_int_enable(NRF_UARTE0, NRF_UARTE_INT_ERROR_MASK | NRF_UARTE_INT_ENDRX_MASK);
  479. )
  480. CODE_FOR_UART
  481. (
  482. nrf_uart_int_enable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  483. )
  484. }
  485. return NRF_ERROR_BUSY;
  486. }
  487. second_buffer = true;
  488. }
  489. if (!second_buffer)
  490. {
  491. m_cb.rx_buffer_length = length;
  492. m_cb.p_rx_buffer = p_data;
  493. m_cb.rx_counter = 0;
  494. m_cb.rx_secondary_buffer_length = 0;
  495. }
  496. else
  497. {
  498. m_cb.p_rx_secondary_buffer = p_data;
  499. m_cb.rx_secondary_buffer_length = length;
  500. }
  501. CODE_FOR_UARTE
  502. (
  503. return nrf_drv_uart_rx_for_uarte(p_data, length, second_buffer);
  504. )
  505. CODE_FOR_UART
  506. (
  507. return nrf_drv_uart_rx_for_uart(p_data, length, second_buffer);
  508. )
  509. }
  510. void nrf_drv_uart_rx_enable(void)
  511. {
  512. //Easy dma mode does not support enabling receiver without setting up buffer.
  513. CODE_FOR_UARTE
  514. (
  515. ASSERT(false);
  516. )
  517. CODE_FOR_UART
  518. (
  519. if (!m_cb.rx_enabled)
  520. {
  521. rx_enable();
  522. m_cb.rx_enabled = true;
  523. }
  524. )
  525. }
  526. void nrf_drv_uart_rx_disable(void)
  527. {
  528. //Easy dma mode does not support enabling receiver without setting up buffer.
  529. CODE_FOR_UARTE
  530. (
  531. ASSERT(false);
  532. )
  533. CODE_FOR_UART
  534. (
  535. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
  536. m_cb.rx_enabled = false;
  537. )
  538. }
  539. uint32_t nrf_drv_uart_errorsrc_get(void)
  540. {
  541. uint32_t errsrc;
  542. CODE_FOR_UARTE
  543. (
  544. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ERROR);
  545. errsrc = nrf_uarte_errorsrc_get_and_clear(NRF_UARTE0);
  546. )
  547. CODE_FOR_UART
  548. (
  549. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_ERROR);
  550. errsrc = nrf_uart_errorsrc_get_and_clear(NRF_UART0);
  551. )
  552. return errsrc;
  553. }
  554. __STATIC_INLINE void rx_done_event(uint8_t bytes, uint8_t * p_data)
  555. {
  556. nrf_drv_uart_event_t event;
  557. event.type = NRF_DRV_UART_EVT_RX_DONE;
  558. event.data.rxtx.bytes = bytes;
  559. event.data.rxtx.p_data = p_data;
  560. m_cb.handler(&event,m_cb.p_context);
  561. }
  562. __STATIC_INLINE void tx_done_event(uint8_t bytes)
  563. {
  564. nrf_drv_uart_event_t event;
  565. event.type = NRF_DRV_UART_EVT_TX_DONE;
  566. event.data.rxtx.bytes = bytes;
  567. event.data.rxtx.p_data = (uint8_t *)m_cb.p_tx_buffer;
  568. m_cb.tx_buffer_length = 0;
  569. m_cb.handler(&event,m_cb.p_context);
  570. }
  571. void nrf_drv_uart_tx_abort(void)
  572. {
  573. CODE_FOR_UARTE
  574. (
  575. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_TXSTOPPED);
  576. nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STOPTX);
  577. if (m_cb.handler == NULL)
  578. {
  579. while(!nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_TXSTOPPED));
  580. }
  581. )
  582. CODE_FOR_UART
  583. (
  584. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPTX);
  585. if (m_cb.handler)
  586. {
  587. tx_done_event(m_cb.tx_counter);
  588. }
  589. else
  590. {
  591. m_cb.tx_counter = TX_COUNTER_ABORT_REQ_VALUE;
  592. }
  593. )
  594. }
  595. void nrf_drv_uart_rx_abort(void)
  596. {
  597. CODE_FOR_UARTE
  598. (
  599. nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STOPRX);
  600. )
  601. CODE_FOR_UART
  602. (
  603. nrf_uart_int_disable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  604. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
  605. )
  606. }
  607. #if defined(UART_IN_USE)
  608. __STATIC_INLINE void uart_irq_handler()
  609. {
  610. if (nrf_uart_int_enable_check(NRF_UART0, NRF_UART_INT_MASK_ERROR) &&
  611. nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_ERROR))
  612. {
  613. nrf_drv_uart_event_t event;
  614. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_ERROR);
  615. nrf_uart_int_disable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  616. if (!m_cb.rx_enabled)
  617. {
  618. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
  619. }
  620. event.type = NRF_DRV_UART_EVT_ERROR;
  621. event.data.error.error_mask = nrf_uart_errorsrc_get_and_clear(NRF_UART0);
  622. event.data.error.rxtx.bytes = m_cb.rx_buffer_length;
  623. event.data.error.rxtx.p_data = m_cb.p_rx_buffer;
  624. //abort transfer
  625. m_cb.rx_buffer_length = 0;
  626. m_cb.rx_secondary_buffer_length = 0;
  627. m_cb.handler(&event,m_cb.p_context);
  628. }
  629. else if (nrf_uart_int_enable_check(NRF_UART0, NRF_UART_INT_MASK_RXDRDY) &&
  630. nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_RXDRDY))
  631. {
  632. rx_byte();
  633. if (m_cb.rx_buffer_length == m_cb.rx_counter)
  634. {
  635. if (m_cb.rx_secondary_buffer_length)
  636. {
  637. uint8_t * p_data = m_cb.p_rx_buffer;
  638. uint8_t rx_counter = m_cb.rx_counter;
  639. //Switch to secondary buffer.
  640. m_cb.rx_buffer_length = m_cb.rx_secondary_buffer_length;
  641. m_cb.p_rx_buffer = m_cb.p_rx_secondary_buffer;
  642. m_cb.rx_secondary_buffer_length = 0;
  643. m_cb.rx_counter = 0;
  644. rx_done_event(rx_counter, p_data);
  645. }
  646. else
  647. {
  648. if (!m_cb.rx_enabled)
  649. {
  650. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
  651. }
  652. nrf_uart_int_disable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY | NRF_UART_INT_MASK_ERROR);
  653. m_cb.rx_buffer_length = 0;
  654. rx_done_event(m_cb.rx_counter, m_cb.p_rx_buffer);
  655. }
  656. }
  657. }
  658. if (nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_TXDRDY))
  659. {
  660. if (m_cb.tx_counter < (uint16_t) m_cb.tx_buffer_length)
  661. {
  662. tx_byte();
  663. }
  664. else
  665. {
  666. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
  667. if (m_cb.tx_buffer_length)
  668. {
  669. tx_done_event(m_cb.tx_buffer_length);
  670. }
  671. }
  672. }
  673. if (nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_RXTO))
  674. {
  675. nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXTO);
  676. // RXTO event may be triggered as a result of abort call. In th
  677. if (m_cb.rx_enabled)
  678. {
  679. nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTRX);
  680. }
  681. if (m_cb.rx_buffer_length)
  682. {
  683. m_cb.rx_buffer_length = 0;
  684. rx_done_event(m_cb.rx_counter, m_cb.p_rx_buffer);
  685. }
  686. }
  687. }
  688. #endif
  689. #if defined(UARTE_IN_USE)
  690. __STATIC_INLINE void uarte_irq_handler()
  691. {
  692. if (nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ERROR))
  693. {
  694. nrf_drv_uart_event_t event;
  695. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ERROR);
  696. event.type = NRF_DRV_UART_EVT_ERROR;
  697. event.data.error.error_mask = nrf_uarte_errorsrc_get_and_clear(NRF_UARTE0);
  698. event.data.error.rxtx.bytes = nrf_uarte_rx_amount_get(NRF_UARTE0);
  699. event.data.error.rxtx.p_data = m_cb.p_rx_buffer;
  700. //abort transfer
  701. m_cb.rx_buffer_length = 0;
  702. m_cb.rx_secondary_buffer_length = 0;
  703. m_cb.handler(&event,m_cb.p_context);
  704. }
  705. else if (nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ENDRX))
  706. {
  707. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDRX);
  708. uint8_t amount = nrf_uarte_rx_amount_get(NRF_UARTE0);
  709. // If the transfer was stopped before completion, amount of transfered bytes
  710. // will not be equal to the buffer length. Interrupted trunsfer is ignored.
  711. if (amount == m_cb.rx_buffer_length)
  712. {
  713. if (m_cb.rx_secondary_buffer_length)
  714. {
  715. uint8_t * p_data = m_cb.p_rx_buffer;
  716. nrf_uarte_shorts_disable(NRF_UARTE0, NRF_UARTE_SHORT_ENDRX_STARTRX);
  717. m_cb.rx_buffer_length = m_cb.rx_secondary_buffer_length;
  718. m_cb.p_rx_buffer = m_cb.p_rx_secondary_buffer;
  719. m_cb.rx_secondary_buffer_length = 0;
  720. rx_done_event(amount, p_data);
  721. }
  722. else
  723. {
  724. m_cb.rx_buffer_length = 0;
  725. rx_done_event(amount, m_cb.p_rx_buffer);
  726. }
  727. }
  728. }
  729. if (nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_RXTO))
  730. {
  731. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_RXTO);
  732. if (m_cb.rx_buffer_length)
  733. {
  734. m_cb.rx_buffer_length = 0;
  735. rx_done_event(nrf_uarte_rx_amount_get(NRF_UARTE0), m_cb.p_rx_buffer);
  736. }
  737. }
  738. if (nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_ENDTX))
  739. {
  740. nrf_uarte_event_clear(NRF_UARTE0, NRF_UARTE_EVENT_ENDTX);
  741. if (m_cb.tx_buffer_length)
  742. {
  743. tx_done_event(nrf_uarte_tx_amount_get(NRF_UARTE0));
  744. }
  745. }
  746. }
  747. #endif
  748. void UART0_IRQHandler(void)
  749. {
  750. CODE_FOR_UARTE
  751. (
  752. uarte_irq_handler();
  753. )
  754. CODE_FOR_UART
  755. (
  756. uart_irq_handler();
  757. )
  758. }