nrf_libuarte.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2018, 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 <stdint.h>
  45. #include <stdbool.h>
  46. /**
  47. * @defgroup nrf_libuarte libUARTE
  48. * @ingroup app_common
  49. *
  50. * @brief Module for reliable communication over UARTE.
  51. *
  52. * @{
  53. */
  54. typedef enum
  55. {
  56. NRF_LIBUARTE_EVT_RX_DATA, ///< Data received.
  57. NRF_LIBUARTE_EVT_RX_BUF_REQ, ///< Requesting new buffer for receiving data.
  58. NRF_LIBUARTE_EVT_TX_DONE, ///< Requested TX transfer completed.
  59. NRF_LIBUARTE_EVT_ERROR ///< Error reported by the UARTE peripheral.
  60. } nrf_libuarte_evt_type_t;
  61. typedef struct
  62. {
  63. uint8_t * p_data; ///< Pointer to the data to be sent or received.
  64. size_t length; ///< Length of the data.
  65. } nrf_libuarte_data_t;
  66. typedef struct
  67. {
  68. nrf_libuarte_evt_type_t type; ///< Event type.
  69. union {
  70. nrf_libuarte_data_t rxtx; ///< Data provided for transfer completion events.
  71. } data;
  72. } nrf_libuarte_evt_t;
  73. typedef struct {
  74. uint32_t tx_pin; ///< TXD pin number.
  75. uint32_t rx_pin; ///< RXD pin number.
  76. uint32_t cts_pin; ///< CTS pin number.
  77. uint32_t rts_pin; ///< RTS pin number.
  78. uint32_t startrx_evt; ///< Event to trigger STARTRX task in UARTE.
  79. uint32_t endrx_evt; ///< Event to trigger STOPRX task in UARTE.
  80. uint32_t rxstarted_tsk; ///< Task to be triggered when RXSTARTED UARTE event occurs.
  81. uint32_t rxdone_tsk; ///< Task to be triggered when ENDRX UARTE event occurs.
  82. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  83. nrf_uarte_parity_t parity; ///< Parity configuration.
  84. nrf_uarte_baudrate_t baudrate; ///< Baud rate.
  85. uint8_t irq_priority; ///< Interrupt priority.
  86. } nrf_libuarte_config_t;
  87. typedef void (*nrf_libuarte_evt_handler_t)(nrf_libuarte_evt_t * p_evt);
  88. /**
  89. * @brief Function for initializing the libUARTE library.
  90. *
  91. * @param[in] p_config Pointer to the structure with initial configuration.
  92. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  93. *
  94. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  95. */
  96. ret_code_t nrf_libuarte_init(nrf_libuarte_config_t * p_config, nrf_libuarte_evt_handler_t evt_handler);
  97. /** @brief Function for uninitializing the libUARTE library. */
  98. void nrf_libuarte_uninit(void);
  99. /**
  100. * @brief Function for sending data over UARTE using EasyDMA.
  101. *
  102. * @param[in] p_data Pointer to data.
  103. * @param[in] len Number of bytes to send.
  104. *
  105. * @retval NRF_ERROR_BUSY Data is transferring.
  106. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  107. * @retval NRF_SUCCESS Buffer set for sending.
  108. */
  109. ret_code_t nrf_libuarte_tx(uint8_t * p_data, size_t len);
  110. /**
  111. * @brief Function for starting receiving data with additional configuration of external
  112. * trigger to start receiving.
  113. *
  114. * @param p_data Pointer to data.
  115. * @param len Number of bytes to receive. Maximum possible length is
  116. * dependent on the used SoC (see the MAXCNT register
  117. * description in the Product Specification). The library
  118. * checks it with an assertion.
  119. * @param ext_trigger_en True to disable immediate start.
  120. *
  121. * @retval NRF_ERROR_INTERNAL Error during PPI channel configuration.
  122. * @retval NRF_SUCCESS Buffer set for receiving.
  123. */
  124. ret_code_t nrf_libuarte_rx_start(uint8_t * p_data, size_t len, bool ext_trigger_en);
  125. /**
  126. * @brief Function for setting a buffer for data that will be later received in UARTE.
  127. *
  128. * @param p_data Pointer to data.
  129. * @param len Number of bytes to receive. Maximum possible length is
  130. * dependent on the used SoC (see the MAXCNT register
  131. * description in the Product Specification). The library
  132. * checks it with an assertion.
  133. */
  134. void nrf_libuarte_rx_buf_rsp(uint8_t * p_data, size_t len);
  135. /** @brief Function for stopping receiving data over UARTE. */
  136. void nrf_libuarte_rx_stop(void);
  137. /** @} */
  138. #endif //NRF_libuarte_H