So, long story short - I finally managed to get myself out of the Arduino framework which I never liked but it has been too convenient for most of the stuff I've had to do in embedded MCUs the past few years. I'm trying to get things up and running on a WeAct BlackPill which is based on a STM32F401CC controller.
At this point, all I want to do is get a USB CDC device going so that I can communicate properly with my software using a serial terminal on my PC.
The steps I follow to make this happen, is that I set up the device in STM32Cube, enable USB_OTG_FS as device only, no low power, no link power management, no VBUS sensing and no SOF. I then add a CDC class for it and make no changes to that. Finally I save and use stm32pio to generate a PlatformIO compatible project.
So far, so good. The code compiles and runs fine on the board. There seems to be an issue with the generated code so CDC_SET_LINE_CODING and CDC_GET_LINE_CODING needs some massaging to get the device to enumerate but google was very helpful there. With that problem sorted, I can now have
while (1)
{
CDC_Transmit_FS((uint8_t *)".", 1);
HAL_Delay(1000);
}
print a dot every second. The issue is that it the checks in the CDC_Transmit_FS() function doesn't seem to be able to detect if I'm connected or not until after a the first transmission. So if I wait a couple of seconds before I open the COM port from the PC, I will get ".." as the first printout and after that I get single period. Same thing if I disconnect and reconnect a few seconds later - I get ".." at the start.
I've been trying to google this problem but the best solution I see is to monitor VBUS but that isn't connected on a BlackPill. Apparently the abstraction layers are capable of detecting that the first "." sent is not transmitted and doesn't queue another transmission until I'm actually connected again.
I've done some debugging and pretty much concluded that the pClassData->TxState variable being set to 1 is the only thing that causes CDC_Transmit_FS() to bail out early after the first "." is in the queue until the host connects again.
I feel like I'm missing something very basic here but I'm kind of lost as to what that is.
Anyone have any ideas/pointers?