diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 3470ec27..54b6ee7a 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -57,6 +57,8 @@ uint32_t setLoopStacksize(void); #include "pulse.h" #include "HardwarePWM.h" #include "utility/SoftwareTimer.h" + + #include "Uart.h" #endif #include "delay.h" @@ -67,11 +69,12 @@ uint32_t setLoopStacksize(void); #include "utility/AdaCallback.h" #ifdef NRF52840_XXAA -#include "tusb.h" -#endif + #include "tusb.h" #ifdef __cplusplus - #include "Uart.h" + #include "USBSerial.h" +#endif + #endif // Include board variant diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index af621be5..dd982b4d 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -202,16 +202,19 @@ size_t Uart::write(const uint8_t data) #define NRF_UART0_IRQn UARTE0_UART0_IRQn -#if defined(PIN_SERIAL_CTS) && defined(PIN_SERIAL_RTS) - Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX, PIN_SERIAL_CTS, PIN_SERIAL_RTS ); + +#ifdef NRF52840_XXAA + #define SERIAL_HW Serial1 #else - Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX ); + #define SERIAL_HW Serial #endif +Uart SERIAL_HW( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX ); + extern "C" { void UARTE0_UART0_IRQHandler() { - Serial.IrqHandler(); + SERIAL_HW.IrqHandler(); } } diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index d791aab9..1ba79f2f 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -70,7 +70,11 @@ class Uart : public HardwareSerial #ifdef __cplusplus -extern Uart Serial; +#ifdef NRF52840_XXAA + extern Uart Serial1; +#else + extern Uart Serial; +#endif #endif diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index d7d38ebc..e81826a2 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -61,6 +61,11 @@ static void loop_task(void* arg) { loop(); + #ifdef NRF52840_XXAA + tud_cdc_write_flush(); + #endif + + // Serial events if (serialEvent && serialEventRun) serialEventRun(); #if CFG_DEBUG >= 2 && DBG_MEM_INFO diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index 7fb60686..b4063bbf 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -36,12 +36,13 @@ #include "Arduino.h" +SchedulerRTOS Scheduler; + void yield(void) { taskYIELD(); } -SchedulerRTOS Scheduler; static void _redirect_task(void* arg) { @@ -82,12 +83,21 @@ bool SchedulerRTOS::startLoop(taskfunc_t task, const char* name, uint32_t stack_ return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_LOW, &handle); } + +//--------------------------------------------------------------------+ +// FreeRTOS Hooks +//--------------------------------------------------------------------+ + extern "C" { void vApplicationIdleHook( void ) { // Internal background task +#ifdef NRF52840_XXAA + // flush cdc in case loop() is suspended for low-power + tud_cdc_write_flush(); +#endif // Call user callback if defined if ( rtos_idle_callback ) rtos_idle_callback(); diff --git a/cores/nRF5/usb/USBSerial.cpp b/cores/nRF5/usb/USBSerial.cpp new file mode 100644 index 00000000..338a9ee8 --- /dev/null +++ b/cores/nRF5/usb/USBSerial.cpp @@ -0,0 +1,100 @@ +/**************************************************************************/ +/*! + @file USBSerial.cpp + @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. +*/ +/**************************************************************************/ + +#ifdef NRF52840_XXAA + +#include "USBSerial.h" +#include "Arduino.h" +#include "tusb.h" + + +USBSerial Serial; + +USBSerial::USBSerial(void) +{ + +} + +// Baud and config is ignore in CDC +void USBSerial::begin(uint32_t baud) { } +void USBSerial::begin(uint32_t baud, uint8_t config) { } + +void USBSerial::end(void) +{ + // nothing to do +} + +USBSerial::operator bool() +{ + return tud_cdc_connected(); +} + +int USBSerial::available(void) +{ + return tud_cdc_available(); +} + +int USBSerial::peek(void) +{ + return tud_cdc_peek(0); +} + +int USBSerial::read(void) +{ + return (int) tud_cdc_read_char(); +} + +size_t USBSerial::readBytes(char *buffer, size_t length) +{ + return tud_cdc_read(buffer, length); +} + +void USBSerial::flush(void) +{ + tud_cdc_write_flush(); +} + +size_t USBSerial::write(uint8_t ch) +{ + return tud_cdc_write_char((char) ch); +} + +size_t USBSerial::write(const uint8_t *buffer, size_t size) +{ + return tud_cdc_write(buffer, size); +} + +#endif // NRF52840_XXAA diff --git a/cores/nRF5/usb/USBSerial.h b/cores/nRF5/usb/USBSerial.h new file mode 100644 index 00000000..a4bd5fad --- /dev/null +++ b/cores/nRF5/usb/USBSerial.h @@ -0,0 +1,70 @@ +/**************************************************************************/ +/*! + @file USBSerial.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 USBSERIAL_H_ +#define USBSERIAL_H_ + +#ifdef NRF52840_XXAA + +#include "Stream.h" + +class USBSerial : public Stream +{ +public: + USBSerial(void); + + void begin(uint32_t baud_count); + void begin(uint32_t baud, uint8_t config); + void end(void); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buffer, size_t size); + using Print::write; // pull in write(str) from Print + operator bool(); + + size_t readBytes(char *buffer, size_t length); +}; + + +extern USBSerial Serial; + +#endif + +#endif /* USBSERIAL_H_ */ diff --git a/cores/nRF5/usb/usb.c b/cores/nRF5/usb/usb.c index 335d62d5..5ebfc8f4 100644 --- a/cores/nRF5/usb/usb.c +++ b/cores/nRF5/usb/usb.c @@ -47,9 +47,7 @@ //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ - - -extern uint16_t usb_desc_str_serial[1+16]; +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. */ diff --git a/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino b/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino index 7c9806cd..0daf7167 100644 --- a/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/SerialEcho/SerialEcho.ino @@ -28,8 +28,6 @@ void setup() Serial.println("Serial Echo demo"); Serial.print("Badurate : "); Serial.println(baudrate); - - } /**************************************************************************/ @@ -38,9 +36,7 @@ void setup() */ /**************************************************************************/ void loop() -{ - char ch; - +{ // From Serial monitor to All if ( Serial.available() ) {