diff options
| author | hathach <thach@tinyusb.org> | 2018-07-17 23:57:49 +0700 |
|---|---|---|
| committer | hathach <thach@tinyusb.org> | 2018-07-17 23:57:49 +0700 |
| commit | bb28faf39c49c785475aff1e6b1680b9360d140f (patch) | |
| tree | 9324f872015aec091319bb814d630aee41999a97 | |
| parent | a0b9871ba93f970ddf6ca03a2e40a2dc1f9caf0f (diff) | |
arturo182 review
| m--------- | lib/tinyusb | 0 | ||||
| -rw-r--r-- | ports/nrf/interrupt_char.c | 3 | ||||
| -rw-r--r-- | ports/nrf/usb/usb.c | 56 | ||||
| -rw-r--r-- | ports/nrf/usb/usb.h | 8 |
4 files changed, 45 insertions, 22 deletions
diff --git a/lib/tinyusb b/lib/tinyusb -Subproject 6129670e4a23812d1926b15c191d76538f7a325 +Subproject 421ae8fc82b884e087e99c8ab3fa25883a3dd62 diff --git a/ports/nrf/interrupt_char.c b/ports/nrf/interrupt_char.c index f0f6b3a35..8793b0d88 100644 --- a/ports/nrf/interrupt_char.c +++ b/ports/nrf/interrupt_char.c @@ -26,7 +26,10 @@ #include "py/obj.h" #include "py/mpstate.h" + +#ifdef NRF52840_XXAA #include "usb.h" +#endif #if MICROPY_KBD_EXCEPTION diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 0105ef6bf..648ee708d 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -30,7 +30,10 @@ #include "usb.h" #include "lib/utils/interrupt_char.h" -#ifdef NRF52840_XXAA +#ifdef SOFTDEVICE_PRESENT +#include "nrf_sdm.h" +#include "nrf_soc.h" +#endif //--------------------------------------------------------------------+ // STRING DESCRIPTORS @@ -71,26 +74,53 @@ tud_desc_set_t tud_desc_set = .hid_report = NULL }; + +/* 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); + void usb_init(void) { + // 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 - // TODO support Softdevice config + uint8_t sd_en = false; + (void) sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) { + sd_power_usbdetected_enable(true); + sd_power_usbpwrrdy_enable(true); + sd_power_usbremoved_enable(true); + + sd_power_usbregstatus_get(&usb_reg); + }else #else - // Softdevice is not present, init power module and register tusb power event function - // for vusb detect, ready, removed - extern void tusb_hal_nrf_power_event(uint32_t event); + { + // Power module init + const nrfx_power_config_t pwr_cfg = { 0 }; + nrfx_power_init(&pwr_cfg); - // Power module init - const nrfx_power_config_t pwr_cfg = { 0 }; - nrfx_power_init(&pwr_cfg); + // Register tusb function as USB power handler + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; + nrfx_power_usbevt_init(&config); - // USB Power detection - const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; - nrfx_power_usbevt_init(&config); + nrfx_power_usbevt_enable(); - nrfx_power_usbevt_enable(); + usb_reg = NRF_POWER->USBREGSTATUS; + } #endif + 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); + } + tusb_init(); } @@ -129,5 +159,3 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) #endif -#endif // NRF52840_XXAA - diff --git a/ports/nrf/usb/usb.h b/ports/nrf/usb/usb.h index c39b59a3f..203260874 100644 --- a/ports/nrf/usb/usb.h +++ b/ports/nrf/usb/usb.h @@ -27,16 +27,8 @@ #ifndef MICROPY_INCLUDED_NRF_USB_H #define MICROPY_INCLUDED_NRF_USB_H -#ifdef NRF52840_XXAA - #include "tusb.h" void usb_init(void); -#else - -#define usb_init() - -#endif - #endif // MICROPY_INCLUDED_NRF_USB_H |
