cherryusb.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. void usb_dc_low_level_post_init(void)
  11. {
  12. /* Enable interrupt globally */
  13. NRFX_IRQ_PRIORITY_SET(USBD_IRQn, NRFX_USBD_CONFIG_IRQ_PRIORITY);
  14. NRFX_IRQ_ENABLE(USBD_IRQn);
  15. }
  16. extern void cherry_usb_hal_nrf_power_event(uint32_t event);
  17. static void power_event_handler(nrfx_power_usb_evt_t event)
  18. {
  19. cherry_usb_hal_nrf_power_event((uint32_t)event);
  20. }
  21. void usb_dc_low_level_pre_init(void)
  22. {
  23. uint32_t usb_reg;
  24. const nrfx_power_usbevt_config_t config = {.handler = power_event_handler};
  25. nrfx_power_usbevt_init(&config);
  26. nrfx_power_usbevt_enable();
  27. usb_reg = NRF_POWER->USBREGSTATUS;
  28. if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk)
  29. {
  30. cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
  31. }
  32. if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk)
  33. {
  34. cherry_usb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
  35. }
  36. }
  37. void usb_low_clear_pending_irq(void)
  38. {
  39. NVIC_ClearPendingIRQ(USBD_IRQn);
  40. }
  41. void usb_low_disable_irq(void)
  42. {
  43. NVIC_DisableIRQ(USBD_IRQn);
  44. }
  45. int cherryusb_protocol_stack_init(void)
  46. {
  47. #ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM
  48. extern void cdc_acm_init(void);
  49. cdc_acm_init();
  50. rt_kprintf("cdc acm example started. \r\n");
  51. #elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_MSC
  52. extern void msc_ram_init(void);
  53. msc_ram_init();
  54. rt_kprintf("msc ram example started. \r\n");
  55. #elif defined RT_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD
  56. extern void hid_keyboard_init(uint8_t busid, uintptr_t reg_base);
  57. hid_keyboard_init(0,NULL);
  58. rt_kprintf("hid keyboard example started. \r\n");
  59. #endif
  60. }
  61. INIT_APP_EXPORT(cherryusb_protocol_stack_init);