refactor nrfx from supervisor/usb.c

This commit is contained in:
hathach 2019-01-30 22:30:23 +07:00
parent 164e1e2341
commit a51f2b0716
2 changed files with 20 additions and 35 deletions

View File

@ -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);
}
}
}

View File

@ -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();