cancel
Showing results for 
Search instead for 
Did you mean: 

Hard Fault in USBH_MSC_RdWrProcess() and USBH_MSC_GetLUNInfo()

I have a STM32H7 and I use USB MSC. I do not use an RTOS.
If I plug in my USB thumb drive I append a counter to a file and close it.
This works.
If I remove the thumb drive very quickly at precisely the wrong moment I get a hardfault.
if Appli_state == APPLICATION_READY I set a flag and call f_open in the main loop. But then the USB drive is already gone, but there is no NULL pointer check.

We call the USBH_Process() and USBH_MSC_AppProcess() in a timer interrupt and have modified it to have non-blocking delays. I wonder if this could be the cause or if there is an actual bug in the driver that misses to check the USB drive is not available.

The following modification made to USBH_MSC_RdWrProcess() fixes the problem of hardfaults.

 

 

 

 

static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun)
{
  USBH_StatusTypeDef error = USBH_BUSY;
  USBH_StatusTypeDef scsi_status = USBH_BUSY;

  if (phost->pActiveClass == NULL)
  {
      error = USBH_FAIL;
      return error;
  }

  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;

 

 

 

 

Edit: I found an old thread that mentions the same problem:

https://community.st.com/t5/stm32-mcus-embedded-software/usb-host-library-is-not-thread-safe/m-p/307489

Another modification I made is to call an idle handler in busy waiting loops. This is not the cause of the issue, but it does address the issue mentioned by @NAgha.1 in that topic:

 

  while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY)
  {
    if (((phost->Timer - timeout) > (10000U * length)) || (phost->device.PortEnabled == 0U))
    {
      return USBH_FAIL;
    }
    // start of modification
    else
    {
    	// prevent blocking other processes from main context
    	usbIdleLoop();
    }
    // end of modification
  }

 

 

main:

 

void usbIdleLoop()
{
    //does non-usb things
}

int main(){
...
while(1)
{
usbIdleLoop();
usbApplication();//does fat-fs things with usb and calls usbIdleLoop in loops that were previously busy waiting to prevent blocking
}
}

 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.
0 REPLIES 0