This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STML151 issue: MCU resume after unplugging USB cable

Hello all,

I'm using simple USB mass storage (MSC using FS_Device_Lib_4.0, STM32L151 in a custom PCB). I need to detect USB connectivity; MCU will run normal data logging operation if USB cable not connected. If connected, it'll halt all operation and only use as mass-storage. Following sample code works but when I disconnect cable after PC transfer, MCU isn't resuming its normal state, keeps USB connected indicator LED on.

Is there any other way to detect USB connectivity and switch between states. Anything to change in interrupt handler? I request the sample code if available. I've posted the same query in ST forum, haven't found any response there.

Thanks in advance.

int main(void)
{
  .......
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStructure);

  // Does it need suspend-wakeup interrupt ?

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);

  // Works without internal pull-up (SYSCFG_USB) for STM32L151. 1K pull-up Resistor connected with PA11 and D+ pin..

  SD_Init();
  USB_Init();
  delay_us(450000); // 450ms delay

    while(1)
    {
    if (bDeviceState == UNCONNECTED)
    {
       GPIO_SetBits(GPIOB, GPIO_Pin_5);      // USB Disconnected LED ON.
       GPIO_ResetBits(GPIOB, GPIO_Pin_6);   // USB Connected LED OFF.
    }
    else
    {
       GPIO_SetBits(GPIOB, GPIO_Pin_6);        // USB Connected LED ON.
       GPIO_ResetBits(GPIOB, GPIO_Pin_5);    // USB Disconnected LED OFF.
    }

/*Also tried bDeviceState == CONFIGURED for connected and ELSE for disconnected*/
   }