当前位置:   article > 正文

nRF52840双UART串口功能开发_52840有几个uart

52840有几个uart

使用nRF52840进行开发的时候需要用到UART通信功能,同时我们也需要串口进行打印和调试。所以进行了双UART串口功能的开发。我这边在nRF52840 DK开发板上面进行开发。nRF52840的UART的PIN脚可以任选4个通用GPIO Pin脚。

官方例程中,使用了UART0作为虚拟COM串口,这个COM串口是通过USB引出来的。所以使用这个UART0作为调试打印串口。

 接下来,开发UART1通信功能。任选四个引脚:

  1. #define UART1_RX_PIN_NUMBER NRF_GPIO_PIN_MAP(1, 4)
  2. #define UART1_TX_PIN_NUMBER NRF_GPIO_PIN_MAP(1, 2)
  3. #define UART1_CTS_PIN_NUMBER NRF_GPIO_PIN_MAP(1, 3)
  4. #define UART1_RTS_PIN_NUMBER NRF_GPIO_PIN_MAP(1, 1)

user_uart.c 

  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "app_uart.h"
  5. #include "app_error.h"
  6. #include "nrf_delay.h"
  7. #include "nrf.h"
  8. #include "bsp.h"
  9. #if defined (UART_PRESENT)
  10. #include "nrf_uart.h"
  11. #endif
  12. #if defined (UARTE_PRESENT)
  13. #include "nrf_uarte.h"
  14. #endif
  15. //#define ENABLE_LOOPBACK_TEST /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
  16. #define MAX_TEST_DATA_BYTES (15U) /**< max number of test bytes to be used for tx and rx. */
  17. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  18. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  19. void uart_error_handle(app_uart_evt_t * p_event)
  20. {
  21. if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  22. {
  23. APP_ERROR_HANDLER(p_event->data.error_communication);
  24. }
  25. else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  26. {
  27. APP_ERROR_HANDLER(p_event->data.error_code);
  28. }
  29. }
  30. /**@brief Function for handling app_uart events.
  31. *
  32. * @details This function will receive a single character from the app_uart module and append it to
  33. * a string. The string will be be sent over BLE when the last character received was a
  34. * 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
  35. */
  36. /**@snippet [Handling the data received over UART] */
  37. void uart_event_handle(app_uart_evt_t * p_event)
  38. {
  39. // static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  40. // static uint8_t index = 0;
  41. uint32_t err_code;
  42. uint8_t data;
  43. switch (p_event->evt_type)
  44. {
  45. case APP_UART_DATA_READY:
  46. UNUSED_VARIABLE(app_uart_get(&data));
  47. while (app_uart_put(data) != NRF_SUCCESS);
  48. break;
  49. case APP_UART_COMMUNICATION_ERROR:
  50. APP_ERROR_HANDLER(p_event->data.error_communication);
  51. break;
  52. case APP_UART_FIFO_ERROR:
  53. APP_ERROR_HANDLER(p_event->data.error_code);
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. /**@snippet [Handling the data received over UART] */
  60. /* When UART is used for communication with the host do not use flow control.*/
  61. #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
  62. /**@brief Function for initializing the UART module.
  63. */
  64. /**@snippet [UART Initialization] */
  65. void uart_init(void)
  66. {
  67. uint32_t err_code;
  68. app_uart_comm_params_t const uart0_comm_params =
  69. {
  70. .rx_pin_no = RX_PIN_NUMBER,
  71. .tx_pin_no = TX_PIN_NUMBER,
  72. .rts_pin_no = RTS_PIN_NUMBER,
  73. .cts_pin_no = CTS_PIN_NUMBER,
  74. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  75. .use_parity = false,
  76. #if defined (UART_PRESENT)
  77. .baud_rate = NRF_UART_BAUDRATE_115200
  78. #else
  79. .baud_rate = NRF_UARTE_BAUDRATE_115200
  80. #endif
  81. };
  82. APP_UART_FIFO_INIT(APP_UART_DRIVER_INSTANCE,
  83. &uart0_comm_params,
  84. UART_RX_BUF_SIZE,
  85. UART_TX_BUF_SIZE,
  86. NULL,
  87. APP_IRQ_PRIORITY_LOWEST,
  88. err_code);
  89. APP_ERROR_CHECK(err_code);
  90. const app_uart_comm_params_t uart1_comm_params =
  91. {
  92. UART1_RX_PIN_NUMBER,
  93. UART1_TX_PIN_NUMBER,
  94. UART1_RTS_PIN_NUMBER,
  95. UART1_CTS_PIN_NUMBER,
  96. UART_HWFC,
  97. false,
  98. #if defined (UART_PRESENT)
  99. NRF_UART_BAUDRATE_115200
  100. #else
  101. NRF_UARTE_BAUDRATE_115200
  102. #endif
  103. };
  104. APP_UART_FIFO_INIT(APP_UART1_DRIVER_INSTANCE,
  105. &uart1_comm_params,
  106. UART_RX_BUF_SIZE,
  107. UART_TX_BUF_SIZE,
  108. uart_event_handle,
  109. APP_IRQ_PRIORITY_LOWEST,
  110. err_code);
  111. APP_ERROR_CHECK(err_code);
  112. }

 app_uart.h

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

 app_uart_fifo.c

  1. #include "sdk_common.h"
  2. #if NRF_MODULE_ENABLED(APP_UART)
  3. #include "app_uart.h"
  4. #include "app_fifo.h"
  5. #include "nrf_drv_uart.h"
  6. #include "nrf_assert.h"
  7. // Use array instead
  8. static nrf_drv_uart_t app_uart_inst[2] = {NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE), NRF_DRV_UART_INSTANCE(APP_UART1_DRIVER_INSTANCE)};
  9. static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
  10. {
  11. uint32_t tmp = fifo->read_pos;
  12. return fifo->write_pos - tmp;
  13. }
  14. #define FIFO_LENGTH(F) fifo_length(&F) /**< Macro to calculate length of a FIFO. */
  15. static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
  16. static uint8_t tx_buffer[2];
  17. static uint8_t rx_buffer[1]; // Only UART1 receive data
  18. static bool m_rx_ovf;
  19. // Use array instead
  20. static app_fifo_t m_rx_fifo; /**< RX FIFO buffer for storing data received on the UART until the application fetches them using app_uart_get(). */
  21. static app_fifo_t m_tx_fifo[2]; /**< TX FIFO buffer for storing data to be transmitted on the UART when TXD is ready. Data is put to the buffer on using app_uart_put(). */
  22. static void uart0_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  23. {
  24. app_uart_evt_t app_uart_event;
  25. uint32_t err_code;
  26. switch (p_event->type)
  27. {
  28. case NRF_DRV_UART_EVT_TX_DONE:
  29. // Get next byte from FIFO.
  30. if (app_fifo_get(&m_tx_fifo[APP_UART_DRIVER_INSTANCE], &tx_buffer[APP_UART_DRIVER_INSTANCE]) == NRF_SUCCESS)
  31. {
  32. (void)nrf_drv_uart_tx(&app_uart_inst[APP_UART_DRIVER_INSTANCE], &tx_buffer[APP_UART_DRIVER_INSTANCE], 1);
  33. }
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. static void uart1_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  40. {
  41. app_uart_evt_t app_uart_event;
  42. uint32_t err_code;
  43. switch (p_event->type)
  44. {
  45. case NRF_DRV_UART_EVT_RX_DONE:
  46. // If 0, then this is a RXTO event with no new bytes.
  47. if(p_event->data.rxtx.bytes == 0)
  48. {
  49. // A new start RX is needed to continue to receive data
  50. (void)nrf_drv_uart_rx(&app_uart_inst[APP_UART1_DRIVER_INSTANCE], rx_buffer, 1);
  51. break;
  52. }
  53. // Write received byte to FIFO.
  54. err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
  55. if (err_code != NRF_SUCCESS)
  56. {
  57. app_uart_event.evt_type = APP_UART_FIFO_ERROR;
  58. app_uart_event.data.error_code = err_code;
  59. m_event_handler(&app_uart_event);
  60. }
  61. // Notify that there are data available.
  62. else if (FIFO_LENGTH(m_rx_fifo) != 0)
  63. {
  64. app_uart_event.evt_type = APP_UART_DATA_READY;
  65. m_event_handler(&app_uart_event);
  66. }
  67. // Start new RX if size in buffer.
  68. if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
  69. {
  70. (void)nrf_drv_uart_rx(&app_uart_inst[APP_UART1_DRIVER_INSTANCE], rx_buffer, 1);
  71. }
  72. else
  73. {
  74. // Overflow in RX FIFO.
  75. m_rx_ovf = true;
  76. }
  77. break;
  78. case NRF_DRV_UART_EVT_ERROR:
  79. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  80. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  81. (void)nrf_drv_uart_rx(&app_uart_inst[APP_UART1_DRIVER_INSTANCE], rx_buffer, 1);
  82. m_event_handler(&app_uart_event);
  83. break;
  84. case NRF_DRV_UART_EVT_TX_DONE:
  85. // Get next byte from FIFO.
  86. if (app_fifo_get(&m_tx_fifo[APP_UART1_DRIVER_INSTANCE], &tx_buffer[APP_UART1_DRIVER_INSTANCE]) == NRF_SUCCESS)
  87. {
  88. (void)nrf_drv_uart_tx(&app_uart_inst[APP_UART1_DRIVER_INSTANCE], &tx_buffer[APP_UART1_DRIVER_INSTANCE], 1);
  89. }
  90. else
  91. {
  92. // Last byte from FIFO transmitted, notify the application.
  93. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  94. m_event_handler(&app_uart_event);
  95. }
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. uint32_t app_uart_init(const uint8_t instance,
  102. const app_uart_comm_params_t * p_comm_params,
  103. app_uart_buffers_t * p_buffers,
  104. app_uart_event_handler_t event_handler,
  105. app_irq_priority_t irq_priority)
  106. {
  107. uint32_t err_code;
  108. if(instance == APP_UART1_DRIVER_INSTANCE)
  109. {
  110. m_event_handler = event_handler;
  111. }
  112. if (p_buffers == NULL)
  113. {
  114. return NRF_ERROR_INVALID_PARAM;
  115. }
  116. if(instance == APP_UART1_DRIVER_INSTANCE)
  117. {
  118. // Configure buffer RX buffer.
  119. err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
  120. VERIFY_SUCCESS(err_code);
  121. }
  122. // Configure buffer TX buffer.
  123. err_code = app_fifo_init(&m_tx_fifo[instance], p_buffers->tx_buf, p_buffers->tx_buf_size);
  124. VERIFY_SUCCESS(err_code);
  125. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  126. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  127. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  128. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  129. config.interrupt_priority = irq_priority;
  130. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  131. config.pselcts = p_comm_params->cts_pin_no;
  132. config.pselrts = p_comm_params->rts_pin_no;
  133. config.pselrxd = p_comm_params->rx_pin_no;
  134. config.pseltxd = p_comm_params->tx_pin_no;
  135. nrf_uart_event_handler_t local_event_handler = (instance == APP_UART1_DRIVER_INSTANCE) ? uart1_event_handler : uart0_event_handler;
  136. err_code = nrf_drv_uart_init(&app_uart_inst[instance], &config, local_event_handler);
  137. VERIFY_SUCCESS(err_code);
  138. if(instance == APP_UART1_DRIVER_INSTANCE)
  139. {
  140. m_rx_ovf = false;
  141. // Turn on receiver if RX pin is connected
  142. if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
  143. {
  144. return nrf_drv_uart_rx(&app_uart_inst[instance], rx_buffer,1);
  145. }
  146. else
  147. {
  148. return NRF_SUCCESS;
  149. }
  150. }
  151. }
  152. uint32_t app_uart_flush(void)
  153. {
  154. uint32_t err_code;
  155. err_code = app_fifo_flush(&m_rx_fifo);
  156. VERIFY_SUCCESS(err_code);
  157. err_code = app_fifo_flush(&m_tx_fifo[APP_UART_DRIVER_INSTANCE]);
  158. VERIFY_SUCCESS(err_code);
  159. err_code = app_fifo_flush(&m_tx_fifo[APP_UART1_DRIVER_INSTANCE]);
  160. VERIFY_SUCCESS(err_code);
  161. return NRF_SUCCESS;
  162. }
  163. uint32_t app_uart_get(uint8_t * p_byte)
  164. {
  165. ASSERT(p_byte);
  166. bool rx_ovf = m_rx_ovf;
  167. ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
  168. // If FIFO was full new request to receive one byte was not scheduled. Must be done here.
  169. if (rx_ovf)
  170. {
  171. m_rx_ovf = false;
  172. uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst[APP_UART1_DRIVER_INSTANCE], rx_buffer, 1);
  173. // RX resume should never fail.
  174. APP_ERROR_CHECK(uart_err_code);
  175. }
  176. return err_code;
  177. }
  178. uint32_t app_uart_instance_put(int instance, uint8_t byte)
  179. {
  180. uint32_t err_code;
  181. err_code = app_fifo_put(&m_tx_fifo[instance], byte);
  182. if (err_code == NRF_SUCCESS)
  183. {
  184. // The new byte has been added to FIFO. It will be picked up from there
  185. // (in 'uart_event_handler') when all preceding bytes are transmitted.
  186. // But if UART is not transmitting anything at the moment, we must start
  187. // a new transmission here.
  188. if (!nrf_drv_uart_tx_in_progress(&app_uart_inst[instance]))
  189. {
  190. // This operation should be almost always successful, since we've
  191. // just added a byte to FIFO, but if some bigger delay occurred
  192. // (some heavy interrupt handler routine has been executed) since
  193. // that time, FIFO might be empty already.
  194. if (app_fifo_get(&m_tx_fifo[instance], &tx_buffer[instance]) == NRF_SUCCESS)
  195. {
  196. err_code = nrf_drv_uart_tx(&app_uart_inst[instance], &tx_buffer[instance], 1);
  197. }
  198. }
  199. }
  200. return err_code;
  201. }
  202. // Default use UART1
  203. uint32_t app_uart_put(uint8_t byte)
  204. {
  205. return app_uart_instance_put(APP_UART1_DRIVER_INSTANCE, byte);
  206. }
  207. uint32_t app_uart_close(int instance)
  208. {
  209. nrf_drv_uart_uninit(&app_uart_inst[instance]);
  210. return NRF_SUCCESS;
  211. }
  212. #endif //NRF_MODULE_ENABLED(APP_UART)

sdk_config.h

  1. // <e> UART1_ENABLED - Enable UART1 instance
  2. //==========================================================
  3. #ifndef UART1_ENABLED
  4. #define UART1_ENABLED 1
  5. #endif
  6. #ifndef APP_UART1_DRIVER_INSTANCE
  7. #define APP_UART1_DRIVER_INSTANCE 1
  8. #endif

retarget.c

  1. #if defined(__SES_VERSION) && (__SES_VERSION >= 34000)
  2. int __putchar(int ch, __printf_tag_ptr tag_ptr)
  3. {
  4. UNUSED_PARAMETER(tag_ptr);
  5. UNUSED_VARIABLE(app_uart_instance_put(APP_UART_DRIVER_INSTANCE, (uint8_t)ch));
  6. return ch;
  7. }

 注意:

nrf52840的uart管脚输出是TTL格式的,用TTL线才可以正常显示。而且接收和发送最好间隔一个PIN脚,否则容易产生干扰。如果用RS232转USB模块进行接收的话,会不断接收到乱码数据。

UART1的接收和发送引脚不要和TTL接收器接反,否则容易重启。接反的时候还是会收到数据的,可能是干扰所致。正常接线以后,就正常了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/194155
推荐阅读
相关标签
  

闽ICP备14008679号