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?

Related