cherry-embedded / CherryUSB

CherryUSB is a tiny and portable USB Stack (device & host) for embedded system with USB IP
https://cherryusb.readthedocs.io/
Apache License 2.0
1.14k stars 245 forks source link

FreeRTOS OSAL function for starting a timer needs to be safe to call from an ISR #189

Closed dkonigsberg closed 3 months ago

dkonigsberg commented 3 months ago

In the latest version of CherryUSB (most likely as of commit 2660af5), the function usb_osal_timer_start is called from within an ISR during host-side device enumeration.

As of the v1.1.2 release, in the FreeRTOS port (usb_osal_freertos.c) this function just calls xTimerStart. Doing that is not safe from within an ISR, and will cause an assertion to be thrown.

One fix for this is to modify the function as follows:

void usb_osal_timer_start(struct usb_osal_timer *timer)
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    int ret;

    if (xPortIsInsideInterrupt()) {
        ret = xTimerStartFromISR(timer->timer, &xHigherPriorityTaskWoken);
        if (ret == pdPASS) {
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }
    } else {
        xTimerStart(timer->timer, 0);
    }
}

I've had to make this change in my local codebase to get CherryUSB working in my environment.

sakumisu commented 3 months ago

Thanks, has fixed.