AnsweredAssumed Answered

STM32L432KC HAL_PCDEx_BCD_VBUSDetect Bug

Question asked by riggs.rob on Feb 4, 2017

I am using CubeMX with the STM32L4 1.5.2 library. [EDIT -- just confirmed that this exists in 1.6.0 as well]

 

The non-OTG implementation of HAL_PCDEx_BCD_VBUSDetect() will only call  HAL_PCDEx_BCD_Callback() with PCD_BCD_DISCOVERY_COMPLETED if it detects a PCD_BCD_CHARGING_DOWNSTREAM_PORT.

 

The correct behavior should be to call HAL_PCDEx_BCD_Callback() with PCD_BCD_DISCOVERY_COMPLETED after any type of port is detected.

 

This is the relevant section of the defective code:

 

  /* 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);
  }

 

This is what a correctly working version looks like:

 

  /* 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);

As an aside, it would be helpful if STM's HAL libraries were hosted in a public git repo so one could file a bug report and provide a pull request for the fix, rather than having to post this sort of thing in a public forum.  It is ill suited for reporting and fixing software defects.

Outcomes