|
30 | 30 | #include "usb.h"
|
31 | 31 | #include "lib/utils/interrupt_char.h"
|
32 | 32 |
|
33 |
| -#ifdef NRF52840_XXAA |
| 33 | +#ifdef SOFTDEVICE_PRESENT |
| 34 | +#include "nrf_sdm.h" |
| 35 | +#include "nrf_soc.h" |
| 36 | +#endif |
34 | 37 |
|
35 | 38 | //--------------------------------------------------------------------+
|
36 | 39 | // STRING DESCRIPTORS
|
@@ -71,26 +74,53 @@ tud_desc_set_t tud_desc_set =
|
71 | 74 | .hid_report = NULL
|
72 | 75 | };
|
73 | 76 |
|
| 77 | + |
| 78 | +/* tinyusb function that handles power event (detected, ready, removed) |
| 79 | + * We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. |
| 80 | + */ |
| 81 | +extern void tusb_hal_nrf_power_event(uint32_t event); |
| 82 | + |
74 | 83 | void usb_init(void) {
|
75 | 84 |
|
| 85 | + // USB power may already be ready at this time -> no event generated |
| 86 | + // We need to invoke the handler based on the status initially |
| 87 | + uint32_t usb_reg; |
| 88 | + |
76 | 89 | #ifdef SOFTDEVICE_PRESENT
|
77 |
| - // TODO support Softdevice config |
| 90 | + uint8_t sd_en = false; |
| 91 | + (void) sd_softdevice_is_enabled(&sd_en); |
| 92 | + |
| 93 | + if ( sd_en ) { |
| 94 | + sd_power_usbdetected_enable(true); |
| 95 | + sd_power_usbpwrrdy_enable(true); |
| 96 | + sd_power_usbremoved_enable(true); |
| 97 | + |
| 98 | + sd_power_usbregstatus_get(&usb_reg); |
| 99 | + }else |
78 | 100 | #else
|
79 |
| - // Softdevice is not present, init power module and register tusb power event function |
80 |
| - // for vusb detect, ready, removed |
81 |
| - extern void tusb_hal_nrf_power_event(uint32_t event); |
| 101 | + { |
| 102 | + // Power module init |
| 103 | + const nrfx_power_config_t pwr_cfg = { 0 }; |
| 104 | + nrfx_power_init(&pwr_cfg); |
82 | 105 |
|
83 |
| - // Power module init |
84 |
| - const nrfx_power_config_t pwr_cfg = { 0 }; |
85 |
| - nrfx_power_init(&pwr_cfg); |
| 106 | + // Register tusb function as USB power handler |
| 107 | + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; |
| 108 | + nrfx_power_usbevt_init(&config); |
86 | 109 |
|
87 |
| - // USB Power detection |
88 |
| - const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; |
89 |
| - nrfx_power_usbevt_init(&config); |
| 110 | + nrfx_power_usbevt_enable(); |
90 | 111 |
|
91 |
| - nrfx_power_usbevt_enable(); |
| 112 | + usb_reg = NRF_POWER->USBREGSTATUS; |
| 113 | + } |
92 | 114 | #endif
|
93 | 115 |
|
| 116 | + if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { |
| 117 | + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); |
| 118 | + } |
| 119 | + |
| 120 | + if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) { |
| 121 | + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); |
| 122 | + } |
| 123 | + |
94 | 124 | tusb_init();
|
95 | 125 | }
|
96 | 126 |
|
@@ -129,5 +159,3 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
|
129 | 159 |
|
130 | 160 | #endif
|
131 | 161 |
|
132 |
| -#endif // NRF52840_XXAA |
133 |
| - |
|
0 commit comments