Skip to content

Commit 782ab61

Browse files
authored
Merge pull request #546 from adafruit/fix-usbpower-racing
fix power usbready not handled if bluefruit begin() is called fast enough
2 parents 3a8c438 + 24f3907 commit 782ab61

File tree

6 files changed

+59
-41
lines changed

6 files changed

+59
-41
lines changed

cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ extern "C" void USBD_IRQHandler(void)
6363
// Init usb hardware when starting up. Softdevice is not enabled yet
6464
static void usb_hardware_init(void)
6565
{
66+
// Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice
67+
// 2 is highest for application
68+
NVIC_SetPriority(USBD_IRQn, 2);
69+
70+
// USB power may already be ready at this time -> no event generated
71+
// We need to invoke the handler based on the status initially
72+
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
73+
6674
// Power module init
6775
const nrfx_power_config_t pwr_cfg = { 0 };
6876
nrfx_power_init(&pwr_cfg);
@@ -73,16 +81,7 @@ static void usb_hardware_init(void)
7381

7482
nrfx_power_usbevt_enable();
7583

76-
// Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice
77-
// 2 is highest for application
78-
NVIC_SetPriority(USBD_IRQn, 2);
79-
80-
// USB power may already be ready at this time -> no event generated
81-
// We need to invoke the handler based on the status initially
82-
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
83-
8484
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
85-
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
8685
}
8786

8887
// USB Device Driver task
@@ -91,7 +90,7 @@ static void usb_device_task(void* param)
9190
{
9291
(void) param;
9392

94-
USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial);
93+
USBDevice.addInterface((Adafruit_USBD_Interface&) Serial);
9594
USBDevice.setID(USB_VID, USB_PID);
9695
USBDevice.begin();
9796

@@ -111,7 +110,7 @@ static void usb_device_task(void* param)
111110
void Adafruit_TinyUSB_Core_init(void)
112111
{
113112
// Create a task for tinyusb device stack
114-
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
113+
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
115114
}
116115

117116
void Adafruit_TinyUSB_Core_touch1200(void)

cores/nRF5/TinyUSB/tusb_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#define CFG_TUSB_MEM_SECTION
4545
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
4646

47+
#ifndef CFG_TUSB_DEBUG
48+
#define CFG_TUSB_DEBUG 0
49+
#endif
50+
4751
//--------------------------------------------------------------------
4852
// DEVICE CONFIGURATION
4953
//--------------------------------------------------------------------

cores/nRF5/freertos/config/FreeRTOSConfig.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@
139139
#endif
140140
#endif /* !assembler */
141141

142-
/* The lowest interrupt priority that can be used in a call to a "set priority"
143-
function. */
142+
/* The lowest interrupt priority that can be used in a call to a "set priority" function. */
144143
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY ((1<<configPRIO_BITS) - 1)
145144

