Configuring USB Serial For STM32F4 in Custom Board

Dear all,

I am struggling to get SerialUSB to work in a custom board I’ve developed using STM32F407VE. I have read a few topics here on similar issue, such as Configuring USB Serial For Marlin 2.0 and STM32F4, Difficulty with getting USB serial [USB CDC] working, among others, but none brought me to a solution. Even more intriguing, a colleague could use the SerialUSB with the same board. :frowning:

I am using up-to-date Platform IO in a Windows 10 PC. I wrote a simple project, just to begin the SerialUSB and toggle a pin to check that the clock settings are fine. The board has a 25MHz HSE crystal and uses the USB HS peripheral. I am using the black_f407ve board configuration as basis for my setup.

Below are the code and platformio.ini.

Main.c:

#include <Arduino.h>

void setup() {
	SerialUSB.begin();
  // Serial.begin(115200);
  pinMode(PC10, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  SerialUSB.println("Hello world");
	digitalToggle(PC10);
  delay(1000);
}

SystemClock_Config():

// Override default clock source configuration
extern "C" void SystemClock_Config() {

  RCC_OscInitTypeDef RCC_OscInitStruct = {};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {};

  /**Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /**Initializes the CPU, AHB and APB busses clocks
   * VCO CLOCK = 25MHz * PLLN / PLLM
   * PLL General Clock = VCO CLOCK / PLLP
   * USB CLOCK = VCO CLOCK / PLLQ 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25; // 25MHz Crystal frequency
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    Error_Handler();
  }

  /**Initializes the CPU, AHB and APB busses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
    Error_Handler();
  }

  /* Ensure CCM RAM clock is enabled */
  __HAL_RCC_CCMDATARAMEN_CLK_ENABLE();

} 

I have modified the the HSE define in .platformio\packages\framework-arduinoststm32\system\STM32F4xx\stm32f4xx_hal_conf_default.h:

/* #################    ######### HSE/HSI Values adaptation ##################### */
/**
  * @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
  *        This value is used by the RCC HAL module to compute the system frequency
  *        (when HSE is used as system clock source, directly or through the PLL).
    */
  #if !defined  (HSE_VALUE)
  #define HSE_VALUE              25000000U /*!< Value of the External oscillator in Hz */
  // #define HSE_VALUE              8000000U /*!< Value of the External oscillator in Hz */
  #endif /* HSE_VALUE */

And this is my platformio.ini:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:black_f407ve]
platform = ststm32
board = black_f407ve
framework = arduino
; uncomment to change upload protocol from stlink to dfu if needed
;upload_protocol = dfu
monitor_port = /dev/ttyACM0
build_flags = 
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D USBCON
monitor_dtr = 1

I get a correct toggle period at the pin, but my PC doesn’t recognize the USB COMM port.

I also tried more detailed build_flags, the same my colleague uses, without any result:

board_build.mcu = stm32f407vet6
board_build.f_cpu = 168000000L
monitor_port = COM19
monitor_speed = 115200
upload_protocol = stlink
build_flags =
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
-D USBCON
-D PIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
-D PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED_FULLMODE
-D USBD_USE_CDC
-D HAL_PCD_MODULE_ENABLED
-D USE_USB_HS
-D USB_MANUFACTURER_STRING="\"Datalogger_8\""
-D USB_PRODUCT_STRING="\"Datalogger_8_v0.6\""

Does anyone has a clue where could be the problem?