Skip to content

alexeykosinov/Redirect-printf-to-USB-VCP-on-STM32H7-MCU

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

Redirect printf() to USB Virtual COM Port on STM32H7 MCU

Enable printf() function to work with USB Virtual COM Port (STM32H743ZI)

STM32CubeMX

  1. Turn on USB_OTG_FS with mode Device_Only
  2. In the Middleware turn on USB_DEVICE with class Communication Device Class (Virtual Com Port)
  3. Default settings
  4. Generate Code

main.c

  1. #include <stdio.h>
  2. Add code below between /* USER CODE BEGIN 4 */
int _write(int file, char *ptr, int len) { 
    CDC_Transmit_FS((uint8_t*) ptr, len); return len; 
}

For cpp project:

extern "C" int _write(int file, char *ptr, int len) { 
    CDC_Transmit_FS((uint8_t*) ptr, len); return len; 
}

That's it! Now you can use printf() anywere you want.

NOTICE! When I started to using FreeRTOS I've noticed that printf displays an incorrect value. Digging a bit, I found out that if you are enable the delay after CDC_Transmit_FS(), then everything works fine, so:

using osDelay function (click to expand)
int _write(int file, char *ptr, int len) { 
    CDC_Transmit_FS((uint8_t*) ptr, len); 
    osDelay(1);
    return len; 
}

There is a better solution suggested by Paunida:

int _write(int file, char *ptr, int len) {
    static uint8_t rc = USBD_OK;

    do {
        rc = CDC_Transmit_FS(ptr, len);
    } while (USBD_BUSY == rc);

    if (USBD_FAIL == rc) {
        /// NOTE: Should never reach here.
        /// TODO: Handle this error.
        return 0;
    }
    return len;
}

work's for me!

About

Enable printf() function to work with USB Virtual COM Port (STM32H743ZI)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published