2017-02-17 10:57 AM
I'm developing firmware for a low power USB 2.0 Full Speed device (12 Mbps) which uses an
http://www.st.com/en/microcontrollers/stm32f070cb.html
microcontroller. The clock source for the USB module isthe High Speed External (HSE) oscillator. I'm usinghttp://www.st.com/en/development-tools/stm32cubemx.html
with version 1.7.0 of to generate the codebase.To conserve our power budget, we need to turn off the HSE when the USB is not connected. I'm thinking we should make the firmware delay USB detection by the host until the device is ready, so that the events in the firmware would go something like this:
Is the above the right way to go about this? Are the actions all required? Are they in the right order?
Do I need to call any functions besides USBD_DeInit() to de-init USB?
How can I make the device appear disconnected on the host while physically connected? (i.e. It should disappear from Windows Device Manager.) Why doesn't calling HAL_PCD_DevDisconnect() cause that to happen?
In the following paragraphs, to 'physically connect' means to connect the USB cable to the device and the host PC.
I would expect all calls to MX_USB_DEVICE_Init() and MX_USB_DEVICE_DeInit() to take effect immediately, i.e. that I would see the device appear/disappear in Windows Device Manager if the function was called while the device is physically connected. However, I've observed this only on the first call to MX_USB_DEVICE_Init(). All calls to MX_USB_DEVICE_DeInit() and subsequent calls to MX_USB_DEVICE_Init() don't cause the device to appear/disappear from Windows Device Manager until after physical reconnection. (i.e. If it's connected, when the function is called, no change in Device Manager until after disconnect/reconnect.)
CodeAs generated by STM32CubeMX:
/* USB Device Core handle declaration */USBD_HandleTypeDef hUsbDeviceFS;/* init function */ void MX_USB_DEVICE_Init(void){ /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC); USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS); USBD_Start(&hUsbDeviceFS);}
I created:
void MX_USB_DEVICE_DeInit(void){ HAL_PCD_DevDisconnect((PCD_HandleTypeDef *)&hUsbDeviceFS.pData); USBD_Stop (&hUsbDeviceFS); USBD_DeInit(&hUsbDeviceFS);}
It makes no difference whether or when I call HAL_PCD_DevDisconnect(). I have never observed the device disappear from Windows Device Manager while physically connected.
#usb-disconnect #hse #usb-device #stm32f070 #usb