nrf_libuarte.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_H
  41. #define NRF_LIBUARTE_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 libUARTE
  50. * @ingroup app_common
  51. *
  52. * @brief Module for reliable communication over UARTE.
  53. *
  54. * @{
  55. */
  56. typedef enum
  57. {
  58. NRF_LIBUARTE_EVT_RX_DATA, ///< Data received.
  59. NRF_LIBUARTE_EVT_RX_BUF_REQ, ///< Requesting new buffer for receiving data.
  60. NRF_LIBUARTE_EVT_TX_DONE, ///< Requested TX transfer completed.
  61. NRF_LIBUARTE_EVT_ERROR ///< Error reported by the UARTE peripheral.
  62. } nrf_libuarte_evt_type_t;
  63. /**
  64. * @brief PPI channels used by libuarte
  65. */
  66. typedef enum
  67. {
  68. NRF_LIBUARTE_PPI_CH_EXT_TRIGGER_STARTRX_EN_ENDRX_STARTX,
  69. NRF_LIBUARTE_PPI_CH_RXSTARTED_EXT_TSK,
  70. NRF_LIBUARTE_PPI_CH_EXT_STOP_STOPRX,
  71. NRF_LIBUARTE_PPI_CH_EXT_STOP_GROUPS_EN,
  72. NRF_LIBUARTE_PPI_CH_RXRDY_TIMER_COUNT,
  73. NRF_LIBUARTE_PPI_CH_RX_MAX,
  74. NRF_LIBUARTE_PPI_CH_ENDRX_STARTRX = NRF_LIBUARTE_PPI_CH_RX_MAX,
  75. NRF_LIBUARTE_PPI_CH_ENDRX_EXT_TSK,
  76. NRF_LIBUARTE_PPI_CH_RX_GROUP_MAX,
  77. NRF_LIBUARTE_PPI_CH_ENDTX_STARTTX = NRF_LIBUARTE_PPI_CH_RX_GROUP_MAX,
  78. NRF_LIBUARTE_PPI_CH_MAX
  79. } nrf_libuarte_ppi_channel_t;
  80. /**
  81. * @brief PPI groups used by libuarte
  82. */
  83. typedef enum
  84. {
  85. NRF_LIBUARTE_PPI_GROUP_ENDRX_STARTRX, ///< Group used for controlling PPI connection between ENDRX and STARTRX
  86. NRF_LIBUARTE_PPI_GROUP_ENDRX_EXT_RXDONE_TSK, ///< Group used for controlling PPI connection between ENDRX and RXDONE
  87. NRF_LIBUARTE_PPI_GROUP_MAX
  88. } nrf_libuarte_ppi_group_t;
  89. typedef struct
  90. {
  91. uint8_t * p_data; ///< Pointer to the data to be sent or received.
  92. size_t length; ///< Length of the data.
  93. } nrf_libuarte_data_t;
  94. typedef struct
  95. {
  96. nrf_libuarte_evt_type_t type; ///< Event type.
  97. union {
  98. nrf_libuarte_data_t rxtx; ///< Data provided for transfer completion events.
  99. } data;
  100. } nrf_libuarte_evt_t;
  101. typedef struct {
  102. uint32_t tx_pin; ///< TXD pin number.
  103. uint32_t rx_pin; ///< RXD pin number.
  104. uint32_t cts_pin; ///< CTS pin number.
  105. uint32_t rts_pin; ///< RTS pin number.
  106. uint32_t startrx_evt; ///< Event to trigger STARTRX task in UARTE.
  107. uint32_t endrx_evt; ///< Event to trigger STOPRX task in UARTE.
  108. uint32_t rxstarted_tsk; ///< Task to be triggered when RXSTARTED UARTE event occurs.
  109. uint32_t rxdone_tsk; ///< Task to be triggered when ENDRX UARTE event occurs.
  110. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  111. nrf_uarte_parity_t parity; ///< Parity configuration.
  112. nrf_uarte_baudrate_t baudrate; ///< Baud rate.
  113. uint8_t irq_priority; ///< Interrupt priority.
  114. } nrf_libuarte_config_t;
  115. typedef void (*nrf_libuarte_evt_handler_t)(void * context,
  116. nrf_libuarte_evt_t * p_evt);
  117. extern const IRQn_Type libuarte_irqn[];
  118. typedef struct {
  119. nrf_ppi_channel_t ppi_channels[NRF_LIBUARTE_PPI_CH_MAX];
  120. nrf_ppi_channel_group_t ppi_groups[NRF_LIBUARTE_PPI_GROUP_MAX];
  121. uint8_t * p_tx;
  122. size_t tx_len;
  123. size_t tx_cur_idx;
  124. uint8_t * p_cur_rx;
  125. uint8_t * p_next_rx;
  126. uint8_t * p_next_next_rx;
  127. nrf_libuarte_evt_handler_t evt_handler;
  128. uint32_t last_rx_byte_cnt;
  129. uint32_t last_pin_rx_byte_cnt;
  130. uint32_t chunk_size;
  131. void * context;
  132. uint16_t tx_chunk8;
  133. } nrf_libuarte_ctrl_blk_t;
  134. typedef struct {
  135. nrf_libuarte_ctrl_blk_t * ctrl_blk;
  136. nrfx_timer_t timer;
  137. NRF_UARTE_Type * uarte;
  138. } nrf_libuarte_t;
  139. #define NRF_LIBUARTE_DEFINE(_name, _uarte_idx, _timer_idx) \
  140. STATIC_ASSERT(_uarte_idx < UARTE_COUNT, "UARTE instance not present");\
  141. STATIC_ASSERT(CONCAT_2(NRF_LIBUARTE_UARTE,_uarte_idx) == 1, "UARTE instance not enabled");\
  142. STATIC_ASSERT(CONCAT_3(NRFX_TIMER,_timer_idx, _ENABLED) == 1, "Timer instance not enabled");\
  143. static nrf_libuarte_ctrl_blk_t CONCAT_2(_name, ctrl_blk); \
  144. static const nrf_libuarte_t _name = { \
  145. .ctrl_blk = &CONCAT_2(_name, ctrl_blk), \
  146. .timer = NRFX_TIMER_INSTANCE(_timer_idx), \
  147. .uarte = CONCAT_2(NRF_UARTE, _uarte_idx),\
  148. }
  149. /**
  150. * @brief Function for initializing the libUARTE library.
  151. *
  152. * @param[in] p_libuarte Pointer to libuarte instance.
  153. * @param[in] p_config Pointer to the structure with initial configuration.
  154. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  155. * @param[in] context User context passed in the callback.
  156. *
  157. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  158. */
  159. ret_code_t nrf_libuarte_init(const nrf_libuarte_t * const p_libuarte,
  160. nrf_libuarte_config_t * p_config,
  161. nrf_libuarte_evt_handler_t evt_handler, void * context);
  162. /**
  163. * @brief Function for uninitializing the libUARTE library.
  164. *
  165. * @param[in] p_libuarte Pointer to libuarte instance.
  166. */
  167. void nrf_libuarte_uninit(const nrf_libuarte_t * const p_libuarte);
  168. /**
  169. * @brief Function for sending data over UARTE using EasyDMA.
  170. *
  171. * @param[in] p_libuarte Pointer to libuarte instance.
  172. * @param[in] p_data Pointer to data.
  173. * @param[in] len Number of bytes to send.
  174. *
  175. * @retval NRF_ERROR_BUSY Data is transferring.
  176. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  177. * @retval NRF_SUCCESS Buffer set for sending.
  178. */
  179. ret_code_t nrf_libuarte_tx(const nrf_libuarte_t * const p_libuarte, uint8_t * p_data, size_t len);
  180. /**
  181. * @brief Function for starting receiving data with additional configuration of external
  182. * trigger to start receiving.
  183. *
  184. * @param p_libuarte Pointer to libuarte instance.
  185. * @param p_data Pointer to data.
  186. * @param len Number of bytes to receive. Maximum possible length is
  187. * dependent on the used SoC (see the MAXCNT register
  188. * description in the Product Specification). The library
  189. * checks it with an assertion.
  190. * @param ext_trigger_en True to disable immediate start.
  191. *
  192. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  193. * @retval NRF_SUCCESS Buffer set for receiving.
  194. */
  195. ret_code_t nrf_libuarte_rx_start(const nrf_libuarte_t * const p_libuarte,
  196. uint8_t * p_data, size_t len, bool ext_trigger_en);
  197. /**
  198. * @brief Function for setting a buffer for data that will be later received in UARTE.
  199. *
  200. * @param p_libuarte Pointer to libuarte instance.
  201. * @param p_data Pointer to data.
  202. * @param len Number of bytes to receive. Maximum possible length is
  203. * dependent on the used SoC (see the MAXCNT register
  204. * description in the Product Specification). The library
  205. * checks it with an assertion.
  206. */
  207. void nrf_libuarte_rx_buf_rsp(const nrf_libuarte_t * const p_libuarte,
  208. uint8_t * p_data, size_t len);
  209. /**
  210. * @brief Function for stopping receiving data over UARTE.
  211. *
  212. * @param p_libuarte Pointer to libuarte instance.
  213. */
  214. void nrf_libuarte_rx_stop(const nrf_libuarte_t * const p_libuarte);
  215. /** @} */
  216. #endif //NRF_libuarte_H