summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhathach <thach@tinyusb.org>2018-07-05 11:30:12 +0700
committerhathach <thach@tinyusb.org>2018-07-05 11:30:12 +0700
commit1745e20391a23bf65ff17a0515b803e97fa6970e (patch)
tree3181717bbe28b27bc3420d43f170a7474fc04f0c
parentbe2ff20e429bf3cb38c4c94927b0affc0c3ab782 (diff)
replace jlink with native usb cdc for serial REPL
-rw-r--r--ports/nrf/Makefile2
-rw-r--r--ports/nrf/background.c54
-rw-r--r--ports/nrf/mpconfigport.h4
-rw-r--r--ports/nrf/mphalport.c59
-rw-r--r--ports/nrf/supervisor/serial.c29
-rw-r--r--ports/nrf/usb/tusb_config.h4
6 files changed, 150 insertions, 2 deletions
diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile
index 1cc3d9e4a..745357695 100644
--- a/ports/nrf/Makefile
+++ b/ports/nrf/Makefile
@@ -116,6 +116,7 @@ SRC_C += \
boards/$(BOARD)/board.c \
nrfx/mdk/system_$(MCU_SUB_VARIANT).c \
device/$(MCU_VARIANT)/startup_$(MCU_SUB_VARIANT).c \
+ background.c \
lib/oofatfs/ff.c \
lib/oofatfs/option/ccsbcs.c \
lib/timeutils/timeutils.c \
@@ -127,6 +128,7 @@ SRC_C += \
lib/mp-readline/readline.c \
internal_flash.c \
+# lib/utils/stdout_helpers.c
ifeq ($(MCU_SUB_VARIANT),nrf52840)
diff --git a/ports/nrf/background.c b/ports/nrf/background.c
new file mode 100644
index 000000000..fc8128ca8
--- /dev/null
+++ b/ports/nrf/background.c
@@ -0,0 +1,54 @@
+/**************************************************************************/
+/*!
+ @file background.c
+ @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.
+*/
+/**************************************************************************/
+
+#include "tusb.h"
+
+/*------------------------------------------------------------------*/
+/* MACRO TYPEDEF CONSTANT ENUM
+ *------------------------------------------------------------------*/
+
+/*------------------------------------------------------------------*/
+/* VARIABLE DECLARATION
+ *------------------------------------------------------------------*/
+
+/*------------------------------------------------------------------*/
+/* FUNCTION DECLARATION
+ *------------------------------------------------------------------*/
+
+void run_background_tasks(void) {
+ tusb_task();
+ tud_cdc_flush();
+}
diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h
index 4d57d79f0..9fb8f68ee 100644
--- a/ports/nrf/mpconfigport.h
+++ b/ports/nrf/mpconfigport.h
@@ -254,6 +254,10 @@ extern const struct _mp_obj_module_t ble_module;
// We need to provide a declaration/definition of alloca()
#include <alloca.h>
+extern void run_background_tasks(void);
+#define MICROPY_VM_HOOK_LOOP run_background_tasks();
+#define MICROPY_VM_HOOK_RETURN run_background_tasks();
+
//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
#endif
diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c
index 62d2396df..e13554d7b 100644
--- a/ports/nrf/mphalport.c
+++ b/ports/nrf/mphalport.c
@@ -32,6 +32,8 @@
#include "py/mperrno.h"
#include "hal_uart.h"
+#ifndef NRF52840_XXAA
+
#define UART_INSTANCE UART_BASE(0)
#if (MICROPY_PY_BLE_NUS == 0)
@@ -84,3 +86,60 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
void mp_hal_stdout_tx_str(const char *str) {
mp_hal_stdout_tx_strn(str, strlen(str));
}
+
+#else
+
+#include "tusb.h"
+
+int mp_hal_stdin_rx_chr(void) {
+ for (;;) {
+ #ifdef MICROPY_VM_HOOK_LOOP
+ MICROPY_VM_HOOK_LOOP
+ #endif
+ // if (reload_requested) {
+ // return CHAR_CTRL_D;
+ // }
+ if (tud_cdc_available()) {
+ #ifdef MICROPY_HW_LED_RX
+ gpio_toggle_pin_level(MICROPY_HW_LED_RX);
+ #endif
+ return tud_cdc_read_char();
+ }
+ }
+}
+
+bool mp_hal_stdin_any(void) {
+ return tud_cdc_available() > 0;
+}
+
+void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
+
+// #ifdef MICROPY_HW_LED_TX
+// gpio_toggle_pin_level(MICROPY_HW_LED_TX);
+// #endif
+//
+// #ifdef CIRCUITPY_BOOT_OUTPUT_FILE
+// if (boot_output_file != NULL) {
+// UINT bytes_written = 0;
+// f_write(boot_output_file, str, len, &bytes_written);
+// }
+// #endif
+
+ tud_cdc_write(str, len);
+}
+
+void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
+ while (len--) {
+ if (*str == '\n') {
+ mp_hal_stdout_tx_strn("\r", 1);
+ }
+ mp_hal_stdout_tx_strn(str++, 1);
+ }
+}
+
+#endif
+
+void mp_hal_stdout_tx_str(const char *str) {
+ mp_hal_stdout_tx_strn(str, strlen(str));
+}
+
diff --git a/ports/nrf/supervisor/serial.c b/ports/nrf/supervisor/serial.c
index ce801e371..080f77644 100644
--- a/ports/nrf/supervisor/serial.c
+++ b/ports/nrf/supervisor/serial.c
@@ -36,6 +36,8 @@
#include "ble_uart.h"
#endif
+#ifndef NRF52840_XXAA
+
void serial_init(void) {
#if MICROPY_PY_BLE_NUS
@@ -84,3 +86,30 @@ void serial_write(const char* text) {
mp_hal_stdout_tx_str(text);
}
+#else
+
+#include "tusb.h"
+
+void serial_init(void) {
+ // usb should be initialized in board_init()
+}
+
+
+bool serial_connected(void) {
+ return tud_cdc_connected();
+}
+
+char serial_read(void) {
+ return (char) tud_cdc_read_char();
+}
+
+bool serial_bytes_available(void) {
+ return tud_cdc_available() > 0;
+}
+
+void serial_write(const char* text) {
+ tud_cdc_write(text, strlen(text));
+}
+
+#endif
+
diff --git a/ports/nrf/usb/tusb_config.h b/ports/nrf/usb/tusb_config.h
index 0875f91da..755e4cb6a 100644
--- a/ports/nrf/usb/tusb_config.h
+++ b/ports/nrf/usb/tusb_config.h
@@ -52,8 +52,8 @@
/*------------- RTOS -------------*/
#define CFG_TUSB_OS OPT_OS_NONE
-//#define CFG_TUD_TASK_PRIO 0
//#define CFG_TUD_TASK_QUEUE_SZ 16
+//#define CFG_TUD_TASK_PRIO 0
//#define CFG_TUD_TASK_STACK_SZ 150
//--------------------------------------------------------------------+
@@ -90,7 +90,7 @@
#define CFG_TUD_CDC_FLUSH_ON_SOF 0
// Number of supported Logical Unit Number (At least 1)
-#define CFG_TUD_MSC_MAXLUN 1
+#define CFG_TUD_MSC_MAXLUN 1
// Number of Blocks
#define CFG_TUD_MSC_BLOCK_NUM (256*1024)/512