Ben K

USB Device Library descriptor read HardFault

Discussion created by Ben K on Sep 1, 2017

I have been using (and upgrading) the STM32 USB Device Library, and I have found the following bug:

After I have changed the USB device descriptors to const (it is always nice to save on some wasted RAM), the device would end up in a HardFault trap during the readout of the device descriptors. I traced back the problem to the following lines in usbd_ctrlreq.c:

 case USB_DESC_TYPE_CONFIGURATION: 
if(pdev->dev_speed == USBD_SPEED_HIGH )
{
pbuf = (uint8_t *)pdev->pClass->GetHSConfigDescriptor(&len);
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
}
else
{
pbuf = (uint8_t *)pdev->pClass->GetFSConfigDescriptor(&len);
pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
}
break;

 

and 

 

 case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
if(pdev->dev_speed == USBD_SPEED_HIGH )
{
pbuf = (uint8_t *)pdev->pClass->GetOtherSpeedConfigDescriptor(&len);
pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION;
break;
}

When the descriptors are constant data, they are directly allocated to flash memory, and the marked lines are trying to write a read-only memory area, which results in an exception. These lines are actually completely unnecessary and may just be remnants of early development code (see also USBD_RunTestMode), as the same value is already located on that byte of the configuration.

Outcomes