Hi, thanks for your suggestion and sorry for the late response.
I figured out that this method is working well on non-RTOS examples. But unfortunately, it doesn't with the FreeRTOS example. If I call usbReEnumerate() from other contexts, such as the timer task, the USB fails to enumerate. And I'm struggling to answer why that happening. I used lpcxpresso54628_dev_hid_mouse_freertos project with minor modifications as an experimental ground.
#define USB_DEVICE_CONFIG_LPCIP3511HS (1U)
#define USB_DEVICE_CONFIG_USE_TASK (1U)
#define USB_DEVICE_CONFIG_COMPLIANCE_TEST (1U)
#define USB_DEVICE_CONFIG_DETACH_ENABLE (1U)
In mouse.c I have added // TEST_CODE comment in places where example was modified. Basically I created a one shot 1 sec software timer, starting it after usb got configured and call usbReEnumerate() in timer callback. Modified files are in attachment.
static void usbReEnumerate(TimerHandle_t xTimer) // TEST_CODE
{
(void) xTimer;
usb_status_t res = kStatus_USB_Success;
// usb_echo("usbReEnumerate\r\n");
if (g_UsbDeviceHidMouse.attach) {
USB_DeviceStop(g_UsbDeviceHidMouse.deviceHandle); // always returns kStatus_USB_Error
g_UsbDeviceHidMouse.attach = 0; // allows ignoring suspend event after re-enable.
NVIC_DisableIRQ(USB1_IRQn);
usb_status_t res = USB_DeviceClassDeinit(CONTROLLER_ID);
// usb_status_t res = USB_DeviceDeinit(g_UsbDeviceHidMouse.deviceHandle);
// res |= USB_DeviceInit(CONTROLLER_ID, USB_DeviceCallback, &g_UsbDeviceHidMouse.deviceHandle);
res |= USB_DeviceClassInit(CONTROLLER_ID, &g_UsbDeviceHidConfigList, &g_UsbDeviceHidMouse.deviceHandle);
NVIC_ClearPendingIRQ(USB1_IRQn);
NVIC_EnableIRQ(USB1_IRQn);
USB_DeviceRun(g_UsbDeviceHidMouse.deviceHandle); // always returns kStatus_USB_Error
}
assert(kStatus_USB_Success == res);
}
I also tried to use directly USB_DeviceInit() and USB_DeviceDeinit() but that didn't work also. USB_DeviceClassInit and USB_DeviceClassDeinit eventually call those functions so I think it's not a big deal here. Strange thing happens if uncomment // usb_echo("usbReEnumerate\r\n"); After that usb re-enumerates succesfully. And that's confusing.