stm32 printf over usb cdc

Stm32 printf over usb cdc

目前我正在开发一个软件,但我被卡住了。我需要一些帮助。

我正在使用 stm32f103、hal 和库 freertos。

我写了一个小的串行调试器函数,可以通过 uart 或 usb cdc 发送字符。

这是我的定义:

头文件中:

#if ( DEBUG == DEBUG_ENABLE )
    /* printf definetion */
    #define DEBUG_MSG( FLAG, fmt, ...)                                                                          
    do
    {
        if( FLAG )
            printf( "[%08d]%s:%d:%s(): " fmt "\r\n", HAL_GetTick(), __FILE__, __LINE__, __func__, ##__VA_ARGS__);
    }while( 0 );
#else
    #define DEBUG_MSG( FLAG, fmt, ... )
#endif

在c文件中:

#define PUTCHAR_PROTOTYE int fputc( int ch, FILE *f )

PUTCHAR_PROTOTYE
{
#if DEBUG_PORT == USB_DEBUG
    CDC_Transmit_FS((uint8_t *)&ch, 1);
#elif DEBUG_PORT == UART_DEBUG
    HAL_UART_Transmit( pDEBUG_PORT, ( uint8_t * )&ch, 1, 0xFFFF );
#else
    #error "Define a debug port!"
#endif
    return ch;
}

我这样使用:DEBUG_MSG(MAIN_DEBUG, "Gsm manager thread started!"); 当调试器端口为 uart_debug 时,完全没有问题。一切运行完美。

但是每当我 select usb_debug 端口时,就会发生一些事情。

虽然调试端口是 usb cdc,但我收到如下文本:

"[0[0[0[0[6"

但在调试模式下,在 putc 函数中我在 CDC_Transmit_FS((uint8_t *)&ch, 1); 行放置了一个断点,然后按 F5,我得到:

[00010046]../Src/main.c:633:gsmmanager_handler(): Gsm manager thread started!

string 和我期待的一样。如果我在 CDC_Transmit_FS((uint8_t *)&ch, 1); 之后放置 CDC_Transmit_FS((uint8_t *)"test", strlen("test")); 行,我会得到:

[0[0test

字符串.

所以我什么都不懂。可能是什么?

您还在寻找答案吗?

我确实写了下面的代码,它为我运行:

PUTCHAR_PROTOTYPE  
{
    //while(!(CDC_Transmit_FS((uint8_t*)&ch, 1) == USBD_OK));
    while(!(CDC_Transmit_FS((uint8_t*)&ch, 1) == USBD_BUSY));
    return ch;
}

原因可能是当第二个字符传递给函数时,第一个字符还在路上,所以第二个字符被丢弃,因为线路忙。我不确定这是否是你的问题。

添加超时检查以避免无限循环可能很有用。

此致。