NRF5 SDK蓝牙开发——NRF_LOG_INFO(…)通过串口输出的实现
开发环境
- iar for arm开发平台
- nrf52832开发板,使用P06引脚作为串口TX口
- 应用程序:nRF5_SDK_15.0.0_a53641a(examples/ble_central/ble_app_uart_c)
- 协议栈: s132_nrf52_6.0.0_softdevice.hex
由于直接编译sdk会出现Pa050相关错误,在iar选项配置中屏蔽该编译错误即可。
一。打开”sdk_config.h”文件,使能及禁能相关功能
关闭宏NRF_LOG_BACKEND_RTT_ENABLED
如下,
#ifndef NRF_LOG_BACKEND_RTT_ENABLED
#define NRF_LOG_BACKEND_RTT_ENABLED 0
#endif
添加如下宏定义到”sdk_config.h”文件合适位置,
// <e> NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend
//==========================================================
#ifndef NRF_LOG_BACKEND_UART_ENABLED
#define NRF_LOG_BACKEND_UART_ENABLED 1
#endif
// <o> NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin
#ifndef NRF_LOG_BACKEND_UART_TX_PIN
#define NRF_LOG_BACKEND_UART_TX_PIN 6
#endif
// <o> NRF_LOG_BACKEND_UART_BAUDRATE - Default Baudrate
// <323584=> 1200 baud
// <643072=> 2400 baud
// <1290240=> 4800 baud
// <2576384=> 9600 baud
// <3862528=> 14400 baud
// <5152768=> 19200 baud
// <7716864=> 28800 baud
// <10289152=> 38400 baud
// <15400960=> 57600 baud
// <20615168=> 76800 baud
// <30801920=> 115200 baud
// <61865984=> 230400 baud
// <67108864=> 250000 baud
// <121634816=> 460800 baud
// <251658240=> 921600 baud
// <268435456=> 1000000 baud
#ifndef NRF_LOG_BACKEND_UART_BAUDRATE
#define NRF_LOG_BACKEND_UART_BAUDRATE 30801920
#endif
// <o> NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings.
// <i> Size of the buffer is a trade-off between RAM usage and processing.
// <i> if buffer is smaller then strings will often be fragmented.
// <i> It is recommended to use size which will fit typical log and only the
// <i> longer one will be fragmented.
#ifndef NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE
#define NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE 64
#endif
二。工程中,添加nrf_log_backend_uart.c文件,路径为
components\libraries\experimental_log\src\nrf_log_backend_uart.c
三。在main函数中,注释掉uart_init()函数
不注释该函数,会出现串口初始化两次的情况,第一次在log_init()函数,第二次在uart_init()函数。
在第二次串口初始化时,会因为没有先释放串口,直接初始化,导致出错。
另外,sdk_config.h
中更改调试输出等级。设置为4。
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#ifndef NRF_LOG_DEFAULT_LEVEL
#define NRF_LOG_DEFAULT_LEVEL 4