Merge pull request #1503 from hathach/nrf-tinyusb-sd
update tinyusb, work better with sd
This commit is contained in:
commit
323108e2ba
@ -1 +1 @@
|
|||||||
Subproject commit 5804e56e3c2ab4480bf72d94d997f769a645af47
|
Subproject commit 29b49199beb8e9b5fead83e5cd36105f8746f1d7
|
@ -32,6 +32,8 @@
|
|||||||
#include "ble_drv.h"
|
#include "ble_drv.h"
|
||||||
#include "nrf_nvic.h"
|
#include "nrf_nvic.h"
|
||||||
#include "nrf_sdm.h"
|
#include "nrf_sdm.h"
|
||||||
|
#include "nrf_soc.h"
|
||||||
|
#include "nrfx_power.h"
|
||||||
#include "py/misc.h"
|
#include "py/misc.h"
|
||||||
|
|
||||||
nrf_nvic_state_t nrf_nvic_state = { 0 };
|
nrf_nvic_state_t nrf_nvic_state = { 0 };
|
||||||
@ -89,6 +91,23 @@ void SD_EVT_IRQHandler(void) {
|
|||||||
uint32_t evt_id;
|
uint32_t evt_id;
|
||||||
while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) {
|
while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) {
|
||||||
// sd_evt_handler(evt_id);
|
// 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) {
|
while (1) {
|
||||||
|
@ -6,6 +6,7 @@ $(error Incorrect softdevice set flag)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
CFLAGS += -DBLUETOOTH_SD_DEBUG=1
|
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
|
||||||
INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT)
|
INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT)
|
||||||
|
@ -36,6 +36,8 @@
|
|||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
#include "shared-bindings/bleio/Adapter.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) {
|
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
|
||||||
mp_raise_msg_varg(&mp_type_AssertionError,
|
mp_raise_msg_varg(&mp_type_AssertionError,
|
||||||
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc);
|
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
|
.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);
|
uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
|
||||||
if (err_code != NRF_SUCCESS)
|
if (err_code != NRF_SUCCESS)
|
||||||
return err_code;
|
return err_code;
|
||||||
@ -101,9 +100,19 @@ void common_hal_bleio_adapter_set_enabled(bool enabled) {
|
|||||||
|
|
||||||
uint32_t err_code;
|
uint32_t err_code;
|
||||||
if (enabled) {
|
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();
|
err_code = ble_stack_enable();
|
||||||
|
|
||||||
|
// Re-init USB hardware
|
||||||
|
init_usb_hardware();
|
||||||
} else {
|
} else {
|
||||||
err_code = sd_softdevice_disable();
|
err_code = sd_softdevice_disable();
|
||||||
|
|
||||||
|
// Re-init USB hardware
|
||||||
|
init_usb_hardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (err_code != NRF_SUCCESS) {
|
if (err_code != NRF_SUCCESS) {
|
||||||
|
@ -36,15 +36,18 @@
|
|||||||
#include "nrf_soc.h"
|
#include "nrf_soc.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* tinyusb function that handles power event (detected, ready, removed)
|
// 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.
|
// 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);
|
extern void tusb_hal_nrf_power_event(uint32_t event);
|
||||||
|
|
||||||
void init_usb_hardware(void) {
|
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
|
// 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;
|
uint32_t usb_reg;
|
||||||
|
|
||||||
#ifdef SOFTDEVICE_PRESENT
|
#ifdef SOFTDEVICE_PRESENT
|
||||||
@ -73,11 +76,14 @@ void init_usb_hardware(void) {
|
|||||||
usb_reg = NRF_POWER->USBREGSTATUS;
|
usb_reg = NRF_POWER->USBREGSTATUS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
|
if ( first_call ) {
|
||||||
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
|
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 ) {
|
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) {
|
||||||
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
|
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,10 +54,8 @@ void load_serial_number(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _usb_enabled = false;
|
|
||||||
|
|
||||||
bool usb_enabled(void) {
|
bool usb_enabled(void) {
|
||||||
return _usb_enabled;
|
return tusb_inited();
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_init(void) {
|
void usb_init(void) {
|
||||||
@ -65,7 +63,6 @@ void usb_init(void) {
|
|||||||
load_serial_number();
|
load_serial_number();
|
||||||
|
|
||||||
tusb_init();
|
tusb_init();
|
||||||
_usb_enabled = true;
|
|
||||||
|
|
||||||
#if MICROPY_KBD_EXCEPTION
|
#if MICROPY_KBD_EXCEPTION
|
||||||
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received
|
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received
|
||||||
|
Loading…
Reference in New Issue
Block a user