app_uart.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup app_uart UART module
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief UART module interface.
  19. */
  20. #ifndef APP_UART_H__
  21. #define APP_UART_H__
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include "app_util_platform.h"
  25. #define UART_PIN_DISCONNECTED 0xFFFFFFFF /**< Value indicating that no pin is connected to this UART register. */
  26. /**@brief UART Flow Control modes for the peripheral.
  27. */
  28. typedef enum
  29. {
  30. APP_UART_FLOW_CONTROL_DISABLED, /**< UART Hw Flow Control is disabled. */
  31. APP_UART_FLOW_CONTROL_ENABLED, /**< Standard UART Hw Flow Control is enabled. */
  32. APP_UART_FLOW_CONTROL_LOW_POWER /**< Specialized UART Hw Flow Control is used. The Low Power setting allows the \nRFXX to Power Off the UART module when CTS is in-active, and re-enabling the UART when the CTS signal becomes active. This allows the \nRFXX to safe power by only using the UART module when it is needed by the remote site. */
  33. } app_uart_flow_control_t;
  34. /**@brief UART communication structure holding configuration settings for the peripheral.
  35. */
  36. typedef struct
  37. {
  38. uint8_t rx_pin_no; /**< RX pin number. */
  39. uint8_t tx_pin_no; /**< TX pin number. */
  40. uint8_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */
  41. uint8_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */
  42. 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. */
  43. bool use_parity; /**< Even parity if TRUE, no parity if FALSE. */
  44. uint32_t baud_rate; /**< Baud rate configuration. */
  45. } app_uart_comm_params_t;
  46. /**@brief UART buffer for transmitting/receiving data.
  47. */
  48. typedef struct
  49. {
  50. uint8_t * rx_buf; /**< Pointer to the RX buffer. */
  51. uint32_t rx_buf_size; /**< Size of the RX buffer. */
  52. uint8_t * tx_buf; /**< Pointer to the TX buffer. */
  53. uint32_t tx_buf_size; /**< Size of the TX buffer. */
  54. } app_uart_buffers_t;
  55. /**@brief Enumeration which defines events used by the UART module upon data reception or error.
  56. *
  57. * @details The event type is used to indicate the type of additional information in the event
  58. * @ref app_uart_evt_t.
  59. */
  60. typedef enum
  61. {
  62. 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. */
  63. 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. */
  64. APP_UART_COMMUNICATION_ERROR, /**< An communication error has occured during reception. The error is stored in app_uart_evt_t.data.error_communication field. */
  65. APP_UART_TX_EMPTY, /**< An event indicating that UART has completed transmission of all available data in the TX FIFO. */
  66. 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. */
  67. } app_uart_evt_type_t;
  68. /**@brief Struct containing events from the UART module.
  69. *
  70. * @details The app_uart_evt_t is used to notify the application of asynchronous events when data
  71. * are received on the UART peripheral or in case an error occured during data reception.
  72. */
  73. typedef struct
  74. {
  75. app_uart_evt_type_t evt_type; /**< Type of event. */
  76. union
  77. {
  78. 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. */
  79. 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. */
  80. 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. */
  81. } data;
  82. } app_uart_evt_t;
  83. /**@brief Function for handling app_uart event callback.
  84. *
  85. * @details Upon an event in the app_uart module this callback function will be called to notify
  86. * the application about the event.
  87. *
  88. * @param[in] p_app_uart_event Pointer to UART event.
  89. */
  90. typedef void (* app_uart_event_handler_t) (app_uart_evt_t * p_app_uart_event);
  91. /**@brief Macro for safe initialization of the UART module in a single user instance when using
  92. * a FIFO together with UART.
  93. *
  94. * @param[in] P_COMM_PARAMS Pointer to a UART communication structure: app_uart_comm_params_t
  95. * @param[in] RX_BUF_SIZE Size of desired RX buffer, must be a power of 2 or ZERO (No FIFO).
  96. * @param[in] TX_BUF_SIZE Size of desired TX buffer, must be a power of 2 or ZERO (No FIFO).
  97. * @param[in] EVT_HANDLER Event handler function to be called when an event occurs in the
  98. * UART module.
  99. * @param[in] IRQ_PRIO IRQ priority, app_irq_priority_t, for the UART module irq handler.
  100. * @param[out] ERR_CODE The return value of the UART initialization function will be
  101. * written to this parameter.
  102. *
  103. * @note Since this macro allocates a buffer and registers the module as a GPIOTE user when flow
  104. * control is enabled, it must only be called once.
  105. */
  106. #define APP_UART_FIFO_INIT(P_COMM_PARAMS, RX_BUF_SIZE, TX_BUF_SIZE, EVT_HANDLER, IRQ_PRIO, ERR_CODE) \
  107. do \
  108. { \
  109. app_uart_buffers_t buffers; \
  110. static uint8_t rx_buf[RX_BUF_SIZE]; \
  111. static uint8_t tx_buf[TX_BUF_SIZE]; \
  112. \
  113. buffers.rx_buf = rx_buf; \
  114. buffers.rx_buf_size = sizeof (rx_buf); \
  115. buffers.tx_buf = tx_buf; \
  116. buffers.tx_buf_size = sizeof (tx_buf); \
  117. ERR_CODE = app_uart_init(P_COMM_PARAMS, &buffers, EVT_HANDLER, IRQ_PRIO); \
  118. } while (0)
  119. /**@brief Macro for safe initialization of the UART module in a single user instance.
  120. *
  121. * @param[in] P_COMM_PARAMS Pointer to a UART communication structure: app_uart_comm_params_t
  122. * @param[in] EVT_HANDLER Event handler function to be called when an event occurs in the
  123. * UART module.
  124. * @param[in] IRQ_PRIO IRQ priority, app_irq_priority_t, for the UART module irq handler.
  125. * @param[out] ERR_CODE The return value of the UART initialization function will be
  126. * written to this parameter.
  127. *
  128. * @note Since this macro allocates registers the module as a GPIOTE user when flow control is
  129. * enabled, it must only be called once.
  130. */
  131. #define APP_UART_INIT(P_COMM_PARAMS, EVT_HANDLER, IRQ_PRIO, ERR_CODE) \
  132. do \
  133. { \
  134. ERR_CODE = app_uart_init(P_COMM_PARAMS, NULL, EVT_HANDLER, IRQ_PRIO); \
  135. } while (0)
  136. /**@brief Function for initializing the UART module. Use this initialization when several instances of the UART
  137. * module are needed.
  138. *
  139. *
  140. * @note Normally single initialization should be done using the APP_UART_INIT() or
  141. * APP_UART_INIT_FIFO() macro depending on whether the FIFO should be used by the UART, as
  142. * that will allocate the buffers needed by the UART module (including aligning the buffer
  143. * correctly).
  144. * @param[in] p_comm_params Pin and communication parameters.
  145. * @param[in] p_buffers RX and TX buffers, NULL is FIFO is not used.
  146. * @param[in] error_handler Function to be called in case of an error.
  147. * @param[in] irq_priority Interrupt priority level.
  148. *
  149. * @retval NRF_SUCCESS If successful initialization.
  150. * @retval NRF_ERROR_INVALID_LENGTH If a provided buffer is not a power of two.
  151. * @retval NRF_ERROR_NULL If one of the provided buffers is a NULL pointer.
  152. *
  153. * The below errors are propagated by the UART module to the caller upon registration when Hardware
  154. * Flow Control is enabled. When Hardware Flow Control is not used, these errors cannot occur.
  155. * @retval NRF_ERROR_INVALID_STATE The GPIOTE module is not in a valid state when registering
  156. * the UART module as a user.
  157. * @retval NRF_ERROR_INVALID_PARAM The UART module provides an invalid callback function when
  158. * registering the UART module as a user.
  159. * Or the value pointed to by *p_uart_uid is not a valid
  160. * GPIOTE number.
  161. * @retval NRF_ERROR_NO_MEM GPIOTE module has reached the maximum number of users.
  162. */
  163. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  164. app_uart_buffers_t * p_buffers,
  165. app_uart_event_handler_t error_handler,
  166. app_irq_priority_t irq_priority);
  167. /**@brief Function for getting a byte from the UART.
  168. *
  169. * @details This function will get the next byte from the RX buffer. If the RX buffer is empty
  170. * an error code will be returned and the app_uart module will generate an event upon
  171. * reception of the first byte which is added to the RX buffer.
  172. *
  173. * @param[out] p_byte Pointer to an address where next byte received on the UART will be copied.
  174. *
  175. * @retval NRF_SUCCESS If a byte has been received and pushed to the pointer provided.
  176. * @retval NRF_ERROR_NOT_FOUND If no byte is available in the RX buffer of the app_uart module.
  177. */
  178. uint32_t app_uart_get(uint8_t * p_byte);
  179. /**@brief Function for putting a byte on the UART.
  180. *
  181. * @details This call is non-blocking.
  182. *
  183. * @param[in] byte Byte to be transmitted on the UART.
  184. *
  185. * @retval NRF_SUCCESS If the byte was successfully put on the TX buffer for transmission.
  186. * @retval NRF_ERROR_NO_MEM If no more space is available in the TX buffer.
  187. * NRF_ERROR_NO_MEM may occur if flow control is enabled and CTS signal
  188. * is high for a long period and the buffer fills up.
  189. * @retval NRF_ERROR_INTERNAL If UART driver reported error.
  190. */
  191. uint32_t app_uart_put(uint8_t byte);
  192. /**@brief Function for flushing the RX and TX buffers (Only valid if FIFO is used).
  193. * This function does nothing if FIFO is not used.
  194. *
  195. * @retval NRF_SUCCESS Flushing completed (Current implementation will always succeed).
  196. */
  197. uint32_t app_uart_flush(void);
  198. /**@brief Function for closing the UART module.
  199. *
  200. * @retval NRF_SUCCESS If successfully closed.
  201. * @retval NRF_ERROR_INVALID_PARAM If an invalid user id is provided or the user id differs from
  202. * the current active user.
  203. */
  204. uint32_t app_uart_close(void);
  205. #endif //APP_UART_H__
  206. /** @} */