fix nrf power conflict between usb and softdevice
bleuart example work with USB Serial
This commit is contained in:
1 parent
6f8c546595
commit
d1a2b917b6
6 files changed
+129
-45
No files matched your search
+33
-41
@@ -43,57 +43,32 @@
|
||||
|
||||
#include "nrf_usbd.h"
|
||||
#include "tusb.h"
|
||||
#include "usb.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
extern uint16_t usb_desc_str_serial[1+16];
|
||||
|
||||
/* 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);
|
||||
|
||||
|
||||
//------------- IMPLEMENTATION -------------//
|
||||
// Init usb when starting up. Softdevice is not enabled yet
|
||||
void usb_init(void)
|
||||
{
|
||||
// Power module init
|
||||
const nrfx_power_config_t pwr_cfg = { 0 };
|
||||
nrfx_power_init(&pwr_cfg);
|
||||
|
||||
// Register tusb function as USB power handler
|
||||
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
|
||||
nrfx_power_usbevt_init(&config);
|
||||
|
||||
nrfx_power_usbevt_enable();
|
||||
|
||||
// USB power may already be ready at this time -> no event generated
|
||||
// We need to invoke the handler based on the status initially
|
||||
uint32_t usb_reg;
|
||||
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
|
||||
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
uint8_t sd_en = false;
|
||||
(void) sd_softdevice_is_enabled(&sd_en);
|
||||
|
||||
if ( sd_en ) {
|
||||
sd_power_usbdetected_enable(true);
|
||||
sd_power_usbpwrrdy_enable(true);
|
||||
sd_power_usbremoved_enable(true);
|
||||
|
||||
sd_power_usbregstatus_get(&usb_reg);
|
||||
}else
|
||||
#endif
|
||||
{
|
||||
// Power module init
|
||||
const nrfx_power_config_t pwr_cfg = { 0 };
|
||||
nrfx_power_init(&pwr_cfg);
|
||||
|
||||
// Register tusb function as USB power handler
|
||||
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
|
||||
nrfx_power_usbevt_init(&config);
|
||||
|
||||
nrfx_power_usbevt_enable();
|
||||
|
||||
usb_reg = NRF_POWER->USBREGSTATUS;
|
||||
}
|
||||
|
||||
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_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);
|
||||
|
||||
// Create Serial string descriptor
|
||||
char tmp_serial[17];
|
||||
@@ -104,10 +79,27 @@ void usb_init(void)
|
||||
usb_desc_str_serial[1+i] = tmp_serial[i];
|
||||
}
|
||||
|
||||
// Init tusb stack
|
||||
// Init tinyusb stack
|
||||
tusb_init();
|
||||
}
|
||||
|
||||
// Must be called before sd_softdevice_enable()
|
||||
// NRF_POWER is restricted prph used by Softdevice, must be release before enable SD
|
||||
void usb_softdevice_pre_enable(void)
|
||||
{
|
||||
nrfx_power_usbevt_disable();
|
||||
nrfx_power_usbevt_uninit();
|
||||
nrfx_power_uninit();
|
||||
}
|
||||
|
||||
// Must be called after sd_softdevice_enable()
|
||||
// To re-enable USB
|
||||
void usb_softdevice_post_enable(void)
|
||||
{
|
||||
sd_power_usbdetected_enable(true);
|
||||
sd_power_usbpwrrdy_enable(true);
|
||||
sd_power_usbremoved_enable(true);
|
||||
}
|
||||
|
||||
|
||||
// Invoked when cdc when line state changed e.g connected/disconnected
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file usb.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef USB_H_
|
||||
#define USB_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void usb_init(void);
|
||||
void usb_softdevice_pre_enable(void);
|
||||
void usb_softdevice_post_enable(void);
|
||||
|
||||
/* 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. */
|
||||
void tusb_hal_nrf_power_event(uint32_t event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* USB_H_ */
|
||||
@@ -24,6 +24,8 @@ SoftwareTimer blinkTimer;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while( !Serial ) {} // only needed for nRF52840
|
||||
|
||||
Serial.println("Bluefruit52 BLEUART Example");
|
||||
Serial.println("---------------------------\n");
|
||||
|
||||
@@ -178,4 +180,3 @@ void rtos_idle_callback(void)
|
||||
// Don't call any other FreeRTOS blocking API()
|
||||
// Perform background task(s) here
|
||||
}
|
||||
|
||||
@@ -38,6 +38,11 @@
|
||||
#include <Nffs.h>
|
||||
#include "utility/bonding.h"
|
||||
|
||||
#ifdef NRF52840_XXAA
|
||||
#include "nrfx_power.h"
|
||||
#include "usb/usb.h"
|
||||
#endif
|
||||
|
||||
#define CFG_BLE_TX_POWER_LEVEL 0
|
||||
#define CFG_DEFAULT_NAME "Bluefruit52"
|
||||
|
||||
@@ -206,6 +211,10 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
|
||||
_prph_count = prph_count;
|
||||
_central_count = central_count;
|
||||
|
||||
#ifdef NRF52840_XXAA
|
||||
usb_softdevice_pre_enable();
|
||||
#endif
|
||||
|
||||
// Configure Clock
|
||||
#if defined( USE_LFXO )
|
||||
nrf_clock_lf_cfg_t clock_cfg =
|
||||
@@ -220,6 +229,10 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
|
||||
|
||||
VERIFY_STATUS( sd_softdevice_enable(&clock_cfg, nrf_error_cb) );
|
||||
|
||||
#ifdef NRF52840_XXAA
|
||||
usb_softdevice_post_enable();
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* SoftDevice Default Configuration depending on the number of
|
||||
* prph and central connections for optimal SRAM usage.
|
||||
@@ -383,6 +396,13 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN) );
|
||||
|
||||
//------------- USB -------------//
|
||||
#if NRF52840_XXAA
|
||||
sd_power_usbdetected_enable(true);
|
||||
sd_power_usbpwrrdy_enable(true);
|
||||
sd_power_usbremoved_enable(true);
|
||||
#endif
|
||||
|
||||
/*------------- DFU OTA as built-in service -------------*/
|
||||
_dfu_svc.begin();
|
||||
|
||||
@@ -744,11 +764,27 @@ void adafruit_soc_task(void* arg)
|
||||
{
|
||||
switch (soc_evt)
|
||||
{
|
||||
// Flash
|
||||
case NRF_EVT_FLASH_OPERATION_SUCCESS:
|
||||
case NRF_EVT_FLASH_OPERATION_ERROR:
|
||||
if (hal_flash_event_cb) hal_flash_event_cb(soc_evt);
|
||||
break;
|
||||
|
||||
/*------------- usb power event handler -------------*/
|
||||
#ifdef NRF52840_XXAA
|
||||
case NRF_EVT_POWER_USB_DETECTED:
|
||||
case NRF_EVT_POWER_USB_POWER_READY:
|
||||
case NRF_EVT_POWER_USB_REMOVED:
|
||||
{
|
||||
int32_t usbevt = (soc_evt == NRF_EVT_POWER_USB_DETECTED ) ? NRFX_POWER_USB_EVT_DETECTED:
|
||||
(soc_evt == NRF_EVT_POWER_USB_POWER_READY) ? NRFX_POWER_USB_EVT_READY :
|
||||
(soc_evt == NRF_EVT_POWER_USB_REMOVED ) ? NRFX_POWER_USB_EVT_REMOVED : -1;
|
||||
|
||||
if ( usbevt >= 0) tusb_hal_nrf_power_event(usbevt);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "wiring_digital.h"
|
||||
#include "nrf.h"
|
||||
|
||||
extern "C" void usb_init(void);
|
||||
#include "usb.h"
|
||||
|
||||
const uint32_t g_ADigitalPinMap[] =
|
||||
{
|
||||
|
||||
@@ -22,8 +22,7 @@
|
||||
#include "wiring_constants.h"
|
||||
#include "wiring_digital.h"
|
||||
#include "nrf.h"
|
||||
|
||||
extern "C" void usb_init(void);
|
||||
#include "usb.h"
|
||||
|
||||
const uint32_t g_ADigitalPinMap[] =
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user