|
这两天,刚好在学习51822,碰到了printf 的问题,我回答一下我的解决方法,希望有参考价值,具体解决方案如下:
第一步、
需要把retarget.c 从SDK中拷贝出来(xx\nRF5_SDK\nRF5_SDK_12.3.0_d7731ad\components\libraries\uart\retarget.c),放到工程下,因为默认重定义的fputc,函数不会打印,需要改为以下:
改前:
```c
int fputc(int ch, FILE * p_file)
{
UNUSED_PARAMETER(p_file);
UNUSED_VARIABLE(app_uart_put((uint8_t)ch));
return ch;
}
```
改后:
```c
int fputc(int ch, FILE * p_file)
{
UNUSED_PARAMETER(p_file);
app_uart_put((uint8_t)ch);
return ch;
}
```
第二步、
添加配置,在`sdk_config.h`中添加以下配置
```c
#ifndef UART_ENABLED
#define UART_ENABLED 1
#endif
// <q> APP_FIFO_ENABLED - app_fifo - Software FIFO implementation
#ifndef APP_FIFO_ENABLED
#define APP_FIFO_ENABLED 1
#endif
// <e> APP_UART_ENABLED - app_uart - UART driver
//==========================================================
#ifndef APP_UART_ENABLED
#define APP_UART_ENABLED 1
#endif
// <q> RETARGET_ENABLED - retarget - Retargeting stdio functions
#ifndef RETARGET_ENABLED
#define RETARGET_ENABLED 1
#endif
```
第三步、添加如下文件
`nRF5_SDK\nRF5_SDK_12.3.0_d7731ad\components\libraries\uart\app_uart_fifo.c`,`nRF5_SDK_12.3.0_d7731ad\components\libraries\fifo\app_fifo.c`,`nRF5_SDK_12.3.0_d7731ad\examples\ble_peripheral\experimental_ble_app_blinky\retarget.c`.
第四步、初始串口
代码如下
```c
void uart_error_handle(app_uart_evt_t * p_event)
{
if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_communication);
}
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_code);
}
}
void openPrintf( void )
{
ret_code_t err_code;
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
APP_ERROR_CHECK(err_code);
}
```
|
|