AnsweredAssumed Answered

STM32L4 HAL_PCDEx_BCD_VBUSDetect() Broken for (non-OTG) USB

Question asked by riggs.rob on Jul 2, 2017
Latest reply on Jul 3, 2017 by Imen D

I am using STM32CubeMX 4.21.0 and STM32L4 HAL version 1.8.1.

 

The HAL_PCDEx_BCD_VBUSDetect() function for "USB" and "USB_OTG_FS" differ and the "USB" version is buggy.

 

The HAL_PCDEx_BCD_VBUSDetect() function is supposed to call HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED) when the BCD discovery is complete.  However, the way the "USB" version of the function is written, it will only be called when a "Charging Downstream Port" is detected and not for the other port types.

 

The code as written looks like this:

 /* If Charger detect ? */
if (USBx->BCDR & USB_BCDR_PDET)
{
   /* Start secondary detection to check connection to Charging Downstream
   Port or Dedicated Charging Port */

   USBx->BCDR &= ~(USB_BCDR_PDEN);
   USBx->BCDR |= (USB_BCDR_SDEN);
   HAL_Delay(300);

   /* If CDP ? */
   if (USBx->BCDR & USB_BCDR_SDET)
   {
     /* Dedicated Downstream Port DCP */
     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
   }
   else
   {
     /* Charging Downstream Port CDP */
     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);

     /* Battery Charging capability discovery finished
     Start Enumeration*/

     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
   }
}
else /* NO */
{
   /* Standard Downstream Port */
   HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
}

The code should look like this (which is essentially the same as the USB_OTG_FS version in the same file):

 /* If Charger detect ? */
if (USBx->BCDR & USB_BCDR_PDET)
{
   /* Start secondary detection to check connection to Charging Downstream
   Port or Dedicated Charging Port */

   USBx->BCDR &= ~(USB_BCDR_PDEN);
   USBx->BCDR |= (USB_BCDR_SDEN);
   HAL_Delay(300);

   /* If CDP ? */
   if (USBx->BCDR & USB_BCDR_SDET)
   {
     /* Dedicated Downstream Port DCP */
     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
   }
   else
   {
     /* Charging Downstream Port CDP */
     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
   }
}
else /* NO */
{
   /* Standard Downstream Port */
   HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
}

/* Battery Charging capability discovery finished
Start Enumeration*/

HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);

 

HAL_PCDEx_BCD_VBUSDetect() must callHAL_PCDEx_BCD_Callback() with PCD_BCD_DISCOVERY_COMPLETED without regard to the port type detected.

 

Please fix this.

Outcomes