cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect USB MSC device connection and disconnection in stm32H743

ykn
Senior

hi,

I have just implemented USB MSC device and file system in my stm32h743. it work properly, as I connect to PC it shows the device. my code logic is, i will write configuration file to file system and and controller will read that file. this is happening in right manner.

problem is, whenever there is new config file in filesystem my controller should know it.

this is happen only when the device is disconnected from host. then controller will do whatever it want.

for that there is two function in usbd_conf.c file

#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
static void PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
#else
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
{
  /* Inform USB library that core enters in suspend Mode. */
  USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
  __HAL_PCD_GATE_PHYCLOCK(hpcd);
  /* Enter in STOP mode. */
  /* USER CODE BEGIN 2 */
  if (hpcd->Init.low_power_enable)
  {
    /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
    SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  }
  flag = 1;
  //HAL_Delay(1000);
  /* USER CODE END 2 */
}

but this function detect the USb at the time connection and disconnection.

Is any thing I have to do my side to detect at once . please guide me.

thanking you.

1 REPLY 1
ykn
Senior
USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev)
{
  pdev->dev_old_state = pdev->dev_state;
  pdev->dev_state = USBD_STATE_SUSPENDED;
  HAL_UART_Transmit (&huart1, "SUspend", 7, 1000);
  return USBD_OK;
}
 
/**
  * @brief  USBD_LL_Resume
  *         Handle Resume event
  * @param  pdev: device instance
  * @retval status
  */
 
USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev)
{
  if (pdev->dev_state == USBD_STATE_SUSPENDED)
  {
    pdev->dev_state = pdev->dev_old_state;
    HAL_UART_Transmit (&huart1, "Resume", 6, 1000);
  }
 
  return USBD_OK;
}

the above both the function executed when insert USb to PC