app_uart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Copyright (c) 2013 - 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. /**@file
  41. *
  42. * @defgroup app_uart UART module
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief UART module interface.
  47. */
  48. #ifndef APP_UART_H__
  49. #define APP_UART_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "app_util_platform.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define UART_PIN_DISCONNECTED 0xFFFFFFFF /**< Value indicating that no pin is connected to this UART register. */
  57. /**@brief UART Flow Control modes for the peripheral.
  58. */
  59. typedef enum
  60. {
  61. APP_UART_FLOW_CONTROL_DISABLED, /**< UART Hw Flow Control is disabled. */
  62. APP_UART_FLOW_CONTROL_ENABLED, /**< Standard UART Hw Flow Control is enabled. */
  63. } app_uart_flow_control_t;
  64. /**@brief UART communication structure holding configuration settings for the peripheral.
  65. */
  66. typedef struct
  67. {
  68. uint32_t rx_pin_no; /**< RX pin number. */
  69. uint32_t tx_pin_no; /**< TX pin number. */
  70. uint32_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */
  71. uint32_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */
  72. app_uart_flow_control_t flow_control; /**< Flow control setting, if flow control is used, the system will use low power UART mode, based on CTS signal. */
  73. bool use_parity; /**< Even parity if TRUE, no parity if FALSE. */
  74. uint32_t baud_rate; /**< Baud rate configuration. */
  75. } app_uart_comm_params_t;
  76. /**@brief UART buffer for transmitting/receiving data.
  77. */
  78. typedef struct
  79. {
  80. uint8_t * rx_buf; /**< Pointer to the RX buffer. */
  81. uint32_t rx_buf_size; /**< Size of the RX buffer. */
  82. uint8_t * tx_buf; /**< Pointer to the TX buffer. */
  83. uint32_t tx_buf_size; /**< Size of the TX buffer. */
  84. } app_uart_buffers_t;
  85. /**@brief Enumeration which defines events used by the UART module upon data reception or error.
  86. *
  87. * @details The event type is used to indicate the type of additional information in the event
  88. * @ref app_uart_evt_t.
  89. */
  90. typedef enum
  91. {
  92. APP_UART_DATA_READY, /**< An event indicating that UART data has been received. The data is available in the FIFO and can be fetched using @ref app_uart_get. */
  93. APP_UART_FIFO_ERROR, /**< An error in the FIFO module used by the app_uart module has occured. The FIFO error code is stored in app_uart_evt_t.data.error_code field. */
  94. APP_UART_COMMUNICATION_ERROR, /**< An communication error has occured during reception. The error is stored in app_uart_evt_t.data.error_communication field. */
  95. APP_UART_TX_EMPTY, /**< An event indicating that UART has completed transmission of all available data in the TX FIFO. */
  96. APP_UART_DATA, /**< An event indicating that UART data has been received, and data is present in data field. This event is only used when no FIFO is configured. */
  97. } app_uart_evt_type_t;
  98. /**@brief Struct containing events from the UART module.
  99. *
  100. * @details The app_uart_evt_t is used to notify the application of asynchronous events when data
  101. * are received on the UART peripheral or in case an error occured during data reception.
  102. */
  103. typedef struct
  104. {
  105. app_uart_evt_type_t evt_type; /**< Type of event. */
  106. union
  107. {
  108. uint32_t error_communication; /**< Field used if evt_type is: APP_UART_COMMUNICATION_ERROR. This field contains the value in the ERRORSRC register for the UART peripheral. The UART_ERRORSRC_x defines from nrf5x_bitfields.h can be used to parse the error code. See also the \nRFXX Series Reference Manual for specification. */
  109. uint32_t error_code; /**< Field used if evt_type is: NRF_ERROR_x. Additional status/error code if the error event type is APP_UART_FIFO_ERROR. This error code refer to errors defined in nrf_error.h. */
  110. uint8_t value; /**< Field used if evt_type is: NRF_ERROR_x. Additional status/error code if the error event type is APP_UART_FIFO_ERROR. This error code refer to errors defined in nrf_error.h. */
  111. } data;
  112. } app_uart_evt_t;
  113. /**@brief Function for handling app_uart event callback.
  114. *
  115. * @details Upon an event in the app_uart module this callback function will be called to notify
  116. * the application about the event.
  117. *
  118. * @param[in] p_app_uart_event Pointer to UART event.
  119. */
  120. typedef void (* app_uart_event_handler_t) (app_uart_evt_t * p_app_uart_event);
  121. /**@brief Macro for safe initialization of the UART module in a single user instance when using
  122. * a FIFO together with UART.
  123. *
  124. * @param[in] P_COMM_PARAMS Pointer to a UART communication structure: app_uart_comm_params_t
  125. * @param[in] RX_BUF_SIZE Size of desired RX buffer, must be a power of 2 or ZERO (No FIFO).
  126. * @param[in] TX_BUF_SIZE Size of desired TX buffer, must be a power of 2 or ZERO (No FIFO).
  127. * @param[in] EVT_HANDLER Event handler function to be called when an event occurs in the
  128. * UART module.
  129. * @param[in] IRQ_PRIO IRQ priority, app_irq_priority_t, for the UART module irq handler.
  130. * @param[out] ERR_CODE The return value of the UART initialization function will be
  131. * written to this parameter.
  132. *
  133. * @note Since this macro allocates a buffer and registers the module as a GPIOTE user when flow
  134. * control is enabled, it must only be called once.
  135. */
  136. #define APP_UART_FIFO_INIT(P_COMM_PARAMS, RX_BUF_SIZE, TX_BUF_SIZE, EVT_HANDLER, IRQ_PRIO, ERR_CODE) \
  137. do \
  138. { \
  139. app_uart_buffers_t buffers; \
  140. static uint8_t rx_buf[RX_BUF_SIZE]; \
  141. static uint8_t tx_buf[TX_BUF_SIZE]; \
  142. \
  143. buffers.rx_buf = rx_buf; \
  144. buffers.rx_buf_size = sizeof (rx_buf); \
  145. buffers.tx_buf = tx_buf; \
  146. buffers.tx_buf_size = sizeof (tx_buf); \
  147. ERR_CODE = app_uart_init(P_COMM_PARAMS, &buffers, EVT_HANDLER, IRQ_PRIO); \
  148. } while (0)
  149. /**@brief Macro for safe initialization of the UART module in a single user instance.
  150. *
  151. * @param[in] P_COMM_PARAMS Pointer to a UART communication structure: app_uart_comm_params_t
  152. * @param[in] EVT_HANDLER Event handler function to be called when an event occurs in the
  153. * UART module.
  154. * @param[in] IRQ_PRIO IRQ priority, app_irq_priority_t, for the UART module irq handler.
  155. * @param[out] ERR_CODE The return value of the UART initialization function will be
  156. * written to this parameter.
  157. *
  158. * @note Since this macro allocates registers the module as a GPIOTE user when flow control is
  159. * enabled, it must only be called once.
  160. */
  161. #define APP_UART_INIT(P_COMM_PARAMS, EVT_HANDLER, IRQ_PRIO, ERR_CODE) \
  162. do \
  163. { \
  164. ERR_CODE = app_uart_init(P_COMM_PARAMS, NULL, EVT_HANDLER, IRQ_PRIO); \
  165. } while (0)
  166. /**@brief Function for initializing the UART module. Use this initialization when several instances of the UART
  167. * module are needed.
  168. *
  169. *
  170. * @note Normally single initialization should be done using the APP_UART_INIT() or
  171. * APP_UART_INIT_FIFO() macro depending on whether the FIFO should be used by the UART, as
  172. * that will allocate the buffers needed by the UART module (including aligning the buffer
  173. * correctly).
  174. * @param[in] p_comm_params Pin and communication parameters.
  175. * @param[in] p_buffers RX and TX buffers, NULL is FIFO is not used.
  176. * @param[in] error_handler Function to be called in case of an error.
  177. * @param[in] irq_priority Interrupt priority level.
  178. *
  179. * @retval NRF_SUCCESS If successful initialization.
  180. * @retval NRF_ERROR_INVALID_LENGTH If a provided buffer is not a power of two.
  181. * @retval NRF_ERROR_NULL If one of the provided buffers is a NULL pointer.
  182. *
  183. * The below errors are propagated by the UART module to the caller upon registration when Hardware
  184. * Flow Control is enabled. When Hardware Flow Control is not used, these errors cannot occur.
  185. * @retval NRF_ERROR_INVALID_STATE The GPIOTE module is not in a valid state when registering
  186. * the UART module as a user.
  187. * @retval NRF_ERROR_INVALID_PARAM The UART module provides an invalid callback function when
  188. * registering the UART module as a user.
  189. * Or the value pointed to by *p_uart_uid is not a valid
  190. * GPIOTE number.
  191. * @retval NRF_ERROR_NO_MEM GPIOTE module has reached the maximum number of users.
  192. */
  193. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  194. app_uart_buffers_t * p_buffers,
  195. app_uart_event_handler_t error_handler,
  196. app_irq_priority_t irq_priority);
  197. /**@brief Function for getting a byte from the UART.
  198. *
  199. * @details This function will get the next byte from the RX buffer. If the RX buffer is empty
  200. * an error code will be returned and the app_uart module will generate an event upon
  201. * reception of the first byte which is added to the RX buffer.
  202. *
  203. * @param[out] p_byte Pointer to an address where next byte received on the UART will be copied.
  204. *
  205. * @retval NRF_SUCCESS If a byte has been received and pushed to the pointer provided.
  206. * @retval NRF_ERROR_NOT_FOUND If no byte is available in the RX buffer of the app_uart module.
  207. */
  208. uint32_t app_uart_get(uint8_t * p_byte);
  209. /**@brief Function for putting a byte on the UART.
  210. *
  211. * @details This call is non-blocking.
  212. *
  213. * @param[in] byte Byte to be transmitted on the UART.
  214. *
  215. * @retval NRF_SUCCESS If the byte was successfully put on the TX buffer for transmission.
  216. * @retval NRF_ERROR_NO_MEM If no more space is available in the TX buffer.
  217. * NRF_ERROR_NO_MEM may occur if flow control is enabled and CTS signal
  218. * is high for a long period and the buffer fills up.
  219. * @retval NRF_ERROR_INTERNAL If UART driver reported error.
  220. */
  221. uint32_t app_uart_put(uint8_t byte);
  222. /**@brief Function for flushing the RX and TX buffers (Only valid if FIFO is used).
  223. * This function does nothing if FIFO is not used.
  224. *
  225. * @retval NRF_SUCCESS Flushing completed (Current implementation will always succeed).
  226. */
  227. uint32_t app_uart_flush(void);
  228. /**@brief Function for closing the UART module.
  229. *
  230. * @retval NRF_SUCCESS If successfully closed.
  231. * @retval NRF_ERROR_INVALID_PARAM If an invalid user id is provided or the user id differs from
  232. * the current active user.
  233. */
  234. uint32_t app_uart_close(void);
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif //APP_UART_H__
  239. /** @} */