We have a working solution for anyone interested:
Hardware condition is:
- VDDUSB pin is connected to 3.3V power supply (fix supply)
- USB-Mode is FS Device only, no OTG, no USB-C
in CubeMX configuration do the following:
- In NVIC configuration activate Interrupt for PVD/PVM1/...
- On the Advanced Settings Page of the Project Manager activate "Not Generate Function Call" for MX_USB_Device_Init
- In USB Setup select "Link Power Management not supported"
1) At power up, after intialisation of GPIOS etc. has been done, call following routine "DoInitUSB" which configures the PVM (Peripheral Voltage Monitoring) interrupt for PVM1 (for USB) as follows:
void DoInitUSB(void)
{
static int bInit = TRUE;
PWR_PVMTypeDef sConfigPVM;
sConfigPVM.PVMType = PWR_PVM_1;
sConfigPVM.Mode = PVM_MODE_IT | PVM_RISING_EDGE | PVM_FALLING_EDGE; // Create an interrupt for power detection
HAL_PWREx_ConfigPVM(&sConfigPVM);
//Activate Peripheral Voltage Monitoring
HAL_PWREx_EnablePVM1();
// Activate Power for USB
HAL_PWREx_EnableVddUSB();
// Wait 2 MS
ParaDelay(PARA_DELAY_1MS);
ParaDelay(PARA_DELAY_1MS);
//Check if USB plug connected:
if (READ_BIT(PWR->SR2, PWR_SR2_PVMO1) == 0) // Power is above 1.2 Volt
{
HAL_PWREx_EnableVddUSB();
if (bInit)
{
MX_USB_Device_Init();
bInit = FALSE; // Only do once after power on
}
bPowerIsOn = TRUE;
}
else
{
HAL_PWREx_DisableVddUSB();
bPowerIsOn = FALSE;
}
}
2) Include following callback fpr PVM Interrupt:
void HAL_PWREx_PVM1Callback()
{
if (READ_BIT(PWR->SR2, PWR_SR2_PVMO1) == 0) // Power is above 1.2 Volt
{
HAL_PWREx_EnableVddUSB();
if (bInit)
{
MX_USB_Device_Init();
bInit = FALSE;
}
bPowerIsOn = TRUE;
}
else // Power is below 1.2 Volt
{
HAL_PWREx_DisableVddUSB();
bPowerIsOn = FALSE;
}
}
I hope this setup helps others!