redirect Serial to USBSerial for nrf52840

This commit is contained in:
hathach committed 2018-08-30 15:40:16 +07:00
1 parent e5d69d7b9a
commit 057cba9a93
9 files changed
+206 -17

No files matched your search

+6 -3
View File
@@ -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
+7 -4
View File
@@ -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();
}
}
+5 -1
View File
@@ -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
+5
View File
@@ -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
+11 -1
View File
@@ -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();
+100
View File
@@ -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
+70
View File
@@ -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_ */
+1 -3
View File
@@ -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. */
@@ -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() )
{