nrf_libuarte_async.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 UART_ASYNC_H
  41. #define UART_ASYNC_H
  42. #include <stdint.h>
  43. #include "sdk_errors.h"
  44. #include <hal/nrf_uarte.h>
  45. /** @brief Types of libuarte driver events. */
  46. typedef enum
  47. {
  48. NRF_LIBUARTE_ASYNC_EVT_RX_DATA, ///< Requested TX transfer completed.
  49. NRF_LIBUARTE_ASYNC_EVT_TX_DONE, ///< Requested RX transfer completed.
  50. NRF_LIBUARTE_ASYNC_EVT_ERROR ///< Error reported by UARTE peripheral.
  51. } nrf_libuarte_async_evt_type_t;
  52. /** @brief Structure for libuarte async transfer completion event. */
  53. typedef struct
  54. {
  55. uint8_t * p_data; ///< Pointer to memory used for transfer.
  56. size_t length; ///< Number of bytes transfered.
  57. } nrf_libuarte_async_data_t;
  58. /** @brief Structure for libuarte async configuration. */
  59. typedef struct
  60. {
  61. uint32_t rx_pin; ///< RXD pin number.
  62. uint32_t tx_pin; ///< TXD pin number.
  63. uint32_t cts_pin; ///< CTS pin number.
  64. uint32_t rts_pin; ///< RTS pin number.
  65. uint32_t timeout_us; ///< Receiver timeout in us unit.
  66. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  67. nrf_uarte_parity_t parity; ///< Parity configuration.
  68. nrf_uarte_baudrate_t baudrate; ///< Baudrate.
  69. } nrf_libuarte_async_config_t;
  70. /** @brief Structure for libuarte error event. */
  71. typedef struct
  72. {
  73. nrf_libuarte_async_evt_type_t type; ///< Event type.
  74. union {
  75. nrf_libuarte_async_data_t rxtx; ///< RXD/TXD data.
  76. } data; ///< Union with data.
  77. } nrf_libuarte_async_evt_t;
  78. /**
  79. * @brief Interrupt event handler.
  80. *
  81. * @param[in] p_evt Pointer to event structure. Event is allocated on the stack so it is available
  82. * only within the context of the event handler.
  83. */
  84. typedef void (*nrf_libuarte_async_evt_handler)(nrf_libuarte_async_evt_t * p_evt);
  85. /**
  86. * @brief Function for initializing the libuarte async library.
  87. *
  88. * @param[in] p_config Pointer to the structure with initial configuration.
  89. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  90. *
  91. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  92. */
  93. ret_code_t nrf_libuarte_async_init(nrf_libuarte_async_config_t const * p_config,
  94. nrf_libuarte_async_evt_handler evt_handler);
  95. /** @brief Function for uninitializing the libuarte async library */
  96. void nrf_libuarte_async_uninit(void);
  97. /**
  98. * @brief Funtrion for setting buffer and its size for received data.
  99. *
  100. * @param chunk_size Number of bytes in chunk of data
  101. */
  102. void nrf_libuarte_async_enable(size_t chunk_size);
  103. /**
  104. * @brief Function for sending data asynchronously over UARTE.
  105. *
  106. * @param[in] p_data Pointer to data.
  107. * @param[in] length Number of bytes to send. Maximum possible length is
  108. * dependent on the used SoC (see the MAXCNT register
  109. * description in the Product Specification). The library
  110. * checks it with assertion.
  111. *
  112. * @retval NRF_ERROR_BUSY Data is transferring.
  113. * @retval NRF_ERROR_INTERNAL Error during configuration.
  114. * @retval NRF_SUCCESS Buffer set for sending.
  115. */
  116. ret_code_t nrf_libuarte_async_tx(uint8_t * p_data, size_t length);
  117. /**
  118. * @brief Function for deallocating received buffer data.
  119. *
  120. * @param[in] p_data Pointer to data.
  121. * @param[in] length Number of bytes to free.
  122. */
  123. void nrf_libuarte_async_rx_free(uint8_t * p_data, size_t length);
  124. #endif //UART_ASYNC_H