This is an automated email from the ASF dual-hosted git repository. jerzy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit d2b4b3cf546670a85928e7ccd55439fb9defe1bd Author: Jerzy Kasenberg <jerzy.kasenb...@codecoup.pl> AuthorDate: Thu Apr 30 15:06:21 2020 +0200 console: Add console over USB CDC This adds package that handles console over TinyUSB provided USB stack. --- hw/usb/tinyusb/README.md | 6 + .../full => hw/usb/tinyusb/cdc_console}/pkg.yml | 23 ++-- hw/usb/tinyusb/cdc_console/src/cdc_console.c | 147 +++++++++++++++++++++ .../usb/tinyusb/cdc_console/syscfg.yml | 30 ++--- sys/console/full/pkg.yml | 2 + sys/console/full/syscfg.yml | 3 + 6 files changed, 174 insertions(+), 37 deletions(-) diff --git a/hw/usb/tinyusb/README.md b/hw/usb/tinyusb/README.md index 5142326..5b1aa51 100644 --- a/hw/usb/tinyusb/README.md +++ b/hw/usb/tinyusb/README.md @@ -45,4 +45,10 @@ Vendor and product ID must be specified in **syscfg.vals:** section ```yaml USBD_VID: 0xABCD USBD_PID: 0x1234 + +``` +To use console over USB, set those values in **syscfg.vals:** section +```yaml + CONSOLE_USB: 1 + USBD_CDC: 1 ``` diff --git a/sys/console/full/pkg.yml b/hw/usb/tinyusb/cdc_console/pkg.yml old mode 100644 new mode 100755 similarity index 64% copy from sys/console/full/pkg.yml copy to hw/usb/tinyusb/cdc_console/pkg.yml index 150b5b0..45800d7 --- a/sys/console/full/pkg.yml +++ b/hw/usb/tinyusb/cdc_console/pkg.yml @@ -6,7 +6,7 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, @@ -17,26 +17,19 @@ # under the License. # -pkg.name: sys/console/full -pkg.description: Text-based IO interface. +pkg.name: hw/usb/tinyusb/cdc_console +pkg.description: Console over USB CDC. pkg.author: "Apache Mynewt <d...@mynewt.apache.org>" pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: + - console + - usb pkg.deps: - "@apache-mynewt-core/hw/hal" - "@apache-mynewt-core/kernel/os" -pkg.deps.CONSOLE_UART: - - "@apache-mynewt-core/hw/drivers/uart" -pkg.deps.CONSOLE_RTT: - - "@apache-mynewt-core/hw/drivers/rtt" -pkg.deps.CONSOLE_SEMIHOSTING: - - "@apache-mynewt-core/hw/drivers/semihosting" -pkg.deps.'CONSOLE_HISTORY == "ram"': - - "@apache-mynewt-core/sys/console/full/history_ram" -pkg.deps.'CONSOLE_HISTORY == "log"': - - "@apache-mynewt-core/sys/console/full/history_log" -pkg.apis: console + - "@apache-mynewt-core/hw/usb/tinyusb" + - "@tinyusb/tinyusb" pkg.init: - console_pkg_init: 'MYNEWT_VAL(CONSOLE_SYSINIT_STAGE)' + usb_cdc_console_pkg_init: 'MYNEWT_VAL(CONSOLE_SYSINIT_STAGE)' diff --git a/hw/usb/tinyusb/cdc_console/src/cdc_console.c b/hw/usb/tinyusb/cdc_console/src/cdc_console.c new file mode 100755 index 0000000..1eaa828 --- /dev/null +++ b/hw/usb/tinyusb/cdc_console/src/cdc_console.c @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <os/mynewt.h> + +#include <class/cdc/cdc_device.h> + +#include <console/console.h> +#include <bsp/bsp.h> + +static struct os_event rx_receive_event; +static struct os_event tx_flush_event; +static bool connected; + +static void +cdc_schedule_tx_flush(void) +{ + os_eventq_put(os_eventq_dflt_get(), &tx_flush_event); +} + +static void +cdc_write(int c) +{ + uint32_t written; + + written = tud_cdc_write_char(c); + if (tud_cdc_write_available() == 0) { + tud_cdc_write_flush(); + if (written == 0) { + tud_cdc_write_char(c); + } + } +} + +int +console_out_nolock(int c) +{ + cdc_write(c); + + if ('\n' == c) { + cdc_write('\r'); + } + + cdc_schedule_tx_flush(); + + return c; +} + +void +console_rx_restart(void) +{ + os_eventq_put(os_eventq_dflt_get(), &rx_receive_event); +} + +static void +tx_flush_ev_cb(struct os_event *ev) +{ + if (connected && tud_cdc_write_available() < USBD_CDC_DATA_EP_SIZE) { + if (tud_cdc_write_flush() == 0) { + /* + * Previous data not sent yet. + * There is no data sent notification in tinyusb/cdc, retry flush later. + */ + cdc_schedule_tx_flush(); + } + } +} + +static void +rx_ev_cb(struct os_event *ev) +{ + static int console_rejected_char = -1; + int ret; + + /* We may have unhandled character - try it first */ + if (console_rejected_char >= 0) { + ret = console_handle_char(console_rejected_char); + if (ret < 0) { + return; + } + } + + while (tud_cdc_available()) { + console_rejected_char = tud_cdc_read_char(); + if (console_rejected_char >= 0) { + ret = console_handle_char(console_rejected_char); + if (ret < 0) { + return; + } + } else { + break; + } + } + + console_rejected_char = -1; +} + +void +tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) +{ + if (dtr != connected) { + connected = dtr; + cdc_schedule_tx_flush(); + } +} + +/* Invoked when CDC interface received data from host */ +void +tud_cdc_rx_cb(uint8_t itf) +{ + os_eventq_put(os_eventq_dflt_get(), &rx_receive_event); +} + +void +tud_cdc_line_coding_cb(uint8_t itf, const cdc_line_coding_t *p_line_coding) +{ +} + +void +tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) +{ +} + +int +usb_cdc_console_pkg_init(void) +{ + rx_receive_event.ev_cb = rx_ev_cb; + tx_flush_event.ev_cb = tx_flush_ev_cb; + + return 0; +} diff --git a/sys/console/full/pkg.yml b/hw/usb/tinyusb/cdc_console/syscfg.yml old mode 100644 new mode 100755 similarity index 51% copy from sys/console/full/pkg.yml copy to hw/usb/tinyusb/cdc_console/syscfg.yml index 150b5b0..ed5a3bb --- a/sys/console/full/pkg.yml +++ b/hw/usb/tinyusb/cdc_console/syscfg.yml @@ -6,7 +6,7 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, @@ -17,26 +17,12 @@ # under the License. # -pkg.name: sys/console/full -pkg.description: Text-based IO interface. -pkg.author: "Apache Mynewt <d...@mynewt.apache.org>" -pkg.homepage: "http://mynewt.apache.org/" -pkg.keywords: +syscfg.defs: + USBD_CDC_DECRIPTOR_STRING: + description: String for CDC interface + value: '"Mynewt console"' -pkg.deps: - - "@apache-mynewt-core/hw/hal" - - "@apache-mynewt-core/kernel/os" -pkg.deps.CONSOLE_UART: - - "@apache-mynewt-core/hw/drivers/uart" -pkg.deps.CONSOLE_RTT: - - "@apache-mynewt-core/hw/drivers/rtt" -pkg.deps.CONSOLE_SEMIHOSTING: - - "@apache-mynewt-core/hw/drivers/semihosting" -pkg.deps.'CONSOLE_HISTORY == "ram"': - - "@apache-mynewt-core/sys/console/full/history_ram" -pkg.deps.'CONSOLE_HISTORY == "log"': - - "@apache-mynewt-core/sys/console/full/history_log" -pkg.apis: console +syscfg.vals: -pkg.init: - console_pkg_init: 'MYNEWT_VAL(CONSOLE_SYSINIT_STAGE)' +syscfg.restrictions: + - "USBD_CDC" diff --git a/sys/console/full/pkg.yml b/sys/console/full/pkg.yml index 150b5b0..cda20d9 100644 --- a/sys/console/full/pkg.yml +++ b/sys/console/full/pkg.yml @@ -32,6 +32,8 @@ pkg.deps.CONSOLE_RTT: - "@apache-mynewt-core/hw/drivers/rtt" pkg.deps.CONSOLE_SEMIHOSTING: - "@apache-mynewt-core/hw/drivers/semihosting" +pkg.deps.CONSOLE_USB: + - "@apache-mynewt-core/hw/usb/tinyusb/cdc_console" pkg.deps.'CONSOLE_HISTORY == "ram"': - "@apache-mynewt-core/sys/console/full/history_ram" pkg.deps.'CONSOLE_HISTORY == "log"': diff --git a/sys/console/full/syscfg.yml b/sys/console/full/syscfg.yml index 2289163..f8fcb84 100644 --- a/sys/console/full/syscfg.yml +++ b/sys/console/full/syscfg.yml @@ -26,6 +26,9 @@ syscfg.defs: CONSOLE_SEMIHOSTING: description: 'Set console output to ARM semihosting' value: 0 + CONSOLE_USB: + description: 'Set console to USB' + value: 0 CONSOLE_BLE_MONITOR: description: 'Set console output to BLE Monitor' value: 0