Trouble with STM32F411CE Serial Communication on PlatformIO (USB Device N - Page 1
Forum section: electronics / general

author: hagy
author_rank: Newbie
timestamp: September 04, 2025, 10:12:22 am
content: Hi everyone,I'm running into consistent issues with serial communication on my STM32F411CE board (aka "Black Pill") using PlatformIO with the Arduino framework.Everything seems fine during compilation and upload — the code uploads successfully — but as soon as I open the Serial Monitor, I get the following error:UserSideException: Cannot configure port, something went wrong. Original message: PermissionError(13, 'A device attached to the system is not functioning.', None, 31)Alternatively, sometimes Windows pops up an error saying:"USB device not recognized"I'm trying to figure out what drivers are supposed to be installed for this to work smoothly. I've heard conflicting things about needing ST-Link drivers, STM32 Virtual COM Port (VCP) drivers, or Zadig-based WinUSB replacements depending on the uploader method.Some additional details:Board: STM32F411CEU6 ("Black Pill")Platform: PlatformIO with Arduino frameworkUpload method: Using USB (no external ST-Link currently)Cable: Known good data-capable micro USB cableSystem: Windows 10 (fully updated)My Questions:What drivers do I actually need for this board when using it as a USB device (CDC serial)?Is there something I need to change in my Arduino code or PlatformIO configuration to enable proper serial communication?Is it possible my board doesn’t come with a USB bootloader or proper firmware for CDC?Any steps I can take to diagnose whether the USB peripheral is even being initialized correctly?Thanks in advance for your help — I've been stuck on this for a while and would love to get this board working reliably over USB!Attached below are my code and the platformio.ini configuration file.

author: darkspr1te
author_rank: Frequent Contributor
timestamp: September 04, 2025, 02:05:58 pm
content: Depending on the way your code/bootloader is setup you may have to unplug and re-plug the device before windows will accept it and setup the correct driver.Also there are two different bootloaders for arduino system and you also have tell platformio to include CDC files in it's build system, off the top of my head i cant remember what you add to your platformio.ini but in the arduino system you just select CDC U(s)art from the menu.(i dont often use arduino system but stmcube system instead)

author: darkspr1te
author_rank: Frequent Contributor
timestamp: September 04, 2025, 02:16:37 pm
content: I did quick google and found you have to add hte following to platformio.iniCode:build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC-D PIO_FRAMEWORK_ARDUINO_USB_HIGHSPEED_FULLMODEanother site also says this,Code:build_flags =-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC-D USBCON-D USBD_VID=0x0483-D USBD_PID=0x5740-D USB_MANUFACTURER="Unknown"-D USB_PRODUCT="\"BLUEPILL_F103C8\""-D HAL_PCD_MODULE_ENABLED

author: hagy
author_rank: Newbie
timestamp: September 04, 2025, 03:30:20 pm
content: Hi, thanks a lot. I tried what you said and it still didn't work...I think I have a problem with the connection between the virtual CDC and the COM on the computer

author: hagy
author_rank: Newbie
timestamp: September 04, 2025, 03:33:09 pm
content: Hi, thanks.I tried uploading WeAct_HID_Bootloader_F4x1.hexvia STM32cubeProgremmer but still didn't help...

author: darkspr1te
author_rank: Frequent Contributor
timestamp: September 05, 2025, 12:53:48 pm
content: Ok i took your example code and added this to platformio.inibuild_flags =-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC-D USBCON-D USBD_VID=0x0483-D USBD_PID=0x5740-D USB_MANUFACTURER="Stm"-D USB_PRODUCT="\"BLUEPILL_F103C8\""-D HAL_PCD_MODULE_ENABLEDand serial worked

author: hagy
author_rank: Newbie
timestamp: September 05, 2025, 01:28:59 pm
content: Which means that the problem is really with my computer's drivers or the BOOTLOADER installed on the component.I don't see a new COM created when I connect it to the computer.In addition, when I work with Arduino IDE, it doesn't recognize a connection or disconnection when it happens.

author: darkspr1te
author_rank: Frequent Contributor
timestamp: September 05, 2025, 02:10:24 pm
content: I would try this,Install arduino IDE and load the serial port demo, enable CDC UART from the menu and upload,also try the blink demo too.the blink demo at least should work, if it does not then there is a issue elsewhere , a example would be having the wrong MCU set , even though the stm32f411 and stm32f407 are similar the clock system is not, so i cant upload 411 code to a 407 and vise versa,worth a check.darkspr1te

author: hagy
author_rank: Newbie
timestamp: September 05, 2025, 02:50:19 pm
content: After much testing, I see that the function:void timer_setup(void) {// Enable timer for GPIOA and TIM1RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;// Set pins PA8-PA11 as Alternative Function for TIM1GPIOA->MODER &= ~(0xFF << 16); // Clear bits 16-23GPIOA->MODER |= (0xAA << 16); // Set as Alternative FunctionGPIOA->AFR &= ~((0xF << 0) | (0xF << 4) | (0xF <<| (0xF << 12));GPIOA->AFR |= (1 << 0) | (1 << 4) | (1 <<| (1 << 12); // AF1 for TIM1// Timer 1 settingTIM1->CR1 = TIM_CR1_ARPE; // Only ARPE at the beginning, CEN at the endTIM1->CR2 = 0;TIM1->SMCR = 0;TIM1->DIER = 0;TIM1->EGR = 0;// PWM Mode setting for all 4 channelsTIM1->CCMR1 = (0b110 << 4) | TIM_CCMR1_OC1PE | (0b110 << 12) | TIM_CCMR1_OC2PE;TIM1->CCMR2 = (0b110 << 4) | TIM_CCMR2_OC3PE | (0b110 << 12) | TIM_CCMR2_OC4PE;// Channel activationTIM1->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E;// Setting frequency and initializing valuesTIM1->PSC = 71; // 72MHz / 72 = 1MHz (1μs resolution)TIM1->ARR = 5000; // 5000μs = 5ms period (200Hz)TIM1->DCR = 0;// Initializing all channels to 1000μs (engines off)TIM1->CCR1 = 1000;TIM1->CCR2 = 1000;TIM1->CCR3 = 1000;TIM1->CCR4 = 1000;// Enabling the outputs and the timer - finally!TIM1->BDTR |= TIM_BDTR_MOE; // Enable main outputTIM1->CR1 |= TIM_CR1_CEN; // Start timer}Problematic, and every time I upload code with it it throws the error.I'm afraid that the pins connected to TIM1 are also the TX and RX pins and maybe that's what's creating the problem...

author: ozcar
author_rank: Frequent Contributor
timestamp: September 06, 2025, 07:43:13 pm
content: I did not check if you do have conflicting pin usage there, but for when you sort that out, are you sure this is correct?Code:TIM1->PSC = 71; // 72MHz / 72 = 1MHz (1μs resolution)I don't think 72MHz would be the default for the board you are using, and I don't see that you did anything to the change the system clock configuration.