Skip to content

Commit b263d36

Browse files
committed
add Adafruit_TinyUSB_Arduino to libraries
1 parent 1f0da83 commit b263d36

File tree

129 files changed

+33635
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+33635
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Ha Thach for Adafruit Industries
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
13+
all 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
21+
THE SOFTWARE.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Adafruit TinyUSB Library for Arduino
2+
3+
[![Build Status](https://github.com/adafruit/Adafruit_TinyUSB_Arduino/workflows/Build/badge.svg)](https://github.com/adafruit/Adafruit_TinyUSB_Arduino/actions) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
4+
5+
This library is a Arduino-friendly version of [TinyUSB](https://github.com/hathach/tinyusb) stack. It is designed with structure and APIs that are easily integrated to existing or new Arduino Core. Supported platform including:
6+
7+
- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
8+
- [Adafruit/ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd) selectable via menu`Tools->USB Stack->TinyUSB`
9+
- [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico) selectable via menu `Tools->USB Stack->Adafruit TinyUSB`
10+
11+
Current class drivers supported are
12+
13+
- Communication (CDC): which is used to implement `Serial` monitor
14+
- Human Interface Device (HID): Generic (In & Out), Keyboard, Mouse, Gamepad etc ...
15+
- Mass Storage Class (MSC): with multiple LUNs
16+
- Musical Instrument Digital Interface (MIDI)
17+
- WebUSB with vendor specific class
18+
19+
For supported ArduinoCore, to use this library, you only need to have `<Adafruit_TinyUSB.h>` in your sketch. If your ArduinoCore does not support TinyUSB library yet, it is rather simple to port.
20+
21+
## Class Driver API
22+
23+
More document to write ...
24+
25+
## Porting Guide
26+
27+
It is rather easy if you want to integrate TinyUSB lib to your ArduinoCore.
28+
29+
### ArduinoCore Changes
30+
31+
1. Add this repo as submodule (or have local copy) at your ArduioCore/libraries/Adafruit_TinyUSB_Arduino (much like SPI).
32+
2. Since Serial as CDC is considered as part of the core, we need to have `#include "Adafruit_USBD_CDC.h"` within your `Arduino.h`. For this to work, your `platform.txt` include path need to have `"-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"`.
33+
3. In your `main.cpp` before setup() invoke the `TinyUSB_Device_Init(rhport)`. This will initialize usb device hardware and tinyusb stack and also include Serial as an instance of CDC class.
34+
4. `TinyUSB_Device_Task()` must be called whenever there is new USB event. Depending on your core and MCU with or without RTOS. There are many ways to run the task. For example:
35+
- Use USB IRQn to set flag then invoke function later on after exiting IRQ.
36+
- Just invoke function after the loop(), within yield(), and delay()
37+
5. `TinyUSB_Device_FlushCDC()` should also be called often to send out Serial data as well.
38+
6. Note: For low power platform that make use of WFI()/WFE(), extra care is required before mcu go into low power mode. Check out my PR to circuipython for reference https://github.com/adafruit/circuitpython/pull/2956
39+
40+
### Library Changes
41+
42+
In addition to core changes, library need to be ported to your platform. Don't worry, tinyusb stack has already done most of heavy-lifting. You only need to write a few APIs
43+
44+
1. `TinyUSB_Port_InitDevice()` hardware specific (clock, phy) to enable usb hardware then call tud_init(). This API is called as part of TinyUSB_Device_Init() invocation.
45+
2. `TinyUSB_Port_EnterDFU()` which is called when device need to enter DFU mode, usually by touch1200 feature
46+
3. `TinyUSB_Port_GetSerialNumber()` which is called to get unique MCU Serial ID to used as Serial string descriptor.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Adafruit TinyUSB Arduino Library Changelog
2+
3+
## 1.1.0 - 2021.06.21
4+
5+
- Add support for multiple CDC ports (need to modify tusb_config.h)
6+
- fix #86 when calling midi API when device is not fully enumerated
7+
- fix Serial connection issue with nrf52 on windows
8+
- Update device driver of rp2040 to match tinyusb upstream
9+
- Added feather rp2040 to ci build (skipped the external flash example for now due to SdFat compilation error)
10+
- Add optional debug log for stack with Serial1 on samd core (will expand later to other core).
11+
- lost more bug fixes to the stack
12+
13+
## 1.0.3 - 2021.05.26
14+
15+
- Update tinyusb stack to latest
16+
- Added HID Gamepad example with Dhat support
17+
- Fix warnings with -Wall -Wextra
18+
- Fix warnings with cast function type for nrf
19+
20+
## 1.0.1 - 2021.05.21
21+
22+
Warn user to select TinyUSB via menu on ports where it is optional e.g SAMD and RP2040
23+
24+
## 1.0.0 - 2021.05.19
25+
26+
Rework to work as independent libraries, sources from https://github.com/adafruit/Adafruit_TinyUSB_ArduinoCore is now integrated as part of this libraries. Require
27+
- Adafruit SAMD core with version at least 1.7.0
28+
- Adafruit nRF52 core with version at least 0.22.0
29+
- Support [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico) version released after https://github.com/earlephilhower/arduino-pico/pull/127
30+
31+
## 0.9.0 - 2020.04.23
32+
33+
- Fixed mouseButtonRelease() error
34+
- Supported multiple cables for USB MIDI (requires BSP nRF52 0.20.0 and SAMD 1.5.12 )
35+
- Added consumer control support to HID/hid_composite example
36+
- Added Adafruit_USBD_HID send report helper: sendReport8(), sendReport16(), sendReport32()
37+
38+
**Minor Breaking Changes**
39+
- Removed trailing comma in hid report descriptor, this is required to use with BSP nRF52 0.20.0 and SAMD 1.5.12 e.g
40+
41+
from
42+
43+
```
44+
uint8_t const desc_hid_report[] =
45+
{
46+
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD), ),
47+
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE ), ),
48+
};
49+
```
50+
to
51+
52+
```
53+
uint8_t const desc_hid_report[] =
54+
{
55+
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) /*, no more trailing comma */ ),
56+
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE ) /*, no more trailing comma */ ),
57+
};
58+
```
59+
60+
## 0.8.2 - 2020.04.06
61+
62+
- Removed package-lock.json in hid generic inout example due to security warning from github
63+
64+
## 0.8.1 - 2020.01.08
65+
66+
- More CI migrating work, no function changes
67+
68+
## 0.8.0 - 2019.12.30
69+
70+
- Correct USB BCD version to 2.1 for webUSB
71+
- Migrate CI from travis to github actions
72+
73+
## 0.7.1 - 2019.10.12
74+
75+
- Fixed MIDI build failed since it is under development
76+
77+
## 0.7.0 - 2019.10.09
78+
79+
- Added MIDI virtual wires/plugs
80+
81+
## 0.6.0 - 2019.08.05
82+
83+
- Added webUSB support with 2 example: webusb-serial, webusb-rgb
84+
- Alligned mouse examples, added newer hid in/out example from main repo, added new composite example for ramdisk and hid in/out. PR #19 thanks to @PTS93
85+
86+
## 0.5.0 - 2019.07.17
87+
88+
- Added travis build
89+
- Fixed msc setID
90+
- Added itfnum to internal API getDescriptor()
91+
- Added MIDI support
92+
- Added midi_test example
93+
- Added pizza box dj example for cplayground express
94+
- Mass Storage
95+
- Added msc_sdfat, msc dual lun (external flash + sd card) example
96+
- Updated msc example to use new SPIFlash 3.x API
97+
- Update msc example to print root contents
98+
- HID
99+
- Added hid_mouse, hid_keyboard
100+
- Added hid_composite_joy_featherwing
101+
- Added Composite: mouse_ramdisk, mouse_external_flash example
102+
103+
## 0.0.1 Initial Release
104+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
This example demonstrates the use of multiple USB CDC/ACM "Virtual
3+
Serial" ports, using Ha Thach's TinyUSB library, and the port of
4+
that library to the Arduino environment
5+
6+
https://github.com/hathach/tinyusb
7+
https://github.com/adafruit/Adafruit_TinyUSB_Arduino
8+
9+
Written by Bill Westfield (aka WestfW), June 2021.
10+
Copyright 2021 by Bill Westfield
11+
MIT license, check LICENSE for more information
12+
13+
The example creates three virtual serial ports. Text entered on
14+
any of the ports will be echoed to the all ports.
15+
16+
The max number of CDC ports (CFG_TUD_CDC) has to be changed to at
17+
least 2, changed in the core tusb_config.h file.
18+
*/
19+
20+
#include <Adafruit_TinyUSB.h>
21+
22+
#define LED LED_BUILTIN
23+
24+
// Create extra USB Serial Ports. "Serial" is already created.
25+
Adafruit_USBD_CDC USBSer1;
26+
27+
void setup() {
28+
pinMode(LED, OUTPUT);
29+
30+
// start up all of the USB Vitual ports, and wait for them to enumerate.
31+
Serial.begin(115200);
32+
USBSer1.begin(115200);
33+
34+
while (!Serial || !USBSer1) {
35+
if (Serial) {
36+
Serial.println("Waiting for other USB ports");
37+
}
38+
39+
if (USBSer1) {
40+
USBSer1.println("Waiting for other USB ports");
41+
}
42+
43+
delay(1000);
44+
}
45+
46+
Serial.print("You are port 0\n\r\n0> ");
47+
USBSer1.print("You are port 1\n\r\n1> ");
48+
}
49+
50+
int LEDstate = 0;
51+
52+
void loop() {
53+
int ch;
54+
55+
ch = Serial.read();
56+
if (ch > 0) {
57+
printAll(ch);
58+
}
59+
60+
ch = USBSer1.read();
61+
if (ch > 0) {
62+
printAll(ch);
63+
}
64+
65+
if (delay_without_delaying(500)) {
66+
LEDstate = !LEDstate;
67+
digitalWrite(LED, LEDstate);
68+
}
69+
}
70+
71+
// print to all CDC ports
72+
void printAll(int ch) {
73+
// always lower case
74+
Serial.write(tolower(ch));
75+
76+
// always upper case
77+
USBSer1.write(toupper(ch));
78+
}
79+
80+
// Helper: non-blocking "delay" alternative.
81+
boolean delay_without_delaying(unsigned long time) {
82+
// return false if we're still "delaying", true if time ms has passed.
83+
// this should look a lot like "blink without delay"
84+
static unsigned long previousmillis = 0;
85+
unsigned long currentmillis = millis();
86+
if (currentmillis - previousmillis >= time) {
87+
previousmillis = currentmillis;
88+
return true;
89+
}
90+
return false;
91+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
#include "Adafruit_TinyUSB.h"
13+
14+
/* This sketch demonstrates USB CDC Serial can be dropped by simply
15+
* call Serial.end() within setup(). This must be called before any
16+
* other USB interfaces (MSC / HID) begin to have a clean configuration
17+
*
18+
* Note: this will cause device to loose the touch1200 and require
19+
* user manual interaction to put device into bootloader/DFU mode.
20+
*/
21+
22+
int led = LED_BUILTIN;
23+
24+
void setup()
25+
{
26+
Serial.end();
27+
pinMode(led, OUTPUT);
28+
}
29+
30+
void loop()
31+
{
32+
digitalWrite(led, HIGH);
33+
delay(1000);
34+
digitalWrite(led, LOW);
35+
delay(1000);
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check the example host application in `../../HID/hid_generic_inout`

0 commit comments

Comments
 (0)