nrf_libuarte_drv.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Copyright (c) 2018 - 2019, 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. } nrf_libuarte_drv_ctrl_blk_t;
  147. typedef struct {
  148. nrf_libuarte_drv_ctrl_blk_t * ctrl_blk;
  149. nrfx_timer_t timer;
  150. NRF_UARTE_Type * uarte;
  151. } nrf_libuarte_drv_t;
  152. #define NRF_LIBUARTE_DRV_DEFINE(_name, _uarte_idx, _timer_idx) \
  153. STATIC_ASSERT(_uarte_idx < UARTE_COUNT, "UARTE instance not present");\
  154. STATIC_ASSERT(CONCAT_2(NRF_LIBUARTE_DRV_UARTE,_uarte_idx) == 1, "UARTE instance not enabled");\
  155. STATIC_ASSERT(CONCAT_3(NRFX_TIMER,_timer_idx, _ENABLED) == 1, "Timer instance not enabled");\
  156. static nrf_libuarte_drv_ctrl_blk_t CONCAT_2(_name, ctrl_blk); \
  157. static const nrf_libuarte_drv_t _name = { \
  158. .ctrl_blk = &CONCAT_2(_name, ctrl_blk), \
  159. .timer = NRFX_TIMER_INSTANCE(_timer_idx), \
  160. .uarte = CONCAT_2(NRF_UARTE, _uarte_idx),\
  161. }
  162. /**
  163. * @brief Function for initializing the libUARTE library.
  164. *
  165. * @param[in] p_libuarte Pointer to libuarte instance.
  166. * @param[in] p_config Pointer to the structure with initial configuration.
  167. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  168. * @param[in] context User context passed in the callback.
  169. *
  170. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  171. */
  172. ret_code_t nrf_libuarte_drv_init(const nrf_libuarte_drv_t * const p_libuarte,
  173. nrf_libuarte_drv_config_t * p_config,
  174. nrf_libuarte_drv_evt_handler_t evt_handler, void * context);
  175. /**
  176. * @brief Function for uninitializing the libUARTE library.
  177. *
  178. * @param[in] p_libuarte Pointer to libuarte instance.
  179. */
  180. void nrf_libuarte_drv_uninit(const nrf_libuarte_drv_t * const p_libuarte);
  181. /**
  182. * @brief Function for sending data over UARTE using EasyDMA.
  183. *
  184. * @param[in] p_libuarte Pointer to libuarte instance.
  185. * @param[in] p_data Pointer to data.
  186. * @param[in] len Number of bytes to send.
  187. *
  188. * @retval NRF_ERROR_BUSY Data is transferring.
  189. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  190. * @retval NRF_SUCCESS Buffer set for sending.
  191. */
  192. ret_code_t nrf_libuarte_drv_tx(const nrf_libuarte_drv_t * const p_libuarte,
  193. uint8_t * p_data, size_t len);
  194. /**
  195. * @brief Function for starting receiving data with additional configuration of external
  196. * trigger to start receiving.
  197. *
  198. * @param p_libuarte Pointer to libuarte instance.
  199. * @param p_data Pointer to data.
  200. * @param len Number of bytes to receive. Maximum possible length is
  201. * dependent on the used SoC (see the MAXCNT register
  202. * description in the Product Specification). The library
  203. * checks it with an assertion.
  204. * @param ext_trigger_en True to disable immediate start.
  205. *
  206. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  207. * @retval NRF_SUCCESS Buffer set for receiving.
  208. */
  209. ret_code_t nrf_libuarte_drv_rx_start(const nrf_libuarte_drv_t * const p_libuarte,
  210. uint8_t * p_data, size_t len, bool ext_trigger_en);
  211. /**
  212. * @brief Function for setting a buffer for data that will be later received in UARTE.
  213. *
  214. * @param p_libuarte Pointer to libuarte instance.
  215. * @param p_data Pointer to data.
  216. * @param len Number of bytes to receive. Maximum possible length is
  217. * dependent on the used SoC (see the MAXCNT register
  218. * description in the Product Specification). The library
  219. * checks it with an assertion.
  220. */
  221. void nrf_libuarte_drv_rx_buf_rsp(const nrf_libuarte_drv_t * const p_libuarte,
  222. uint8_t * p_data, size_t len);
  223. /**
  224. * @brief Function for stopping receiving data over UARTE.
  225. *
  226. * @param p_libuarte Pointer to libuarte instance.
  227. */
  228. void nrf_libuarte_drv_rx_stop(const nrf_libuarte_drv_t * const p_libuarte);
  229. /**
  230. * @brief Function for deasserting RTS to pause the transmission.
  231. *
  232. * Flow control must be enabled.
  233. *
  234. * @param p_libuarte Pointer to libuarte instance.
  235. */
  236. void nrf_libuarte_drv_rts_clear(const nrf_libuarte_drv_t * const p_libuarte);
  237. /**
  238. * @brief Function for asserting RTS to restart the transmission.
  239. *
  240. * Flow control must be enabled.
  241. *
  242. * @param p_libuarte Pointer to libuarte instance.
  243. */
  244. void nrf_libuarte_drv_rts_set(const nrf_libuarte_drv_t * const p_libuarte);
  245. /** @} */
  246. #endif //NRF_LIBUARTE_DRV_H