如何通过 STM32F4xx USB 将 Keil printf 重定向到连接的 Windows 计算机

how to redirect Keil printf out through STM32F4xx USB to connected Windows computer

如何配置 Keil uVision5 将 STM32F4xx 的 printf 输出重定向到 MCU 的 USB 接口?然后,USB 将连接到 Windows 计算机、虚拟端口驱动程序和终端程序。

我找不到将 printf 配置为通过 STM32F4xx MCU 的 USB 接口输出的示例 uVision5 项目。

STM32F4xx MCU的USB必须实现USB CDC作为设备class interface class (https://en.wikipedia.org/wiki/USB_communications_device_class) and in host mode (USB OTG) in its firmware.在 PC 的 USB 上必须创建一个 虚拟 COM 端口 (驱动程序)来处理 STM32F4 通过 USB CDC(在主机模式下)发送的 RS232 协议。当 STM32F4 上的 CDC 固件正常工作时,STM32F4 在 linux 上显示为 /dev/ttyACM0,这可以通过标准 RS232 终端程序访问。

基础知识 -> http://www.keil.com/support/man/docs/rlarm/rlarm_usb_create_cdc_acm.htm

这是一个基本示例,如何通过 USB CDC 发送串行数据(在 pc 的 USB 上显示为虚拟 COM 端口,即 /dev/ttyACM0):http://visualgdb.com/tutorials/arm/stm32/usb/

另请参阅此 http://stm32f4-discovery.net/2014/08/library-24-virtual-com-port-vcp-stm32f4xx/

按照以上教程进行操作或将其改编为 linux (http://visualgdb.com/tutorials/arm/stm32/usb/)。

重定向 printf() 到 USB CDC 覆盖标准 I/O 例程,如下所示。然后 printf() 的 ARM 版本调用这些例程,请参阅此 https://mcuoneclipse.com/2014/07/11/printf-and-scanf-with-gnu-arm-libraries/printf() 的 ARM 版本与 x86/x64 版本有很大不同printf()):

#include<sys/stat.h>

extern"C"
{
   int _fstat (int fd, struct stat *pStat)
    {
        pStat->st_mode = S_IFCHR;
       return 0;
    }

   int _close(int)
    {
       return -1;
    }

   int _write (int fd, char *pBuffer, int size)
    {
       return VCP_write(pBuffer, size);
    }

   int _isatty (int fd)
    {
       return 1;
    }

   int _lseek(int, int, int)
    {
       return -1;
    }

   int _read (int fd, char *pBuffer, int size)
    {
       for (;;)
        {
           int done = VCP_read(pBuffer, size);
           if (done)
               return done;
        }
    }
}

或使用 ARM 半主机接口 (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0471c/Bgbjhiea.html)。它将提供自动重定向 printf() 到 USB CDC

的功能

更多信息:

这是一个如何实现 USB-RS232 适配器的示例:http://www.wolinlabs.com/blog/stm32f4.virtual.com.port.html

git还包含很多有趣的文件git clone https://github.com/rowol/stm32_discovery_arm_gcc