Skip to content

Commit 323108e

Browse files
authored
Merge pull request #1503 from hathach/nrf-tinyusb-sd
update tinyusb, work better with sd
2 parents 68d524f + a51f2b0 commit 323108e

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

lib/tinyusb

Submodule tinyusb updated 34 files

ports/nrf/bluetooth/ble_drv.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "ble_drv.h"
3333
#include "nrf_nvic.h"
3434
#include "nrf_sdm.h"
35+
#include "nrf_soc.h"
36+
#include "nrfx_power.h"
3537
#include "py/misc.h"
3638

3739
nrf_nvic_state_t nrf_nvic_state = { 0 };
@@ -89,6 +91,23 @@ void SD_EVT_IRQHandler(void) {
8991
uint32_t evt_id;
9092
while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) {
9193
// sd_evt_handler(evt_id);
94+
95+
switch (evt_id) {
96+
// usb power event
97+
case NRF_EVT_POWER_USB_DETECTED:
98+
case NRF_EVT_POWER_USB_POWER_READY:
99+
case NRF_EVT_POWER_USB_REMOVED: {
100+
int32_t usbevt = (evt_id == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED:
101+
(evt_id == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY :
102+
(evt_id == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1;
103+
104+
extern void tusb_hal_nrf_power_event (uint32_t event);
105+
tusb_hal_nrf_power_event(usbevt);
106+
}
107+
break;
108+
109+
default: break;
110+
}
92111
}
93112

94113
while (1) {

ports/nrf/bluetooth/bluetooth_common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $(error Incorrect softdevice set flag)
66
endif
77

88
CFLAGS += -DBLUETOOTH_SD_DEBUG=1
9+
CFLAGS += -DSOFTDEVICE_PRESENT
910

1011
INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include
1112
INC += -Ibluetooth/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)/$(SD)_$(MCU_VARIANT)_$(SOFTDEV_VERSION)_API/include/$(MCU_VARIANT)

ports/nrf/common-hal/bleio/Adapter.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "py/runtime.h"
3737
#include "shared-bindings/bleio/Adapter.h"
3838

39+
#include "supervisor/usb.h"
40+
3941
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
4042
mp_raise_msg_varg(&mp_type_AssertionError,
4143
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc);
@@ -47,9 +49,6 @@ STATIC uint32_t ble_stack_enable(void) {
4749
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
4850
};
4951

50-
// The SD takes over the POWER IRQ and will fail if the IRQ is already in use
51-
nrfx_power_uninit();
52-
5352
uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
5453
if (err_code != NRF_SUCCESS)
5554
return err_code;
@@ -101,9 +100,19 @@ void common_hal_bleio_adapter_set_enabled(bool enabled) {
101100

102101
uint32_t err_code;
103102
if (enabled) {
103+
// The SD takes over the POWER module and will fail if the module is already in use.
104+
// Occurs when USB is initialized previously
105+
nrfx_power_uninit();
106+
104107
err_code = ble_stack_enable();
108+
109+
// Re-init USB hardware
110+
init_usb_hardware();
105111
} else {
106112
err_code = sd_softdevice_disable();
113+
114+
// Re-init USB hardware
115+
init_usb_hardware();
107116
}
108117

109118
if (err_code != NRF_SUCCESS) {

ports/nrf/supervisor/usb.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@
3636
#include "nrf_soc.h"
3737
#endif
3838

39-
/* tinyusb function that handles power event (detected, ready, removed)
40-
* We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
41-
*/
39+
// tinyusb function that handles power event (detected, ready, removed)
40+
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
4241
extern void tusb_hal_nrf_power_event(uint32_t event);
4342

4443
void init_usb_hardware(void) {
4544

45+
// 2 is max priority (0, 1 are reserved for SD)
46+
NVIC_SetPriority(USBD_IRQn, 2);
47+
4648
// USB power may already be ready at this time -> no event generated
47-
// We need to invoke the handler based on the status initially
49+
// We need to invoke the handler based on the status initially for the first call
50+
static bool first_call = true;
4851
uint32_t usb_reg;
4952

5053
#ifdef SOFTDEVICE_PRESENT
@@ -73,11 +76,14 @@ void init_usb_hardware(void) {
7376
usb_reg = NRF_POWER->USBREGSTATUS;
7477
}
7578

76-
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
77-
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
78-
}
79+
if ( first_call ) {
80+
first_call = false;
81+
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
82+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
83+
}
7984

80-
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) {
81-
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
85+
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) {
86+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
87+
}
8288
}
8389
}

supervisor/shared/usb/usb.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,15 @@ void load_serial_number(void) {
5454
}
5555
}
5656

57-
bool _usb_enabled = false;
58-
5957
bool usb_enabled(void) {
60-
return _usb_enabled;
58+
return tusb_inited();
6159
}
6260

6361
void usb_init(void) {
6462
init_usb_hardware();
6563
load_serial_number();
6664

6765
tusb_init();
68-
_usb_enabled = true;
6966

7067
#if MICROPY_KBD_EXCEPTION
7168
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received

0 commit comments

Comments
 (0)