cancel
Showing results for 
Search instead for 
Did you mean: 

osThreadTerminate() kills the application when called in usb_host.c.

HTD
Senior III

Here's my USBH_UserHostProcess():

/*
 * user callback definition
 */
static void USBH_UserProcess  (USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  switch(id)
  {
  case HOST_USER_SELECT_CONFIGURATION:
    break;
 
  case HOST_USER_DISCONNECTION:
    Appli_state = APPLICATION_DISCONNECT;
    fs_available &= ~FS_AVAILABLE_USB;
    osThreadTerminate(usbhApplicationTaskHandle);
    usbhApplicationTaskHandle = NULL;
    break;
 
  case HOST_USER_CLASS_ACTIVE:
    Appli_state = APPLICATION_READY;
    usbhApplicationTaskHandle = osThreadNew((osThreadFunc_t)USBH_Application, phost, &usbhApplication_attributes);
    break;
 
  case HOST_USER_CONNECTION:
    Appli_state = APPLICATION_START;
    break;
 
  default:
    break;
  }
  /* USER CODE END CALL_BACK_1 */
}

And USBH_Application():

void USBH_Application(USBH_HandleTypeDef *phost)
{
  FRESULT fr = f_mount(&USBHFatFS, USBHPath, 0x1);
  if (fr == FR_OK) fs_available |= FS_AVAILABLE_USB;
  else X_Error(X_E_FS_USB_INIT);
  X_ExternalStorageAvailable();
  for (;;) osDelay(1);
}

When the USB disk is inserted USBH_Application() is called, the thread reaches line 7 and spins there. When I remove the USB disk, the debugger reaches line 15 of USBH_UserProcess(), when osThreadTerminate(usbhApplicationTaskHandle) is called. Then everything stops, application is dead.

BTW:

static osThreadId_t usbhApplicationTaskHandle;
static const osThreadAttr_t usbhApplication_attributes = {
  .name = "USB_Host_Application",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

What am I doing wrong? I assumed I should not block the USB host thread, so I start a new thread that mounts the USB filesystem and handles tasks that automatically load files from the disk. It works. Everything is completed in that thread and it just sits there waiting. Then, when the USB disk is removed I want to dispose the thread and release the memory allocated to it, as I know FreeRTOS allocates stack for the thread.

Why does it crash when the osThreadTerminate() is called?

I'm not sure if it worked before, but now it happens every time I remove the USB disk.

Is it possible the USB Host kills the task itself, so calling task termination manually causes the crash?

0 REPLIES 0