diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-01-31 10:36:12 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-31 10:36:12 -0500 |
| commit | 323108e2bab7196df7dad0c8c965974b7a17c294 (patch) | |
| tree | ca5d025c44db1a82fb974fb5b96b10bcd5013303 | |
| parent | 68d524fc2a7747fbbde9a067404b180f4d9db9ed (diff) | |
| parent | a51f2b07161239e0a3805cb40914f159f2038c87 (diff) | |
Merge pull request #1503 from hathach/nrf-tinyusb-sd
update tinyusb, work better with sd
| m--------- | lib/tinyusb | 0 | ||||
| -rw-r--r-- | ports/nrf/bluetooth/ble_drv.c | 19 | ||||
| -rw-r--r-- | ports/nrf/bluetooth/bluetooth_common.mk | 1 | ||||
| -rw-r--r-- | ports/nrf/common-hal/bleio/Adapter.c | 15 | ||||
| -rw-r--r-- | ports/nrf/supervisor/usb.c | 24 | ||||
| -rw-r--r-- | supervisor/shared/usb/usb.c | 5 |
6 files changed, 48 insertions, 16 deletions
diff --git a/lib/tinyusb b/lib/tinyusb -Subproject 5804e56e3c2ab4480bf72d94d997f769a645af4 +Subproject 29b49199beb8e9b5fead83e5cd36105f8746f1d 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/common-hal/bleio/Adapter.c b/ports/nrf/common-hal/bleio/Adapter.c index cd116711e..540659aa1 100644 --- a/ports/nrf/common-hal/bleio/Adapter.c +++ b/ports/nrf/common-hal/bleio/Adapter.c @@ -36,6 +36,8 @@ #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" +#include "supervisor/usb.h" + STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) { mp_raise_msg_varg(&mp_type_AssertionError, translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc); @@ -47,9 +49,6 @@ STATIC uint32_t ble_stack_enable(void) { .accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM }; - // The SD takes over the POWER IRQ and will fail if the IRQ is already in use - nrfx_power_uninit(); - uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler); if (err_code != NRF_SUCCESS) return err_code; @@ -101,9 +100,19 @@ void common_hal_bleio_adapter_set_enabled(bool enabled) { uint32_t err_code; if (enabled) { + // The SD takes over the POWER module and will fail if the module is already in use. + // Occurs when USB is initialized previously + nrfx_power_uninit(); + err_code = ble_stack_enable(); + + // Re-init USB hardware + init_usb_hardware(); } else { err_code = sd_softdevice_disable(); + + // Re-init USB hardware + init_usb_hardware(); } if (err_code != NRF_SUCCESS) { diff --git a/ports/nrf/supervisor/usb.c b/ports/nrf/supervisor/usb.c index a67c9311a..bb9d78101 100644 --- a/ports/nrf/supervisor/usb.c +++ b/ports/nrf/supervisor/usb.c @@ -36,15 +36,18 @@ #include "nrf_soc.h" #endif -/* 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. - */ +// 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 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 + // 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 @@ -73,11 +76,14 @@ void init_usb_hardware(void) { usb_reg = NRF_POWER->USBREGSTATUS; } - if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { - tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); - } + 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); + 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 5898323e8..136137f77 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 |
