1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "global.h"
#include "core/synopsysotg/synopsysotg.h"

static const struct usb_instance* synopsysotg_fsinstance;
static const struct usb_instance* synopsysotg_hsinstance;

void synopsysotg_target_enable_clocks(const struct usb_instance* instance)
{
    const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config;
    if (data->core == &USB_OTG_HS) RCC->AHB1ENR |= RCC_AHB1ENR_OTGHSEN;
    else if (data->core == &USB_OTG_FS) RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN;
}

void synopsysotg_target_enable_irq(const struct usb_instance* instance)
{
    const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config;
    if (data->core == &USB_OTG_HS)
    {
        synopsysotg_hsinstance = instance;
        NVIC_EnableIRQ(OTG_HS_IRQn);
    }
    else if (data->core == &USB_OTG_FS)
    {
        synopsysotg_fsinstance = instance;
        NVIC_EnableIRQ(OTG_FS_IRQn);
    }
}

void synopsysotg_target_disable_irq(const struct usb_instance* instance)
{
    const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config;
    if (data->core == &USB_OTG_HS) NVIC_DisableIRQ(OTG_HS_IRQn);
    else if (data->core == &USB_OTG_FS) NVIC_DisableIRQ(OTG_FS_IRQn);
}

void synopsysotg_target_clear_irq(const struct usb_instance* instance)
{
    const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config;
    if (data->core == &USB_OTG_HS) NVIC_ClearPendingIRQ(OTG_HS_IRQn);
    else if (data->core == &USB_OTG_FS) NVIC_ClearPendingIRQ(OTG_FS_IRQn);
}

void otg_fs_irqhandler()
{
    if (synopsysotg_fsinstance) synopsysotg_irq(synopsysotg_fsinstance);
    else NVIC_DisableIRQ(OTG_FS_IRQn);
}

void otg_hs_irqhandler()
{
    if (synopsysotg_hsinstance) synopsysotg_irq(synopsysotg_hsinstance);
    else NVIC_DisableIRQ(OTG_HS_IRQn);
}

void __attribute__((weak)) synopsysotg_irq(const struct usb_instance* instance)
{
    if (instance == synopsysotg_fsinstance) NVIC_DisableIRQ(OTG_FS_IRQn);
    if (instance == synopsysotg_hsinstance) NVIC_DisableIRQ(OTG_HS_IRQn);
}