Line data Source code
1 : #pragma once 2 : 3 : /** 4 : * @file 5 : * When using the earlephilhower/arduino-pico core with the TinyUSB backend, 6 : * calling Serial.read/print/write before TinyUSBDevice.mounted() returns true 7 : * crashes the chip. The function in this file checks if the given stream is 8 : * actually the `Serial` port (USB CDC), and if so returns whether TinyUSB 9 : * has been mounted or not. If it returns true, it is safe to read/write from/to 10 : * the stream. 11 : */ 12 : 13 : #include <AH/Arduino-Wrapper.h> 14 : 15 : #if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) && \ 16 : defined(USE_TINYUSB) 17 : #include <Adafruit_TinyUSB.h> 18 : namespace { 19 : bool ensure_usb_init(Print &stream) { 20 : if (&stream == static_cast<Print *>(&Serial)) 21 : return TinyUSBDevice.mounted(); 22 : return true; 23 : } 24 : } // namespace 25 : #else 26 : namespace { 27 50 : constexpr bool ensure_usb_init(Print &) { return true; } 28 : } // namespace 29 : #endif