summaryrefslogtreecommitdiff
path: root/bsp/nrf5x/nrf52840/board/port/cherryusb/cherryusb.c
blob: 3212e3ecc9894fcd76db5b6e921c62cc92071817 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
#include <rtthread.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "nrf.h"
#include "nrfx_usbd.h"
#include "nrfx_clock.h"
#include "nrfx_power.h"

void usb_dc_low_level_post_init(void)
{
    /* Enable interrupt globally */
    NRFX_IRQ_PRIORITY_SET(USBD_IRQn, NRFX_USBD_CONFIG_IRQ_PRIORITY);
    NRFX_IRQ_ENABLE(USBD_IRQn);
}

extern void cherry_usb_hal_nrf_power_event(uint32_t event);
static void power_event_handler(nrfx_power_usb_evt_t event)
{
    cherry_usb_hal_nrf_power_event((uint32_t)event);
}

void usb_dc_low_level_pre_init(void)
{
    uint32_t usb_reg;
    const nrfx_power_usbevt_config_t config = {.handler = power_event_handler};
    nrfx_power_usbevt_init(&config);
    nrfx_power_usbevt_enable();
    usb_reg = NRF_POWER->USBREGSTATUS;

    if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk)
    {
        cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
    }

    if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
    {
        cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
    }
}

void usb_low_clear_pending_irq(void)
{
    NVIC_ClearPendingIRQ(USBD_IRQn);
}

void usb_low_disable_irq(void)
{
    NVIC_DisableIRQ(USBD_IRQn);
}

int cherryusb_protocol_stack_init(void)
{
#ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
    extern void cdc_acm_init(void);
    cdc_acm_init();
    rt_kprintf("cdc acm example started. \r\n");
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_MSC
    extern void msc_ram_init(void);
    msc_ram_init();
    rt_kprintf("msc ram example started. \r\n");
#elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD
    extern void hid_keyboard_init(uint8_t busid, uintptr_t reg_base);
    hid_keyboard_init(0,NULL);
    rt_kprintf("hid keyboard example started. \r\n");
#endif
}

INIT_APP_EXPORT(cherryusb_protocol_stack_init);