How to initialize and uninitialize USBD dynamically

Hello there

I need to dynamically initialize and uninitialize the USB HID in my application. Because there is a condition on which I don't want any PC to recognize my device as a USB HID.

If I initialize the USB in Startup and uninitialize it at the end of Startup (I mean in the beginning and end of the main function before the main loop), there is no problem with uninitializing and the USB HID that I initialized in the beginning won't be visible as a USB HID device. Now if I initialize the USB in the middle of my app though some conditions, it is visible as a USB HID device and I can do the data transfer but when I'm done with the data transfer and want to uninitialize it to not be visible as a USB HID device, the microcontroller resets.

I use app_usbd_uninit().

I tried having a timer for uninitializing, or doing it after app_usbd_stop() and app_usbd_disable() but it does not work. I also tried nrf_drv_usbd_uninit()...

Can anyone help me with this procedure?

Parents Reply Children
  • Sorry for the late answer. You might have solved your issue but I just wanted to say that for implementing the dynamic initialization, it looks like you need to reset the microcontroller. 

    If I wanted to implement the dynamic initialization and did not mind microcontroller reset, I would use the fds functions to save the desired mode and check it on startup.
    Anytime I wanted to reinitialize the USBD in a different way (different HID config for instance) I would save the desired initialization flag in flash and reset the microcontroller by software.

Related