cancel
Showing results for 
Search instead for 
Did you mean: 

How to disable USB Mass storage at runtime.

ABatt.1
Senior

I to all,

I have a project where an STM32F013RTC use an SD card.

At run time the SD is share with FAT_FS or it is used as USB mass storage device, but not at the same time.

I need to disable USB module.

The configuration is made from CubeMX, so I have the function MX_USB_DEVICE_Init() to enable and configure USB.

void MX_USB_DEVICE_Init(void)
{
  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
 
  /* USER CODE END USB_DEVICE_Init_PreTreatment */
 
  /* Init Device Library, add supported class and start the library. */
  if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_Storage_Interface_fops_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  {
    Error_Handler();
  }
 
  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
 
  /* USER CODE END USB_DEVICE_Init_PostTreatment */
}

I wrote a function to disable USB:

void MX_USB_DEVICE_DeInit(void){
	USBD_DeInit(&hUsbDeviceFS);
}

But this cause an Hard Fault reset.

Thre is an HAL function to disable USB module ?

This discussion is locked. Please start a new topic to ask your question.
1 ACCEPTED SOLUTION

Accepted Solutions
ABatt.1
Senior

HI, I resolved the issue by using low level function provided from HAL:

void MX_USB_DEVICE_Stop(void){
	USBD_LL_Stop(&hUsbDeviceFS);
}
 
void MX_USB_DEVICE_Start(void){
	USBD_LL_Start(&hUsbDeviceFS);
}

I add this two in USB_device.c ( forv isibility of hUsbDeviceFS).

It works fine!

View solution in original post

3 REPLIES 3
TDK
Super User

Perhaps debug the hard fault to determine the reason for the error.

HAL libraries are not created with robustness in mind, especially for uncommon scenarios.

If you feel a post has answered your question, please click "Accept as Solution".
ABatt.1
Senior

Hi TDK,

Hard fault is rised at exit of this function

void MSC_BOT_DeInit(USBD_HandleTypeDef *pdev){
 
 USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassData;
 
 hmsc->bot_state = USBD_BOT_IDLE;
}

It is called in USBD_MSC_DeInit();

uint8_t USBD_MSC_DeInit(USBD_HandleTypeDef *pdev,
                         uint8_t cfgidx){
  /* Close MSC EPs */
  USBD_LL_CloseEP(pdev, MSC_EPOUT_ADDR);
  pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 0U;
 
  /* Close EP IN */
  USBD_LL_CloseEP(pdev, MSC_EPIN_ADDR);
  pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 0U;
 
  /* De-Init the BOT layer */
  MSC_BOT_DeInit(pdev);
 
  /* Free MSC Class Resources */
  if (pdev->pClassData != NULL)
  {
    USBD_free(pdev->pClassData);
    pdev->pClassData  = NULL;
  }
 
  return USBD_OK;
}

Every pointer used is not NULL.

For the scenarios, this is simply:

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
 
  MX_USB_DEVICE_DeInit();

I did this to avoid having to manually modify the code every time I change something in CubeMX (the project is still in the prototype phase)

ABatt.1
Senior

HI, I resolved the issue by using low level function provided from HAL:

void MX_USB_DEVICE_Stop(void){
	USBD_LL_Stop(&hUsbDeviceFS);
}
 
void MX_USB_DEVICE_Start(void){
	USBD_LL_Start(&hUsbDeviceFS);
}

I add this two in USB_device.c ( forv isibility of hUsbDeviceFS).

It works fine!