Merge pull request #1503 from hathach/nrf-tinyusb-sd

update tinyusb, work better with sd
This commit is contained in:
Dan Halbert 2019-01-31 10:36:12 -05:00 committed by GitHub
commit 323108e2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 17 deletions

@ -1 +1 @@
Subproject commit 5804e56e3c2ab4480bf72d94d997f769a645af47
Subproject commit 29b49199beb8e9b5fead83e5cd36105f8746f1d7

View File

@ -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) {

View File

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

View File

@ -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) {

View File

@ -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,6 +76,8 @@ void init_usb_hardware(void) {
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);
}
@ -80,4 +85,5 @@ void init_usb_hardware(void) {
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) {
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
}
}
}

View File

@ -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