|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "tusb_option.h" |
| 26 | + |
| 27 | +#if CFG_TUD_ENABLED || CFG_TUH_ENABLED |
| 28 | + |
| 29 | +#include "Adafruit_USBD_Device.h" |
| 30 | +#include "Arduino.h" |
| 31 | + |
| 32 | +extern "C" { |
| 33 | + |
| 34 | +uint32_t tusb_time_millis_api(void) { return millis(); } |
| 35 | + |
| 36 | +//--------------------------------------------------------------------+ |
| 37 | +// Device |
| 38 | +//--------------------------------------------------------------------+ |
| 39 | +#if CFG_TUD_ENABLED |
| 40 | +void TinyUSB_Device_Init(uint8_t rhport) { |
| 41 | + // Init USB Device controller and stack |
| 42 | + TinyUSBDevice.begin(rhport); |
| 43 | +} |
| 44 | + |
| 45 | +// RP2040 has its own implementation since it needs mutex for dual core |
| 46 | +#ifndef ARDUINO_ARCH_RP2040 |
| 47 | +void TinyUSB_Device_Task(void) { |
| 48 | + // Run tinyusb device task |
| 49 | + tud_task(); |
| 50 | +} |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifndef ARDUINO_ARCH_ESP32 |
| 54 | +void TinyUSB_Device_FlushCDC(void) { |
| 55 | + uint8_t const cdc_instance = Adafruit_USBD_CDC::getInstanceCount(); |
| 56 | + for (uint8_t instance = 0; instance < cdc_instance; instance++) { |
| 57 | + tud_cdc_n_write_flush(instance); |
| 58 | + } |
| 59 | +} |
| 60 | +#endif |
| 61 | +#endif // CFG_TUD_ENABLED |
| 62 | + |
| 63 | +//------------- Debug log with Serial1 -------------// |
| 64 | +#if CFG_TUSB_DEBUG && defined(CFG_TUSB_DEBUG_PRINTF) && \ |
| 65 | + !defined(ARDUINO_ARCH_ESP32) |
| 66 | + |
| 67 | +// #define USE_SEGGER_RTT |
| 68 | +#ifndef SERIAL_TUSB_DEBUG |
| 69 | +#define SERIAL_TUSB_DEBUG Serial1 |
| 70 | +#endif |
| 71 | + |
| 72 | +#ifdef USE_SEGGER_RTT |
| 73 | +#include "SEGGER_RTT/RTT/SEGGER_RTT.h" |
| 74 | +#endif |
| 75 | + |
| 76 | +__attribute__((used)) int CFG_TUSB_DEBUG_PRINTF(const char *__restrict format, |
| 77 | + ...) { |
| 78 | + char buf[256]; |
| 79 | + int len; |
| 80 | + va_list ap; |
| 81 | + va_start(ap, format); |
| 82 | + len = vsnprintf(buf, sizeof(buf), format, ap); |
| 83 | + |
| 84 | +#ifdef USE_SEGGER_RTT |
| 85 | + SEGGER_RTT_Write(0, buf, len); |
| 86 | +#else |
| 87 | + static volatile bool ser_inited = false; |
| 88 | + if (!ser_inited) { |
| 89 | + ser_inited = true; |
| 90 | + SERIAL_TUSB_DEBUG.begin(115200); |
| 91 | + // SERIAL_TUSB_DEBUG.begin(921600); |
| 92 | + } |
| 93 | + SERIAL_TUSB_DEBUG.write(buf); |
| 94 | +#endif |
| 95 | + |
| 96 | + va_end(ap); |
| 97 | + return len; |
| 98 | +} |
| 99 | +#endif // CFG_TUSB_DEBUG |
| 100 | + |
| 101 | +} // extern C |
| 102 | + |
| 103 | +#endif // CFG_TUD_ENABLED || CFG_TUH_ENABLED |
0 commit comments