cancel
Showing results for 
Search instead for 
Did you mean: 

How to start/stop USB with change in application state?

crwper
Senior

I am building a test application on the P-NUCLEO-WB55-Nucleo and would like to switch between two application states with the push of a button:

  1. In the first state, the application writes to an attached microSD card using FatFS.
  2. In the second state, the application acts as a USB mass storage device allowing access to the contents of the microSD card.

I've been able to implement both states in separate applications. However, in order to do this in one application, I will need to start/stop USB functionality when the application state changes.

How would I start/stop USB in my application code so that the board does not enumerate if plugged in while in the first state, but shows up as a USB mass storage device if plugged in while in the second state? The CubeMX generated code seems well-adapted for an application which initializes USB once and then leaves it running until the application shuts down, but I haven't been able to figure out a good way to start/stop USB while the application is running.

Thanks for your help!

1 REPLY 1
crwper
Senior

I think I've found an answer to my own question.

Initialization code for the peripheral libraries can be controlled by opening the device configuration tool, going to "Project Manager", then "Advanced Settings", then checking/unchecking the box under "Not Generate Function Call". When this box is checked, the initialization function for that peripheral will not be called, and it can then be called where needed.

For USB in particular, I get good results if I initialize USB by calling:

MX_USB_Device_Init();

And de-initialize it by calling:

USBD_Stop(&hUsbDeviceFS);