nrf_libuarte_drv.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_LIBUARTE_DRV_H
  41. #define NRF_LIBUARTE_DRV_H
  42. #include "sdk_errors.h"
  43. #include "nrf_uarte.h"
  44. #include "nrfx_ppi.h"
  45. #include "nrfx_timer.h"
  46. #include <stdint.h>
  47. #include <stdbool.h>
  48. /**
  49. * @defgroup nrf_libuarte_drv libUARTE driver
  50. * @ingroup app_common
  51. *
  52. * @brief Module for reliable communication over UARTE.
  53. *
  54. * @{
  55. */
  56. /* Number of bytes available in the buffer when RTS line is set. */
  57. #define NRF_LIBUARTE_DRV_HWFC_BYTE_LIMIT 4
  58. typedef enum
  59. {
  60. NRF_LIBUARTE_DRV_EVT_RX_DATA, ///< Data received.
  61. NRF_LIBUARTE_DRV_EVT_RX_BUF_REQ, ///< Requesting new buffer for receiving data.
  62. NRF_LIBUARTE_DRV_EVT_TX_DONE, ///< Requested TX transfer completed.
  63. NRF_LIBUARTE_DRV_EVT_ERROR, ///< Error reported by the UARTE peripheral.
  64. NRF_LIBUARTE_DRV_EVT_OVERRUN_ERROR ///< Error reported by the driver.
  65. } nrf_libuarte_drv_evt_type_t;
  66. /**
  67. * @brief PPI channels used by libuarte
  68. */
  69. typedef enum
  70. {
  71. NRF_LIBUARTE_DRV_PPI_CH_EXT_TRIGGER_STARTRX_EN_ENDRX_STARTX,
  72. NRF_LIBUARTE_DRV_PPI_CH_RXSTARTED_EXT_TSK,
  73. NRF_LIBUARTE_DRV_PPI_CH_EXT_STOP_STOPRX,
  74. NRF_LIBUARTE_DRV_PPI_CH_EXT_STOP_GROUPS_EN,
  75. NRF_LIBUARTE_DRV_PPI_CH_RXRDY_TIMER_COUNT,
  76. NRF_LIBUARTE_DRV_PPI_CH_ENDRX_STARTRX,
  77. NRF_LIBUARTE_DRV_PPI_CH_ENDRX_EXT_TSK,
  78. NRF_LIBUARTE_DRV_PPI_CH_RTS_PIN,
  79. NRF_LIBUARTE_DRV_PPI_CH_RX_MAX,
  80. NRF_LIBUARTE_DRV_PPI_CH_RX_GROUP_MAX = NRF_LIBUARTE_DRV_PPI_CH_RX_MAX,
  81. NRF_LIBUARTE_DRV_PPI_CH_ENDTX_STARTTX = NRF_LIBUARTE_DRV_PPI_CH_RX_GROUP_MAX,
  82. NRF_LIBUARTE_DRV_PPI_CH_MAX
  83. } nrf_libuarte_drv_ppi_channel_t;
  84. /**
  85. * @brief PPI groups used by libuarte
  86. */
  87. typedef enum
  88. {
  89. NRF_LIBUARTE_DRV_PPI_GROUP_ENDRX_STARTRX, ///< Group used for controlling PPI connection between ENDRX and STARTRX
  90. NRF_LIBUARTE_DRV_PPI_GROUP_ENDRX_EXT_RXDONE_TSK, ///< Group used for controlling PPI connection between ENDRX and RXDONE
  91. NRF_LIBUARTE_DRV_PPI_GROUP_MAX
  92. } nrf_libuarte_drv_ppi_group_t;
  93. typedef struct
  94. {
  95. uint8_t * p_data; ///< Pointer to the data to be sent or received.
  96. size_t length; ///< Length of the data.
  97. } nrf_libuarte_drv_data_t;
  98. typedef struct
  99. {
  100. uint32_t overrun_length;
  101. } nrf_libuarte_drv_overrun_err_evt_t;
  102. typedef struct
  103. {
  104. nrf_libuarte_drv_evt_type_t type; ///< Event type.
  105. union {
  106. nrf_libuarte_drv_data_t rxtx; ///< Data provided for transfer completion events.
  107. uint8_t errorsrc; ///< Error source flags.
  108. nrf_libuarte_drv_overrun_err_evt_t overrun_err; ///< SW Error structure.
  109. } data;
  110. } nrf_libuarte_drv_evt_t;
  111. typedef struct {
  112. uint32_t tx_pin; ///< TXD pin number.
  113. uint32_t rx_pin; ///< RXD pin number.
  114. uint32_t cts_pin; ///< CTS pin number.
  115. uint32_t rts_pin; ///< RTS pin number.
  116. uint32_t startrx_evt; ///< Event to trigger STARTRX task in UARTE.
  117. uint32_t endrx_evt; ///< Event to trigger STOPRX task in UARTE.
  118. uint32_t rxstarted_tsk; ///< Task to be triggered when RXSTARTED UARTE event occurs.
  119. uint32_t rxdone_tsk; ///< Task to be triggered when ENDRX UARTE event occurs.
  120. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  121. nrf_uarte_parity_t parity; ///< Parity configuration.
  122. nrf_uarte_baudrate_t baudrate; ///< Baud rate.
  123. uint8_t irq_priority; ///< Interrupt priority.
  124. bool pullup_rx; ///< Pull up on RX pin.
  125. } nrf_libuarte_drv_config_t;
  126. typedef void (*nrf_libuarte_drv_evt_handler_t)(void * context,
  127. nrf_libuarte_drv_evt_t * p_evt);
  128. extern const IRQn_Type libuarte_irqn[];
  129. typedef struct {
  130. nrf_ppi_channel_t ppi_channels[NRF_LIBUARTE_DRV_PPI_CH_MAX];
  131. nrf_ppi_channel_group_t ppi_groups[NRF_LIBUARTE_DRV_PPI_GROUP_MAX];
  132. uint8_t * p_tx;
  133. size_t tx_len;
  134. size_t tx_cur_idx;
  135. uint8_t * p_cur_rx;
  136. uint8_t * p_next_rx;
  137. uint8_t * p_next_next_rx;
  138. nrf_libuarte_drv_evt_handler_t evt_handler;
  139. uint32_t last_rx_byte_cnt;
  140. uint32_t last_pin_rx_byte_cnt;
  141. uint32_t chunk_size;
  142. void * context;
  143. uint16_t tx_chunk8;
  144. uint8_t rts_pin;
  145. bool rts_manual;
  146. bool enabled;
  147. } nrf_libuarte_drv_ctrl_blk_t;
  148. typedef struct {
  149. nrf_libuarte_drv_ctrl_blk_t * ctrl_blk;
  150. nrfx_timer_t timer;
  151. NRF_UARTE_Type * uarte;
  152. } nrf_libuarte_drv_t;
  153. #define NRF_LIBUARTE_DRV_DEFINE(_name, _uarte_idx, _timer_idx) \
  154. STATIC_ASSERT(_uarte_idx < UARTE_COUNT, "UARTE instance not present");\
  155. STATIC_ASSERT(CONCAT_2(NRF_LIBUARTE_DRV_UARTE,_uarte_idx) == 1, "UARTE instance not enabled");\
  156. STATIC_ASSERT(CONCAT_3(NRFX_TIMER,_timer_idx, _ENABLED) == 1, "Timer instance not enabled");\
  157. static nrf_libuarte_drv_ctrl_blk_t CONCAT_2(_name, ctrl_blk); \
  158. static const nrf_libuarte_drv_t _name = { \
  159. .ctrl_blk = &CONCAT_2(_name, ctrl_blk), \
  160. .timer = NRFX_TIMER_INSTANCE(_timer_idx), \
  161. .uarte = CONCAT_2(NRF_UARTE, _uarte_idx),\
  162. }
  163. /**
  164. * @brief Function for initializing the libUARTE library.
  165. *
  166. * @param[in] p_libuarte Pointer to libuarte instance.
  167. * @param[in] p_config Pointer to the structure with initial configuration.
  168. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  169. * @param[in] context User context passed in the callback.
  170. *
  171. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  172. */
  173. ret_code_t nrf_libuarte_drv_init(const nrf_libuarte_drv_t * const p_libuarte,
  174. nrf_libuarte_drv_config_t * p_config,
  175. nrf_libuarte_drv_evt_handler_t evt_handler, void * context);
  176. /**
  177. * @brief Function for uninitializing the libUARTE library.
  178. *
  179. * @param[in] p_libuarte Pointer to libuarte instance.
  180. */
  181. void nrf_libuarte_drv_uninit(const nrf_libuarte_drv_t * const p_libuarte);
  182. /**
  183. * @brief Function for sending data over UARTE using EasyDMA.
  184. *
  185. * @param[in] p_libuarte Pointer to libuarte instance.
  186. * @param[in] p_data Pointer to data.
  187. * @param[in] len Number of bytes to send.
  188. *
  189. * @retval NRF_ERROR_BUSY Data is transferring.
  190. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  191. * @retval NRF_SUCCESS Buffer set for sending.
  192. */
  193. ret_code_t nrf_libuarte_drv_tx(const nrf_libuarte_drv_t * const p_libuarte,
  194. uint8_t * p_data, size_t len);
  195. /**
  196. * @brief Function for starting receiving data with additional configuration of external
  197. * trigger to start receiving.
  198. *
  199. * @param p_libuarte Pointer to libuarte instance.
  200. * @param p_data Pointer to data.
  201. * @param len Number of bytes to receive. Maximum possible length is
  202. * dependent on the used SoC (see the MAXCNT register
  203. * description in the Product Specification). The library
  204. * checks it with an assertion.
  205. * @param ext_trigger_en True to disable immediate start.
  206. *
  207. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  208. * @retval NRF_SUCCESS Buffer set for receiving.
  209. */
  210. ret_code_t nrf_libuarte_drv_rx_start(const nrf_libuarte_drv_t * const p_libuarte,
  211. uint8_t * p_data, size_t len, bool ext_trigger_en);
  212. /**
  213. * @brief Function for setting a buffer for data that will be later received in UARTE.
  214. *
  215. * @param p_libuarte Pointer to libuarte instance.
  216. * @param p_data Pointer to data.
  217. * @param len Number of bytes to receive. Maximum possible length is
  218. * dependent on the used SoC (see the MAXCNT register
  219. * description in the Product Specification). The library
  220. * checks it with an assertion.
  221. */
  222. void nrf_libuarte_drv_rx_buf_rsp(const nrf_libuarte_drv_t * const p_libuarte,
  223. uint8_t * p_data, size_t len);
  224. /**
  225. * @brief Function for stopping receiving data over UARTE.
  226. *
  227. * @param p_libuarte Pointer to libuarte instance.
  228. */
  229. void nrf_libuarte_drv_rx_stop(const nrf_libuarte_drv_t * const p_libuarte);
  230. /**
  231. * @brief Function for deasserting RTS to pause the transmission.
  232. *
  233. * Flow control must be enabled.
  234. *
  235. * @param p_libuarte Pointer to libuarte instance.
  236. */
  237. void nrf_libuarte_drv_rts_clear(const nrf_libuarte_drv_t * const p_libuarte);
  238. /**
  239. * @brief Function for asserting RTS to restart the transmission.
  240. *
  241. * Flow control must be enabled.
  242. *
  243. * @param p_libuarte Pointer to libuarte instance.
  244. */
  245. void nrf_libuarte_drv_rts_set(const nrf_libuarte_drv_t * const p_libuarte);
  246. /** @} */
  247. #endif //NRF_LIBUARTE_DRV_H