Nordic 使用串口UART打印

港控/mmm° 2022-12-29 06:41 477阅读 0赞

Nordic 使用串口UART打印

1. sdk_config

nRF_Drivers UART_ENABLE
在这里插入图片描述
nRF_Libraries APP_UART_ENABLE
在这里插入图片描述

2. 添加文件

在这里插入图片描述
在这里插入图片描述
接下来是,添加串口初始化函数和回调,可以添加到我们字节的.c文件中。
myuart.c

  1. #include "app_uart.h"
  2. #include "myuart.h"
  3. #include "nrf_log.h"
  4. #include "nrf_uart.h"
  5. #include "nrf_uarte.h"
  6. #include "pca_rail_lubricator.h"
  7. #include "mylog.h"
  8. #include "myota.h"
  9. #define UART_RX_BUFF_LEN (255)
  10. /**@brief Function for handling app_uart events. * * @details This function will receive a single character from the app_uart module and append it to * a string. The string will be be sent over BLE when the last character received was a * 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length. */
  11. /**@snippet [Handling the data received over UART] */
  12. void uart_event_handle(app_uart_evt_t * p_event)
  13. {
  14. static uint8_t data_array[UART_RX_BUFF_LEN];
  15. static uint8_t index = 0;
  16. uint32_t err_code;
  17. switch (p_event->evt_type)
  18. {
  19. case APP_UART_DATA_READY:
  20. UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  21. index++;
  22. if ((data_array[index - 1] == '\n') ||
  23. (data_array[index - 1] == '\r') ||
  24. (index >= UART_RX_BUFF_LEN))
  25. {
  26. if (index > 1)
  27. {
  28. NRF_NUS_LOG_INFO("rx data: %s", data_array);
  29. if ( 0 == strncmp((const char *)data_array, "ota_upgrade", 11))
  30. {
  31. NRF_NUS_LOG_INFO("ota_upgrade");
  32. myota_upgrade_processing();
  33. }
  34. }
  35. index = 0;
  36. }
  37. break;
  38. case APP_UART_COMMUNICATION_ERROR:
  39. APP_ERROR_HANDLER(p_event->data.error_communication);
  40. break;
  41. case APP_UART_FIFO_ERROR:
  42. APP_ERROR_HANDLER(p_event->data.error_code);
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. /**@snippet [Handling the data received over UART] */
  49. /**@brief Function for initializing the UART module. */
  50. /**@snippet [UART Initialization] */
  51. void myuart_init(void)
  52. {
  53. uint32_t err_code;
  54. app_uart_comm_params_t const comm_params =
  55. {
  56. .rx_pin_no = RX_PIN_NUMBER,
  57. .tx_pin_no = TX_PIN_NUMBER,
  58. .rts_pin_no = RTS_PIN_NUMBER,
  59. .cts_pin_no = CTS_PIN_NUMBER,
  60. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  61. .use_parity = false,
  62. #if defined (UART_PRESENT)
  63. .baud_rate = NRF_UART_BAUDRATE_115200
  64. #else
  65. .baud_rate = NRF_UARTE_BAUDRATE_115200
  66. #endif
  67. };
  68. APP_UART_FIFO_INIT(&comm_params,
  69. UART_RX_BUF_SIZE,
  70. UART_TX_BUF_SIZE,
  71. uart_event_handle,
  72. APP_IRQ_PRIORITY_LOWEST,
  73. err_code);
  74. APP_ERROR_CHECK(err_code);
  75. }
  76. /**@snippet [UART Initialization] */

myuart.h

  1. #ifndef __MYUART_H
  2. #define __MYUART_H
  3. #define UART_TX_BUF_SIZE (256)
  4. #define UART_RX_BUF_SIZE (256)
  5. void myuart_init(void);
  6. #endif /* __MYUART_H */

pca_**.h板子定义.h文件中,定义我们的UART引脚。我这里使用的是P1口的13,15脚。
在这里插入图片描述
main.c中调用我们的myuart_init,初始化我们的串口。

此时,已经可以通过app_uart_getapp_uart_put接收和发送字符了。

3. 定义自己的UART_LOG_INFO

uart_pint.c

  1. #include "uart_print.h"
  2. #include "string.h"
  3. #include "app_uart.h"
  4. void uart_send_str(char *data)
  5. {
  6. size_t len = 0;
  7. len = strlen(data);
  8. for (int i = 0; i < len; i++) {
  9. app_uart_put(data[i]);
  10. }
  11. }
  12. void uart_send_log(char *data)
  13. {
  14. uart_send_str(data);
  15. app_uart_put('\r');
  16. app_uart_put('\n');
  17. }

uart_print.h

  1. #ifndef __UART_PRINT_H
  2. #define __UART_PRINT_H
  3. #include "nrf_log.h"
  4. void uart_send_str(char *data);
  5. void uart_send_log(char *data);
  6. #ifdef UART_PRINT
  7. #define UART_LOG_INFO(...) \ do { char buff[256]; \ snprintf(buff, 256, __VA_ARGS__);\ uart_send_log(buff); \ } while (0);
  8. #else
  9. #define UART_LOG_INFO(...) NRF_LOG_INFO(__VA_ARGS__)
  10. #endif
  11. #endif /* __UART_PRINT_H */

在这里插入图片描述

添加串口打印宏定义
在这里插入图片描述

好了,在需要的文件中导入uart_print.h,即可使用串口打印。当不需要串口打印的时候,删除UART_PRINT宏定义,则使用NRF_LOG_INFO进行日志输出。

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,477人围观)

还没有评论,来说两句吧...

相关阅读

    相关 51单片机串口通信(UART)

    项目描述: 1.串口工作方式为1(8位UART,波特率可变),无校验位; 2.通信数据格式为:1位起始位 + 8位数据位 + 1位停止位; 3.上电后MCU给上位机

    相关 串口Uart)的基础知识

    一、串口 1.串口定义:串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。 2.串口的特点:数据一位一位的传输。