diff --git a/README.md b/README.md index d9fa9fb8..dd034874 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # Arduino Core for Adafruit Bluefruit nRF52 Boards -This repository contains the Arduino BSP for Adafruit nRF52 series: +This repository contains the Arduino BSP for Adafruit Bluefruit nRF52 series: -- [Bluefruit Feather nRF52832](https://www.adafruit.com/product/3406) -- [Bluefruit Feather nRF52840](https://www.adafruit.com/product/4062) +- [Adafruit Feather nRF52832](https://www.adafruit.com/product/3406) +- [Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062) +- Adafruit Metro nRF52840 Express Following boards are also included but are not officially supported: diff --git a/cores/nRF5/Adafruit_TinyUSB_core/usb.c b/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.cpp similarity index 83% rename from cores/nRF5/Adafruit_TinyUSB_core/usb.c rename to cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.cpp index 254eea1b..3b10aa7b 100644 --- a/cores/nRF5/Adafruit_TinyUSB_core/usb.c +++ b/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.cpp @@ -25,14 +25,9 @@ #include "nrfx.h" #include "nrfx_power.h" -#include "nrf_sdm.h" -#include "nrf_soc.h" -#include "nrf_usbd.h" -#include "tusb.h" -#include "usb.h" - -#include "rtos.h" +#include "Arduino.h" +#include "Adafruit_TinyUSB_core.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION @@ -43,7 +38,35 @@ // Serial is 64-bit DeviceID -> 16 chars len uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) }; -static void usb_device_task(void* param); +/* 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 "C" void tusb_hal_nrf_power_event(uint32_t event); + +void usb_init(void); + +void Adafruit_TinyUSB_core_init(void) +{ + USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial); + USBDevice.setID(USB_VID, USB_PID); + USBDevice.begin(); + + usb_init(); +} + + +// USB Device Driver task +// This top level thread process all usb events and invoke callbacks +static void usb_device_task(void* param) +{ + (void) param; + + // RTOS forever loop + while (1) + { + // tinyusb device task + tud_task(); + } +} // Init usb when starting up. Softdevice is not enabled yet void usb_init(void) @@ -85,36 +108,4 @@ void usb_init(void) xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL); } -// 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); -} - -// USB Device Driver task -// This top level thread process all usb events and invoke callbacks -static void usb_device_task(void* param) -{ - (void) param; - - // RTOS forever loop - while (1) - { - // tinyusb device task - tud_task(); - } -} - #endif diff --git a/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.h b/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.h index db0ca72c..4cae1463 100644 --- a/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.h +++ b/cores/nRF5/Adafruit_TinyUSB_core/Adafruit_TinyUSB_core.h @@ -32,4 +32,6 @@ #include "Adafruit_USBD_CDC.h" #endif +void Adafruit_TinyUSB_core_init(void); + #endif /* ADAFRUIT_TINYUSB_CORE_H_ */ diff --git a/cores/nRF5/Adafruit_TinyUSB_core/usb.h b/cores/nRF5/Adafruit_TinyUSB_core/usb.h deleted file mode 100644 index 5572ed9a..00000000 --- a/cores/nRF5/Adafruit_TinyUSB_core/usb.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2018, hathach for Adafruit - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#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_ */ diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index 8b2cf836..808a5dbb 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -16,8 +16,6 @@ #define ARDUINO_MAIN #include "Arduino.h" -#include "usb.h" - // DEBUG Level 1 #if CFG_DEBUG // weak function to avoid compilation error with @@ -75,10 +73,7 @@ int main( void ) initVariant(); #ifdef NRF52840_XXAA - USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial); - USBDevice.setID(USB_VID, USB_PID); - USBDevice.begin(); - usb_init(); + Adafruit_TinyUSB_core_init(); #endif #if CFG_DEBUG >= 3 diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index e8266df3..ccde4dd4 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -37,11 +37,6 @@ #include "bluefruit.h" #include "utility/bonding.h" -#ifdef NRF52840_XXAA -#include "nrfx_power.h" -#include "usb/usb.h" -#endif - #ifndef CFG_BLE_TX_POWER_LEVEL #define CFG_BLE_TX_POWER_LEVEL 0 #endif @@ -59,6 +54,32 @@ #define CFG_SOC_TASK_STACKSIZE (200) #endif +#ifdef NRF52840_XXAA +#include "nrfx_power.h" + +/* 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 "C" void tusb_hal_nrf_power_event(uint32_t event); + +// 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); +} + +#endif AdafruitBluefruit Bluefruit;