From d1fb384a4a4cda91f293aac7ae49c570a97daa7c Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 29 Jan 2019 20:47:01 +0700 Subject: update tinyusb, work better with sd --- lib/tinyusb | 2 +- ports/nrf/bluetooth/ble_drv.c | 19 +++++++++++++++++++ ports/nrf/bluetooth/bluetooth_common.mk | 1 + ports/nrf/supervisor/usb.c | 3 +++ supervisor/shared/usb/usb.c | 7 ++----- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 5804e56e3..29b49199b 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 5804e56e3c2ab4480bf72d94d997f769a645af47 +Subproject commit 29b49199beb8e9b5fead83e5cd36105f8746f1d7 diff --git a/ports/nrf/bluetooth/ble_drv.c b/ports/nrf/bluetooth/ble_drv.c index cdc56319b..f80c7b1f0 100644 --- a/ports/nrf/bluetooth/ble_drv.c +++ b/ports/nrf/bluetooth/ble_drv.c @@ -32,6 +32,8 @@ #include "ble_drv.h" #include "nrf_nvic.h" #include "nrf_sdm.h" +#include "nrf_soc.h" +#include "nrfx_power.h" #include "py/misc.h" nrf_nvic_state_t nrf_nvic_state = { 0 }; @@ -89,6 +91,23 @@ void SD_EVT_IRQHandler(void) { uint32_t evt_id; while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) { // sd_evt_handler(evt_id); + + switch (evt_id) { + // usb power event + case NRF_EVT_POWER_USB_DETECTED: + case NRF_EVT_POWER_USB_POWER_READY: + case NRF_EVT_POWER_USB_REMOVED: { + int32_t usbevt = (evt_id == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED: + (evt_id == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY : + (evt_id == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1; + + extern void tusb_hal_nrf_power_event (uint32_t event); + tusb_hal_nrf_power_event(usbevt); + } + break; + + default: break; + } } while (1) { diff --git a/ports/nrf/bluetooth/bluetooth_common.mk b/ports/nrf/bluetooth/bluetooth_common.mk index 5d094fcf8..67b779c90 100644 --- a/ports/nrf/bluetooth/bluetooth_common.mk +++ b/ports/nrf/bluetooth/bluetooth_common.mk @@ -6,6 +6,7 @@ $(error Incorrect softdevice set flag) endif CFLAGS += -DBLUETOOTH_SD_DEBUG=1 +CFLAGS += -DSOFTDEVICE_PRESENT INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT) diff --git a/ports/nrf/supervisor/usb.c b/ports/nrf/supervisor/usb.c index a67c9311a..52eb07040 100644 --- a/ports/nrf/supervisor/usb.c +++ b/ports/nrf/supervisor/usb.c @@ -43,6 +43,9 @@ extern void tusb_hal_nrf_power_event(uint32_t event); 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 uint32_t usb_reg; diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index 5898323e8..b73c9f73f 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -54,10 +54,8 @@ void load_serial_number(void) { } } -bool _usb_enabled = false; - bool usb_enabled(void) { - return _usb_enabled; + return tusb_inited(); } void usb_init(void) { @@ -65,7 +63,6 @@ void usb_init(void) { load_serial_number(); tusb_init(); - _usb_enabled = true; #if MICROPY_KBD_EXCEPTION // Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received @@ -77,7 +74,7 @@ void usb_init(void) { } void usb_background(void) { - if (usb_enabled()) { + if (tusb_inited()) { tud_task(); tud_cdc_write_flush(); } -- cgit v1.2.3