【问题标题】:Stm32 printf over usb cdcstm32 printf over usb cdc
【发布时间】:2018-09-24 00:11:17
【问题描述】:

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

我正在使用 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 时,完全没有问题。一切运行完美。

但是每当我选择 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 管理器线程已启动!

字符串就像我期望的那样。如果我在CDC_Transmit_FS((uint8_t *)&ch, 1); 后面加上CDC_Transmit_FS((uint8_t *)"test", strlen("test")); 行,我得到:

[0[0测试

字符串。

所以我什么都不懂。会是什么?

【问题讨论】:

    标签: debugging usb stm32 cdc


    【解决方案1】:

    你还在寻找答案吗?

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

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

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

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

    问候。

    【讨论】:

    • 非常感谢。我还没有测试它。 dma usb 呢?
    猜你喜欢
    • 2015-07-25
    • 2021-02-28
    • 2020-08-09
    • 2020-12-25
    • 2019-07-23
    • 2021-02-12
    • 1970-01-01
    • 2017-10-31
    • 2021-07-18
    相关资源
    最近更新 更多