146145
/* The highest interrupt priority that can be used by any interrupt service

cores/nRF5/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void Bluefruit_printInfo() {}
2626

2727
// From the UI, setting debug level to 3 will enable SysView
2828
#if CFG_SYSVIEW
29+
#include "SEGGER_RTT.h"
2930
#include "SEGGER_SYSVIEW.h"
3031
#endif
3132

@@ -107,10 +108,16 @@ int _write (int fd, const void *buf, size_t count)
107108
{
108109
(void) fd;
109110

111+
#if 0 // CFG_SYSVIEW
112+
SEGGER_RTT_Write(0, (char*) buf, (int) count);
113+
return count;
114+
#else
110115
if ( Serial )
111116
{
112117
return Serial.write( (const uint8_t *) buf, count);
113118
}
119+
#endif
120+
114121
return 0;
115122
}
116123

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ void usb_softdevice_post_enable(void)
7676
sd_power_usbdetected_enable(true);
7777
sd_power_usbpwrrdy_enable(true);
7878
sd_power_usbremoved_enable(true);
79+
80+
uint32_t usb_reg;
81+
sd_power_usbregstatus_get(&usb_reg);
82+
83+
// Note: Detect event is possibly handled by usb_hardware_init() however depending on how fast
84+
// Bluefruit.begin() is called, Ready event may or may not be handled before we disable the nrfx_power.
85+
// USBPULLUP not enabled -> Ready event not yet handled
86+
if ( (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk) && (NRF_USBD->USBPULLUP == 0) )
87+
{
88+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
89+
}
7990
}
8091

8192
#endif
@@ -163,7 +174,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
163174
_ble_event_sem = NULL;
164175
_soc_event_sem = NULL;
165176
#ifdef ANT_LICENSE_KEY
166-
_mprot_event_sem = NULL; //additiona semaphore for multiprotocol
177+
_mprot_event_sem = NULL;
167178
#endif
168179

169180
_led_blink_th = NULL;
@@ -294,34 +305,31 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
294305
#if defined( USE_LFXO )
295306
nrf_clock_lf_cfg_t clock_cfg =
296307
{
297-
// LFXO
298-
.source = NRF_CLOCK_LF_SRC_XTAL,
299-
.rc_ctiv = 0,
300-
.rc_temp_ctiv = 0,
301-
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
308+
// LFXO
309+
.source = NRF_CLOCK_LF_SRC_XTAL,
310+
.rc_ctiv = 0,
311+
.rc_temp_ctiv = 0,
312+
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM
302313
};
303314
#elif defined( USE_LFRC )
304315
nrf_clock_lf_cfg_t clock_cfg =
305316
{
306-
// LXRC
307-
.source = NRF_CLOCK_LF_SRC_RC,
308-
.rc_ctiv = 16,
309-
.rc_temp_ctiv = 2,
310-
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM
317+
// LXRC
318+
.source = NRF_CLOCK_LF_SRC_RC,
319+
.rc_ctiv = 16,
320+
.rc_temp_ctiv = 2,
321+
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM
311322
};
312323
#else
313324
#error Clock Source is not configured, define USE_LFXO or USE_LFRC according to your board in variant.h
314325
#endif
315326

316-
/*------------------------------------------------------------------
317-
** BLE only Softdevices have 2-args sd_softdevice_enable()
318-
** BLE & ANT+ Softdevices have 3-args sd_softdevice_enable()
319-
*/
320-
#ifdef ANT_LICENSE_KEY
327+
// Enable SoftDevice
328+
#ifdef ANT_LICENSE_KEY
321329
VERIFY_STATUS( sd_softdevice_enable(&clock_cfg, nrf_error_cb, ANT_LICENSE_KEY), false );
322-
#else //#ifdef ANT_LICENSE_KEY
330+
#else
323331
VERIFY_STATUS( sd_softdevice_enable(&clock_cfg, nrf_error_cb), false );
324-
#endif //#ifdef ANT_LICENSE_KEY
332+
#endif
325333

326334
#ifdef USE_TINYUSB
327335
usb_softdevice_post_enable();
@@ -464,13 +472,6 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
464472
ble_gap_conn_sec_mode_t sec_mode = BLE_SECMODE_OPEN;
465473
VERIFY_STATUS(sd_ble_gap_device_name_set(&sec_mode, (uint8_t const *) CFG_DEFAULT_NAME, strlen(CFG_DEFAULT_NAME)), false);
466474

467-
//------------- USB -------------//
468-
#ifdef USE_TINYUSB
469-
sd_power_usbdetected_enable(true);
470-
sd_power_usbpwrrdy_enable(true);
471-
sd_power_usbremoved_enable(true);
472-
#endif
473-
474475
// Init Central role
475476
if (_central_count) Central.begin();
476477

@@ -684,13 +685,21 @@ bool AdafruitBluefruit::setPIN(const char* pin)
684685
*------------------------------------------------------------------*/
685686
void SD_EVT_IRQHandler(void)
686687
{
687-
// Notify both BLE & SOC Task
688+
#if CFG_SYSVIEW
689+
SEGGER_SYSVIEW_RecordEnterISR();
690+
#endif
691+
692+
// Notify both BLE & SOC & MultiProtocol (if any) Task
688693
xSemaphoreGiveFromISR(Bluefruit._soc_event_sem, NULL);
689694
xSemaphoreGiveFromISR(Bluefruit._ble_event_sem, NULL);
695+
690696
#ifdef ANT_LICENSE_KEY
691-
// Notify parallel multiprotocol Task, if any
692697
if (Bluefruit._mprot_event_sem) xSemaphoreGiveFromISR(Bluefruit._mprot_event_sem, NULL);
693698
#endif
699+
700+
#if CFG_SYSVIEW
701+
SEGGER_SYSVIEW_RecordExitISR();
702+
#endif
694703
}
695704

696705
/**

0 commit comments

Comments
 (0)