AnsweredAssumed Answered

MCU resume issue after USB cable unplugged: STML151

Question asked by Lyen.Rum on Mar 21, 2016
Hello all,

I'm using USB as a 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.
Thanks in advance.

int main(void)
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;   
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  // Does it need suspend-wakeup interrupt ?
  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..
  delay_us(450000); // 450ms delay
    if (bDeviceState == UNCONNECTED)       
       GPIO_SetBits(GPIOB, GPIO_Pin_5);      // USB Disconnected LED ON.
       GPIO_ResetBits(GPIOB, GPIO_Pin_6);   // USB Connected LED OFF.
       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*/