1 #include <rtthread.h>
2 #include <stdint.h>
3 #include <stdbool.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include "nrf.h"
7 #include "nrfx_usbd.h"
8 #include "nrfx_clock.h"
9 #include "nrfx_power.h"
10 
usb_dc_low_level_post_init(void)11 void usb_dc_low_level_post_init(void)
12 {
13     /* Enable interrupt globally */
14     NRFX_IRQ_PRIORITY_SET(USBD_IRQn, NRFX_USBD_CONFIG_IRQ_PRIORITY);
15     NRFX_IRQ_ENABLE(USBD_IRQn);
16 }
17 
18 extern void cherry_usb_hal_nrf_power_event(uint32_t event);
power_event_handler(nrfx_power_usb_evt_t event)19 static void power_event_handler(nrfx_power_usb_evt_t event)
20 {
21     cherry_usb_hal_nrf_power_event((uint32_t)event);
22 }
23 
usb_dc_low_level_pre_init(void)24 void usb_dc_low_level_pre_init(void)
25 {
26     uint32_t usb_reg;
27     const nrfx_power_usbevt_config_t config = {.handler = power_event_handler};
28     nrfx_power_usbevt_init(&config);
29     nrfx_power_usbevt_enable();
30     usb_reg = NRF_POWER->USBREGSTATUS;
31 
32     if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk)
33     {
34         cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
35     }
36 
37     if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
38     {
39         cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
40     }
41 }
42 
usb_low_clear_pending_irq(void)43 void usb_low_clear_pending_irq(void)
44 {
45     NVIC_ClearPendingIRQ(USBD_IRQn);
46 }
47 
usb_low_disable_irq(void)48 void usb_low_disable_irq(void)
49 {
50     NVIC_DisableIRQ(USBD_IRQn);
51 }
52 
cherryusb_protocol_stack_init(void)53 int cherryusb_protocol_stack_init(void)
54 {
55 #ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
56     extern void cdc_acm_init(void);
57     cdc_acm_init();
58     rt_kprintf("cdc acm example started. \r\n");
59 #elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_MSC
60     extern void msc_ram_init(void);
61     msc_ram_init();
62     rt_kprintf("msc ram example started. \r\n");
63 #elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD
64     extern void hid_keyboard_init(uint8_t busid, uintptr_t reg_base);
65     hid_keyboard_init(0,NULL);
66     rt_kprintf("hid keyboard example started. \r\n");
67 #endif
68 }
69 
70 INIT_APP_EXPORT(cherryusb_protocol_stack_init);