├── library.json ├── library.properties ├── LICENSE ├── src ├── arduino │ ├── Adafruit_USBD_Interface.cpp │ ├── Adafruit_USBD_Interface.h │ ├── midi │ │ ├── Adafruit_USBD_MIDI.h │ │ └── Adafruit_USBD_MIDI.cpp │ ├── msc │ │ ├── Adafruit_USBH_MSC.h │ │ ├── Adafruit_USBH_MSC.cpp │ │ └── Adafruit_USBD_MSC.h │ ├── cdc │ │ └── Adafruit_USBH_CDC.h │ ├── video │ │ └── Adafruit_USBD_Video.h │ ├── Adafruit_TinyUSB_API.h │ ├── webusb │ │ └── Adafruit_USBD_WebUSB.h │ ├── Adafruit_TinyUSB_API.cpp │ ├── Adafruit_USBD_CDC.h │ ├── Adafruit_USBH_Host.h │ ├── ports │ │ ├── nrf │ │ │ ├── Adafruit_TinyUSB_nrf.cpp │ │ │ └── tusb_config_nrf.h │ │ ├── samd │ │ │ ├── tusb_config_samd.h │ │ │ └── Adafruit_TinyUSB_samd.cpp │ │ └── rp2040 │ │ │ ├── Adafruit_TinyUSB_rp2040.cpp │ │ │ └── tusb_config_rp2040.h │ ├── Adafruit_USBD_Device.h │ └── hid │ │ └── Adafruit_USBD_HID.h ├── class │ ├── dfu │ │ ├── dfu_rt_device.h │ │ ├── dfu.h │ │ ├── dfu_device.h │ │ └── dfu_rt_device.c │ ├── cdc │ │ └── serial │ │ │ ├── cp210x.h │ │ │ └── ch34x.h │ ├── net │ │ ├── ncm.h │ │ └── net_device.h │ ├── video │ │ └── video_device.h │ ├── usbtmc │ │ └── usbtmc_device.h │ └── bth │ │ └── bth_device.h ├── tusb_config.h ├── portable │ ├── synopsys │ │ └── dwc2 │ │ │ ├── dwc2_bcm.h │ │ │ ├── dwc2_xmc.h │ │ │ ├── dwc2_efm32.h │ │ │ ├── dwc2_esp32.h │ │ │ └── dwc2_gd32.h │ └── raspberrypi │ │ └── rp2040 │ │ └── rp2040_usb.h ├── M5_Max3421E_Usb.h ├── osal │ ├── osal.h │ └── osal_rtthread.h ├── tusb.h └── host │ └── usbh_pvt.h ├── README.md └── examples ├── GPIN └── GPIN.ino ├── CDC └── usbh_helper.h ├── HID ├── usbh_helper.h └── hid_device_report.ino ├── Simple └── device_info │ └── usbh_helper.h ├── MassStorage ├── msc_data_logger │ └── usbh_helper.h └── msc_file_explorer │ ├── usbh_helper.h │ └── msc_file_explorer.ino └── GPOUT └── GPOUT.ino /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "M5 Max3421e USB Arduino Library", 3 | "build": { 4 | "libArchive": false, 5 | "flags": "-DUSE_M5_Max3421e_USB" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=M5_Max3421e_USB_Arduino Library 2 | version=1.0.0 3 | author=Pandian 4 | maintainer=M5Stack 5 | sentence=M5 Max3421e USB library for Arduino 6 | paragraph=Support nRF5x, SAMD21, SAMD51, RP2040, ESP32-S2/S3 7 | category=Communication 8 | url=https://github.com/m5stack/M5-Max3421E-USBShield 9 | architectures=* 10 | includes=M5_Max3421E_Usb.h 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 M5Stack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_USBD_Interface.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Ha Thach (tinyusb.org) 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 28 | 29 | #include "Adafruit_USBD_Device.h" 30 | 31 | void Adafruit_USBD_Interface::setStringDescriptor(const char *str) { 32 | _strid = TinyUSBDevice.addStringDescriptor(str); 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M5-Max3421E-USBShield 2 | USB driver module, integrated MAX3421E is a dual-role USB controller, which can be programmed either as a USB peripheral or host capability to any system with an SPI interface 3 | 4 | English | [中文](README_cn.md) 5 | 6 | M5Core2_P1M5Core2_P1 7 | 8 | * **For the Detailed documentation of USB, Please [Click here](https://docs.m5stack.com/en/module/usb)** 9 | * **For the Detailed documentation of USB V1.2, Please [Click here](https://docs.m5stack.com/en/module/USB%20v1.2%20Module)** 10 | 11 | ## Description: 12 | 13 | USB driver module in the M5Stack stackable module series, adopting the MAX3421E chip solution. It provides a standard USB interface, supporting both USB host and peripheral functions. It is compatible with a wide range of USB devices and operating systems, connecting to the system via SPI communication, and reserves pins for controlling external devices or reading their status. 14 | 15 | ## Notes Module V1.2: 16 | 17 | The board is equipped with two dip switches to suit different M5 series host devices. 18 | 19 | library 20 | --------------------------- 21 | 22 | - [M5GFX](https://github.com/m5stack/M5GFX) 23 | - [M5Unified](https://github.com/m5stack/M5Unified) 24 | 25 | 26 | ## Credits & License: 27 | 28 | - [Adafruit_TinyUSB_Arduino] Copyright (c) 2019 Ha Thach for Adafruit Industries and licensed under MIT License. 29 | 30 | [Adafruit_TinyUSB_Arduino]: https://github.com/adafruit/Adafruit_TinyUSB_Arduino -------------------------------------------------------------------------------- /src/arduino/Adafruit_USBD_Interface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Ha Thach (tinyusb.org) 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 | #ifndef ADAFRUIT_USBD_INTERFACE_H_ 26 | #define ADAFRUIT_USBD_INTERFACE_H_ 27 | 28 | #include 29 | #include 30 | 31 | class Adafruit_USBD_Interface { 32 | protected: 33 | uint8_t _strid; 34 | 35 | public: 36 | Adafruit_USBD_Interface(void) { _strid = 0; } 37 | 38 | // Get Interface Descriptor 39 | // Fill the descriptor (if buf is not NULL) and return its length 40 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 41 | uint8_t *buf, uint16_t bufsize) = 0; 42 | // Get Interface Descriptor Length 43 | uint16_t getInterfaceDescriptorLen() { 44 | return getInterfaceDescriptor(0, NULL, 0); 45 | } 46 | 47 | void setStringDescriptor(const char *str); 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /src/class/dfu/dfu_rt_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Sylvain Munaut 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_DFU_RT_DEVICE_H_ 28 | #define _TUSB_DFU_RT_DEVICE_H_ 29 | 30 | #include "dfu.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | //--------------------------------------------------------------------+ 37 | // Application Callback API (weak is optional) 38 | //--------------------------------------------------------------------+ 39 | // Invoked when a DFU_DETACH request is received and bitWillDetach is set 40 | void tud_dfu_runtime_reboot_to_dfu_cb(void); 41 | 42 | //--------------------------------------------------------------------+ 43 | // Internal Class Driver API 44 | //--------------------------------------------------------------------+ 45 | void dfu_rtd_init(void); 46 | void dfu_rtd_reset(uint8_t rhport); 47 | uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 48 | bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* _TUSB_DFU_RT_DEVICE_H_ */ 55 | -------------------------------------------------------------------------------- /src/tusb_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018, hathach for Adafruit 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 | #ifndef _TUSB_CONFIG_ARDUINO_H_ 26 | #define _TUSB_CONFIG_ARDUINO_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #if defined(ARDUINO_ARCH_SAMD) 33 | #include "arduino/ports/samd/tusb_config_samd.h" 34 | 35 | #elif defined(ARDUINO_NRF52_ADAFRUIT) 36 | #include "arduino/ports/nrf/tusb_config_nrf.h" 37 | 38 | #elif defined(ARDUINO_ARCH_RP2040) 39 | #include "arduino/ports/rp2040/tusb_config_rp2040.h" 40 | 41 | #elif defined(ARDUINO_ARCH_ESP32) 42 | // Note: when compiling core Arduino IDEs will include tusb_config.h in the BSP 43 | // sdk/include/arduino_tinyusb/include. While compiling .c file in this library this 44 | // file will be used instead. For consistency: include the one in BSP here as well 45 | #include "sdkconfig.h" 46 | #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 47 | #include "../../arduino_tinyusb/include/tusb_config.h" 48 | #else 49 | #include "arduino/ports/esp32/tusb_config_esp32.h" 50 | #endif 51 | 52 | // Note: For platformio prioritize this file over the one in BSP in all cases 53 | 54 | #else 55 | #error TinyUSB Arduino Library does not support your core yet 56 | #endif 57 | 58 | // Debug TinyUSB with Serial1 59 | #if CFG_TUSB_DEBUG 60 | #define CFG_TUSB_DEBUG_PRINTF log_printf 61 | #endif 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | #endif /* _TUSB_CONFIG_ARDUINO_H_ */ 68 | -------------------------------------------------------------------------------- /src/arduino/midi/Adafruit_USBD_MIDI.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 hathach 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 | #ifndef ADAFRUIT_USBD_MIDI_H_ 26 | #define ADAFRUIT_USBD_MIDI_H_ 27 | 28 | #include "Stream.h" 29 | #include "arduino/Adafruit_USBD_Device.h" 30 | 31 | class Adafruit_USBD_MIDI : public Stream, public Adafruit_USBD_Interface { 32 | public: 33 | Adafruit_USBD_MIDI(uint8_t n_cables = 1); 34 | 35 | void setCables(uint8_t n_cables); 36 | 37 | // Set the cable number with a USB device descriptor string 38 | // Note: per MIDI specs cable_id (or jackid/elementid) starting from 1. 0 is 39 | // reserved for undefined ID. 40 | bool setCableName(uint8_t cable_id, const char *str); 41 | bool begin(void); 42 | 43 | // for MIDI library 44 | bool begin(uint32_t baud) { 45 | (void)baud; 46 | return begin(); 47 | } 48 | 49 | // Stream interface to use with MIDI Library 50 | virtual int read(void); 51 | virtual size_t write(uint8_t b); 52 | virtual int available(void); 53 | virtual int peek(void); 54 | virtual void flush(void); 55 | 56 | using Stream::write; 57 | 58 | // Raw MIDI USB packet interface. 59 | bool writePacket(const uint8_t packet[4]); 60 | bool readPacket(uint8_t packet[4]); 61 | 62 | // from Adafruit_USBD_Interface 63 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 64 | uint8_t *buf, uint16_t bufsize); 65 | 66 | private: 67 | uint8_t _n_cables; 68 | uint8_t _cable_name_strid[16]; 69 | }; 70 | 71 | #endif /* ADAFRUIT_USBD_MIDI_H_ */ 72 | -------------------------------------------------------------------------------- /src/class/cdc/serial/cp210x.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2023 Ha Thach (thach@tinyusb.org) 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 | #ifndef TUSB_CP210X_H 26 | #define TUSB_CP210X_H 27 | 28 | // Protocol details can be found at AN571: CP210x Virtual COM Port Interface 29 | // https://www.silabs.com/documents/public/application-notes/AN571.pdf 30 | 31 | #define TU_CP210X_VID 0x10C4 32 | 33 | /* Config request codes */ 34 | #define CP210X_IFC_ENABLE 0x00 35 | #define CP210X_SET_BAUDDIV 0x01 36 | #define CP210X_GET_BAUDDIV 0x02 37 | #define CP210X_SET_LINE_CTL 0x03 // Set parity, data bits, stop bits 38 | #define CP210X_GET_LINE_CTL 0x04 39 | #define CP210X_SET_BREAK 0x05 40 | #define CP210X_IMM_CHAR 0x06 41 | #define CP210X_SET_MHS 0x07 // Set DTR, RTS 42 | #define CP210X_GET_MDMSTS 0x08 // Get modem status (DTR, RTS, CTS, DSR, RI, DCD) 43 | #define CP210X_SET_XON 0x09 44 | #define CP210X_SET_XOFF 0x0A 45 | #define CP210X_SET_EVENTMASK 0x0B 46 | #define CP210X_GET_EVENTMASK 0x0C 47 | #define CP210X_SET_CHAR 0x0D 48 | #define CP210X_GET_CHARS 0x0E 49 | #define CP210X_GET_PROPS 0x0F 50 | #define CP210X_GET_COMM_STATUS 0x10 51 | #define CP210X_RESET 0x11 52 | #define CP210X_PURGE 0x12 53 | #define CP210X_SET_FLOW 0x13 54 | #define CP210X_GET_FLOW 0x14 55 | #define CP210X_EMBED_EVENTS 0x15 56 | #define CP210X_GET_EVENTSTATE 0x16 57 | #define CP210X_SET_CHARS 0x19 58 | #define CP210X_GET_BAUDRATE 0x1D 59 | #define CP210X_SET_BAUDRATE 0x1E 60 | #define CP210X_VENDOR_SPECIFIC 0xFF // GPIO, Recipient must be Device 61 | 62 | #endif //TUSB_CP210X_H 63 | -------------------------------------------------------------------------------- /src/arduino/msc/Adafruit_USBH_MSC.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2022 Ha Thach (tinyusb.org) 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 | #ifndef ADAFRUIT_USBH_MSC_H_ 26 | #define ADAFRUIT_USBH_MSC_H_ 27 | 28 | #include "tusb.h" 29 | 30 | // define SdFat host helper class if SdFat library is available 31 | #if __has_include("SdFat.h") 32 | 33 | #include "SdFat.h" 34 | 35 | class Adafruit_USBH_MSC_BlockDevice : public FsBlockDeviceInterface { 36 | public: 37 | Adafruit_USBH_MSC_BlockDevice(); 38 | 39 | bool begin(uint8_t dev_addr); 40 | void end(void); 41 | 42 | bool mounted(void); 43 | 44 | // Set active LUN 45 | bool setActiveLUN(uint8_t lun); 46 | void setWriteCompleteCallback(tuh_msc_complete_cb_t cb); 47 | 48 | //------------- SdFat v2 FsBlockDeviceInterface API -------------// 49 | virtual bool isBusy(); 50 | virtual uint32_t sectorCount(); 51 | virtual bool syncDevice(); 52 | 53 | virtual bool readSector(uint32_t block, uint8_t *dst); 54 | virtual bool readSectors(uint32_t block, uint8_t *dst, size_t ns); 55 | virtual bool writeSector(uint32_t block, const uint8_t *src); 56 | virtual bool writeSectors(uint32_t block, const uint8_t *src, size_t ns); 57 | 58 | //------------- Internal APIs -------------// 59 | bool _io_complete_cb(uint8_t dev_addr, 60 | tuh_msc_complete_data_t const *cb_data); 61 | 62 | private: 63 | uint8_t _daddr; 64 | uint8_t _lun; 65 | 66 | // TODO use mutex to prevent race condition or atomic for better 67 | // implementation 68 | volatile bool _busy; 69 | 70 | tuh_msc_complete_cb_t _wr_cb; 71 | 72 | bool wait_for_io(void); 73 | }; 74 | 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/class/net/ncm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | 28 | #ifndef _TUSB_NCM_H_ 29 | #define _TUSB_NCM_H_ 30 | 31 | #include "common/tusb_common.h" 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | // Table 4.3 Data Class Interface Protocol Codes 38 | typedef enum 39 | { 40 | NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01 41 | } ncm_data_interface_protocol_code_t; 42 | 43 | 44 | // Table 6.2 Class-Specific Request Codes for Network Control Model subclass 45 | typedef enum 46 | { 47 | NCM_SET_ETHERNET_MULTICAST_FILTERS = 0x40, 48 | NCM_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41, 49 | NCM_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42, 50 | NCM_SET_ETHERNET_PACKET_FILTER = 0x43, 51 | NCM_GET_ETHERNET_STATISTIC = 0x44, 52 | NCM_GET_NTB_PARAMETERS = 0x80, 53 | NCM_GET_NET_ADDRESS = 0x81, 54 | NCM_SET_NET_ADDRESS = 0x82, 55 | NCM_GET_NTB_FORMAT = 0x83, 56 | NCM_SET_NTB_FORMAT = 0x84, 57 | NCM_GET_NTB_INPUT_SIZE = 0x85, 58 | NCM_SET_NTB_INPUT_SIZE = 0x86, 59 | NCM_GET_MAX_DATAGRAM_SIZE = 0x87, 60 | NCM_SET_MAX_DATAGRAM_SIZE = 0x88, 61 | NCM_GET_CRC_MODE = 0x89, 62 | NCM_SET_CRC_MODE = 0x8A, 63 | } ncm_request_code_t; 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/portable/synopsys/dwc2/dwc2_bcm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_DWC2_BCM_H_ 28 | #define _TUSB_DWC2_BCM_H_ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include "broadcom/defines.h" 35 | #include "broadcom/interrupts.h" 36 | #include "broadcom/caches.h" 37 | 38 | #define DWC2_EP_MAX 8 39 | 40 | static const dwc2_controller_t _dwc2_controller[] = 41 | { 42 | { .reg_base = USB_OTG_GLOBAL_BASE, .irqnum = USB_IRQn, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 4096 } 43 | }; 44 | 45 | #define dcache_clean(_addr, _size) data_clean(_addr, _size) 46 | #define dcache_invalidate(_addr, _size) data_invalidate(_addr, _size) 47 | #define dcache_clean_invalidate(_addr, _size) data_clean_and_invalidate(_addr, _size) 48 | 49 | TU_ATTR_ALWAYS_INLINE 50 | static inline void dwc2_dcd_int_enable(uint8_t rhport) 51 | { 52 | BP_EnableIRQ(_dwc2_controller[rhport].irqnum); 53 | } 54 | 55 | TU_ATTR_ALWAYS_INLINE 56 | static inline void dwc2_dcd_int_disable (uint8_t rhport) 57 | { 58 | BP_DisableIRQ(_dwc2_controller[rhport].irqnum); 59 | } 60 | 61 | static inline void dwc2_remote_wakeup_delay(void) 62 | { 63 | // try to delay for 1 ms 64 | // TODO implement later 65 | } 66 | 67 | // MCU specific PHY init, called BEFORE core reset 68 | static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 69 | { 70 | (void) dwc2; 71 | (void) hs_phy_type; 72 | 73 | // nothing to do 74 | } 75 | 76 | // MCU specific PHY update, it is called AFTER init() and core reset 77 | static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 78 | { 79 | (void) dwc2; 80 | (void) hs_phy_type; 81 | 82 | // nothing to do 83 | } 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /src/portable/synopsys/dwc2/dwc2_xmc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Rafael Silva (@perigoso) 5 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | * This file is part of the TinyUSB stack. 26 | */ 27 | 28 | #ifndef _DWC2_XMC_H_ 29 | #define _DWC2_XMC_H_ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include "xmc_device.h" 36 | 37 | #define DWC2_EP_MAX 7 38 | 39 | static const dwc2_controller_t _dwc2_controller[] = 40 | { 41 | // Note: XMC has some custom control registers before DWC registers 42 | { .reg_base = USB0_BASE, .irqnum = USB0_0_IRQn, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 2048 } 43 | }; 44 | 45 | TU_ATTR_ALWAYS_INLINE 46 | static inline void dwc2_dcd_int_enable(uint8_t rhport) 47 | { 48 | NVIC_EnableIRQ(_dwc2_controller[rhport].irqnum); 49 | } 50 | 51 | TU_ATTR_ALWAYS_INLINE 52 | static inline void dwc2_dcd_int_disable (uint8_t rhport) 53 | { 54 | NVIC_DisableIRQ(_dwc2_controller[rhport].irqnum); 55 | } 56 | 57 | static inline void dwc2_remote_wakeup_delay(void) 58 | { 59 | // try to delay for 1 ms 60 | // uint32_t count = SystemCoreClock / 1000; 61 | // while ( count-- ) __NOP(); 62 | } 63 | 64 | // MCU specific PHY init, called BEFORE core reset 65 | static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 66 | { 67 | (void) dwc2; 68 | (void) hs_phy_type; 69 | 70 | // Enable PHY 71 | //USB->ROUTE = USB_ROUTE_PHYPEN; 72 | } 73 | 74 | // MCU specific PHY update, it is called AFTER init() and core reset 75 | static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 76 | { 77 | (void) dwc2; 78 | (void) hs_phy_type; 79 | 80 | // XMC Manual: turn around must be 5 (reset & default value) 81 | // dwc2->gusbcfg = (dwc2->gusbcfg & ~GUSBCFG_TRDT_Msk) | (5u << GUSBCFG_TRDT_Pos); 82 | } 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /src/M5_Max3421E_Usb.h: -------------------------------------------------------------------------------- 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 | #ifndef M5_MAX3421E_USB_H_ 26 | #define M5_MAX3421E_USB_H_ 27 | 28 | // Error message for Core that must select TinyUSB via menu 29 | #if !defined(USE_TINYUSB) && \ 30 | (defined(ARDUINO_ARCH_SAMD) || \ 31 | (defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED))) 32 | #error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack" 33 | #endif 34 | 35 | // ESP32 out-of-sync 36 | #ifdef ARDUINO_ARCH_ESP32 37 | #include "arduino/ports/esp32/tusb_config_esp32.h" 38 | #endif 39 | 40 | #include "tusb_option.h" 41 | 42 | // Device 43 | #if CFG_TUD_ENABLED 44 | 45 | #include "arduino/Adafruit_USBD_Device.h" 46 | 47 | #if CFG_TUD_CDC 48 | #include "arduino/Adafruit_USBD_CDC.h" 49 | #endif 50 | 51 | #if CFG_TUD_HID 52 | #include "arduino/hid/Adafruit_USBD_HID.h" 53 | #endif 54 | 55 | #if CFG_TUD_MIDI 56 | #include "arduino/midi/Adafruit_USBD_MIDI.h" 57 | #endif 58 | 59 | #if CFG_TUD_MSC 60 | #include "arduino/msc/Adafruit_USBD_MSC.h" 61 | #endif 62 | 63 | #if CFG_TUD_VENDOR 64 | #include "arduino/webusb/Adafruit_USBD_WebUSB.h" 65 | #endif 66 | 67 | #if CFG_TUD_VIDEO 68 | #include "arduino/video/Adafruit_USBD_Video.h" 69 | #endif 70 | 71 | // Initialize device hardware, stack, also Serial as CDC 72 | // Wrapper for TinyUSBDevice.begin(rhport) 73 | void TinyUSB_Device_Init(uint8_t rhport); 74 | 75 | #endif 76 | 77 | // Host 78 | #if CFG_TUH_ENABLED 79 | 80 | #include "arduino/Adafruit_USBH_Host.h" 81 | 82 | #if CFG_TUH_CDC 83 | #include "arduino/cdc/Adafruit_USBH_CDC.h" 84 | #endif 85 | 86 | #if CFG_TUH_MSC 87 | #include "arduino/msc/Adafruit_USBH_MSC.h" 88 | #endif 89 | 90 | #include "arduino/hid/Adafruit_USBD_HID.h" 91 | 92 | #endif 93 | 94 | #endif /* ADAFRUIT_TINYUSB_H_ */ 95 | -------------------------------------------------------------------------------- /src/portable/synopsys/dwc2/dwc2_efm32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Rafael Silva (@perigoso) 5 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | * This file is part of the TinyUSB stack. 26 | */ 27 | 28 | #ifndef _DWC2_EFM32_H_ 29 | #define _DWC2_EFM32_H_ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include "em_device.h" 36 | 37 | // EFM32 has custom control register before DWC registers 38 | #define DWC2_REG_BASE (USB_BASE + offsetof(USB_TypeDef, GOTGCTL)) 39 | #define DWC2_EP_MAX 7 40 | 41 | static const dwc2_controller_t _dwc2_controller[] = 42 | { 43 | { .reg_base = DWC2_REG_BASE, .irqnum = USB_IRQn, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 2048 } 44 | }; 45 | 46 | TU_ATTR_ALWAYS_INLINE 47 | static inline void dwc2_dcd_int_enable(uint8_t rhport) 48 | { 49 | NVIC_EnableIRQ(_dwc2_controller[rhport].irqnum); 50 | } 51 | 52 | TU_ATTR_ALWAYS_INLINE 53 | static inline void dwc2_dcd_int_disable (uint8_t rhport) 54 | { 55 | NVIC_DisableIRQ(_dwc2_controller[rhport].irqnum); 56 | } 57 | 58 | static inline void dwc2_remote_wakeup_delay(void) 59 | { 60 | // try to delay for 1 ms 61 | // uint32_t count = SystemCoreClock / 1000; 62 | // while ( count-- ) __NOP(); 63 | } 64 | 65 | // MCU specific PHY init, called BEFORE core reset 66 | static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 67 | { 68 | (void) dwc2; 69 | (void) hs_phy_type; 70 | 71 | // Enable PHY 72 | USB->ROUTE = USB_ROUTE_PHYPEN; 73 | } 74 | 75 | // MCU specific PHY update, it is called AFTER init() and core reset 76 | static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 77 | { 78 | (void) dwc2; 79 | (void) hs_phy_type; 80 | 81 | // EFM32 Manual: turn around must be 5 (reset & default value) 82 | // dwc2->gusbcfg = (dwc2->gusbcfg & ~GUSBCFG_TRDT_Msk) | (5u << GUSBCFG_TRDT_Pos); 83 | } 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /src/portable/synopsys/dwc2/dwc2_esp32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | 28 | #ifndef _DWC2_ESP32_H_ 29 | #define _DWC2_ESP32_H_ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include "esp_intr_alloc.h" 36 | #include "soc/periph_defs.h" 37 | //#include "soc/usb_periph.h" 38 | 39 | #define DWC2_REG_BASE 0x60080000UL 40 | #define DWC2_EP_MAX 6 // USB_OUT_EP_NUM. TODO ESP32Sx only has 5 tx fifo (5 endpoint IN) 41 | 42 | static const dwc2_controller_t _dwc2_controller[] = 43 | { 44 | { .reg_base = DWC2_REG_BASE, .irqnum = 0, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 1024 } 45 | }; 46 | 47 | static intr_handle_t usb_ih; 48 | 49 | static void dcd_int_handler_wrap(void* arg) 50 | { 51 | (void) arg; 52 | dcd_int_handler(0); 53 | } 54 | 55 | TU_ATTR_ALWAYS_INLINE 56 | static inline void dwc2_dcd_int_enable (uint8_t rhport) 57 | { 58 | (void) rhport; 59 | esp_intr_alloc(ETS_USB_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, dcd_int_handler_wrap, NULL, &usb_ih); 60 | } 61 | 62 | TU_ATTR_ALWAYS_INLINE 63 | static inline void dwc2_dcd_int_disable (uint8_t rhport) 64 | { 65 | (void) rhport; 66 | esp_intr_free(usb_ih); 67 | } 68 | 69 | static inline void dwc2_remote_wakeup_delay(void) 70 | { 71 | vTaskDelay(pdMS_TO_TICKS(1)); 72 | } 73 | 74 | // MCU specific PHY init, called BEFORE core reset 75 | static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 76 | { 77 | (void) dwc2; 78 | (void) hs_phy_type; 79 | 80 | // nothing to do 81 | } 82 | 83 | // MCU specific PHY update, it is called AFTER init() and core reset 84 | static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 85 | { 86 | (void) dwc2; 87 | (void) hs_phy_type; 88 | 89 | // nothing to do 90 | } 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /* _DWC2_ESP32_H_ */ 97 | -------------------------------------------------------------------------------- /src/arduino/cdc/Adafruit_USBH_CDC.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2022 Ha Thach (tinyusb.org) 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 | #ifndef ADAFRUIT_USBH_CDC_H_ 26 | #define ADAFRUIT_USBH_CDC_H_ 27 | 28 | #include "HardwareSerial.h" 29 | 30 | class Adafruit_USBH_CDC : public HardwareSerial { 31 | public: 32 | Adafruit_USBH_CDC(void); 33 | 34 | // Set/Get index of cdc interface 35 | void setInterfaceIndex(uint8_t idx) { _idx = idx; } 36 | uint8_t getInterfaceIndex(void) { return _idx; } 37 | 38 | void begin(unsigned long baudrate); 39 | void begin(unsigned long baudrate, uint16_t config); 40 | 41 | bool mount(uint8_t idx); 42 | void umount(uint8_t idx); 43 | 44 | // unbind cdc interface 45 | void end(void); 46 | 47 | // If cdc is mounted 48 | bool mounted(void); 49 | operator bool() { return mounted(); } 50 | 51 | // if cdc's DTR is asserted 52 | bool connected(void); 53 | 54 | // Line encoding 55 | uint32_t baud(); 56 | 57 | //------------- Control API -------------// 58 | bool setDtrRts(bool dtr, bool rts, tuh_xfer_cb_t complete_cb = NULL, 59 | uintptr_t user_data = 0); 60 | bool setBaudrate(uint32_t baudrate, tuh_xfer_cb_t complete_cb = NULL, 61 | uintptr_t user_data = 0); 62 | 63 | //------------- Stream API -------------// 64 | virtual int available(void); 65 | virtual int peek(void); 66 | 67 | virtual int read(void); 68 | size_t read(uint8_t *buffer, size_t size); 69 | 70 | virtual void flush(void); 71 | virtual size_t write(uint8_t ch); 72 | 73 | virtual size_t write(const uint8_t *buffer, size_t size); 74 | size_t write(const char *buffer, size_t size) { 75 | return write((const uint8_t *)buffer, size); 76 | } 77 | 78 | virtual int availableForWrite(void); 79 | using Print::write; // pull in write(str) from Print 80 | 81 | private: 82 | uint8_t _idx; // TinyUSB CDC Interface Index 83 | uint32_t _baud; 84 | }; 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /src/arduino/video/Adafruit_USBD_Video.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2024 Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef ADAFRUIT_USBD_VIDEO_H 28 | #define ADAFRUIT_USBD_VIDEO_H 29 | 30 | #include "arduino/Adafruit_USBD_Device.h" 31 | 32 | class Adafruit_USBD_Video : public Adafruit_USBD_Interface { 33 | public: 34 | Adafruit_USBD_Video(void); 35 | 36 | bool begin(); 37 | 38 | //------------- Video Control -------------// 39 | // bool isStreaming(uint8_t stream_idx); 40 | bool 41 | addTerminal(tusb_desc_video_control_camera_terminal_t const *camera_terminal); 42 | bool 43 | addTerminal(tusb_desc_video_control_output_terminal_t const *output_terminal); 44 | 45 | //------------- Video Streaming -------------// 46 | // bool setIsochronousStreaming(bool enabled); 47 | 48 | // Add format descriptor, return format index 49 | bool addFormat(tusb_desc_video_format_uncompressed_t const *format); 50 | bool addFrame(tusb_desc_video_frame_uncompressed_continuous_t const *frame); 51 | void 52 | addColorMatching(tusb_desc_video_streaming_color_matching_t const *color); 53 | 54 | // from Adafruit_USBD_Interface 55 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 56 | uint8_t *buf, uint16_t bufsize); 57 | 58 | private: 59 | uint8_t _vc_id; 60 | 61 | tusb_desc_video_control_camera_terminal_t _camera_terminal; 62 | tusb_desc_video_control_output_terminal_t _output_terminal; 63 | 64 | // currently only support 1 format 65 | union { 66 | tusb_desc_video_format_uncompressed_t uncompressed; 67 | tusb_desc_video_format_mjpeg_t mjpeg; 68 | } _format; 69 | 70 | union { 71 | tusb_desc_video_frame_uncompressed_continuous_t uncompressed_cont; 72 | tusb_desc_video_frame_mjpeg_continuous_t mjpeg; 73 | } _frame; 74 | 75 | tusb_desc_video_streaming_color_matching_t _color_matching; 76 | }; 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_TinyUSB_API.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_TINYUSB_API_H_ 26 | #define ADAFRUIT_TINYUSB_API_H_ 27 | 28 | #include 29 | #include 30 | 31 | // API Version, need to be updated when there is changes for 32 | // TinyUSB_API, USBD_CDC, USBD_Device, USBD_Interface, 33 | #define TINYUSB_API_VERSION 30000 34 | 35 | //--------------------------------------------------------------------+ 36 | // Core API 37 | // Should be called by BSP Core to initialize, process task 38 | // Weak function allow compile arduino core before linking with this library 39 | //--------------------------------------------------------------------+ 40 | #ifdef __cplusplus 41 | extern "C" { 42 | #endif 43 | 44 | // Called by core/sketch to initialize usb device hardware and stack 45 | // This also initialize Serial as CDC device 46 | void TinyUSB_Device_Init(uint8_t rhport) __attribute__((weak)); 47 | 48 | // Called by core/sketch to handle device event 49 | void TinyUSB_Device_Task(void) __attribute__((weak)); 50 | 51 | // Called by core/sketch to flush write on CDC 52 | void TinyUSB_Device_FlushCDC(void) __attribute__((weak)); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | //--------------------------------------------------------------------+ 59 | // Port API 60 | // Must be implemented by each BSP core/platform 61 | //--------------------------------------------------------------------+ 62 | 63 | // To enter/reboot to bootloader 64 | // usually when host disconnects cdc at baud 1200 (touch 1200) 65 | void TinyUSB_Port_EnterDFU(void); 66 | 67 | // Init device hardware. 68 | // Called by TinyUSB_Device_Init() 69 | void TinyUSB_Port_InitDevice(uint8_t rhport); 70 | 71 | // Get unique serial number, needed for Serial String Descriptor 72 | // Fill serial_id (raw bytes) and return its length (limit to 16 bytes) 73 | // Note: Serial descriptor can be overwritten by user API 74 | uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]); 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /examples/GPIN/GPIN.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD 3 | * 4 | * SPDX-License-Identifier: MIT 5 | * 6 | * @Dependent Library: 7 | * M5GFX: https://github.com/m5stack/M5GFX 8 | * M5Unified: https://github.com/m5stack/M5Unified 9 | * M5-Max3421E-USBShield: https://github.com/m5stack/M5-Max3421E-USBShield 10 | */ 11 | #include "M5Unified.h" 12 | #include "M5_Max3421E_Usb.h" 13 | 14 | #define rIOPINS1 (0xa0) // 20<<3 15 | /* IOPINS1 Bits */ 16 | #define bmGPOUT0 (0x01) 17 | #define bmGPOUT1 (0x02) 18 | #define bmGPOUT2 (0x04) 19 | #define bmGPOUT3 (0x08) 20 | #define bmGPIN0 (0x10) 21 | #define bmGPIN1 (0x20) 22 | #define bmGPIN2 (0x40) 23 | #define bmGPIN3 (0x80) 24 | 25 | #define rIOPINS2 (0xa8) // 21<<3 26 | /* IOPINS2 Bits */ 27 | #define bmGPOUT4 (0x01) 28 | #define bmGPOUT5 (0x02) 29 | #define bmGPOUT6 (0x04) 30 | #define bmGPOUT7 (0x08) 31 | #define bmGPIN4 (0x10) 32 | #define bmGPIN5 (0x20) 33 | #define bmGPIN6 (0x40) 34 | #define bmGPIN7 (0x80) 35 | 36 | /* 37 | The board is equipped with two dip switches to adapt to different M5 series hosts. 38 | https://static-cdn.m5stack.com/resource/docs/products/module/USB%20v1.2%20Module/pinMap-70b8e2ad-8325-4887-af33-44e3dae91520.png 39 | If you need to change the spi pin, use these spi configuration settings 40 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 41 | */ 42 | // Initialize USB Host 43 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 44 | 45 | /** 46 | * @brief Reads the level of an input pin. 47 | * 48 | * This function retrieves the current logic level of a specified input pin. 49 | * 50 | * @param pin The pin number to read (range: 0~4). 51 | * 52 | * @return The logic level of the pin (1 for high level, 0 for low level). 53 | */ 54 | uint8_t read_input_pin(uint8_t pin); 55 | 56 | /** 57 | * @brief Displays the current level of a specified pin. 58 | * 59 | * This function outputs the current logic state (high or low) of the specified pin. 60 | * 61 | * @param pin The pin number to display (range: 0~4). 62 | */ 63 | void print_pin_level(uint8_t pin); 64 | 65 | void setup() 66 | { 67 | M5.begin(); 68 | M5.Power.begin(); 69 | Serial.begin(115200); 70 | 71 | Serial.println("Initializing MAX3421E with USB Host..."); 72 | 73 | // Initialize USB Host 74 | if (USBHost.begin(0)) { 75 | Serial.println("USB Host initialized successfully."); 76 | } else { 77 | Serial.println("Failed to initialize USB Host."); 78 | while (1) { 79 | }; 80 | } 81 | } 82 | 83 | void loop() 84 | { 85 | for (uint8_t i = 0; i <= 4; i++) { 86 | print_pin_level(i); 87 | delay(100); 88 | } 89 | delay(3000); 90 | } 91 | 92 | uint8_t read_input_pin(uint8_t pin) 93 | { 94 | uint8_t gpout = USBHost.max3421_readRegister(rIOPINS2, false); 95 | gpout &= 0xF0; 96 | gpout |= (USBHost.max3421_readRegister(rIOPINS1, false) >> 4); 97 | return ((gpout >> pin) & 0x01); 98 | } 99 | 100 | void print_pin_level(uint8_t pin) 101 | { 102 | if (read_input_pin(pin)) { 103 | Serial.printf("GPIN%d is high\r\n", pin); 104 | } else { 105 | Serial.printf("GPIN%d is low\r\n", pin); 106 | } 107 | } 108 | 109 | void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len) 110 | { 111 | } -------------------------------------------------------------------------------- /src/arduino/webusb/Adafruit_USBD_WebUSB.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 hathach 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 | #ifndef ADAFRUIT_USBD_WEBUSB_H_ 26 | #define ADAFRUIT_USBD_WEBUSB_H_ 27 | 28 | #include "Stream.h" 29 | #include "arduino/Adafruit_USBD_Device.h" 30 | 31 | #define WEBUSB_URL_DEF(_name, _scheme, _url) \ 32 | struct TU_ATTR_PACKED { \ 33 | uint8_t bLength; \ 34 | uint8_t bDescriptorType; \ 35 | uint8_t bScheme; \ 36 | char url[3 + sizeof(_url)]; \ 37 | } const _name = {3 + sizeof(_url) - 1, 3, _scheme, _url} 38 | 39 | class Adafruit_USBD_WebUSB : public Stream, public Adafruit_USBD_Interface { 40 | public: 41 | typedef void (*linestate_callback_t)(bool connected); 42 | Adafruit_USBD_WebUSB(const void *url = NULL); 43 | 44 | bool begin(void); 45 | 46 | bool setLandingPage(const void *url); 47 | void setLineStateCallback(linestate_callback_t fp); 48 | 49 | // Stream API 50 | virtual int available(void); 51 | virtual int peek(void); 52 | 53 | virtual int read(void); 54 | size_t read(uint8_t *buffer, size_t size); 55 | 56 | virtual void flush(void); 57 | virtual size_t write(uint8_t b); 58 | 59 | virtual size_t write(const uint8_t *buffer, size_t size); 60 | size_t write(const char *buffer, size_t size) { 61 | return write((const uint8_t *)buffer, size); 62 | } 63 | 64 | bool connected(void); 65 | operator bool(); 66 | 67 | // from Adafruit_USBD_Interface 68 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 69 | uint8_t *buf, uint16_t bufsize); 70 | 71 | private: 72 | bool _connected; 73 | const uint8_t *_url; 74 | linestate_callback_t _linestate_cb; 75 | 76 | // Make all tinyusb callback friend to access private data 77 | friend bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, 78 | tusb_control_request_t const *request); 79 | }; 80 | 81 | #endif /* ADAFRUIT_USBD_WEBUSB_H_ */ 82 | -------------------------------------------------------------------------------- /examples/CDC/usbh_helper.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | #ifndef USBH_HELPER_H 13 | #define USBH_HELPER_H 14 | 15 | #ifdef ARDUINO_ARCH_RP2040 16 | // pio-usb is required for rp2040 host 17 | #include "pio_usb.h" 18 | 19 | // Pin D+ for host, D- = D+ + 1 20 | #ifndef PIN_USB_HOST_DP 21 | #define PIN_USB_HOST_DP 16 22 | #endif 23 | 24 | // Pin for enabling Host VBUS. comment out if not used 25 | #ifndef PIN_5V_EN 26 | #define PIN_5V_EN 18 27 | #endif 28 | 29 | #ifndef PIN_5V_EN_STATE 30 | #define PIN_5V_EN_STATE 1 31 | #endif 32 | #endif // ARDUINO_ARCH_RP2040 33 | 34 | #include "M5_Max3421E_Usb.h" 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | // USB Host using MAX3421E: SPI, CS, INT 38 | #include "SPI.h" 39 | 40 | #if defined(ARDUINO_METRO_ESP32S2) 41 | M5_USBH_Host USBHost(&SPI, 15, 14); 42 | #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) 43 | M5_USBH_Host USBHost(&SPI, 33, 15); 44 | #else 45 | // Default CS and INT are pin 5, 35 46 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 47 | #endif 48 | #else 49 | // Native USB Host such as rp2040 50 | M5_USBH_Host USBHost; 51 | #endif 52 | 53 | //--------------------------------------------------------------------+ 54 | // Helper Functions 55 | //--------------------------------------------------------------------+ 56 | 57 | #ifdef ARDUINO_ARCH_RP2040 58 | static void rp2040_configure_pio_usb(void) { 59 | //while ( !Serial ) delay(10); // wait for native usb 60 | Serial.println("Core1 setup to run TinyUSB host with pio-usb"); 61 | 62 | // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB 63 | uint32_t cpu_hz = clock_get_hz(clk_sys); 64 | if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { 65 | while (!Serial) { 66 | delay(10); // wait for native usb 67 | } 68 | Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); 69 | Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); 70 | while (1) { 71 | delay(1); 72 | } 73 | } 74 | 75 | #ifdef PIN_5V_EN 76 | pinMode(PIN_5V_EN, OUTPUT); 77 | digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); 78 | #endif 79 | 80 | pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; 81 | pio_cfg.pin_dp = PIN_USB_HOST_DP; 82 | 83 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 84 | // For pico-w, PIO is also used to communicate with cyw43 85 | // Therefore we need to alternate the pio-usb configuration 86 | // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 87 | pio_cfg.sm_tx = 3; 88 | pio_cfg.sm_rx = 2; 89 | pio_cfg.sm_eop = 3; 90 | pio_cfg.pio_rx_num = 0; 91 | pio_cfg.pio_tx_num = 1; 92 | pio_cfg.tx_ch = 9; 93 | #endif 94 | 95 | USBHost.configure_pio_usb(1, &pio_cfg); 96 | } 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /examples/HID/usbh_helper.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | #ifndef USBH_HELPER_H 13 | #define USBH_HELPER_H 14 | 15 | #ifdef ARDUINO_ARCH_RP2040 16 | // pio-usb is required for rp2040 host 17 | #include "pio_usb.h" 18 | 19 | // Pin D+ for host, D- = D+ + 1 20 | #ifndef PIN_USB_HOST_DP 21 | #define PIN_USB_HOST_DP 16 22 | #endif 23 | 24 | // Pin for enabling Host VBUS. comment out if not used 25 | #ifndef PIN_5V_EN 26 | #define PIN_5V_EN 18 27 | #endif 28 | 29 | #ifndef PIN_5V_EN_STATE 30 | #define PIN_5V_EN_STATE 1 31 | #endif 32 | #endif // ARDUINO_ARCH_RP2040 33 | 34 | #include "M5_Max3421E_Usb.h" 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | // USB Host using MAX3421E: SPI, CS, INT 38 | #include "SPI.h" 39 | 40 | #if defined(ARDUINO_METRO_ESP32S2) 41 | M5_USBH_Host USBHost(&SPI, 15, 14); 42 | #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) 43 | M5_USBH_Host USBHost(&SPI, 33, 15); 44 | #else 45 | // Default CS and INT are pin 5, 35 46 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 47 | #endif 48 | #else 49 | // Native USB Host such as rp2040 50 | M5_USBH_Host USBHost; 51 | #endif 52 | 53 | //--------------------------------------------------------------------+ 54 | // Helper Functions 55 | //--------------------------------------------------------------------+ 56 | 57 | #ifdef ARDUINO_ARCH_RP2040 58 | static void rp2040_configure_pio_usb(void) { 59 | //while ( !Serial ) delay(10); // wait for native usb 60 | Serial.println("Core1 setup to run TinyUSB host with pio-usb"); 61 | 62 | // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB 63 | uint32_t cpu_hz = clock_get_hz(clk_sys); 64 | if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { 65 | while (!Serial) { 66 | delay(10); // wait for native usb 67 | } 68 | Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); 69 | Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); 70 | while (1) { 71 | delay(1); 72 | } 73 | } 74 | 75 | #ifdef PIN_5V_EN 76 | pinMode(PIN_5V_EN, OUTPUT); 77 | digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); 78 | #endif 79 | 80 | pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; 81 | pio_cfg.pin_dp = PIN_USB_HOST_DP; 82 | 83 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 84 | // For pico-w, PIO is also used to communicate with cyw43 85 | // Therefore we need to alternate the pio-usb configuration 86 | // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 87 | pio_cfg.sm_tx = 3; 88 | pio_cfg.sm_rx = 2; 89 | pio_cfg.sm_eop = 3; 90 | pio_cfg.pio_rx_num = 0; 91 | pio_cfg.pio_tx_num = 1; 92 | pio_cfg.tx_ch = 9; 93 | #endif 94 | 95 | USBHost.configure_pio_usb(1, &pio_cfg); 96 | } 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /examples/Simple/device_info/usbh_helper.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | #ifndef USBH_HELPER_H 13 | #define USBH_HELPER_H 14 | 15 | #ifdef ARDUINO_ARCH_RP2040 16 | // pio-usb is required for rp2040 host 17 | #include "pio_usb.h" 18 | 19 | // Pin D+ for host, D- = D+ + 1 20 | #ifndef PIN_USB_HOST_DP 21 | #define PIN_USB_HOST_DP 16 22 | #endif 23 | 24 | // Pin for enabling Host VBUS. comment out if not used 25 | #ifndef PIN_5V_EN 26 | #define PIN_5V_EN 18 27 | #endif 28 | 29 | #ifndef PIN_5V_EN_STATE 30 | #define PIN_5V_EN_STATE 1 31 | #endif 32 | #endif // ARDUINO_ARCH_RP2040 33 | 34 | #include "M5_Max3421E_Usb.h" 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | // USB Host using MAX3421E: SPI, CS, INT 38 | #include "SPI.h" 39 | 40 | #if defined(ARDUINO_METRO_ESP32S2) 41 | M5_USBH_Host USBHost(&SPI, 15, 14); 42 | #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) 43 | M5_USBH_Host USBHost(&SPI, 33, 15); 44 | #else 45 | // Default CS and INT are pin 5, 35 46 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 47 | #endif 48 | #else 49 | // Native USB Host such as rp2040 50 | M5_USBH_Host USBHost; 51 | #endif 52 | 53 | //--------------------------------------------------------------------+ 54 | // Helper Functions 55 | //--------------------------------------------------------------------+ 56 | 57 | #ifdef ARDUINO_ARCH_RP2040 58 | static void rp2040_configure_pio_usb(void) { 59 | //while ( !Serial ) delay(10); // wait for native usb 60 | Serial.println("Core1 setup to run TinyUSB host with pio-usb"); 61 | 62 | // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB 63 | uint32_t cpu_hz = clock_get_hz(clk_sys); 64 | if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { 65 | while (!Serial) { 66 | delay(10); // wait for native usb 67 | } 68 | Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); 69 | Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); 70 | while (1) { 71 | delay(1); 72 | } 73 | } 74 | 75 | #ifdef PIN_5V_EN 76 | pinMode(PIN_5V_EN, OUTPUT); 77 | digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); 78 | #endif 79 | 80 | pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; 81 | pio_cfg.pin_dp = PIN_USB_HOST_DP; 82 | 83 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 84 | // For pico-w, PIO is also used to communicate with cyw43 85 | // Therefore we need to alternate the pio-usb configuration 86 | // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 87 | pio_cfg.sm_tx = 3; 88 | pio_cfg.sm_rx = 2; 89 | pio_cfg.sm_eop = 3; 90 | pio_cfg.pio_rx_num = 0; 91 | pio_cfg.pio_tx_num = 1; 92 | pio_cfg.tx_ch = 9; 93 | #endif 94 | 95 | USBHost.configure_pio_usb(1, &pio_cfg); 96 | } 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /examples/MassStorage/msc_data_logger/usbh_helper.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | #ifndef USBH_HELPER_H 13 | #define USBH_HELPER_H 14 | 15 | #ifdef ARDUINO_ARCH_RP2040 16 | // pio-usb is required for rp2040 host 17 | #include "pio_usb.h" 18 | 19 | // Pin D+ for host, D- = D+ + 1 20 | #ifndef PIN_USB_HOST_DP 21 | #define PIN_USB_HOST_DP 16 22 | #endif 23 | 24 | // Pin for enabling Host VBUS. comment out if not used 25 | #ifndef PIN_5V_EN 26 | #define PIN_5V_EN 18 27 | #endif 28 | 29 | #ifndef PIN_5V_EN_STATE 30 | #define PIN_5V_EN_STATE 1 31 | #endif 32 | #endif // ARDUINO_ARCH_RP2040 33 | 34 | #include "M5_Max3421E_Usb.h" 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | // USB Host using MAX3421E: SPI, CS, INT 38 | #include "SPI.h" 39 | 40 | #if defined(ARDUINO_METRO_ESP32S2) 41 | M5_USBH_Host USBHost(&SPI, 15, 14); 42 | #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) 43 | M5_USBH_Host USBHost(&SPI, 33, 15); 44 | #else 45 | // Default CS and INT are pin 5, 35 46 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 47 | #endif 48 | #else 49 | // Native USB Host such as rp2040 50 | M5_USBH_Host USBHost; 51 | #endif 52 | 53 | //--------------------------------------------------------------------+ 54 | // Helper Functions 55 | //--------------------------------------------------------------------+ 56 | 57 | #ifdef ARDUINO_ARCH_RP2040 58 | static void rp2040_configure_pio_usb(void) { 59 | //while ( !Serial ) delay(10); // wait for native usb 60 | Serial.println("Core1 setup to run TinyUSB host with pio-usb"); 61 | 62 | // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB 63 | uint32_t cpu_hz = clock_get_hz(clk_sys); 64 | if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { 65 | while (!Serial) { 66 | delay(10); // wait for native usb 67 | } 68 | Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); 69 | Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); 70 | while (1) { 71 | delay(1); 72 | } 73 | } 74 | 75 | #ifdef PIN_5V_EN 76 | pinMode(PIN_5V_EN, OUTPUT); 77 | digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); 78 | #endif 79 | 80 | pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; 81 | pio_cfg.pin_dp = PIN_USB_HOST_DP; 82 | 83 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 84 | // For pico-w, PIO is also used to communicate with cyw43 85 | // Therefore we need to alternate the pio-usb configuration 86 | // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 87 | pio_cfg.sm_tx = 3; 88 | pio_cfg.sm_rx = 2; 89 | pio_cfg.sm_eop = 3; 90 | pio_cfg.pio_rx_num = 0; 91 | pio_cfg.pio_tx_num = 1; 92 | pio_cfg.tx_ch = 9; 93 | #endif 94 | 95 | USBHost.configure_pio_usb(1, &pio_cfg); 96 | } 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /examples/MassStorage/msc_file_explorer/usbh_helper.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | #ifndef USBH_HELPER_H 13 | #define USBH_HELPER_H 14 | 15 | #ifdef ARDUINO_ARCH_RP2040 16 | // pio-usb is required for rp2040 host 17 | #include "pio_usb.h" 18 | 19 | // Pin D+ for host, D- = D+ + 1 20 | #ifndef PIN_USB_HOST_DP 21 | #define PIN_USB_HOST_DP 16 22 | #endif 23 | 24 | // Pin for enabling Host VBUS. comment out if not used 25 | #ifndef PIN_5V_EN 26 | #define PIN_5V_EN 18 27 | #endif 28 | 29 | #ifndef PIN_5V_EN_STATE 30 | #define PIN_5V_EN_STATE 1 31 | #endif 32 | #endif // ARDUINO_ARCH_RP2040 33 | 34 | #include "M5_Max3421E_Usb.h" 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | // USB Host using MAX3421E: SPI, CS, INT 38 | #include "SPI.h" 39 | 40 | #if defined(ARDUINO_METRO_ESP32S2) 41 | M5_USBH_Host USBHost(&SPI, 15, 14); 42 | #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) 43 | M5_USBH_Host USBHost(&SPI, 33, 15); 44 | #else 45 | // Default CS and INT are pin 5, 35 46 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 47 | #endif 48 | #else 49 | // Native USB Host such as rp2040 50 | M5_USBH_Host USBHost; 51 | #endif 52 | 53 | //--------------------------------------------------------------------+ 54 | // Helper Functions 55 | //--------------------------------------------------------------------+ 56 | 57 | #ifdef ARDUINO_ARCH_RP2040 58 | static void rp2040_configure_pio_usb(void) { 59 | //while ( !Serial ) delay(10); // wait for native usb 60 | Serial.println("Core1 setup to run TinyUSB host with pio-usb"); 61 | 62 | // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB 63 | uint32_t cpu_hz = clock_get_hz(clk_sys); 64 | if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { 65 | while (!Serial) { 66 | delay(10); // wait for native usb 67 | } 68 | Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); 69 | Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); 70 | while (1) { 71 | delay(1); 72 | } 73 | } 74 | 75 | #ifdef PIN_5V_EN 76 | pinMode(PIN_5V_EN, OUTPUT); 77 | digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); 78 | #endif 79 | 80 | pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; 81 | pio_cfg.pin_dp = PIN_USB_HOST_DP; 82 | 83 | #if defined(ARDUINO_RASPBERRY_PI_PICO_W) 84 | // For pico-w, PIO is also used to communicate with cyw43 85 | // Therefore we need to alternate the pio-usb configuration 86 | // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 87 | pio_cfg.sm_tx = 3; 88 | pio_cfg.sm_rx = 2; 89 | pio_cfg.sm_eop = 3; 90 | pio_cfg.pio_rx_num = 0; 91 | pio_cfg.pio_tx_num = 1; 92 | pio_cfg.tx_ch = 9; 93 | #endif 94 | 95 | USBHost.configure_pio_usb(1, &pio_cfg); 96 | } 97 | #endif 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_TinyUSB_API.cpp: -------------------------------------------------------------------------------- 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 "M5_Max3421E_Usb.h" 30 | #include "Arduino.h" 31 | 32 | extern "C" { 33 | 34 | //--------------------------------------------------------------------+ 35 | // Device 36 | //--------------------------------------------------------------------+ 37 | #if CFG_TUD_ENABLED 38 | void TinyUSB_Device_Init(uint8_t rhport) { 39 | // Init USB Device controller and stack 40 | TinyUSBDevice.begin(rhport); 41 | } 42 | 43 | // RP2040 has its own implementation since it needs mutex for dual core 44 | #ifndef ARDUINO_ARCH_RP2040 45 | void TinyUSB_Device_Task(void) { 46 | // Run tinyusb device task 47 | tud_task(); 48 | } 49 | #endif 50 | 51 | #ifndef ARDUINO_ARCH_ESP32 52 | void TinyUSB_Device_FlushCDC(void) { 53 | uint8_t const cdc_instance = Adafruit_USBD_CDC::getInstanceCount(); 54 | for (uint8_t instance = 0; instance < cdc_instance; instance++) { 55 | tud_cdc_n_write_flush(instance); 56 | } 57 | } 58 | #endif 59 | #endif // CFG_TUD_ENABLED 60 | 61 | //------------- Debug log with Serial1 -------------// 62 | #if CFG_TUSB_DEBUG && defined(CFG_TUSB_DEBUG_PRINTF) && \ 63 | !defined(ARDUINO_ARCH_ESP32) 64 | 65 | // #define USE_SEGGER_RTT 66 | #define SERIAL_TUSB_DEBUG Serial1 67 | 68 | #ifdef USE_SEGGER_RTT 69 | #include "SEGGER_RTT/RTT/SEGGER_RTT.h" 70 | #endif 71 | 72 | __attribute__((used)) int CFG_TUSB_DEBUG_PRINTF(const char *__restrict format, 73 | ...) { 74 | char buf[256]; 75 | int len; 76 | va_list ap; 77 | va_start(ap, format); 78 | len = vsnprintf(buf, sizeof(buf), format, ap); 79 | 80 | #ifdef USE_SEGGER_RTT 81 | SEGGER_RTT_Write(0, buf, len); 82 | #else 83 | static volatile bool ser_inited = false; 84 | if (!ser_inited) { 85 | ser_inited = true; 86 | SERIAL_TUSB_DEBUG.begin(115200); 87 | // SERIAL_TUSB_DEBUG.begin(921600); 88 | } 89 | SERIAL_TUSB_DEBUG.write(buf); 90 | #endif 91 | 92 | va_end(ap); 93 | return len; 94 | } 95 | #endif // CFG_TUSB_DEBUG 96 | 97 | } // extern C 98 | 99 | #endif // CFG_TUD_ENABLED || CFG_TUH_ENABLED 100 | -------------------------------------------------------------------------------- /src/portable/synopsys/dwc2/dwc2_gd32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | 28 | #ifndef DWC2_GD32_H_ 29 | #define DWC2_GD32_H_ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #define DWC2_REG_BASE 0x50000000UL 36 | #define DWC2_EP_MAX 4 37 | 38 | static const dwc2_controller_t _dwc2_controller[] = 39 | { 40 | { .reg_base = DWC2_REG_BASE, .irqnum = 86, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 1280 } 41 | }; 42 | 43 | extern uint32_t SystemCoreClock; 44 | 45 | // The GD32VF103 is a RISC-V MCU, which implements the ECLIC Core-Local 46 | // Interrupt Controller by Nuclei. It is nearly API compatible to the 47 | // NVIC used by ARM MCUs. 48 | #define ECLIC_INTERRUPT_ENABLE_BASE 0xD2001001UL 49 | 50 | TU_ATTR_ALWAYS_INLINE 51 | static inline void __eclic_enable_interrupt (uint32_t irq) { 52 | *(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 1; 53 | } 54 | 55 | TU_ATTR_ALWAYS_INLINE 56 | static inline void __eclic_disable_interrupt (uint32_t irq){ 57 | *(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 0; 58 | } 59 | 60 | TU_ATTR_ALWAYS_INLINE 61 | static inline void dwc2_dcd_int_enable(uint8_t rhport) 62 | { 63 | __eclic_enable_interrupt(_dwc2_controller[rhport].irqnum); 64 | } 65 | 66 | TU_ATTR_ALWAYS_INLINE 67 | static inline void dwc2_dcd_int_disable (uint8_t rhport) 68 | { 69 | __eclic_disable_interrupt(_dwc2_controller[rhport].irqnum); 70 | } 71 | 72 | static inline void dwc2_remote_wakeup_delay(void) 73 | { 74 | // try to delay for 1 ms 75 | uint32_t count = SystemCoreClock / 1000; 76 | while ( count-- ) __asm volatile ("nop"); 77 | } 78 | 79 | // MCU specific PHY init, called BEFORE core reset 80 | static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 81 | { 82 | (void) dwc2; 83 | (void) hs_phy_type; 84 | 85 | // nothing to do 86 | } 87 | 88 | // MCU specific PHY update, it is called AFTER init() and core reset 89 | static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type) 90 | { 91 | (void) dwc2; 92 | (void) hs_phy_type; 93 | 94 | // nothing to do 95 | } 96 | 97 | #ifdef __cplusplus 98 | } 99 | #endif 100 | 101 | #endif /* DWC2_GD32_H_ */ 102 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_USBD_CDC.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_USBD_CDC_H_ 26 | #define ADAFRUIT_USBD_CDC_H_ 27 | 28 | #include "Adafruit_TinyUSB_API.h" 29 | 30 | #if defined(__cplusplus) 31 | 32 | #if defined(ARDUINO_ARCH_ESP32) 33 | 34 | // For ESP32 use USBCDC as it is compatible 35 | #define Adafruit_USBD_CDC USBCDC 36 | 37 | #else 38 | 39 | #include "Adafruit_USBD_Interface.h" 40 | #include "Stream.h" 41 | 42 | class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { 43 | public: 44 | Adafruit_USBD_CDC(void); 45 | 46 | static uint8_t getInstanceCount(void) { return _instance_count; } 47 | 48 | void setPins(uint8_t pin_rx, uint8_t pin_tx) { 49 | (void)pin_rx; 50 | (void)pin_tx; 51 | } 52 | void begin(uint32_t baud); 53 | void begin(uint32_t baud, uint8_t config); 54 | void end(void); 55 | 56 | // return line coding set by host 57 | uint32_t baud(void); 58 | uint8_t stopbits(void); 59 | uint8_t paritytype(void); 60 | uint8_t numbits(void); 61 | int dtr(void); 62 | 63 | // Stream API 64 | virtual int available(void); 65 | virtual int peek(void); 66 | 67 | virtual int read(void); 68 | size_t read(uint8_t *buffer, size_t size); 69 | 70 | virtual void flush(void); 71 | virtual size_t write(uint8_t); 72 | 73 | virtual size_t write(const uint8_t *buffer, size_t size); 74 | size_t write(const char *buffer, size_t size) { 75 | return write((const uint8_t *)buffer, size); 76 | } 77 | 78 | virtual int availableForWrite(void); 79 | using Print::write; // pull in write(str) from Print 80 | operator bool(); 81 | 82 | // from Adafruit_USBD_Interface 83 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 84 | uint8_t *buf, uint16_t bufsize); 85 | 86 | private: 87 | enum { INVALID_INSTANCE = 0xffu }; 88 | static uint8_t _instance_count; 89 | 90 | uint8_t _instance; 91 | 92 | bool isValid(void) { return _instance != INVALID_INSTANCE; } 93 | }; 94 | 95 | // "Serial" is used with TinyUSB CDC 96 | #if defined(USE_TINYUSB) 97 | extern Adafruit_USBD_CDC Serial; 98 | #define SerialTinyUSB Serial 99 | #endif 100 | 101 | // Serial is probably used with HW Uart 102 | #ifndef SerialTinyUSB 103 | extern Adafruit_USBD_CDC SerialTinyUSB; 104 | #endif 105 | 106 | #endif // else of ESP32 107 | #endif // __cplusplus 108 | 109 | #endif 110 | -------------------------------------------------------------------------------- /src/class/cdc/serial/ch34x.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2023 Heiko Kuester 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _CH34X_H_ 28 | #define _CH34X_H_ 29 | 30 | // There is no official documentation for the CH34x (CH340, CH341) chips. Reference can be found 31 | // - https://github.com/WCHSoftGroup/ch341ser_linux 32 | // - https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c 33 | // - https://github.com/freebsd/freebsd-src/blob/main/sys/dev/usb/serial/uchcom.c 34 | 35 | // set line_coding @ enumeration 36 | #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM 37 | #define CFG_TUH_CDC_LINE_CODING_ON_ENUM_CH34X CFG_TUH_CDC_LINE_CODING_ON_ENUM 38 | #else // this default is necessary to work properly 39 | #define CFG_TUH_CDC_LINE_CODING_ON_ENUM_CH34X { 9600, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } 40 | #endif 41 | 42 | // USB requests 43 | #define CH34X_REQ_READ_VERSION 0x5F // dec 95 44 | #define CH34X_REQ_WRITE_REG 0x9A // dec 154 45 | #define CH34X_REQ_READ_REG 0x95 // dec 149 46 | #define CH34X_REQ_SERIAL_INIT 0xA1 // dec 161 47 | #define CH34X_REQ_MODEM_CTRL 0xA4 // dev 164 48 | 49 | // registers 50 | #define CH34X_REG_BREAK 0x05 51 | #define CH34X_REG_PRESCALER 0x12 52 | #define CH34X_REG_DIVISOR 0x13 53 | #define CH34X_REG_LCR 0x18 54 | #define CH34X_REG_LCR2 0x25 55 | #define CH34X_REG_MCR_MSR 0x06 56 | #define CH34X_REG_MCR_MSR2 0x07 57 | #define CH34X_NBREAK_BITS 0x01 58 | 59 | #define CH341_REG_0x0F 0x0F // undocumented register 60 | #define CH341_REG_0x2C 0x2C // undocumented register 61 | #define CH341_REG_0x27 0x27 // hardware flow control (cts/rts) 62 | 63 | #define CH34X_REG16_DIVISOR_PRESCALER TU_U16(CH34X_REG_DIVISOR, CH34X_REG_PRESCALER) 64 | #define CH32X_REG16_LCR2_LCR TU_U16(CH34X_REG_LCR2, CH34X_REG_LCR) 65 | 66 | // modem control bits 67 | #define CH34X_BIT_RTS ( 1 << 6 ) 68 | #define CH34X_BIT_DTR ( 1 << 5 ) 69 | 70 | // line control bits 71 | #define CH34X_LCR_ENABLE_RX 0x80 72 | #define CH34X_LCR_ENABLE_TX 0x40 73 | #define CH34X_LCR_MARK_SPACE 0x20 74 | #define CH34X_LCR_PAR_EVEN 0x10 75 | #define CH34X_LCR_ENABLE_PAR 0x08 76 | #define CH34X_LCR_PAR_MASK 0x38 // all parity bits 77 | #define CH34X_LCR_STOP_BITS_2 0x04 78 | #define CH34X_LCR_CS8 0x03 79 | #define CH34X_LCR_CS7 0x02 80 | #define CH34X_LCR_CS6 0x01 81 | #define CH34X_LCR_CS5 0x00 82 | #define CH34X_LCR_CS_MASK 0x03 // all CSx bits 83 | 84 | #endif /* _CH34X_H_ */ 85 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_USBH_Host.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_USBH_HOST_H_ 26 | #define ADAFRUIT_USBH_HOST_H_ 27 | 28 | #include "Adafruit_USBD_Interface.h" 29 | #include "tusb.h" 30 | #include 31 | 32 | #ifdef ARDUINO_ARCH_ESP32 33 | #include "esp32-hal-tinyusb.h" 34 | #endif 35 | 36 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 37 | extern "C" { 38 | void tuh_max3421_spi_cs_api(uint8_t rhport, bool active); 39 | bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf, 40 | uint8_t *rx_buf, size_t xfer_bytes); 41 | void tuh_max3421_int_api(uint8_t rhport, bool enabled); 42 | } 43 | 44 | class M5_USBH_Host { 45 | private: 46 | SPIClass *_spi; 47 | 48 | // for esp32 or using softwareSPI 49 | int8_t _sck, _mosi, _miso; 50 | 51 | public: 52 | int8_t _cs; 53 | int8_t _intr; 54 | 55 | // constructor for using MAX3421E (host shield) 56 | M5_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr); 57 | M5_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi, int8_t miso, 58 | int8_t cs, int8_t intr); 59 | 60 | uint8_t max3421_readRegister(uint8_t reg, bool in_isr); 61 | bool max3421_writeRegister(uint8_t reg, uint8_t data, bool in_isr); 62 | bool max3421_writeIOPINS1(uint8_t data, bool in_isr) { 63 | enum { IOPINS1_ADDR = 20u << 3 }; // 0xA0 64 | return max3421_writeRegister(IOPINS1_ADDR, data, in_isr); 65 | } 66 | bool max3421_writeIOPINS2(uint8_t data, bool in_isr) { 67 | enum { IOPINS2_ADDR = 21u << 3 }; // 0xA8 68 | return max3421_writeRegister(IOPINS2_ADDR, data, in_isr); 69 | } 70 | 71 | private: 72 | friend void tuh_max3421_spi_cs_api(uint8_t rhport, bool active); 73 | friend bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf, 74 | uint8_t *rx_buf, size_t xfer_bytes); 75 | friend void tuh_max3421_int_api(uint8_t rhport, bool enabled); 76 | #else 77 | 78 | class M5_USBH_Host { 79 | #endif 80 | 81 | public: 82 | // default constructor 83 | M5_USBH_Host(void); 84 | 85 | bool configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param); 86 | 87 | #ifdef ARDUINO_ARCH_RP2040 88 | bool configure_pio_usb(uint8_t rhport, const void *cfg_param); 89 | #endif 90 | 91 | bool begin(uint8_t rhport); 92 | void task(uint32_t timeout_ms = UINT32_MAX, bool in_isr = false); 93 | 94 | //------------- internal usage -------------// 95 | static M5_USBH_Host *_instance; 96 | 97 | private: 98 | uint8_t _rhport; 99 | }; 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /src/osal/osal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_OSAL_H_ 28 | #define _TUSB_OSAL_H_ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include "common/tusb_common.h" 35 | 36 | typedef void (*osal_task_func_t)( void * ); 37 | 38 | // Timeout 39 | #define OSAL_TIMEOUT_NOTIMEOUT (0) // Return immediately 40 | #define OSAL_TIMEOUT_NORMAL (10) // Default timeout 41 | #define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX) // Wait forever 42 | #define OSAL_TIMEOUT_CONTROL_XFER OSAL_TIMEOUT_WAIT_FOREVER 43 | 44 | // Mutex is required when using a preempted RTOS or MCU has multiple cores 45 | #if (CFG_TUSB_OS == OPT_OS_NONE) && !TUP_MCU_MULTIPLE_CORE 46 | #define OSAL_MUTEX_REQUIRED 0 47 | #define OSAL_MUTEX_DEF(_name) uint8_t :0 48 | #else 49 | #define OSAL_MUTEX_REQUIRED 1 50 | #define OSAL_MUTEX_DEF(_name) osal_mutex_def_t _name 51 | #endif 52 | 53 | // OS thin implementation 54 | #if CFG_TUSB_OS == OPT_OS_NONE 55 | #include "osal_none.h" 56 | #elif CFG_TUSB_OS == OPT_OS_FREERTOS 57 | #include "osal_freertos.h" 58 | #elif CFG_TUSB_OS == OPT_OS_MYNEWT 59 | #include "osal_mynewt.h" 60 | #elif CFG_TUSB_OS == OPT_OS_PICO 61 | #include "osal_pico.h" 62 | #elif CFG_TUSB_OS == OPT_OS_RTTHREAD 63 | #include "osal_rtthread.h" 64 | #elif CFG_TUSB_OS == OPT_OS_RTX4 65 | #include "osal_rtx4.h" 66 | #elif CFG_TUSB_OS == OPT_OS_CUSTOM 67 | #include "tusb_os_custom.h" // implemented by application 68 | #else 69 | #error OS is not supported yet 70 | #endif 71 | 72 | //--------------------------------------------------------------------+ 73 | // OSAL Porting API 74 | // Should be implemented as static inline function in osal_port.h header 75 | /* 76 | osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); 77 | bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr); 78 | bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec); 79 | void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed 80 | 81 | osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef); 82 | bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec); 83 | bool osal_mutex_unlock(osal_mutex_t mutex_hdl); 84 | 85 | osal_queue_t osal_queue_create(osal_queue_def_t* qdef); 86 | bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec); 87 | bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); 88 | bool osal_queue_empty(osal_queue_t qhdl); 89 | */ 90 | //--------------------------------------------------------------------+ 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /* _TUSB_OSAL_H_ */ 97 | -------------------------------------------------------------------------------- /src/class/dfu/dfu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 XMOS LIMITED 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_DFU_H_ 28 | #define _TUSB_DFU_H_ 29 | 30 | #include "common/tusb_common.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | //--------------------------------------------------------------------+ 37 | // Common Definitions 38 | //--------------------------------------------------------------------+ 39 | 40 | // DFU Protocol 41 | typedef enum 42 | { 43 | DFU_PROTOCOL_RT = 0x01, 44 | DFU_PROTOCOL_DFU = 0x02, 45 | } dfu_protocol_type_t; 46 | 47 | // DFU Descriptor Type 48 | typedef enum 49 | { 50 | DFU_DESC_FUNCTIONAL = 0x21, 51 | } dfu_descriptor_type_t; 52 | 53 | // DFU Requests 54 | typedef enum { 55 | DFU_REQUEST_DETACH = 0, 56 | DFU_REQUEST_DNLOAD = 1, 57 | DFU_REQUEST_UPLOAD = 2, 58 | DFU_REQUEST_GETSTATUS = 3, 59 | DFU_REQUEST_CLRSTATUS = 4, 60 | DFU_REQUEST_GETSTATE = 5, 61 | DFU_REQUEST_ABORT = 6, 62 | } dfu_requests_t; 63 | 64 | // DFU States 65 | typedef enum { 66 | APP_IDLE = 0, 67 | APP_DETACH = 1, 68 | DFU_IDLE = 2, 69 | DFU_DNLOAD_SYNC = 3, 70 | DFU_DNBUSY = 4, 71 | DFU_DNLOAD_IDLE = 5, 72 | DFU_MANIFEST_SYNC = 6, 73 | DFU_MANIFEST = 7, 74 | DFU_MANIFEST_WAIT_RESET = 8, 75 | DFU_UPLOAD_IDLE = 9, 76 | DFU_ERROR = 10, 77 | } dfu_state_t; 78 | 79 | // DFU Status 80 | typedef enum { 81 | DFU_STATUS_OK = 0x00, 82 | DFU_STATUS_ERR_TARGET = 0x01, 83 | DFU_STATUS_ERR_FILE = 0x02, 84 | DFU_STATUS_ERR_WRITE = 0x03, 85 | DFU_STATUS_ERR_ERASE = 0x04, 86 | DFU_STATUS_ERR_CHECK_ERASED = 0x05, 87 | DFU_STATUS_ERR_PROG = 0x06, 88 | DFU_STATUS_ERR_VERIFY = 0x07, 89 | DFU_STATUS_ERR_ADDRESS = 0x08, 90 | DFU_STATUS_ERR_NOTDONE = 0x09, 91 | DFU_STATUS_ERR_FIRMWARE = 0x0A, 92 | DFU_STATUS_ERR_VENDOR = 0x0B, 93 | DFU_STATUS_ERR_USBR = 0x0C, 94 | DFU_STATUS_ERR_POR = 0x0D, 95 | DFU_STATUS_ERR_UNKNOWN = 0x0E, 96 | DFU_STATUS_ERR_STALLEDPKT = 0x0F, 97 | } dfu_status_t; 98 | 99 | #define DFU_ATTR_CAN_DOWNLOAD (1u << 0) 100 | #define DFU_ATTR_CAN_UPLOAD (1u << 1) 101 | #define DFU_ATTR_MANIFESTATION_TOLERANT (1u << 2) 102 | #define DFU_ATTR_WILL_DETACH (1u << 3) 103 | 104 | // DFU Status Request Payload 105 | typedef struct TU_ATTR_PACKED 106 | { 107 | uint8_t bStatus; 108 | uint8_t bwPollTimeout[3]; 109 | uint8_t bState; 110 | uint8_t iString; 111 | } dfu_status_response_t; 112 | 113 | TU_VERIFY_STATIC( sizeof(dfu_status_response_t) == 6, "size is not correct"); 114 | 115 | #ifdef __cplusplus 116 | } 117 | #endif 118 | 119 | #endif /* _TUSB_DFU_H_ */ 120 | -------------------------------------------------------------------------------- /examples/GPOUT/GPOUT.ino: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD 4 | * 5 | * SPDX-License-Identifier: MIT 6 | * 7 | * @Dependent Library: 8 | * M5GFX: https://github.com/m5stack/M5GFX 9 | * M5Unified: https://github.com/m5stack/M5Unified 10 | * M5-Max3421E-USBShield: https://github.com/m5stack/M5-Max3421E-USBShield 11 | */ 12 | #include "M5Unified.h" 13 | #include "M5_Max3421E_Usb.h" 14 | 15 | #define rIOPINS1 (0xa0) // 20<<3 16 | /* IOPINS1 Bits */ 17 | #define bmGPOUT0 (0x01) 18 | #define bmGPOUT1 (0x02) 19 | #define bmGPOUT2 (0x04) 20 | #define bmGPOUT3 (0x08) 21 | #define bmGPIN0 (0x10) 22 | #define bmGPIN1 (0x20) 23 | #define bmGPIN2 (0x40) 24 | #define bmGPIN3 (0x80) 25 | 26 | #define rIOPINS2 (0xa8) // 21<<3 27 | /* IOPINS2 Bits */ 28 | #define bmGPOUT4 (0x01) 29 | #define bmGPOUT5 (0x02) 30 | #define bmGPOUT6 (0x04) 31 | #define bmGPOUT7 (0x08) 32 | #define bmGPIN4 (0x10) 33 | #define bmGPIN5 (0x20) 34 | #define bmGPIN6 (0x40) 35 | #define bmGPIN7 (0x80) 36 | 37 | /* 38 | The board is equipped with two dip switches to adapt to different M5 series hosts. 39 | https://static-cdn.m5stack.com/resource/docs/products/module/USB%20v1.2%20Module/pinMap-70b8e2ad-8325-4887-af33-44e3dae91520.png 40 | If you need to change the spi pin, use these spi configuration settings 41 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 42 | */ 43 | // Initialize USB Host 44 | M5_USBH_Host USBHost(&SPI, 18, 23, 19, 5, 35); 45 | 46 | /** 47 | * @brief Sets the output pin level. 48 | * 49 | * pin range: 0~4 50 | * value sets the state: 0 for low level, 1 for high level. 51 | * 52 | * @param pin The pin number to set (0~4). 53 | * @param value The desired state of the pin (0 for low level, 1 for high level). 54 | */ 55 | void write_output_pin(uint8_t pin, uint8_t value); 56 | 57 | /** 58 | * @brief Reads the output pin level. 59 | * 60 | * pin range: 0~4 61 | * 62 | * @param pin The pin number to read (0~4). 63 | * 64 | * @return Returns 1 for high level, 0 for low level. 65 | */ 66 | uint8_t read_output_pin(uint8_t pin); 67 | 68 | /** 69 | * @brief Prints the level of the specified pin. 70 | * 71 | * Prints the state of the given pin (high or low). 72 | * 73 | * @param pin The pin number to print (0~4). 74 | */ 75 | void print_pin_level(uint8_t pin); 76 | 77 | void setup() 78 | { 79 | M5.begin(); 80 | M5.Power.begin(); 81 | Serial.begin(115200); 82 | 83 | Serial.println("Initializing MAX3421E with USB Host..."); 84 | 85 | // Initialize USB Host 86 | if (USBHost.begin(0)) { 87 | Serial.println("USB Host initialized successfully."); 88 | } else { 89 | Serial.println("Failed to initialize USB Host."); 90 | while (1) { 91 | }; 92 | } 93 | write_output_pin(0, 0); 94 | write_output_pin(1, 1); 95 | write_output_pin(2, 0); 96 | write_output_pin(3, 1); 97 | write_output_pin(4, 0); 98 | } 99 | 100 | void loop() 101 | { 102 | for (uint8_t i = 0; i <= 4; i++) { 103 | print_pin_level(i); 104 | delay(100); 105 | } 106 | delay(3000); 107 | } 108 | 109 | void write_output_pin(uint8_t pin, uint8_t value) 110 | { 111 | uint8_t gpout = USBHost.max3421_readRegister(rIOPINS1, false); 112 | gpout &= 0x0f; 113 | gpout |= (USBHost.max3421_readRegister(rIOPINS2, false) << 4); 114 | if (value) { 115 | gpout |= (0x01 << pin); 116 | } else { 117 | gpout &= ~(0x01 << pin); 118 | } 119 | USBHost.max3421_writeRegister(rIOPINS1, (gpout & 0x0F), false); 120 | USBHost.max3421_writeRegister(rIOPINS2, (gpout >> 4), false); 121 | } 122 | 123 | uint8_t read_output_pin(uint8_t pin) 124 | { 125 | uint8_t gpout = USBHost.max3421_readRegister(rIOPINS1, false); 126 | gpout &= 0x0f; 127 | gpout |= (USBHost.max3421_readRegister(rIOPINS2, false) << 4); 128 | return ((gpout >> pin) & 0x01); 129 | } 130 | 131 | void print_pin_level(uint8_t pin) 132 | { 133 | if (read_output_pin(pin)) { 134 | Serial.printf("GPOUT%d is high\r\n", pin); 135 | } else { 136 | Serial.printf("GPOUT%d is low\r\n", pin); 137 | } 138 | } 139 | 140 | void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len) 141 | { 142 | } -------------------------------------------------------------------------------- /src/tusb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_H_ 28 | #define _TUSB_H_ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | //--------------------------------------------------------------------+ 35 | // INCLUDE 36 | //--------------------------------------------------------------------+ 37 | #include "common/tusb_common.h" 38 | #include "osal/osal.h" 39 | #include "common/tusb_fifo.h" 40 | 41 | #include "class/hid/hid.h" 42 | 43 | //------------- TypeC -------------// 44 | #if CFG_TUC_ENABLED 45 | #include "typec/usbc.h" 46 | #endif 47 | 48 | //------------- HOST -------------// 49 | #if CFG_TUH_ENABLED 50 | #include "host/usbh.h" 51 | 52 | #if CFG_TUH_HID 53 | #include "class/hid/hid_host.h" 54 | #endif 55 | 56 | #if CFG_TUH_MSC 57 | #include "class/msc/msc_host.h" 58 | #endif 59 | 60 | #if CFG_TUH_CDC 61 | #include "class/cdc/cdc_host.h" 62 | #endif 63 | 64 | #if CFG_TUH_VENDOR 65 | #include "class/vendor/vendor_host.h" 66 | #endif 67 | #else 68 | #ifndef tuh_int_handler 69 | #define tuh_int_handler(...) 70 | #endif 71 | #endif 72 | 73 | //------------- DEVICE -------------// 74 | #if CFG_TUD_ENABLED 75 | #include "device/usbd.h" 76 | 77 | #if CFG_TUD_HID 78 | #include "class/hid/hid_device.h" 79 | #endif 80 | 81 | #if CFG_TUD_CDC 82 | #include "class/cdc/cdc_device.h" 83 | #endif 84 | 85 | #if CFG_TUD_MSC 86 | #include "class/msc/msc_device.h" 87 | #endif 88 | 89 | #if CFG_TUD_AUDIO 90 | #include "class/audio/audio_device.h" 91 | #endif 92 | 93 | #if CFG_TUD_VIDEO 94 | #include "class/video/video_device.h" 95 | #endif 96 | 97 | #if CFG_TUD_MIDI 98 | #include "class/midi/midi_device.h" 99 | #endif 100 | 101 | #if CFG_TUD_VENDOR 102 | #include "class/vendor/vendor_device.h" 103 | #endif 104 | 105 | #if CFG_TUD_USBTMC 106 | #include "class/usbtmc/usbtmc_device.h" 107 | #endif 108 | 109 | #if CFG_TUD_DFU_RUNTIME 110 | #include "class/dfu/dfu_rt_device.h" 111 | #endif 112 | 113 | #if CFG_TUD_DFU 114 | #include "class/dfu/dfu_device.h" 115 | #endif 116 | 117 | #if CFG_TUD_ECM_RNDIS || CFG_TUD_NCM 118 | #include "class/net/net_device.h" 119 | #endif 120 | 121 | #if CFG_TUD_BTH 122 | #include "class/bth/bth_device.h" 123 | #endif 124 | #else 125 | #ifndef tud_int_handler 126 | #define tud_int_handler(...) 127 | #endif 128 | #endif 129 | 130 | 131 | //--------------------------------------------------------------------+ 132 | // APPLICATION API 133 | //--------------------------------------------------------------------+ 134 | 135 | // Initialize device/host stack 136 | // Note: when using with RTOS, this should be called after scheduler/kernel is started. 137 | // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API. 138 | bool tusb_init(void); 139 | 140 | // Check if stack is initialized 141 | bool tusb_inited(void); 142 | 143 | // TODO 144 | // bool tusb_teardown(void); 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif /* _TUSB_H_ */ 151 | -------------------------------------------------------------------------------- /src/class/video/video_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * Copyright (c) 2021 Koji KITAYAMA 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | * This file is part of the TinyUSB stack. 26 | */ 27 | 28 | #ifndef TUSB_VIDEO_DEVICE_H_ 29 | #define TUSB_VIDEO_DEVICE_H_ 30 | 31 | #include "common/tusb_common.h" 32 | #include "video.h" 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | //--------------------------------------------------------------------+ 39 | // Application API (Multiple Ports) 40 | // CFG_TUD_VIDEO > 1 41 | //--------------------------------------------------------------------+ 42 | 43 | /** Return true if streaming 44 | * 45 | * @param[in] ctl_idx Destination control interface index 46 | * @param[in] stm_idx Destination streaming interface index */ 47 | bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx); 48 | 49 | /** Transfer a frame 50 | * 51 | * @param[in] ctl_idx Destination control interface index 52 | * @param[in] stm_idx Destination streaming interface index 53 | * @param[in] buffer Frame buffer. The caller must not use this buffer until the operation is completed. 54 | * @param[in] bufsize Byte size of the frame buffer */ 55 | bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize); 56 | 57 | /*------------- Optional callbacks -------------*/ 58 | /** Invoked when compeletion of a frame transfer 59 | * 60 | * @param[in] ctl_idx Destination control interface index 61 | * @param[in] stm_idx Destination streaming interface index */ 62 | TU_ATTR_WEAK void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx); 63 | 64 | //--------------------------------------------------------------------+ 65 | // Application Callback API (weak is optional) 66 | //--------------------------------------------------------------------+ 67 | 68 | /** Invoked when SET_POWER_MODE request received 69 | * 70 | * @param[in] ctl_idx Destination control interface index 71 | * @param[in] stm_idx Destination streaming interface index 72 | * @return video_error_code_t */ 73 | TU_ATTR_WEAK int tud_video_power_mode_cb(uint_fast8_t ctl_idx, uint8_t power_mod); 74 | 75 | /** Invoked when VS_COMMIT_CONTROL(SET_CUR) request received 76 | * 77 | * @param[in] ctl_idx Destination control interface index 78 | * @param[in] stm_idx Destination streaming interface index 79 | * @param[in] parameters Video streaming parameters 80 | * @return video_error_code_t */ 81 | TU_ATTR_WEAK int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, 82 | video_probe_and_commit_control_t const *parameters); 83 | 84 | //--------------------------------------------------------------------+ 85 | // INTERNAL USBD-CLASS DRIVER API 86 | //--------------------------------------------------------------------+ 87 | void videod_init (void); 88 | void videod_reset (uint8_t rhport); 89 | uint16_t videod_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 90 | bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); 91 | bool videod_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); 92 | 93 | #ifdef __cplusplus 94 | } 95 | #endif 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /src/host/usbh_pvt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021, Ha Thach (tinyusb.org) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_USBH_PVT_H_ 28 | #define _TUSB_USBH_PVT_H_ 29 | 30 | // ESP32 out-of-sync 31 | #ifdef ARDUINO_ARCH_ESP32 32 | #include "arduino/ports/esp32/tusb_config_esp32.h" 33 | #endif 34 | 35 | #include "osal/osal.h" 36 | #include "common/tusb_fifo.h" 37 | #include "common/tusb_private.h" 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | // Level where CFG_TUSB_DEBUG must be at least for USBH is logged 44 | #ifndef CFG_TUH_LOG_LEVEL 45 | #define CFG_TUH_LOG_LEVEL 2 46 | #endif 47 | 48 | #define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__) 49 | 50 | enum { 51 | USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS) 52 | }; 53 | 54 | //--------------------------------------------------------------------+ 55 | // Class Driver API 56 | //--------------------------------------------------------------------+ 57 | 58 | typedef struct { 59 | #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL 60 | char const* name; 61 | #endif 62 | 63 | void (* const init )(void); 64 | bool (* const open )(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 65 | bool (* const set_config )(uint8_t dev_addr, uint8_t itf_num); 66 | bool (* const xfer_cb )(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); 67 | void (* const close )(uint8_t dev_addr); 68 | } usbh_class_driver_t; 69 | 70 | // Invoked when initializing host stack to get additional class drivers. 71 | // Can be implemented by application to extend/overwrite class driver support. 72 | // Note: The drivers array must be accessible at all time when stack is active 73 | usbh_class_driver_t const* usbh_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK; 74 | 75 | // Call by class driver to tell USBH that it has complete the enumeration 76 | void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num); 77 | 78 | uint8_t usbh_get_rhport(uint8_t dev_addr); 79 | 80 | uint8_t* usbh_get_enum_buf(void); 81 | 82 | void usbh_int_set(bool enabled); 83 | 84 | void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr); 85 | 86 | //--------------------------------------------------------------------+ 87 | // USBH Endpoint API 88 | //--------------------------------------------------------------------+ 89 | 90 | // Submit a usb transfer with callback support, require CFG_TUH_API_EDPT_XFER 91 | bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes, 92 | tuh_xfer_cb_t complete_cb, uintptr_t user_data); 93 | 94 | TU_ATTR_ALWAYS_INLINE 95 | static inline bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { 96 | return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0); 97 | } 98 | 99 | // Claim an endpoint before submitting a transfer. 100 | // If caller does not make any transfer, it must release endpoint for others. 101 | bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr); 102 | 103 | // Release claimed endpoint without submitting a transfer 104 | bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr); 105 | 106 | // Check if endpoint transferring is complete 107 | bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr); 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /src/class/dfu/dfu_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 XMOS LIMITED 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_DFU_DEVICE_H_ 28 | #define _TUSB_DFU_DEVICE_H_ 29 | 30 | #include "dfu.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | //--------------------------------------------------------------------+ 37 | // Class Driver Default Configure & Validation 38 | //--------------------------------------------------------------------+ 39 | 40 | #if !defined(CFG_TUD_DFU_XFER_BUFSIZE) 41 | #error "CFG_TUD_DFU_XFER_BUFSIZE must be defined, it has to be set to the buffer size used in TUD_DFU_DESCRIPTOR" 42 | #endif 43 | 44 | //--------------------------------------------------------------------+ 45 | // Application API 46 | //--------------------------------------------------------------------+ 47 | 48 | // Must be called when the application is done with flashing started by 49 | // tud_dfu_download_cb() and tud_dfu_manifest_cb(). 50 | // status is DFU_STATUS_OK if successful, any other error status will cause state to enter dfuError 51 | void tud_dfu_finish_flashing(uint8_t status); 52 | 53 | //--------------------------------------------------------------------+ 54 | // Application Callback API (weak is optional) 55 | //--------------------------------------------------------------------+ 56 | 57 | // Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc. 58 | 59 | // Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST) 60 | // Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation. 61 | // During this period, USB host won't try to communicate with us. 62 | uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state); 63 | 64 | // Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests 65 | // This callback could be returned before flashing op is complete (async). 66 | // Once finished flashing, application must call tud_dfu_finish_flashing() 67 | void tud_dfu_download_cb (uint8_t alt, uint16_t block_num, uint8_t const *data, uint16_t length); 68 | 69 | // Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest) 70 | // Application can do checksum, or actual flashing if buffered entire image previously. 71 | // Once finished flashing, application must call tud_dfu_finish_flashing() 72 | void tud_dfu_manifest_cb(uint8_t alt); 73 | 74 | // Invoked when received DFU_UPLOAD request 75 | // Application must populate data with up to length bytes and 76 | // Return the number of written bytes 77 | TU_ATTR_WEAK uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length); 78 | 79 | // Invoked when a DFU_DETACH request is received 80 | TU_ATTR_WEAK void tud_dfu_detach_cb(void); 81 | 82 | // Invoked when the Host has terminated a download or upload transfer 83 | TU_ATTR_WEAK void tud_dfu_abort_cb(uint8_t alt); 84 | 85 | //--------------------------------------------------------------------+ 86 | // Internal Class Driver API 87 | //--------------------------------------------------------------------+ 88 | void dfu_moded_init(void); 89 | void dfu_moded_reset(uint8_t rhport); 90 | uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 91 | bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); 92 | 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif /* _TUSB_DFU_MODE_DEVICE_H_ */ 99 | -------------------------------------------------------------------------------- /src/portable/raspberrypi/rp2040/rp2040_usb.h: -------------------------------------------------------------------------------- 1 | #ifndef RP2040_COMMON_H_ 2 | #define RP2040_COMMON_H_ 3 | 4 | #if defined(RP2040_USB_HOST_MODE) && defined(RP2040_USB_DEVICE_MODE) 5 | #error TinyUSB device and host mode not supported at the same time 6 | #endif 7 | 8 | #include "common/tusb_common.h" 9 | 10 | #include "pico.h" 11 | #include "hardware/structs/usb.h" 12 | #include "hardware/irq.h" 13 | #include "hardware/resets.h" 14 | #include "hardware/timer.h" 15 | 16 | #if defined(PICO_RP2040_USB_DEVICE_ENUMERATION_FIX) && !defined(TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX) 17 | #define TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX PICO_RP2040_USB_DEVICE_ENUMERATION_FIX 18 | #endif 19 | 20 | #if defined(PICO_RP2040_USB_DEVICE_UFRAME_FIX) && !defined(TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX) 21 | #define TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX PICO_RP2040_USB_DEVICE_UFRAME_FIX 22 | #endif 23 | 24 | #if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX 25 | #undef PICO_RP2040_USB_FAST_IRQ 26 | #define PICO_RP2040_USB_FAST_IRQ 1 27 | #endif 28 | 29 | #ifndef PICO_RP2040_USB_FAST_IRQ 30 | #define PICO_RP2040_USB_FAST_IRQ 0 31 | #endif 32 | 33 | #if PICO_RP2040_USB_FAST_IRQ 34 | #define __tusb_irq_path_func(x) __no_inline_not_in_flash_func(x) 35 | #else 36 | #define __tusb_irq_path_func(x) x 37 | #endif 38 | 39 | #define usb_hw_set ((usb_hw_t *) hw_set_alias_untyped(usb_hw)) 40 | #define usb_hw_clear ((usb_hw_t *) hw_clear_alias_untyped(usb_hw)) 41 | 42 | #define pico_info(...) TU_LOG(2, __VA_ARGS__) 43 | #define pico_trace(...) TU_LOG(3, __VA_ARGS__) 44 | 45 | // Hardware information per endpoint 46 | typedef struct hw_endpoint 47 | { 48 | // Is this a valid struct 49 | bool configured; 50 | 51 | // Transfer direction (i.e. IN is rx for host but tx for device) 52 | // allows us to common up transfer functions 53 | bool rx; 54 | 55 | uint8_t ep_addr; 56 | uint8_t next_pid; 57 | 58 | // Endpoint control register 59 | io_rw_32 *endpoint_control; 60 | 61 | // Buffer control register 62 | io_rw_32 *buffer_control; 63 | 64 | // Buffer pointer in usb dpram 65 | uint8_t *hw_data_buf; 66 | 67 | // User buffer in main memory 68 | uint8_t *user_buf; 69 | 70 | // Current transfer information 71 | uint16_t remaining_len; 72 | uint16_t xferred_len; 73 | 74 | // Data needed from EP descriptor 75 | uint16_t wMaxPacketSize; 76 | 77 | // Endpoint is in use 78 | bool active; 79 | 80 | // Interrupt, bulk, etc 81 | uint8_t transfer_type; 82 | 83 | // Transfer scheduled but not active 84 | uint8_t pending; 85 | 86 | #if CFG_TUH_ENABLED 87 | // Only needed for host 88 | uint8_t dev_addr; 89 | 90 | // If interrupt endpoint 91 | uint8_t interrupt_num; 92 | #endif 93 | 94 | } hw_endpoint_t; 95 | 96 | #if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX 97 | extern volatile uint32_t e15_last_sof; 98 | #endif 99 | 100 | void rp2040_usb_init(void); 101 | 102 | void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len); 103 | bool hw_endpoint_xfer_continue(struct hw_endpoint *ep); 104 | void hw_endpoint_reset_transfer(struct hw_endpoint *ep); 105 | void hw_endpoint_start_next_buffer(struct hw_endpoint *ep); 106 | 107 | TU_ATTR_ALWAYS_INLINE static inline void hw_endpoint_lock_update(__unused struct hw_endpoint * ep, __unused int delta) { 108 | // todo add critsec as necessary to prevent issues between worker and IRQ... 109 | // note that this is perhaps as simple as disabling IRQs because it would make 110 | // sense to have worker and IRQ on same core, however I think using critsec is about equivalent. 111 | } 112 | 113 | void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask); 114 | 115 | TU_ATTR_ALWAYS_INLINE static inline uint32_t _hw_endpoint_buffer_control_get_value32 (struct hw_endpoint *ep) 116 | { 117 | return *ep->buffer_control; 118 | } 119 | 120 | TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_value32 (struct hw_endpoint *ep, uint32_t value) 121 | { 122 | _hw_endpoint_buffer_control_update32(ep, 0, value); 123 | } 124 | 125 | TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_mask32 (struct hw_endpoint *ep, uint32_t value) 126 | { 127 | _hw_endpoint_buffer_control_update32(ep, ~value, value); 128 | } 129 | 130 | TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_clear_mask32 (struct hw_endpoint *ep, uint32_t value) 131 | { 132 | _hw_endpoint_buffer_control_update32(ep, ~value, 0); 133 | } 134 | 135 | static inline uintptr_t hw_data_offset (uint8_t *buf) 136 | { 137 | // Remove usb base from buffer pointer 138 | return (uintptr_t) buf ^ (uintptr_t) usb_dpram; 139 | } 140 | 141 | extern const char *ep_dir_string[]; 142 | 143 | #endif 144 | -------------------------------------------------------------------------------- /src/class/net/net_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2020 Peter Lawrence 5 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | * This file is part of the TinyUSB stack. 26 | */ 27 | 28 | #ifndef _TUSB_NET_DEVICE_H_ 29 | #define _TUSB_NET_DEVICE_H_ 30 | 31 | #include "class/cdc/cdc.h" 32 | 33 | #if CFG_TUD_ECM_RNDIS && CFG_TUD_NCM 34 | #error "Cannot enable both ECM_RNDIS and NCM network drivers" 35 | #endif 36 | 37 | #include "ncm.h" 38 | 39 | /* declared here, NOT in usb_descriptors.c, so that the driver can intelligently ZLP as needed */ 40 | #define CFG_TUD_NET_ENDPOINT_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 41 | 42 | /* Maximum Transmission Unit (in bytes) of the network, including Ethernet header */ 43 | #ifndef CFG_TUD_NET_MTU 44 | #define CFG_TUD_NET_MTU 1514 45 | #endif 46 | 47 | #ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE 48 | #define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200 49 | #endif 50 | 51 | #ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE 52 | #define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200 53 | #endif 54 | 55 | #ifndef CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB 56 | #define CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB 8 57 | #endif 58 | 59 | #ifndef CFG_TUD_NCM_ALIGNMENT 60 | #define CFG_TUD_NCM_ALIGNMENT 4 61 | #endif 62 | 63 | #ifdef __cplusplus 64 | extern "C" { 65 | #endif 66 | 67 | //--------------------------------------------------------------------+ 68 | // Application API 69 | //--------------------------------------------------------------------+ 70 | 71 | // indicate to network driver that client has finished with the packet provided to network_recv_cb() 72 | void tud_network_recv_renew(void); 73 | 74 | // poll network driver for its ability to accept another packet to transmit 75 | bool tud_network_can_xmit(uint16_t size); 76 | 77 | // if network_can_xmit() returns true, network_xmit() can be called once 78 | void tud_network_xmit(void *ref, uint16_t arg); 79 | 80 | //--------------------------------------------------------------------+ 81 | // Application Callbacks (WEAK is optional) 82 | //--------------------------------------------------------------------+ 83 | 84 | // client must provide this: return false if the packet buffer was not accepted 85 | bool tud_network_recv_cb(const uint8_t *src, uint16_t size); 86 | 87 | // client must provide this: copy from network stack packet pointer to dst 88 | uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg); 89 | 90 | //------------- ECM/RNDIS -------------// 91 | 92 | // client must provide this: initialize any network state back to the beginning 93 | void tud_network_init_cb(void); 94 | 95 | // client must provide this: 48-bit MAC address 96 | // TODO removed later since it is not part of tinyusb stack 97 | extern uint8_t tud_network_mac_address[6]; 98 | 99 | //------------- NCM -------------// 100 | 101 | // callback to client providing optional indication of internal state of network driver 102 | void tud_network_link_state_cb(bool state); 103 | 104 | //--------------------------------------------------------------------+ 105 | // INTERNAL USBD-CLASS DRIVER API 106 | //--------------------------------------------------------------------+ 107 | void netd_init (void); 108 | void netd_reset (uint8_t rhport); 109 | uint16_t netd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 110 | bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); 111 | bool netd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); 112 | void netd_report (uint8_t *buf, uint16_t len); 113 | 114 | #ifdef __cplusplus 115 | } 116 | #endif 117 | 118 | #endif /* _TUSB_NET_DEVICE_H_ */ 119 | -------------------------------------------------------------------------------- /src/class/usbtmc/usbtmc_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 N Conrad 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | 28 | #ifndef CLASS_USBTMC_USBTMC_DEVICE_H_ 29 | #define CLASS_USBTMC_USBTMC_DEVICE_H_ 30 | 31 | #include "usbtmc.h" 32 | 33 | // Enable 488 mode by default 34 | #if !defined(CFG_TUD_USBTMC_ENABLE_488) 35 | #define CFG_TUD_USBTMC_ENABLE_488 (1) 36 | #endif 37 | 38 | /*********************************************** 39 | * Functions to be implemented by the class implementation 40 | */ 41 | 42 | // In order to proceed, app must call call tud_usbtmc_start_bus_read(rhport) during or soon after: 43 | // * tud_usbtmc_open_cb 44 | // * tud_usbtmc_msg_data_cb 45 | // * tud_usbtmc_msgBulkIn_complete_cb 46 | // * tud_usbtmc_msg_trigger_cb 47 | // * (successful) tud_usbtmc_check_abort_bulk_out_cb 48 | // * (successful) tud_usbtmc_check_abort_bulk_in_cb 49 | // * (successful) tud_usmtmc_bulkOut_clearFeature_cb 50 | 51 | #if (CFG_TUD_USBTMC_ENABLE_488) 52 | usbtmc_response_capabilities_488_t const * tud_usbtmc_get_capabilities_cb(void); 53 | #else 54 | usbtmc_response_capabilities_t const * tud_usbtmc_get_capabilities_cb(void); 55 | #endif 56 | 57 | void tud_usbtmc_open_cb(uint8_t interface_id); 58 | 59 | bool tud_usbtmc_msgBulkOut_start_cb(usbtmc_msg_request_dev_dep_out const * msgHeader); 60 | // transfer_complete does not imply that a message is complete. 61 | bool tud_usbtmc_msg_data_cb( void *data, size_t len, bool transfer_complete); 62 | void tud_usbtmc_bulkOut_clearFeature_cb(void); // Notice to clear and abort the pending BULK out transfer 63 | 64 | bool tud_usbtmc_msgBulkIn_request_cb(usbtmc_msg_request_dev_dep_in const * request); 65 | bool tud_usbtmc_msgBulkIn_complete_cb(void); 66 | void tud_usbtmc_bulkIn_clearFeature_cb(void); // Notice to clear and abort the pending BULK out transfer 67 | 68 | bool tud_usbtmc_initiate_abort_bulk_in_cb(uint8_t *tmcResult); 69 | bool tud_usbtmc_initiate_abort_bulk_out_cb(uint8_t *tmcResult); 70 | bool tud_usbtmc_initiate_clear_cb(uint8_t *tmcResult); 71 | 72 | bool tud_usbtmc_check_abort_bulk_in_cb(usbtmc_check_abort_bulk_rsp_t *rsp); 73 | bool tud_usbtmc_check_abort_bulk_out_cb(usbtmc_check_abort_bulk_rsp_t *rsp); 74 | bool tud_usbtmc_check_clear_cb(usbtmc_get_clear_status_rsp_t *rsp); 75 | 76 | // Indicator pulse should be 0.5 to 1.0 seconds long 77 | TU_ATTR_WEAK bool tud_usbtmc_indicator_pulse_cb(tusb_control_request_t const * msg, uint8_t *tmcResult); 78 | 79 | #if (CFG_TUD_USBTMC_ENABLE_488) 80 | uint8_t tud_usbtmc_get_stb_cb(uint8_t *tmcResult); 81 | TU_ATTR_WEAK bool tud_usbtmc_msg_trigger_cb(usbtmc_msg_generic_t* msg); 82 | //TU_ATTR_WEAK bool tud_usbtmc_app_go_to_local_cb(); 83 | #endif 84 | 85 | /******************************************* 86 | * Called from app 87 | * 88 | * We keep a reference to the buffer, so it MUST not change until the app is 89 | * notified that the transfer is complete. 90 | ******************************************/ 91 | 92 | bool tud_usbtmc_transmit_dev_msg_data( 93 | const void * data, size_t len, 94 | bool endOfMessage, bool usingTermChar); 95 | 96 | bool tud_usbtmc_start_bus_read(void); 97 | 98 | 99 | /* "callbacks" from USB device core */ 100 | 101 | uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 102 | void usbtmcd_reset_cb(uint8_t rhport); 103 | bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); 104 | bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); 105 | void usbtmcd_init_cb(void); 106 | 107 | /************************************************************ 108 | * USBTMC Descriptor Templates 109 | *************************************************************/ 110 | 111 | 112 | #endif /* CLASS_USBTMC_USBTMC_DEVICE_H_ */ 113 | -------------------------------------------------------------------------------- /examples/MassStorage/msc_file_explorer/msc_file_explorer.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | /* This example demonstrates use of both device and host, where 13 | * - Device run on native usb controller (roothub port0) 14 | * - Host depending on MCUs run on either: 15 | * - rp2040: bit-banging 2 GPIOs with the help of Pico-PIO-USB library (roothub port1) 16 | * - samd21/51, nrf52840, esp32: using MAX3421e controller (host shield) 17 | * 18 | * Requirements: 19 | * - For rp2040: 20 | * - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library 21 | * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 22 | * - Provide VBus (5v) and GND for peripheral 23 | * - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed" 24 | * - For samd21/51, nrf52840, esp32: 25 | * - Additional MAX2341e USB Host shield or featherwing is required 26 | * - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h 27 | */ 28 | 29 | // SdFat is required for using Adafruit_USBH_MSC_SdFatDevice 30 | #include "SdFat.h" 31 | 32 | // USBHost is defined in usbh_helper.h 33 | #include "usbh_helper.h" 34 | 35 | // USB Host MSC Block Device object which implemented API for use with SdFat 36 | Adafruit_USBH_MSC_BlockDevice msc_block_dev; 37 | 38 | // file system object from SdFat 39 | FatVolume fatfs; 40 | 41 | // if file system is successfully mounted on usb block device 42 | bool is_mounted = false; 43 | 44 | void setup() { 45 | Serial.begin(115200); 46 | 47 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 48 | // init host stack on controller (rhport) 1 49 | // For rp2040: this is called in core1's setup1() 50 | USBHost.begin(1); 51 | #endif 52 | 53 | // while ( !Serial ) delay(10); // wait for native usb 54 | Serial.println("TinyUSB Host Mass Storage File Explorer Example"); 55 | } 56 | 57 | 58 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 59 | //--------------------------------------------------------------------+ 60 | // Using Host shield MAX3421E controller 61 | //--------------------------------------------------------------------+ 62 | void loop() { 63 | USBHost.task(); 64 | Serial.flush(); 65 | } 66 | 67 | #elif defined(ARDUINO_ARCH_RP2040) 68 | //--------------------------------------------------------------------+ 69 | // For RP2040 use both core0 for device stack, core1 for host stack 70 | //--------------------------------------------------------------------+ 71 | void loop() { 72 | } 73 | 74 | //------------- Core1 -------------// 75 | void setup1() { 76 | // configure pio-usb: defined in usbh_helper.h 77 | rp2040_configure_pio_usb(); 78 | 79 | // run host stack on controller (rhport) 1 80 | // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the 81 | // host bit-banging processing works done in core1 to free up core0 for other works 82 | USBHost.begin(1); 83 | } 84 | 85 | void loop1() { 86 | USBHost.task(); 87 | } 88 | 89 | #endif 90 | 91 | void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, 92 | uint8_t const* report, uint16_t len) { 93 | } 94 | 95 | //--------------------------------------------------------------------+ 96 | // TinyUSB Host callbacks 97 | //--------------------------------------------------------------------+ 98 | extern "C" 99 | { 100 | 101 | // Invoked when device is mounted (configured) 102 | void tuh_mount_cb(uint8_t daddr) { 103 | (void) daddr; 104 | } 105 | 106 | /// Invoked when device is unmounted (bus reset/unplugged) 107 | void tuh_umount_cb(uint8_t daddr) { 108 | (void) daddr; 109 | } 110 | 111 | // Invoked when a device with MassStorage interface is mounted 112 | void tuh_msc_mount_cb(uint8_t dev_addr) { 113 | Serial.printf("Device attached, address = %d\r\n", dev_addr); 114 | 115 | // Initialize block device with MSC device address 116 | msc_block_dev.begin(dev_addr); 117 | 118 | // For simplicity this example only support LUN 0 119 | msc_block_dev.setActiveLUN(0); 120 | 121 | is_mounted = fatfs.begin(&msc_block_dev); 122 | 123 | if (is_mounted) { 124 | fatfs.ls(&Serial, LS_SIZE); 125 | } 126 | } 127 | 128 | // Invoked when a device with MassStorage interface is unmounted 129 | void tuh_msc_umount_cb(uint8_t dev_addr) { 130 | Serial.printf("Device removed, address = %d\r\n", dev_addr); 131 | 132 | // unmount file system 133 | is_mounted = false; 134 | fatfs.end(); 135 | 136 | // end block device 137 | msc_block_dev.end(); 138 | } 139 | 140 | } -------------------------------------------------------------------------------- /src/arduino/ports/nrf/Adafruit_TinyUSB_nrf.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, hathach for Adafruit 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 defined ARDUINO_NRF52_ADAFRUIT && CFG_TUD_ENABLED 28 | 29 | #include "nrfx.h" 30 | #include "nrfx_power.h" 31 | 32 | #include "Arduino.h" 33 | #include "arduino/Adafruit_USBD_Device.h" 34 | 35 | //--------------------------------------------------------------------+ 36 | // MACRO TYPEDEF CONSTANT ENUM DECLARATION 37 | //--------------------------------------------------------------------+ 38 | 39 | #define USBD_STACK_SZ (200) 40 | 41 | //--------------------------------------------------------------------+ 42 | // Forward USB interrupt events to TinyUSB IRQ Handler 43 | //--------------------------------------------------------------------+ 44 | extern "C" void USBD_IRQHandler(void) { 45 | #if CFG_SYSVIEW 46 | SEGGER_SYSVIEW_RecordEnterISR(); 47 | #endif 48 | 49 | tud_int_handler(0); 50 | 51 | #if CFG_SYSVIEW 52 | SEGGER_SYSVIEW_RecordExitISR(); 53 | #endif 54 | } 55 | 56 | //--------------------------------------------------------------------+ 57 | // Porting API 58 | //--------------------------------------------------------------------+ 59 | 60 | static void usb_hardware_init(void); 61 | 62 | // USB Device Driver task 63 | // This top level thread process all usb events and invoke callbacks 64 | static void usb_device_task(void *param) { 65 | (void)param; 66 | 67 | // Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice 68 | // 2 is highest for application 69 | NVIC_SetPriority(USBD_IRQn, 2); 70 | 71 | // init device on rhport0 72 | tud_init(0); 73 | 74 | usb_hardware_init(); 75 | 76 | // RTOS forever loop 77 | while (1) { 78 | tud_task(); 79 | TinyUSB_Device_FlushCDC(); 80 | } 81 | } 82 | 83 | void TinyUSB_Port_InitDevice(uint8_t rhport) { 84 | (void)rhport; 85 | 86 | // Create a task for tinyusb device stack 87 | xTaskCreate(usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, 88 | NULL); 89 | } 90 | 91 | void TinyUSB_Port_EnterDFU(void) { 92 | // Reset to Bootloader 93 | enterSerialDfu(); 94 | } 95 | 96 | uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) { 97 | uint32_t *serial_32 = (uint32_t *)serial_id; 98 | 99 | serial_32[0] = __builtin_bswap32(NRF_FICR->DEVICEID[1]); 100 | serial_32[1] = __builtin_bswap32(NRF_FICR->DEVICEID[0]); 101 | 102 | return 8; 103 | } 104 | 105 | //--------------------------------------------------------------------+ 106 | // Helper 107 | //--------------------------------------------------------------------+ 108 | 109 | // tinyusb function that handles power event (detected, ready, removed) 110 | // We must call it within SD's SOC event handler, or set it as power event 111 | // handler if SD is not enabled. 112 | extern "C" void tusb_hal_nrf_power_event(uint32_t event); 113 | 114 | static void power_event_handler(nrfx_power_usb_evt_t event) { 115 | tusb_hal_nrf_power_event((uint32_t)event); 116 | } 117 | 118 | // Init usb hardware when starting up. Softdevice is not enabled yet 119 | static void usb_hardware_init(void) { 120 | // USB power may already be ready at this time -> no event generated 121 | // We need to invoke the handler based on the status initially 122 | uint32_t usb_reg = NRF_POWER->USBREGSTATUS; 123 | 124 | // Power module init 125 | const nrfx_power_config_t pwr_cfg = {0}; 126 | nrfx_power_init(&pwr_cfg); 127 | 128 | // Register tusb function as USB power handler 129 | const nrfx_power_usbevt_config_t config = {.handler = power_event_handler}; 130 | 131 | nrfx_power_usbevt_init(&config); 132 | nrfx_power_usbevt_enable(); 133 | 134 | if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk) { 135 | tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); 136 | } 137 | } 138 | 139 | #endif // USE_TINYUSB 140 | -------------------------------------------------------------------------------- /examples/HID/hid_device_report.ino: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Adafruit invests time and resources providing this open source code, 3 | please support Adafruit and open-source hardware by purchasing 4 | products from Adafruit! 5 | 6 | MIT license, check LICENSE for more information 7 | Copyright (c) 2019 Ha Thach for Adafruit Industries 8 | All text above, and the splash screen below must be included in 9 | any redistribution 10 | *********************************************************************/ 11 | 12 | 13 | /* This example demonstrates use of both device and host, where 14 | * - Device run on native usb controller (roothub port0) 15 | * - Host depending on MCUs run on either: 16 | * - rp2040: bit-banging 2 GPIOs with the help of Pico-PIO-USB library (roothub port1) 17 | * - samd21/51, nrf52840, esp32: using MAX3421e controller (host shield) 18 | * 19 | * Requirements: 20 | * - For rp2040: 21 | * - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library 22 | * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 23 | * - Provide VBus (5v) and GND for peripheral 24 | * - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed" 25 | * - For samd21/51, nrf52840, esp32: 26 | * - Additional MAX2341e USB Host shield or featherwing is required 27 | * - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h 28 | */ 29 | 30 | // USBHost is defined in usbh_helper.h 31 | #include "usbh_helper.h" 32 | 33 | #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 34 | //--------------------------------------------------------------------+ 35 | // Using Host shield MAX3421E controller 36 | //--------------------------------------------------------------------+ 37 | void setup() { 38 | Serial.begin(115200); 39 | 40 | // init host stack on controller (rhport) 1 41 | USBHost.begin(1); 42 | 43 | // while ( !Serial ) delay(10); // wait for native usb 44 | Serial.println("TinyUSB Dual: HID Device Report Example"); 45 | } 46 | 47 | void loop() { 48 | USBHost.task(); 49 | Serial.flush(); 50 | } 51 | 52 | #elif defined(ARDUINO_ARCH_RP2040) 53 | //--------------------------------------------------------------------+ 54 | // For RP2040 use both core0 for device stack, core1 for host stack 55 | //--------------------------------------------------------------------+ 56 | 57 | //------------- Core0 -------------// 58 | void setup() { 59 | Serial.begin(115200); 60 | //while ( !Serial ) delay(10); // wait for native usb 61 | Serial.println("TinyUSB Dual: HID Device Report Example"); 62 | } 63 | 64 | void loop() { 65 | Serial.flush(); 66 | } 67 | 68 | //------------- Core1 -------------// 69 | void setup1() { 70 | // configure pio-usb: defined in usbh_helper.h 71 | rp2040_configure_pio_usb(); 72 | 73 | // run host stack on controller (rhport) 1 74 | // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the 75 | // host bit-banging processing works done in core1 to free up core0 for other works 76 | USBHost.begin(1); 77 | } 78 | 79 | void loop1() { 80 | USBHost.task(); 81 | } 82 | 83 | #endif 84 | 85 | void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, 86 | uint8_t const* report, uint16_t len) { 87 | } 88 | 89 | extern "C" { 90 | 91 | // Invoked when device with hid interface is mounted 92 | // Report descriptor is also available for use. 93 | // tuh_hid_parse_report_descriptor() can be used to parse common/simple enough 94 | // descriptor. Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, 95 | // it will be skipped therefore report_desc = NULL, desc_len = 0 96 | void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) { 97 | (void) desc_report; 98 | (void) desc_len; 99 | uint16_t vid, pid; 100 | tuh_vid_pid_get(dev_addr, &vid, &pid); 101 | 102 | Serial.printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance); 103 | Serial.printf("VID = %04x, PID = %04x\r\n", vid, pid); 104 | if (!tuh_hid_receive_report(dev_addr, instance)) { 105 | Serial.printf("Error: cannot request to receive report\r\n"); 106 | } 107 | } 108 | 109 | // Invoked when device with hid interface is un-mounted 110 | void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { 111 | Serial.printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); 112 | } 113 | 114 | // Invoked when received report from device via interrupt endpoint 115 | void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) { 116 | Serial.printf("HIDreport : "); 117 | for (uint16_t i = 0; i < len; i++) { 118 | Serial.printf("0x%02X ", report[i]); 119 | } 120 | Serial.println(); 121 | // continue to request to receive report 122 | if (!tuh_hid_receive_report(dev_addr, instance)) { 123 | Serial.printf("Error: cannot request to receive report\r\n"); 124 | } 125 | } 126 | 127 | } // extern C -------------------------------------------------------------------------------- /src/arduino/msc/Adafruit_USBH_MSC.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2022 Ha Thach (tinyusb.org) 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 | // ESP32 out-of-sync 26 | #ifdef ARDUINO_ARCH_ESP32 27 | #include "arduino/ports/esp32/tusb_config_esp32.h" 28 | #endif 29 | 30 | #include "tusb_option.h" 31 | 32 | #if CFG_TUH_ENABLED && CFG_TUH_MSC 33 | 34 | #include "Adafruit_USBH_MSC.h" 35 | #include "tusb.h" 36 | 37 | #if __has_include("SdFat.h") 38 | 39 | Adafruit_USBH_MSC_BlockDevice::Adafruit_USBH_MSC_BlockDevice() { 40 | _daddr = _lun = 0; 41 | _busy = false; 42 | _wr_cb = NULL; 43 | } 44 | 45 | bool Adafruit_USBH_MSC_BlockDevice::begin(uint8_t dev_addr) { 46 | _daddr = dev_addr; 47 | return true; 48 | } 49 | 50 | bool Adafruit_USBH_MSC_BlockDevice::setActiveLUN(uint8_t lun) { 51 | _lun = lun; 52 | return true; 53 | } 54 | void Adafruit_USBH_MSC_BlockDevice::setWriteCompleteCallback( 55 | tuh_msc_complete_cb_t cb) { 56 | _wr_cb = cb; 57 | } 58 | 59 | void Adafruit_USBH_MSC_BlockDevice::end(void) { _daddr = _lun = 0; } 60 | 61 | bool Adafruit_USBH_MSC_BlockDevice::mounted(void) { return _daddr > 0; } 62 | 63 | bool Adafruit_USBH_MSC_BlockDevice::isBusy(void) { return _busy; } 64 | 65 | bool Adafruit_USBH_MSC_BlockDevice::wait_for_io(void) { 66 | while (_busy) { 67 | if (tuh_task_event_ready()) { 68 | tuh_task(); 69 | } 70 | } 71 | 72 | return true; 73 | } 74 | 75 | bool Adafruit_USBH_MSC_BlockDevice::_io_complete_cb( 76 | uint8_t dev_addr, tuh_msc_complete_data_t const *cb_data) { 77 | (void)cb_data; 78 | if (dev_addr != _daddr) { 79 | // something wrong occurred, maybe device removed while transferring 80 | return false; 81 | } 82 | 83 | // TODO skip csw status: assuming io is successful 84 | _busy = false; 85 | 86 | switch (cb_data->cbw->command[0]) { 87 | case SCSI_CMD_WRITE_10: 88 | if (_wr_cb) { 89 | _wr_cb(dev_addr, cb_data); 90 | } 91 | break; 92 | } 93 | 94 | return true; 95 | } 96 | 97 | static bool _msc_io_complete_cb(uint8_t dev_addr, 98 | tuh_msc_complete_data_t const *cb_data) { 99 | Adafruit_USBH_MSC_BlockDevice *sdfat_dev = 100 | (Adafruit_USBH_MSC_BlockDevice *)cb_data->user_arg; 101 | sdfat_dev->_io_complete_cb(dev_addr, cb_data); 102 | return true; 103 | } 104 | 105 | uint32_t Adafruit_USBH_MSC_BlockDevice::sectorCount(void) { 106 | return tuh_msc_get_block_count(_daddr, _lun); 107 | } 108 | 109 | bool Adafruit_USBH_MSC_BlockDevice::syncDevice(void) { 110 | // no caching 111 | return true; 112 | } 113 | 114 | bool Adafruit_USBH_MSC_BlockDevice::readSectors(uint32_t block, uint8_t *dst, 115 | size_t ns) { 116 | _busy = true; 117 | if (tuh_msc_read10(_daddr, _lun, dst, block, (uint16_t)ns, 118 | _msc_io_complete_cb, (uintptr_t)this)) { 119 | wait_for_io(); 120 | return true; 121 | } else { 122 | _busy = false; 123 | return false; 124 | } 125 | } 126 | 127 | bool Adafruit_USBH_MSC_BlockDevice::writeSectors(uint32_t block, 128 | const uint8_t *src, 129 | size_t ns) { 130 | _busy = true; 131 | if (tuh_msc_write10(_daddr, _lun, src, block, (uint16_t)ns, 132 | _msc_io_complete_cb, (uintptr_t)this)) { 133 | wait_for_io(); 134 | return true; 135 | } else { 136 | _busy = false; 137 | return false; 138 | } 139 | } 140 | 141 | bool Adafruit_USBH_MSC_BlockDevice::readSector(uint32_t block, uint8_t *dst) { 142 | return readSectors(block, dst, 1); 143 | } 144 | 145 | bool Adafruit_USBH_MSC_BlockDevice::writeSector(uint32_t block, 146 | const uint8_t *src) { 147 | return writeSectors(block, src, 1); 148 | } 149 | 150 | #endif 151 | 152 | #endif 153 | -------------------------------------------------------------------------------- /src/arduino/Adafruit_USBD_Device.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_USBD_DEVICE_H_ 26 | #define ADAFRUIT_USBD_DEVICE_H_ 27 | 28 | #include "Adafruit_USBD_Interface.h" 29 | 30 | #include "../tusb.h" // use relative path to prevent ESP32 out-of-sync issue 31 | 32 | #ifdef ARDUINO_ARCH_ESP32 33 | #include "esp32-hal-tinyusb.h" 34 | #endif 35 | 36 | class Adafruit_USBD_Device { 37 | private: 38 | enum { STRING_DESCRIPTOR_MAX = 12 }; 39 | 40 | // Device descriptor 41 | tusb_desc_device_t _desc_device __attribute__((aligned(4))); 42 | 43 | // Configuration descriptor 44 | uint8_t *_desc_cfg; 45 | uint8_t _desc_cfg_buffer[256]; 46 | uint16_t _desc_cfg_len; 47 | uint16_t _desc_cfg_maxlen; 48 | 49 | uint8_t _itf_count; 50 | 51 | uint8_t _epin_count; 52 | uint8_t _epout_count; 53 | 54 | // String descriptor 55 | const char *_desc_str_arr[STRING_DESCRIPTOR_MAX]; 56 | uint8_t _desc_str_count; 57 | uint16_t _desc_str[32 + 1]; // up to 32 unicode characters with headers 58 | 59 | public: 60 | Adafruit_USBD_Device(void); 61 | 62 | //------------- Device descriptor -------------// 63 | 64 | // Set VID, PID 65 | void setID(uint16_t vid, uint16_t pid); 66 | 67 | // Set bcdUSB version e.g 1.0, 2.0, 2.1 68 | void setVersion(uint16_t bcd); 69 | 70 | // Set bcdDevice version 71 | void setDeviceVersion(uint16_t bcd); 72 | 73 | //------------- Configuration descriptor -------------// 74 | 75 | // Add a new interface 76 | bool addInterface(Adafruit_USBD_Interface &itf); 77 | 78 | // Clear/Reset configuration descriptor 79 | void clearConfiguration(void); 80 | 81 | // Provide user buffer for configuration descriptor, if total length > 256 82 | void setConfigurationBuffer(uint8_t *buf, uint32_t buflen); 83 | 84 | // Allocate a new interface number 85 | uint8_t allocInterface(uint8_t count = 1) { 86 | uint8_t ret = _itf_count; 87 | _itf_count += count; 88 | return ret; 89 | } 90 | 91 | uint8_t allocEndpoint(uint8_t in) { 92 | uint8_t ret = in ? (0x80 | _epin_count++) : _epout_count++; 93 | #if defined(ARDUINO_ARCH_ESP32) && ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE 94 | // ESP32 reserves 0x03, 0x84, 0x85 for CDC Serial 95 | if (ret == 0x03) { 96 | ret = _epout_count++; 97 | } else if (ret == 0x84 || ret == 0x85) { 98 | // Note: ESP32 does not have this much of EP IN 99 | _epin_count = 6; 100 | ret = 0x86; 101 | } 102 | #endif 103 | return ret; 104 | } 105 | 106 | //------------- String descriptor -------------// 107 | void setLanguageDescriptor(uint16_t language_id); 108 | void setManufacturerDescriptor(const char *s); 109 | void setProductDescriptor(const char *s); 110 | void setSerialDescriptor(const char *s); 111 | uint8_t getSerialDescriptor(uint16_t *serial_utf16); 112 | 113 | uint8_t addStringDescriptor(const char *s); 114 | 115 | //------------- Control -------------// 116 | 117 | bool begin(uint8_t rhport = 0); 118 | void task(void); 119 | 120 | // physical disable/enable pull-up 121 | bool detach(void); 122 | bool attach(void); 123 | 124 | //------------- status -------------// 125 | bool mounted(void); 126 | bool suspended(void); 127 | bool ready(void); 128 | bool remoteWakeup(void); 129 | 130 | private: 131 | uint16_t const *descriptor_string_cb(uint8_t index, uint16_t langid); 132 | 133 | friend uint8_t const *tud_descriptor_device_cb(void); 134 | friend uint8_t const *tud_descriptor_configuration_cb(uint8_t index); 135 | friend uint16_t const *tud_descriptor_string_cb(uint8_t index, 136 | uint16_t langid); 137 | }; 138 | 139 | extern Adafruit_USBD_Device TinyUSBDevice; 140 | 141 | // USBDevice has a high chance to conflict with other usb stack 142 | // only define if supported BSP 143 | #ifdef USE_TINYUSB 144 | #define USBDevice TinyUSBDevice 145 | #endif 146 | 147 | #endif /* ADAFRUIT_USBD_DEVICE_H_ */ 148 | -------------------------------------------------------------------------------- /src/arduino/hid/Adafruit_USBD_HID.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_USBD_HID_H_ 26 | #define ADAFRUIT_USBD_HID_H_ 27 | 28 | #include "arduino/Adafruit_USBD_Device.h" 29 | 30 | class Adafruit_USBD_HID : public Adafruit_USBD_Interface { 31 | public: 32 | typedef uint16_t (*get_report_callback_t)(uint8_t report_id, 33 | hid_report_type_t report_type, 34 | uint8_t *buffer, uint16_t reqlen); 35 | typedef void (*set_report_callback_t)(uint8_t report_id, 36 | hid_report_type_t report_type, 37 | uint8_t const *buffer, 38 | uint16_t bufsize); 39 | 40 | enum { INVALID_INSTANCE = 0xffu }; 41 | static uint8_t getInstanceCount(void) { return _instance_count; } 42 | 43 | Adafruit_USBD_HID(void); 44 | Adafruit_USBD_HID(uint8_t const *desc_report, uint16_t len, 45 | uint8_t protocol = HID_ITF_PROTOCOL_NONE, 46 | uint8_t interval_ms = 4, bool has_out_endpoint = false); 47 | 48 | void setPollInterval(uint8_t interval_ms); 49 | void setBootProtocol(uint8_t protocol); // 0: None, 1: Keyboard, 2:Mouse 50 | 51 | void enableOutEndpoint(bool enable); 52 | bool isOutEndpointEnabled(void); 53 | 54 | void setReportDescriptor(uint8_t const *desc_report, uint16_t len); 55 | void setReportCallback(get_report_callback_t get_report, 56 | set_report_callback_t set_report); 57 | 58 | bool begin(void); 59 | bool isValid(void) { return _instance != INVALID_INSTANCE; } 60 | 61 | bool ready(void); 62 | bool sendReport(uint8_t report_id, void const *report, uint8_t len); 63 | 64 | uint8_t getProtocol(); 65 | 66 | // Report helpers 67 | bool sendReport8(uint8_t report_id, uint8_t num); 68 | bool sendReport16(uint8_t report_id, uint16_t num); 69 | bool sendReport32(uint8_t report_id, uint32_t num); 70 | 71 | //------------- Keyboard API -------------// 72 | bool keyboardReport(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]); 73 | bool keyboardPress(uint8_t report_id, char ch); 74 | bool keyboardRelease(uint8_t report_id); 75 | 76 | //------------- Mouse API -------------// 77 | bool mouseReport(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, 78 | int8_t vertical, int8_t horizontal); 79 | bool mouseMove(uint8_t report_id, int8_t x, int8_t y); 80 | bool mouseScroll(uint8_t report_id, int8_t scroll, int8_t pan); 81 | bool mouseButtonPress(uint8_t report_id, uint8_t buttons); 82 | bool mouseButtonRelease(uint8_t report_id); 83 | 84 | // from Adafruit_USBD_Interface 85 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 86 | uint8_t *buf, uint16_t bufsize); 87 | 88 | // internal use only 89 | uint16_t makeItfDesc(uint8_t itfnum, uint8_t *buf, uint16_t bufsize, 90 | uint8_t ep_in, uint8_t ep_out); 91 | 92 | private: 93 | static uint8_t _instance_count; 94 | 95 | uint8_t _instance; 96 | uint8_t _interval_ms; 97 | uint8_t _protocol; 98 | bool _out_endpoint; 99 | uint8_t _mouse_button; 100 | 101 | uint16_t _desc_report_len; 102 | uint8_t const *_desc_report; 103 | 104 | get_report_callback_t _get_report_cb; 105 | set_report_callback_t _set_report_cb; 106 | 107 | friend uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, 108 | hid_report_type_t report_type, 109 | uint8_t *buffer, uint16_t reqlen); 110 | friend void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, 111 | hid_report_type_t report_type, 112 | uint8_t const *buffer, uint16_t bufsize); 113 | friend uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf); 114 | }; 115 | 116 | #endif /* ADAFRUIT_USBD_HID_H_ */ 117 | -------------------------------------------------------------------------------- /src/class/dfu/dfu_rt_device.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Sylvain Munaut 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | // ESP32 out-of-sync 28 | #ifdef ARDUINO_ARCH_ESP32 29 | #include "arduino/ports/esp32/tusb_config_esp32.h" 30 | #endif 31 | 32 | #include "tusb_option.h" 33 | 34 | #if (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME) 35 | 36 | #include "device/usbd.h" 37 | #include "device/usbd_pvt.h" 38 | 39 | #include "dfu_rt_device.h" 40 | 41 | //--------------------------------------------------------------------+ 42 | // MACRO CONSTANT TYPEDEF 43 | //--------------------------------------------------------------------+ 44 | 45 | // Level where CFG_TUSB_DEBUG must be at least for this driver is logged 46 | #ifndef CFG_TUD_DFU_RUNTIME_LOG_LEVEL 47 | #define CFG_TUD_DFU_RUNTIME_LOG_LEVEL CFG_TUD_LOG_LEVEL 48 | #endif 49 | 50 | #define TU_LOG_DRV(...) TU_LOG(CFG_TUD_DFU_RUNTIME_LOG_LEVEL, __VA_ARGS__) 51 | 52 | //--------------------------------------------------------------------+ 53 | // INTERNAL OBJECT & FUNCTION DECLARATION 54 | //--------------------------------------------------------------------+ 55 | 56 | //--------------------------------------------------------------------+ 57 | // USBD Driver API 58 | //--------------------------------------------------------------------+ 59 | void dfu_rtd_init(void) 60 | { 61 | } 62 | 63 | void dfu_rtd_reset(uint8_t rhport) 64 | { 65 | (void) rhport; 66 | } 67 | 68 | uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) 69 | { 70 | (void) rhport; 71 | (void) max_len; 72 | 73 | // Ensure this is DFU Runtime 74 | TU_VERIFY((itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) && 75 | (itf_desc->bInterfaceProtocol == DFU_PROTOCOL_RT), 0); 76 | 77 | uint8_t const * p_desc = tu_desc_next( itf_desc ); 78 | uint16_t drv_len = sizeof(tusb_desc_interface_t); 79 | 80 | if ( TUSB_DESC_FUNCTIONAL == tu_desc_type(p_desc) ) 81 | { 82 | drv_len += tu_desc_len(p_desc); 83 | p_desc = tu_desc_next(p_desc); 84 | } 85 | 86 | return drv_len; 87 | } 88 | 89 | // Invoked when a control transfer occurred on an interface of this class 90 | // Driver response accordingly to the request and the transfer stage (setup/data/ack) 91 | // return false to stall control endpoint (e.g unsupported request) 92 | bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) 93 | { 94 | // nothing to do with DATA or ACK stage 95 | if ( stage != CONTROL_STAGE_SETUP ) return true; 96 | 97 | TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE); 98 | 99 | // dfu-util will try to claim the interface with SET_INTERFACE request before sending DFU request 100 | if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type && 101 | TUSB_REQ_SET_INTERFACE == request->bRequest ) 102 | { 103 | tud_control_status(rhport, request); 104 | return true; 105 | } 106 | 107 | // Handle class request only from here 108 | TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS); 109 | 110 | switch (request->bRequest) 111 | { 112 | case DFU_REQUEST_DETACH: 113 | { 114 | TU_LOG_DRV(" DFU RT Request: DETACH\r\n"); 115 | tud_control_status(rhport, request); 116 | tud_dfu_runtime_reboot_to_dfu_cb(); 117 | } 118 | break; 119 | 120 | case DFU_REQUEST_GETSTATUS: 121 | { 122 | TU_LOG_DRV(" DFU RT Request: GETSTATUS\r\n"); 123 | dfu_status_response_t resp; 124 | // Status = OK, Poll timeout is ignored during RT, State = APP_IDLE, IString = 0 125 | TU_VERIFY(tu_memset_s(&resp, sizeof(resp), 0x00, sizeof(resp))==0); 126 | tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t)); 127 | } 128 | break; 129 | 130 | default: 131 | { 132 | TU_LOG_DRV(" DFU RT Unexpected Request: %d\r\n", request->bRequest); 133 | return false; // stall unsupported request 134 | } 135 | } 136 | 137 | return true; 138 | } 139 | 140 | #endif 141 | -------------------------------------------------------------------------------- /src/arduino/ports/samd/tusb_config_samd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018, hathach for Adafruit 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 | #ifndef _TUSB_CONFIG_SAMD_H_ 26 | #define _TUSB_CONFIG_SAMD_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | //-------------------------------------------------------------------- 33 | // COMMON CONFIGURATION 34 | //-------------------------------------------------------------------- 35 | #ifdef __SAMD51__ 36 | #define CFG_TUSB_MCU OPT_MCU_SAMD51 37 | #else 38 | #define CFG_TUSB_MCU OPT_MCU_SAMD21 39 | #endif 40 | 41 | #ifndef CFG_TUSB_OS 42 | #define CFG_TUSB_OS OPT_OS_NONE 43 | #endif 44 | 45 | #ifndef CFG_TUSB_DEBUG 46 | #define CFG_TUSB_DEBUG 0 47 | #endif 48 | 49 | // For selectively disable device log (when > CFG_TUSB_DEBUG) 50 | // #define CFG_TUD_LOG_LEVEL 3 51 | // #define CFG_TUH_LOG_LEVEL 3 52 | 53 | #define CFG_TUSB_MEM_SECTION 54 | #define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) 55 | 56 | // Enable device stack 57 | #define CFG_TUD_ENABLED 1 58 | 59 | // Enable host stack with MAX3421E (host shield) 60 | #define CFG_TUH_ENABLED 1 61 | #define CFG_TUH_MAX3421 1 62 | 63 | //-------------------------------------------------------------------- 64 | // DEVICE CONFIGURATION 65 | //-------------------------------------------------------------------- 66 | 67 | #define CFG_TUD_ENDOINT0_SIZE 64 68 | 69 | //------------- CLASS -------------// 70 | #define CFG_TUD_CDC 1 71 | #define CFG_TUD_MSC 1 72 | #define CFG_TUD_HID 2 73 | #define CFG_TUD_MIDI 1 74 | #define CFG_TUD_VENDOR 1 75 | #define CFG_TUD_VIDEO 1 // number of video control interfaces 76 | #define CFG_TUD_VIDEO_STREAMING 1 // number of video streaming interfaces 77 | 78 | // video streaming endpoint buffer size 79 | #define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256 80 | 81 | // CDC FIFO size of TX and RX 82 | #define CFG_TUD_CDC_RX_BUFSIZE 256 83 | #define CFG_TUD_CDC_TX_BUFSIZE 256 84 | 85 | // MSC Buffer size of Device Mass storage 86 | #define CFG_TUD_MSC_EP_BUFSIZE 512 87 | 88 | // HID buffer size Should be sufficient to hold ID (if any) + Data 89 | #define CFG_TUD_HID_EP_BUFSIZE 64 90 | 91 | // MIDI FIFO size of TX and RX 92 | #define CFG_TUD_MIDI_RX_BUFSIZE 128 93 | #define CFG_TUD_MIDI_TX_BUFSIZE 128 94 | 95 | // Vendor FIFO size of TX and RX 96 | #define CFG_TUD_VENDOR_RX_BUFSIZE 64 97 | #define CFG_TUD_VENDOR_TX_BUFSIZE 64 98 | 99 | //-------------------------------------------------------------------- 100 | // Host Configuration 101 | //-------------------------------------------------------------------- 102 | 103 | // Size of buffer to hold descriptors and other data used for enumeration 104 | #define CFG_TUH_ENUMERATION_BUFSIZE 256 105 | 106 | // Number of hub devices 107 | #define CFG_TUH_HUB 1 108 | 109 | // max device support (excluding hub device): 1 hub typically has 4 ports 110 | #define CFG_TUH_DEVICE_MAX (3 * CFG_TUH_HUB + 1) 111 | 112 | // Enable tuh_edpt_xfer() API 113 | // #define CFG_TUH_API_EDPT_XFER 1 114 | 115 | // Number of mass storage 116 | #define CFG_TUH_MSC 1 117 | 118 | // Number of HIDs 119 | // typical keyboard + mouse device can have 3,4 HID interfaces 120 | #define CFG_TUH_HID (3 * CFG_TUH_DEVICE_MAX) 121 | 122 | // Number of CDC interfaces 123 | // FTDI and CP210x are not part of CDC class, only to re-use CDC driver API 124 | #define CFG_TUH_CDC 1 125 | #define CFG_TUH_CDC_FTDI 1 126 | #define CFG_TUH_CDC_CP210X 1 127 | #define CFG_TUH_CDC_CH34X 1 128 | 129 | // RX & TX fifo size 130 | #define CFG_TUH_CDC_RX_BUFSIZE 64 131 | #define CFG_TUH_CDC_TX_BUFSIZE 64 132 | 133 | // Set Line Control state on enumeration/mounted: 134 | // DTR ( bit 0), RTS (bit 1) 135 | #define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03 136 | 137 | // Set Line Coding on enumeration/mounted, value for cdc_line_coding_t 138 | // bit rate = 115200, 1 stop bit, no parity, 8 bit data width 139 | // This need Pico-PIO-USB at least 0.5.1 140 | #define CFG_TUH_CDC_LINE_CODING_ON_ENUM \ 141 | { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } 142 | 143 | #ifdef __cplusplus 144 | } 145 | #endif 146 | 147 | #endif /* _TUSB_CONFIG_SAMD_H_ */ 148 | -------------------------------------------------------------------------------- /src/class/bth/bth_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2020 Jerzy Kasenberg 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_BTH_DEVICE_H_ 28 | #define _TUSB_BTH_DEVICE_H_ 29 | 30 | #include 31 | #include 32 | 33 | //--------------------------------------------------------------------+ 34 | // Class Driver Configuration 35 | //--------------------------------------------------------------------+ 36 | #ifndef CFG_TUD_BTH_EVENT_EPSIZE 37 | #define CFG_TUD_BTH_EVENT_EPSIZE 16 38 | #endif 39 | 40 | #ifndef CFG_TUD_BTH_DATA_EPSIZE 41 | #define CFG_TUD_BTH_DATA_EPSIZE 64 42 | #endif 43 | 44 | // Allow BTH class to work in historically compatibility mode where the bRequest is always 0xe0. 45 | // See Bluetooth Core v5.3, Vol. 4, Part B, Section 2.2 46 | #ifndef CFG_TUD_BTH_HISTORICAL_COMPATIBLE 47 | #define CFG_TUD_BTH_HISTORICAL_COMPATIBLE 0 48 | #endif 49 | 50 | typedef struct TU_ATTR_PACKED 51 | { 52 | uint16_t op_code; 53 | uint8_t param_length; 54 | uint8_t param[255]; 55 | } bt_hci_cmd_t; 56 | 57 | #ifdef __cplusplus 58 | extern "C" { 59 | #endif 60 | 61 | //--------------------------------------------------------------------+ 62 | // Application Callback API (weak is optional) 63 | //--------------------------------------------------------------------+ 64 | 65 | // Invoked when HCI command was received over USB from Bluetooth host. 66 | // Detailed format is described in Bluetooth core specification Vol 2, 67 | // Part E, 5.4.1. 68 | // Length of the command is from 3 bytes (2 bytes for OpCode, 69 | // 1 byte for parameter total length) to 258. 70 | TU_ATTR_WEAK void tud_bt_hci_cmd_cb(void *hci_cmd, size_t cmd_len); 71 | 72 | // Invoked when ACL data was received over USB from Bluetooth host. 73 | // Detailed format is described in Bluetooth core specification Vol 2, 74 | // Part E, 5.4.2. 75 | // Length is from 4 bytes, (12 bits for Handle, 4 bits for flags 76 | // and 16 bits for data total length) to endpoint size. 77 | TU_ATTR_WEAK void tud_bt_acl_data_received_cb(void *acl_data, uint16_t data_len); 78 | 79 | // Called when event sent with tud_bt_event_send() was delivered to BT stack. 80 | // Controller can release/reuse buffer with Event packet at this point. 81 | TU_ATTR_WEAK void tud_bt_event_sent_cb(uint16_t sent_bytes); 82 | 83 | // Called when ACL data that was sent with tud_bt_acl_data_send() 84 | // was delivered to BT stack. 85 | // Controller can release/reuse buffer with ACL packet at this point. 86 | TU_ATTR_WEAK void tud_bt_acl_data_sent_cb(uint16_t sent_bytes); 87 | 88 | // Bluetooth controller calls this function when it wants to send even packet 89 | // as described in Bluetooth core specification Vol 2, Part E, 5.4.4. 90 | // Event has at least 2 bytes, first is Event code second contains parameter 91 | // total length. Controller can release/reuse event memory after 92 | // tud_bt_event_sent_cb() is called. 93 | bool tud_bt_event_send(void *event, uint16_t event_len); 94 | 95 | // Bluetooth controller calls this to send ACL data packet 96 | // as described in Bluetooth core specification Vol 2, Part E, 5.4.2 97 | // Minimum length is 4 bytes, (12 bits for Handle, 4 bits for flags 98 | // and 16 bits for data total length). Upper limit is not limited 99 | // to endpoint size since buffer is allocate by controller 100 | // and must not be reused till tud_bt_acl_data_sent_cb() is called. 101 | bool tud_bt_acl_data_send(void *acl_data, uint16_t data_len); 102 | 103 | //--------------------------------------------------------------------+ 104 | // Internal Class Driver API 105 | //--------------------------------------------------------------------+ 106 | void btd_init (void); 107 | void btd_reset (uint8_t rhport); 108 | uint16_t btd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); 109 | bool btd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const *request); 110 | bool btd_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); 111 | 112 | #ifdef __cplusplus 113 | } 114 | #endif 115 | 116 | #endif /* _TUSB_BTH_DEVICE_H_ */ 117 | -------------------------------------------------------------------------------- /src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, hathach for Adafruit 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 defined ARDUINO_ARCH_RP2040 && CFG_TUD_ENABLED 28 | 29 | #include "Arduino.h" 30 | 31 | // mbed old pico-sdk need to wrap with cpp 32 | extern "C" { 33 | #include "hardware/flash.h" 34 | #include "hardware/irq.h" 35 | #include "pico/bootrom.h" 36 | #include "pico/mutex.h" 37 | #include "pico/time.h" 38 | } 39 | 40 | #include "arduino/Adafruit_TinyUSB_API.h" 41 | #include "tusb.h" 42 | 43 | // SDK >= 1.4.0 need to dynamically request the IRQ to avoid conflicts 44 | #if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) < 104 45 | #define USB_TASK_IRQ 31 46 | #else 47 | static unsigned int USB_TASK_IRQ; 48 | #endif 49 | 50 | //--------------------------------------------------------------------+ 51 | // Forward USB interrupt events to TinyUSB IRQ Handler 52 | // rp2040 implementation will install appropriate handler when initializing 53 | // tinyusb. There is no need to forward IRQ from application 54 | //--------------------------------------------------------------------+ 55 | 56 | //--------------------------------------------------------------------+ 57 | // Earle Philhower and mbed specific 58 | //--------------------------------------------------------------------+ 59 | 60 | // mbed use old pico-sdk does not have unique_id 61 | #ifdef ARDUINO_ARCH_MBED 62 | 63 | #define get_unique_id(_serial) flash_get_unique_id(_serial) 64 | 65 | #else 66 | 67 | #include "pico/unique_id.h" 68 | #define get_unique_id(_serial) \ 69 | pico_get_unique_board_id((pico_unique_board_id_t *)_serial); 70 | 71 | #endif 72 | 73 | //--------------------------------------------------------------------+ 74 | // Porting API 75 | //--------------------------------------------------------------------+ 76 | 77 | // Big, global USB mutex, shared with all USB devices to make sure we don't 78 | // have multiple cores updating the TUSB state in parallel 79 | mutex_t __usb_mutex; 80 | 81 | static void usb_task_irq(void) { 82 | // if the mutex is already owned, then we are in user code 83 | // in this file which will do a tud_task itself, so we'll just do nothing 84 | // until the next tick; we won't starve 85 | if (mutex_try_enter(&__usb_mutex, NULL)) { 86 | tud_task(); 87 | mutex_exit(&__usb_mutex); 88 | } 89 | } 90 | 91 | #ifndef PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY 92 | #define PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY 0x00 93 | #endif 94 | 95 | // invoked when there is hardware usb irq, trigger task runner later 96 | static void usb_task_trigger_irq(void) { irq_set_pending(USB_TASK_IRQ); } 97 | 98 | void TinyUSB_Port_InitDevice(uint8_t rhport) { 99 | mutex_init(&__usb_mutex); 100 | 101 | tud_init(rhport); 102 | 103 | // soft irq for task runner 104 | #if (PICO_SDK_VERSION_MAJOR * 100 + PICO_SDK_VERSION_MINOR) >= 104 105 | USB_TASK_IRQ = user_irq_claim_unused(true); 106 | #endif 107 | irq_set_exclusive_handler(USB_TASK_IRQ, usb_task_irq); 108 | irq_set_enabled(USB_TASK_IRQ, true); 109 | 110 | // add shared irq to trigger task runner 111 | irq_add_shared_handler(USBCTRL_IRQ, usb_task_trigger_irq, 112 | PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY); 113 | } 114 | 115 | void TinyUSB_Port_EnterDFU(void) { 116 | reset_usb_boot(0, 0); 117 | while (1) { 118 | } 119 | } 120 | 121 | uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) { 122 | get_unique_id(serial_id); 123 | return FLASH_UNIQUE_ID_SIZE_BYTES; 124 | } 125 | 126 | //--------------------------------------------------------------------+ 127 | // Core API 128 | // Implement Core API since rp2040 need mutex for calling tud_task in 129 | // IRQ context 130 | //--------------------------------------------------------------------+ 131 | 132 | extern "C" { 133 | 134 | void TinyUSB_Device_Task(void) { 135 | // Since tud_task() is also invoked in ISR, we need to get the mutex first 136 | if (mutex_try_enter(&__usb_mutex, NULL)) { 137 | tud_task(); 138 | mutex_exit(&__usb_mutex); 139 | } 140 | } 141 | } 142 | 143 | #endif 144 | -------------------------------------------------------------------------------- /src/osal/osal_rtthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2020 tfx2001 (2479727366@qq.com) 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 | * This file is part of the TinyUSB stack. 25 | */ 26 | 27 | #ifndef _TUSB_OSAL_RTTHREAD_H_ 28 | #define _TUSB_OSAL_RTTHREAD_H_ 29 | 30 | // RT-Thread Headers 31 | #include "rtthread.h" 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | //--------------------------------------------------------------------+ 38 | // TASK API 39 | //--------------------------------------------------------------------+ 40 | TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { 41 | rt_thread_mdelay(msec); 42 | } 43 | 44 | //--------------------------------------------------------------------+ 45 | // Semaphore API 46 | //--------------------------------------------------------------------+ 47 | typedef struct rt_semaphore osal_semaphore_def_t; 48 | typedef rt_sem_t osal_semaphore_t; 49 | 50 | TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t 51 | osal_semaphore_create(osal_semaphore_def_t *semdef) { 52 | rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_PRIO); 53 | return semdef; 54 | } 55 | 56 | TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { 57 | (void) in_isr; 58 | return rt_sem_release(sem_hdl) == RT_EOK; 59 | } 60 | 61 | TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { 62 | return rt_sem_take(sem_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK; 63 | } 64 | 65 | TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { 66 | rt_sem_control(sem_hdl, RT_IPC_CMD_RESET, 0); 67 | } 68 | 69 | //--------------------------------------------------------------------+ 70 | // MUTEX API (priority inheritance) 71 | //--------------------------------------------------------------------+ 72 | typedef struct rt_mutex osal_mutex_def_t; 73 | typedef rt_mutex_t osal_mutex_t; 74 | 75 | TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { 76 | rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_PRIO); 77 | return mdef; 78 | } 79 | 80 | TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { 81 | return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK; 82 | } 83 | 84 | TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { 85 | return rt_mutex_release(mutex_hdl) == RT_EOK; 86 | } 87 | 88 | //--------------------------------------------------------------------+ 89 | // QUEUE API 90 | //--------------------------------------------------------------------+ 91 | 92 | // role device/host is used by OS NONE for mutex (disable usb isr) only 93 | #define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ 94 | static _type _name##_##buf[_depth]; \ 95 | osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; 96 | 97 | typedef struct { 98 | uint16_t depth; 99 | uint16_t item_sz; 100 | void *buf; 101 | 102 | struct rt_messagequeue sq; 103 | } osal_queue_def_t; 104 | 105 | typedef rt_mq_t osal_queue_t; 106 | 107 | TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { 108 | rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz, 109 | qdef->item_sz * qdef->depth, RT_IPC_FLAG_PRIO); 110 | return &(qdef->sq); 111 | } 112 | 113 | TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec) { 114 | 115 | rt_tick_t tick = rt_tick_from_millisecond((rt_int32_t) msec); 116 | return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK; 117 | } 118 | 119 | TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { 120 | (void) in_isr; 121 | return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK; 122 | } 123 | 124 | TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { 125 | return (qhdl->entry) == 0; 126 | } 127 | 128 | #ifdef __cplusplus 129 | } 130 | #endif 131 | 132 | #endif /* _TUSB_OSAL_RTTHREAD_H_ */ 133 | -------------------------------------------------------------------------------- /src/arduino/ports/nrf/tusb_config_nrf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2018, hathach for Adafruit 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 | #ifndef _TUSB_CONFIG_NRF_H_ 26 | #define _TUSB_CONFIG_NRF_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | //-------------------------------------------------------------------- 33 | // COMMON CONFIGURATION 34 | //-------------------------------------------------------------------- 35 | #define CFG_TUSB_MCU OPT_MCU_NRF5X 36 | #define CFG_TUSB_OS OPT_OS_FREERTOS 37 | 38 | #ifndef CFG_TUSB_DEBUG 39 | #define CFG_TUSB_DEBUG 0 40 | #endif 41 | 42 | // For selectively disable device log (when > CFG_TUSB_DEBUG) 43 | // #define CFG_TUD_LOG_LEVEL 3 44 | // #define CFG_TUH_LOG_LEVEL 3 45 | 46 | #define CFG_TUSB_MEM_SECTION 47 | #define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4))) 48 | 49 | #ifdef USE_TINYUSB 50 | // Enable device stack 51 | #define CFG_TUD_ENABLED 1 52 | 53 | // Enable host stack with MAX3421E (host shield) 54 | #define CFG_TUH_ENABLED 1 55 | #define CFG_TUH_MAX3421 1 56 | 57 | #else 58 | #define CFG_TUD_ENABLED 0 59 | #define CFG_TUH_ENABLED 0 60 | #endif 61 | 62 | //-------------------------------------------------------------------- 63 | // DEVICE CONFIGURATION 64 | //-------------------------------------------------------------------- 65 | 66 | #define CFG_TUD_ENDOINT0_SIZE 64 67 | 68 | //------------- CLASS -------------// 69 | #define CFG_TUD_CDC 1 70 | #define CFG_TUD_MSC 1 71 | #define CFG_TUD_HID 2 72 | #define CFG_TUD_MIDI 1 73 | #define CFG_TUD_VENDOR 1 74 | #define CFG_TUD_VIDEO 1 // number of video control interfaces 75 | #define CFG_TUD_VIDEO_STREAMING 1 // number of video streaming interfaces 76 | 77 | // video streaming endpoint buffer size 78 | #define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256 79 | 80 | // CDC FIFO size of TX and RX 81 | #define CFG_TUD_CDC_RX_BUFSIZE 256 82 | #define CFG_TUD_CDC_TX_BUFSIZE 256 83 | 84 | // MSC Buffer size of Device Mass storage 85 | #define CFG_TUD_MSC_EP_BUFSIZE 512 86 | 87 | // HID buffer size Should be sufficient to hold ID (if any) + Data 88 | #define CFG_TUD_HID_EP_BUFSIZE 64 89 | 90 | // MIDI FIFO size of TX and RX 91 | #define CFG_TUD_MIDI_RX_BUFSIZE 128 92 | #define CFG_TUD_MIDI_TX_BUFSIZE 128 93 | 94 | // Vendor FIFO size of TX and RX 95 | #ifndef CFG_TUD_VENDOR_RX_BUFSIZE 96 | #define CFG_TUD_VENDOR_RX_BUFSIZE 64 97 | #endif 98 | 99 | #ifndef CFG_TUD_VENDOR_TX_BUFSIZE 100 | #define CFG_TUD_VENDOR_TX_BUFSIZE 64 101 | #endif 102 | 103 | //-------------------------------------------------------------------- 104 | // Host Configuration 105 | //-------------------------------------------------------------------- 106 | 107 | // Size of buffer to hold descriptors and other data used for enumeration 108 | #define CFG_TUH_ENUMERATION_BUFSIZE 256 109 | 110 | // Number of hub devices 111 | #define CFG_TUH_HUB 1 112 | 113 | // max device support (excluding hub device): 1 hub typically has 4 ports 114 | #define CFG_TUH_DEVICE_MAX (3 * CFG_TUH_HUB + 1) 115 | 116 | // Enable tuh_edpt_xfer() API 117 | // #define CFG_TUH_API_EDPT_XFER 1 118 | 119 | // Number of mass storage 120 | #define CFG_TUH_MSC 1 121 | 122 | // Number of HIDs 123 | // typical keyboard + mouse device can have 3,4 HID interfaces 124 | #define CFG_TUH_HID (3 * CFG_TUH_DEVICE_MAX) 125 | 126 | // Number of CDC interfaces 127 | // FTDI and CP210x are not part of CDC class, only to re-use CDC driver API 128 | #define CFG_TUH_CDC 1 129 | #define CFG_TUH_CDC_FTDI 1 130 | #define CFG_TUH_CDC_CP210X 1 131 | #define CFG_TUH_CDC_CH34X 1 132 | 133 | // RX & TX fifo size 134 | #define CFG_TUH_CDC_RX_BUFSIZE 64 135 | #define CFG_TUH_CDC_TX_BUFSIZE 64 136 | 137 | // Set Line Control state on enumeration/mounted: 138 | // DTR ( bit 0), RTS (bit 1) 139 | #define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03 140 | 141 | // Set Line Coding on enumeration/mounted, value for cdc_line_coding_t 142 | // bit rate = 115200, 1 stop bit, no parity, 8 bit data width 143 | // This need Pico-PIO-USB at least 0.5.1 144 | #define CFG_TUH_CDC_LINE_CODING_ON_ENUM \ 145 | { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } 146 | 147 | #ifdef __cplusplus 148 | } 149 | #endif 150 | 151 | #endif /* _TUSB_CONFIG_NRF_H_ */ 152 | -------------------------------------------------------------------------------- /src/arduino/ports/samd/Adafruit_TinyUSB_samd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019, hathach for Adafruit 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 defined ARDUINO_ARCH_SAMD && CFG_TUD_ENABLED 28 | 29 | #include "Arduino.h" 30 | #include // Needed for auto-reset with 1200bps port touch 31 | 32 | #include "arduino/Adafruit_TinyUSB_API.h" 33 | #include "tusb.h" 34 | 35 | //--------------------------------------------------------------------+ 36 | // Forward USB interrupt events to TinyUSB IRQ Handler 37 | //--------------------------------------------------------------------+ 38 | extern "C" { 39 | 40 | #if CFG_TUSB_MCU == OPT_MCU_SAMD51 || CFG_TUSB_MCU == OPT_MCU_SAME5X 41 | 42 | // SAMD51 43 | void USB_0_Handler(void) { tud_int_handler(0); } 44 | void USB_1_Handler(void) { tud_int_handler(0); } 45 | void USB_2_Handler(void) { tud_int_handler(0); } 46 | void USB_3_Handler(void) { tud_int_handler(0); } 47 | 48 | #elif CFG_TUSB_MCU == OPT_MCU_SAMD21 49 | 50 | // SAMD21 51 | void USB_Handler(void) { tud_int_handler(0); } 52 | 53 | #endif 54 | 55 | } // extern C 56 | 57 | //--------------------------------------------------------------------+ 58 | // Porting API 59 | //--------------------------------------------------------------------+ 60 | void TinyUSB_Port_InitDevice(uint8_t rhport) { 61 | (void)rhport; 62 | 63 | /* Enable USB clock */ 64 | #if defined(__SAMD51__) 65 | MCLK->APBBMASK.reg |= MCLK_APBBMASK_USB; 66 | MCLK->AHBMASK.reg |= MCLK_AHBMASK_USB; 67 | 68 | // Set up the USB DP/DN pins 69 | PORT->Group[0].PINCFG[PIN_PA24H_USB_DM].bit.PMUXEN = 1; 70 | PORT->Group[0].PMUX[PIN_PA24H_USB_DM / 2].reg &= 71 | ~(0xF << (4 * (PIN_PA24H_USB_DM & 0x01u))); 72 | PORT->Group[0].PMUX[PIN_PA24H_USB_DM / 2].reg |= 73 | MUX_PA24H_USB_DM << (4 * (PIN_PA24H_USB_DM & 0x01u)); 74 | PORT->Group[0].PINCFG[PIN_PA25H_USB_DP].bit.PMUXEN = 1; 75 | PORT->Group[0].PMUX[PIN_PA25H_USB_DP / 2].reg &= 76 | ~(0xF << (4 * (PIN_PA25H_USB_DP & 0x01u))); 77 | PORT->Group[0].PMUX[PIN_PA25H_USB_DP / 2].reg |= 78 | MUX_PA25H_USB_DP << (4 * (PIN_PA25H_USB_DP & 0x01u)); 79 | 80 | GCLK->PCHCTRL[USB_GCLK_ID].reg = 81 | GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos); 82 | 83 | NVIC_SetPriority(USB_0_IRQn, 0UL); 84 | NVIC_SetPriority(USB_1_IRQn, 0UL); 85 | NVIC_SetPriority(USB_2_IRQn, 0UL); 86 | NVIC_SetPriority(USB_3_IRQn, 0UL); 87 | #else 88 | PM->APBBMASK.reg |= PM_APBBMASK_USB; 89 | 90 | // Set up the USB DP/DN pins 91 | PORT->Group[0].PINCFG[PIN_PA24G_USB_DM].bit.PMUXEN = 1; 92 | PORT->Group[0].PMUX[PIN_PA24G_USB_DM / 2].reg &= 93 | ~(0xF << (4 * (PIN_PA24G_USB_DM & 0x01u))); 94 | PORT->Group[0].PMUX[PIN_PA24G_USB_DM / 2].reg |= 95 | MUX_PA24G_USB_DM << (4 * (PIN_PA24G_USB_DM & 0x01u)); 96 | PORT->Group[0].PINCFG[PIN_PA25G_USB_DP].bit.PMUXEN = 1; 97 | PORT->Group[0].PMUX[PIN_PA25G_USB_DP / 2].reg &= 98 | ~(0xF << (4 * (PIN_PA25G_USB_DP & 0x01u))); 99 | PORT->Group[0].PMUX[PIN_PA25G_USB_DP / 2].reg |= 100 | MUX_PA25G_USB_DP << (4 * (PIN_PA25G_USB_DP & 0x01u)); 101 | 102 | // Put Generic Clock Generator 0 as source for Generic Clock Multiplexer 6 103 | // (USB reference) 104 | GCLK->CLKCTRL.reg = 105 | GCLK_CLKCTRL_ID(6) | // Generic Clock Multiplexer 6 106 | GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source 107 | GCLK_CLKCTRL_CLKEN; 108 | while (GCLK->STATUS.bit.SYNCBUSY) { 109 | // blocking wait 110 | } 111 | 112 | NVIC_SetPriority((IRQn_Type)USB_IRQn, 0UL); 113 | #endif 114 | 115 | // Init port 0 as device 116 | tud_init(0); 117 | } 118 | 119 | void TinyUSB_Port_EnterDFU(void) { 120 | // Reset to bootloader 121 | initiateReset(250); 122 | } 123 | 124 | uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) { 125 | #ifdef __SAMD51__ 126 | uint32_t *id_addresses[4] = {(uint32_t *)0x008061FC, (uint32_t *)0x00806010, 127 | (uint32_t *)0x00806014, (uint32_t *)0x00806018}; 128 | #else // samd21 129 | uint32_t *id_addresses[4] = {(uint32_t *)0x0080A00C, (uint32_t *)0x0080A040, 130 | (uint32_t *)0x0080A044, (uint32_t *)0x0080A048}; 131 | #endif 132 | 133 | uint32_t *serial_32 = (uint32_t *)serial_id; 134 | 135 | for (int i = 0; i < 4; i++) { 136 | *serial_32++ = __builtin_bswap32(*id_addresses[i]); 137 | } 138 | 139 | return 16; 140 | } 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /src/arduino/msc/Adafruit_USBD_MSC.h: -------------------------------------------------------------------------------- 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 | #ifndef ADAFRUIT_USBD_MSC_H_ 26 | #define ADAFRUIT_USBD_MSC_H_ 27 | 28 | #include "arduino/Adafruit_USBD_Device.h" 29 | 30 | class Adafruit_USBD_MSC : public Adafruit_USBD_Interface { 31 | public: 32 | typedef int32_t (*read_callback_t)(uint32_t lba, void *buffer, 33 | uint32_t bufsize); 34 | typedef int32_t (*write_callback_t)(uint32_t lba, uint8_t *buffer, 35 | uint32_t bufsize); 36 | typedef void (*flush_callback_t)(void); 37 | typedef bool (*ready_callback_t)(void); 38 | typedef bool (*writable_callback_t)(void); 39 | typedef bool (*start_stop_callback_t)(uint8_t power_condition, bool start, 40 | bool load_eject); 41 | 42 | Adafruit_USBD_MSC(void); 43 | 44 | bool begin(void); 45 | 46 | void setMaxLun(uint8_t maxlun); 47 | uint8_t getMaxLun(void); 48 | 49 | //------------- Multiple LUN API -------------// 50 | void setID(uint8_t lun, const char *vendor_id, const char *product_id, 51 | const char *product_rev); 52 | void setCapacity(uint8_t lun, uint32_t block_count, uint16_t block_size); 53 | void setUnitReady(uint8_t lun, bool ready); 54 | void setReadWriteCallback(uint8_t lun, read_callback_t rd_cb, 55 | write_callback_t wr_cb, flush_callback_t fl_cb); 56 | void setReadyCallback(uint8_t lun, ready_callback_t cb); 57 | void setWritableCallback(uint8_t lun, writable_callback_t cb); 58 | void setStartStopCallback(uint8_t lun, start_stop_callback_t cb); 59 | 60 | //------------- Single LUN API -------------// 61 | void setID(const char *vendor_id, const char *product_id, 62 | const char *product_rev) { 63 | setID(0, vendor_id, product_id, product_rev); 64 | } 65 | 66 | void setCapacity(uint32_t block_count, uint16_t block_size) { 67 | setCapacity(0, block_count, block_size); 68 | } 69 | 70 | void setUnitReady(bool ready) { setUnitReady(0, ready); } 71 | 72 | void setReadWriteCallback(read_callback_t rd_cb, write_callback_t wr_cb, 73 | flush_callback_t fl_cb) { 74 | setReadWriteCallback(0, rd_cb, wr_cb, fl_cb); 75 | } 76 | 77 | void setReadyCallback(ready_callback_t cb) { setReadyCallback(0, cb); } 78 | void setWritableCallback(writable_callback_t cb) { 79 | setWritableCallback(0, cb); 80 | } 81 | void setStartStopCallback(start_stop_callback_t cb) { 82 | setStartStopCallback(0, cb); 83 | } 84 | 85 | // from Adafruit_USBD_Interface 86 | virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated, 87 | uint8_t *buf, uint16_t bufsize); 88 | 89 | private: 90 | enum { MAX_LUN = 2 }; // TODO make it configurable 91 | struct { 92 | read_callback_t rd_cb; 93 | write_callback_t wr_cb; 94 | flush_callback_t fl_cb; 95 | ready_callback_t ready_cb; 96 | writable_callback_t writable_cb; 97 | start_stop_callback_t start_stop_cb; 98 | 99 | const char *_inquiry_vid; 100 | const char *_inquiry_pid; 101 | const char *_inquiry_rev; 102 | 103 | uint32_t block_count; 104 | uint16_t block_size; 105 | bool unit_ready; 106 | 107 | } _lun_info[MAX_LUN]; 108 | 109 | uint8_t _maxlun; 110 | 111 | // Make all tinyusb callback friend to access private data 112 | friend void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], 113 | uint8_t product_id[16], 114 | uint8_t product_rev[4]); 115 | friend bool tud_msc_test_unit_ready_cb(uint8_t lun); 116 | friend void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, 117 | uint16_t *block_size); 118 | friend int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, 119 | void *buffer, uint32_t bufsize); 120 | friend int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, 121 | uint8_t *buffer, uint32_t bufsize); 122 | friend void tud_msc_write10_complete_cb(uint8_t lun); 123 | friend bool tud_msc_is_writable_cb(uint8_t lun); 124 | friend bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, 125 | bool start, bool load_eject); 126 | }; 127 | 128 | #endif /* ADAFRUIT_USBD_MSC_H_ */ 129 | -------------------------------------------------------------------------------- /src/arduino/midi/Adafruit_USBD_MIDI.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 hathach 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_TUD_MIDI 28 | 29 | #include "Adafruit_USBD_MIDI.h" 30 | 31 | //--------------------------------------------------------------------+ 32 | // MACRO TYPEDEF CONSTANT ENUM DECLARATION 33 | //--------------------------------------------------------------------+ 34 | #define EPSIZE 64 35 | 36 | // TODO multiple instances 37 | static Adafruit_USBD_MIDI *_midi_dev = NULL; 38 | 39 | Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(uint8_t n_cables) { 40 | _n_cables = n_cables; 41 | memset(_cable_name_strid, 0, sizeof(_cable_name_strid)); 42 | } 43 | 44 | void Adafruit_USBD_MIDI::setCables(uint8_t n_cables) { _n_cables = n_cables; } 45 | 46 | bool Adafruit_USBD_MIDI::setCableName(uint8_t cable_id, const char *str) { 47 | if (cable_id == 0 || cable_id > sizeof(_cable_name_strid)) { 48 | return false; 49 | } 50 | 51 | uint8_t strid = TinyUSBDevice.addStringDescriptor(str); 52 | _cable_name_strid[cable_id - 1] = strid; 53 | 54 | return strid > 0; 55 | } 56 | 57 | bool Adafruit_USBD_MIDI::begin(void) { 58 | if (!TinyUSBDevice.addInterface(*this)) { 59 | return false; 60 | } 61 | 62 | _midi_dev = this; 63 | return true; 64 | } 65 | 66 | uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum_deprecated, 67 | uint8_t *buf, 68 | uint16_t bufsize) { 69 | (void)itfnum_deprecated; 70 | 71 | uint16_t const desc_len = TUD_MIDI_DESC_HEAD_LEN + 72 | TUD_MIDI_DESC_JACK_LEN * _n_cables + 73 | 2 * TUD_MIDI_DESC_EP_LEN(_n_cables); 74 | 75 | // null buffer is used to get the length of descriptor only 76 | if (!buf) { 77 | return desc_len; 78 | } 79 | 80 | if (bufsize < desc_len) { 81 | return 0; 82 | } 83 | 84 | uint8_t itfnum = TinyUSBDevice.allocInterface(2); 85 | uint8_t ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN); 86 | uint8_t ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT); 87 | 88 | uint16_t len = 0; 89 | 90 | // Header 91 | { 92 | uint8_t desc[] = {TUD_MIDI_DESC_HEAD(itfnum, _strid, _n_cables)}; 93 | memcpy(buf + len, desc, sizeof(desc)); 94 | len += sizeof(desc); 95 | } 96 | 97 | // Jack 98 | for (uint8_t i = 1; i <= _n_cables; i++) { 99 | uint8_t jack[] = {TUD_MIDI_DESC_JACK_DESC(i, _cable_name_strid[i - 1])}; 100 | memcpy(buf + len, jack, sizeof(jack)); 101 | len += sizeof(jack); 102 | } 103 | 104 | // Endpoint OUT + jack mapping 105 | { 106 | uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_out, EPSIZE, _n_cables)}; 107 | memcpy(buf + len, desc, sizeof(desc)); 108 | len += sizeof(desc); 109 | } 110 | 111 | for (uint8_t i = 1; i <= _n_cables; i++) { 112 | uint8_t jack[] = {TUD_MIDI_JACKID_IN_EMB(i)}; 113 | memcpy(buf + len, jack, sizeof(jack)); 114 | len += sizeof(jack); 115 | } 116 | 117 | // Endpoint IN + jack mapping 118 | { 119 | uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_in, EPSIZE, _n_cables)}; 120 | memcpy(buf + len, desc, sizeof(desc)); 121 | len += sizeof(desc); 122 | } 123 | 124 | for (uint8_t i = 1; i <= _n_cables; i++) { 125 | uint8_t jack[] = {TUD_MIDI_JACKID_OUT_EMB(i)}; 126 | memcpy(buf + len, jack, sizeof(jack)); 127 | len += sizeof(jack); 128 | } 129 | 130 | if (len != desc_len) { 131 | // TODO should throw an error message 132 | return 0; 133 | } 134 | 135 | return desc_len; 136 | } 137 | 138 | int Adafruit_USBD_MIDI::read(void) { 139 | uint8_t ch; 140 | return tud_midi_stream_read(&ch, 1) ? (int)ch : (-1); 141 | } 142 | 143 | size_t Adafruit_USBD_MIDI::write(uint8_t b) { 144 | return tud_midi_stream_write(0, &b, 1); 145 | } 146 | 147 | int Adafruit_USBD_MIDI::available(void) { return tud_midi_available(); } 148 | 149 | int Adafruit_USBD_MIDI::peek(void) { 150 | // MIDI Library does not use peek 151 | return -1; 152 | } 153 | 154 | void Adafruit_USBD_MIDI::flush(void) { 155 | // MIDI Library does not use flush 156 | } 157 | 158 | bool Adafruit_USBD_MIDI::writePacket(const uint8_t packet[4]) { 159 | return tud_midi_packet_write(packet); 160 | } 161 | 162 | bool Adafruit_USBD_MIDI::readPacket(uint8_t packet[4]) { 163 | return tud_midi_packet_read(packet); 164 | } 165 | 166 | #endif // CFG_TUD_ENABLED 167 | -------------------------------------------------------------------------------- /src/arduino/ports/rp2040/tusb_config_rp2040.h: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2018, hathach for Adafruit 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 8 | deal in the Software without restriction, including without limitation the 9 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 | sell 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 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | IN THE SOFTWARE. 23 | */ 24 | 25 | #ifndef _TUSB_CONFIG_RP2040_H_ 26 | #define _TUSB_CONFIG_RP2040_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | //-------------------------------------------------------------------- 33 | // COMMON CONFIGURATION 34 | //-------------------------------------------------------------------- 35 | 36 | #ifdef USE_TINYUSB_HOST 37 | // native as host 38 | #define CFG_TUD_ENABLED 0 39 | #define CFG_TUH_ENABLED 1 40 | #define CFG_TUH_RPI_PIO_USB 0 41 | 42 | #else 43 | // native as device 44 | #define CFG_TUD_ENABLED 1 45 | 46 | #if __has_include("pio_usb.h") 47 | // Enable host stack with pio-usb if Pico-PIO-USB library is available 48 | #define CFG_TUH_ENABLED 1 49 | #define CFG_TUH_RPI_PIO_USB 1 50 | 51 | #else 52 | // Otherwise enable host controller with MAX3421E 53 | #define CFG_TUH_ENABLED 1 54 | #define CFG_TUH_MAX3421 1 55 | 56 | #endif // pio_usb.h 57 | #endif // USE_TINYUSB_HOST 58 | 59 | #ifndef CFG_TUSB_MCU 60 | #define CFG_TUSB_MCU OPT_MCU_RP2040 61 | #endif 62 | 63 | #ifndef CFG_TUSB_OS 64 | #define CFG_TUSB_OS OPT_OS_PICO 65 | #endif 66 | 67 | #ifndef CFG_TUSB_DEBUG 68 | #define CFG_TUSB_DEBUG 0 69 | #endif 70 | 71 | // For selectively disable device log (when > CFG_TUSB_DEBUG) 72 | // #define CFG_TUD_LOG_LEVEL 3 73 | // #define CFG_TUH_LOG_LEVEL 3 74 | 75 | #define CFG_TUSB_MEM_SECTION 76 | #define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) 77 | 78 | //-------------------------------------------------------------------- 79 | // Device Configuration 80 | //-------------------------------------------------------------------- 81 | 82 | #define CFG_TUD_ENDOINT0_SIZE 64 83 | 84 | #define CFG_TUD_CDC 1 85 | #define CFG_TUD_MSC 1 86 | #define CFG_TUD_HID 2 87 | #define CFG_TUD_MIDI 1 88 | #define CFG_TUD_VENDOR 1 89 | #define CFG_TUD_VIDEO 1 // number of video control interfaces 90 | #define CFG_TUD_VIDEO_STREAMING 1 // number of video streaming interfaces 91 | 92 | // video streaming endpoint buffer size 93 | #define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256 94 | 95 | // CDC FIFO size of TX and RX 96 | #define CFG_TUD_CDC_RX_BUFSIZE 256 97 | #define CFG_TUD_CDC_TX_BUFSIZE 256 98 | 99 | // MSC Buffer size of Device Mass storage 100 | #define CFG_TUD_MSC_EP_BUFSIZE 512 101 | 102 | // HID buffer size Should be sufficient to hold ID (if any) + Data 103 | #define CFG_TUD_HID_EP_BUFSIZE 64 104 | 105 | // MIDI FIFO size of TX and RX 106 | #define CFG_TUD_MIDI_RX_BUFSIZE 128 107 | #define CFG_TUD_MIDI_TX_BUFSIZE 128 108 | 109 | // Vendor FIFO size of TX and RX 110 | #define CFG_TUD_VENDOR_RX_BUFSIZE 64 111 | #define CFG_TUD_VENDOR_TX_BUFSIZE 64 112 | 113 | //-------------------------------------------------------------------- 114 | // Host Configuration 115 | //-------------------------------------------------------------------- 116 | 117 | // Size of buffer to hold descriptors and other data used for enumeration 118 | #define CFG_TUH_ENUMERATION_BUFSIZE 256 119 | 120 | // Number of hub devices 121 | #define CFG_TUH_HUB 1 122 | 123 | // max device support (excluding hub device): 1 hub typically has 4 ports 124 | #define CFG_TUH_DEVICE_MAX (3 * CFG_TUH_HUB + 1) 125 | 126 | // Enable tuh_edpt_xfer() API 127 | // #define CFG_TUH_API_EDPT_XFER 1 128 | 129 | // Number of mass storage 130 | #define CFG_TUH_MSC 1 131 | 132 | // Number of HIDs 133 | // typical keyboard + mouse device can have 3,4 HID interfaces 134 | #define CFG_TUH_HID (3 * CFG_TUH_DEVICE_MAX) 135 | 136 | // Number of CDC interfaces 137 | // FTDI and CP210x are not part of CDC class, only to re-use CDC driver API 138 | #define CFG_TUH_CDC 1 139 | #define CFG_TUH_CDC_FTDI 1 140 | #define CFG_TUH_CDC_CP210X 1 141 | #define CFG_TUH_CDC_CH34X 1 142 | 143 | // RX & TX fifo size 144 | #define CFG_TUH_CDC_RX_BUFSIZE 128 145 | #define CFG_TUH_CDC_TX_BUFSIZE 128 146 | 147 | // Set Line Control state on enumeration/mounted: 148 | // DTR ( bit 0), RTS (bit 1) 149 | #define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03 150 | 151 | // Set Line Coding on enumeration/mounted, value for cdc_line_coding_t 152 | // bit rate = 115200, 1 stop bit, no parity, 8 bit data width 153 | // This need Pico-PIO-USB at least 0.5.1 154 | #define CFG_TUH_CDC_LINE_CODING_ON_ENUM \ 155 | { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } 156 | 157 | #ifdef __cplusplus 158 | } 159 | #endif 160 | 161 | #endif /* _TUSB_CONFIG_RP2040_H_ */ 162 | --------------------------------------------------------------------------------