设为首页收藏本站

Discuz! Board

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 6771|回复: 4
打印 上一主题 下一主题

nrf51822中 printf 的重定向问题

[复制链接]

0

主题

2

帖子

29

积分

新手上路

Rank: 1

积分
29
楼主
Alex 发表于 2021-3-13 22:49:09 | 显示全部楼层
这两天,刚好在学习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);
}
```




回复 支持 反对

使用道具 举报

Archiver|手机版|小黑屋|Comsenz Inc.   

GMT+8, 2024-5-8 19:52 , Processed in 0.111358 second(s), 27 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表