hu25886 发表于 2016-12-14 11:08:26

nrf51822中 printf 的重定向问题

int fputc(int ch)
{
NRF_UART0->TXD = (uint8_t)ch;
while (NRF_UART0->EVENTS_TXDRDY != 1)
{
    // Wait for TXD data to be sent
}
NRF_UART0->EVENTS_TXDRDY=0;
return ch;
}      在一个工程中可以使用, copy到另一个工程中非但不可用还导致simple_uart_put();等函数无法使用。这是为什么?????

Eric 发表于 2016-12-26 16:08:08

我也遇到了这个问题,没人解答吗?

admin 发表于 2016-12-28 16:59:34

KEIL上设置了没有

zjd2357 发表于 2017-2-21 16:12:35

admin 发表于 2016-12-28 16:59
KEIL上设置了没有

请问keil上要怎么设置,我也遇到这个问题

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);
}
```




页: [1]
查看完整版本: nrf51822中 printf 的重定向问题