2

我有一个在我的 Cortex-M4 平台上运行良好的 newlib 4.9.3 2014q4 应用程序。串行控制台的所有输出都使用自定义函数iprintf或自定义send_str函数。send_str只是将字节写入串行外设 tx 缓冲区(循环调用 my )fputc

int _write(int fd, char *buf, int nbytes)
{
    int i;

    for (i = 0; i < nbytes; i++) {
        if (*(buf + i) == '\n') {
            /* fd is incorrectly passed as arguments, as FILE is not used, but needed for build */
            fputc('\r', (FILE *) & fd);
        }
        /* fd is incorrectly passed as arguments, as FILE is not used, but needed for build */
        fputc(*(buf + i), (FILE *) & fd);
    }

    return nbytes;
}

int fputc(int ch, FILE * f)
{
    unsigned char tempch = ch;

    sendchar(&tempch);

    return ch;
}

void send_str(unsigned char* buff)
{

    while(*buff != 0){
        sendchar(buff);
        buff++;
    }
}

在我的应用程序的主程序中(在初始化数据重定位和 bss 归零之后)我打印了一个横幅。当我使用 newlib-nano( --specs=nano.specs) 时send_str,如果我使用iprintf.

int main(int argc, char ** argv)
{
    int i = 0;
    char str[] = "Hello from Cortex-M4!\n";

    init_uart((void*)UART2_BASE_PTR, 115200);
    send_str(str);
}

相对

int main(int argc, char ** argv)
{
    int i = 0;
    char str[] = "Hello from Cortex-M4!\n";

    init_uart((void*)UART2_BASE_PTR, 115200);
    iprintf("%s", str);
}

仅使用 newlib(不是 nano)这两个函数都可以工作。

我一直在阅读的所有内容都表明,不应该要求对我的应用程序进行更改以在两者之间进行交换。这有例外吗?

4

0 回答 0