Bridgetek / ft9xx-sdk

ft90x SDK
MIT License
6 stars 0 forks source link

USB device power management #49

Closed brtchip-gdm closed 8 months ago

brtchip-gdm commented 8 months ago

The USBD source file can have a function which can be called from a power management (PM) interrupt service routine (ISR). The PM routine is usually called from an interrupt when one of the PM interrupts are pending. It is setup with a line like this:

interrupt_attach(interrupt_0, (int8_t)interrupt_0, powermanagement_ISR);

Most applications that use the USB device will have the following code to handle the transitions of PM when the device is suspended, resumed, connected or disconnected.

void powermanagement_ISR(void)
{
    if (SYS->PMCFG_H & MASK_SYS_PMCFG_DEV_CONN_DEV)
    {
        // Clear connection interrupt
        SYS->PMCFG_H = MASK_SYS_PMCFG_DEV_CONN_DEV;
        USBD_attach();
    }

    if (SYS->PMCFG_H & MASK_SYS_PMCFG_DEV_DIS_DEV)
    {
        // Clear disconnection interrupt
        SYS->PMCFG_H = MASK_SYS_PMCFG_DEV_DIS_DEV;
        USBD_detach();
    }

    if (SYS->PMCFG_H & MASK_SYS_PMCFG_HOST_RST_DEV)
    {
        // Clear Host Reset interrupt
        SYS->PMCFG_H = MASK_SYS_PMCFG_HOST_RST_DEV;
        USBD_resume();
    }

    if (SYS->PMCFG_H & MASK_SYS_PMCFG_HOST_RESUME_DEV)
    {
        // Clear Host Resume interrupt
        SYS->PMCFG_H = MASK_SYS_PMCFG_HOST_RESUME_DEV;
        if(! (SYS->MSC0CFG & MASK_SYS_MSC0CFG_DEV_RMWAKEUP))
        {
            // If we are driving K-state on Device USB port;
            // We must maintain the 1ms requirement before resuming the phy
            USBD_resume();
        }
    }
}

Unless there is special handling of these conditions then the code can be added to the USBD API. A function is proposed which can be called from the PM ISR. It is proposed to call it USBD_power_change(void) or similar.

The ISR would therefore look like this after the addition:

void powermanagement_ISR(void)
{
    USBD_power_change();
}
brtchip-gdm commented 8 months ago

This could also apply to the RTC interrupt_0 handler on FT90x Rev B.

brtchip-gdm commented 8 months ago

We could also add functions to sys.c to enable the RTC alarms, sleep and wake the system:

These are only used in the RTC Example 2 project.

brtchip-gdm commented 8 months ago

Done.