2016-03-21 10:55 AM
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_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*/ }