I've build USB-MIDI adapter with two endpoints based on USB-RAW example. I have 2 threads for USB processing: one for receiving data in a loop, one for sending when there is a need to do that.
Everything works without problems until I put the computer to sleep while the device is connected. It crashes right away after resuming from sleep (I made a led blink thread to catch the moment of crash).
MCU used: stm32f103c8t6.
My OS is Linux Manjaro, I didn't test that on Windows.
Assertion happens in receiving thread, inside usbStartReceiveI function:
Code: Select all
osalDbgAssert(!usbGetReceiveStatusI(usbp, ep), "already receiving");In fact, usbp->receiving is equal to 2.
Beginning of thread loop:
Code: Select all
THD_FUNCTION(usb_out_thread_loop, p)
{
(void)p;
while (!chThdShouldTerminateX())
{
//get free item from jack queue
midi_packet_s transaction_packets[16]; //64 bytes in endpoint / sizeof packet(4) = 16
msg_t size;
//receive from usb
while ((size = usbReceive(&USBD1, 1, (uint8_t*)transaction_packets, sizeof(transaction_packets))) <= 0)
{
//todo: sleep?
chThdSleepMilliseconds(100);
}
... packet processing...USB event callback is similar to one in USB_RAW example.
Code: Select all
static const USBEndpointConfig ep1config = {
USB_EP_MODE_TYPE_BULK, // Bulk EP
NULL, // SETUP packet notification callback
NULL, // IN notification callback
NULL, // OUT notification callback
0x0040, // IN maximum packet size
0x0040, // OUT maximum packet size
&ep1instate, // IN Endpoint state
&ep1outstate, // OUT endpoint state
1, // Reserved?
NULL // SETUP buffer (not a SETUP endpoint)
};
static void usb_event_cb(USBDriver* usbp, usbevent_t event)
{
(void)usbp;
switch (event)
{
case USB_EVENT_RESET:
return;
case USB_EVENT_ADDRESS:
return;
case USB_EVENT_CONFIGURED:
chSysLockFromISR();
usbInitEndpointI(usbp, 1, &ep1config);
chSysUnlockFromISR();
return;
case USB_EVENT_SUSPEND:
return;
case USB_EVENT_WAKEUP:
return;
case USB_EVENT_STALLED:
return;
case USB_EVENT_UNCONFIGURED:
return;
}
}
I suspect there is a bug inside HAL, or at least USB_RAW example. What may be causing this?
Thank you for your help