drv_tinyusb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-25 chenyingchun first version
  9. */
  10. #include <rtthread.h>
  11. #include "nrfx.h"
  12. #include "nrf_gpio.h"
  13. #include "nrfx_power.h"
  14. #include "nrfx_uarte.h"
  15. #include "device/usbd.h"
  16. #ifdef SOFTDEVICE_PRESENT
  17. #include "nrf_sdm.h"
  18. #include "nrf_soc.h"
  19. #endif
  20. //--------------------------------------------------------------------+
  21. // Forward USB interrupt events to TinyUSB IRQ Handler
  22. //--------------------------------------------------------------------+
  23. void USBD_IRQHandler(void)
  24. {
  25. rt_interrupt_enter();
  26. tud_int_handler(0);
  27. rt_interrupt_leave();
  28. }
  29. /*------------------------------------------------------------------*/
  30. /* MACRO TYPEDEF CONSTANT ENUM
  31. *------------------------------------------------------------------*/
  32. // tinyusb function that handles power event (detected, ready, removed)
  33. // We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
  34. extern void tusb_hal_nrf_power_event(uint32_t event);
  35. void tud_board_init(void)
  36. {
  37. // stop LF clock just in case we jump from application without reset
  38. NRF_CLOCK->TASKS_LFCLKSTOP = 1UL;
  39. // Use Internal OSC to compatible with all boards
  40. NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC;
  41. NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
  42. //------------- USB -------------//
  43. #if TUSB_OPT_DEVICE_ENABLED
  44. // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice
  45. // 2 is highest for application
  46. NVIC_SetPriority(USBD_IRQn, 2);
  47. // USB power may already be ready at this time -> no event generated
  48. // We need to invoke the handler based on the status initially
  49. uint32_t usb_reg;
  50. #ifdef SOFTDEVICE_PRESENT
  51. uint8_t sd_en = false;
  52. sd_softdevice_is_enabled(&sd_en);
  53. if ( sd_en ) {
  54. sd_power_usbdetected_enable(true);
  55. sd_power_usbpwrrdy_enable(true);
  56. sd_power_usbremoved_enable(true);
  57. sd_power_usbregstatus_get(&usb_reg);
  58. }else
  59. #endif
  60. {
  61. // Power module init
  62. const nrfx_power_config_t pwr_cfg = { 0 };
  63. nrfx_power_init(&pwr_cfg);
  64. // Register tusb function as USB power handler
  65. // cause cast-function-type warning
  66. const nrfx_power_usbevt_config_t config = { .handler = ((nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event) };
  67. nrfx_power_usbevt_init(&config);
  68. nrfx_power_usbevt_enable();
  69. usb_reg = NRF_POWER->USBREGSTATUS;
  70. }
  71. if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
  72. if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
  73. #endif
  74. }
  75. #ifdef SOFTDEVICE_PRESENT
  76. // process SOC event from SD
  77. uint32_t proc_soc(void)
  78. {
  79. uint32_t soc_evt;
  80. uint32_t err = sd_evt_get(&soc_evt);
  81. if (NRF_SUCCESS == err)
  82. {
  83. /*------------- usb power event handler -------------*/
  84. int32_t usbevt = (soc_evt == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED:
  85. (soc_evt == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY :
  86. (soc_evt == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1;
  87. if ( usbevt >= 0) tusb_hal_nrf_power_event(usbevt);
  88. }
  89. return err;
  90. }
  91. uint32_t proc_ble(void)
  92. {
  93. // do nothing with ble
  94. return NRF_ERROR_NOT_FOUND;
  95. }
  96. void SD_EVT_IRQHandler(void)
  97. {
  98. // process BLE and SOC until there is no more events
  99. while( (NRF_ERROR_NOT_FOUND != proc_ble()) || (NRF_ERROR_NOT_FOUND != proc_soc()) )
  100. {
  101. }
  102. }
  103. void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info)
  104. {
  105. (void) id;
  106. (void) pc;
  107. (void) info;
  108. }
  109. #endif