diff options
| author | hathach <thach@tinyusb.org> | 2019-01-30 22:30:23 +0700 |
|---|---|---|
| committer | hathach <thach@tinyusb.org> | 2019-01-30 22:30:23 +0700 |
| commit | a51f2b07161239e0a3805cb40914f159f2038c87 (patch) | |
| tree | 9bfa06732cd34a724eddfe6c1bd00fb88fd9359f | |
| parent | 164e1e23419450ac8cd72af01051ec6fd299bd92 (diff) | |
refactor nrfx from supervisor/usb.c
| -rw-r--r-- | ports/nrf/supervisor/usb.c | 20 | ||||
| -rw-r--r-- | supervisor/shared/usb/usb.c | 35 |
2 files changed, 20 insertions, 35 deletions
diff --git a/ports/nrf/supervisor/usb.c b/ports/nrf/supervisor/usb.c index 5a4d5f193..bb9d78101 100644 --- a/ports/nrf/supervisor/usb.c +++ b/ports/nrf/supervisor/usb.c @@ -45,6 +45,11 @@ void init_usb_hardware(void) { // 2 is max priority (0, 1 are reserved for SD) NVIC_SetPriority(USBD_IRQn, 2); + // USB power may already be ready at this time -> no event generated + // We need to invoke the handler based on the status initially for the first call + static bool first_call = true; + uint32_t usb_reg; + #ifdef SOFTDEVICE_PRESENT uint8_t sd_en = false; (void) sd_softdevice_is_enabled(&sd_en); @@ -53,6 +58,8 @@ void init_usb_hardware(void) { sd_power_usbdetected_enable(true); sd_power_usbpwrrdy_enable(true); sd_power_usbremoved_enable(true); + + sd_power_usbregstatus_get(&usb_reg); }else #endif { @@ -65,5 +72,18 @@ void init_usb_hardware(void) { nrfx_power_usbevt_init(&config); nrfx_power_usbevt_enable(); + + usb_reg = NRF_POWER->USBREGSTATUS; + } + + if ( first_call ) { + first_call = false; + if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); + } + + if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) { + tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); + } } } diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index d15e1c4ff..136137f77 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -32,19 +32,8 @@ #include "lib/utils/interrupt_char.h" #include "lib/mp-readline/readline.h" -#include "nrfx_power.h" - -#ifdef SOFTDEVICE_PRESENT -#include "nrf_sdm.h" -#include "nrf_soc.h" -#endif - #include "tusb.h" -// tinyusb function that handles power event (detected, ready, removed) -// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. -extern void tusb_hal_nrf_power_event(uint32_t event); - // Serial number as hex characters. This writes directly to the USB // descriptor. extern uint16_t usb_serial_number[1 + COMMON_HAL_MCU_PROCESSOR_UID_LENGTH * 2]; @@ -71,30 +60,6 @@ bool usb_enabled(void) { void usb_init(void) { init_usb_hardware(); - - // USB power may already be ready at this time -> no event generated - // We need to invoke the handler based on the status initially - uint32_t usb_reg; - -#ifdef SOFTDEVICE_PRESENT - uint8_t sd_en = false; - (void) sd_softdevice_is_enabled(&sd_en); - - if ( sd_en ) { - sd_power_usbregstatus_get(&usb_reg); - }else { - usb_reg = NRF_POWER->USBREGSTATUS; - } - - if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { - tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); - } - - if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) { - tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY); - } -#endif - load_serial_number(); tusb_init(); |
