Skip to content

Commit a5f8ea0

Browse files
authored
Merge pull request #100 from adafruit/add-esp32s2
Add support for esp32s2
2 parents 39d6187 + 73e8dfd commit a5f8ea0

21 files changed

+708
-86
lines changed

examples/CDC/cdc_multi/cdc_multi.ino

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,37 @@
1414
any of the ports will be echoed to the all ports.
1515
1616
The max number of CDC ports (CFG_TUD_CDC) has to be changed to at
17-
least 3, changed in the core tusb_config.h file.
17+
least 2, changed in the core tusb_config.h file.
1818
*/
1919

2020
#include <Adafruit_TinyUSB.h>
2121

2222
#define LED LED_BUILTIN
2323

24-
/*
25-
Create extra USB Serial Ports. "Serial" is already created.
26-
*/
24+
// Create extra USB Serial Ports. "Serial" is already created.
2725
Adafruit_USBD_CDC USBSer1;
28-
Adafruit_USBD_CDC USBSer2;
2926

3027
void setup() {
3128
pinMode(LED, OUTPUT);
3229

3330
// start up all of the USB Vitual ports, and wait for them to enumerate.
3431
Serial.begin(115200);
3532
USBSer1.begin(115200);
36-
USBSer2.begin(115200);
3733

38-
while (!Serial || !USBSer1 || !USBSer2) {
34+
while (!Serial || !USBSer1) {
3935
if (Serial) {
4036
Serial.println("Waiting for other USB ports");
4137
}
38+
4239
if (USBSer1) {
4340
USBSer1.println("Waiting for other USB ports");
4441
}
45-
if (USBSer2) {
46-
USBSer2.println("Waiting for other USB ports");
47-
}
42+
4843
delay(1000);
4944
}
5045

5146
Serial.print("You are port 0\n\r\n0> ");
5247
USBSer1.print("You are port 1\n\r\n1> ");
53-
USBSer2.print("You are port 2\n\r\n2> ");
5448
}
5549

5650
int LEDstate = 0;
@@ -68,11 +62,6 @@ void loop() {
6862
printAll(ch);
6963
}
7064

71-
ch = USBSer2.read();
72-
if (ch > 0) {
73-
printAll(ch);
74-
}
75-
7665
if (delay_without_delaying(500)) {
7766
LEDstate = !LEDstate;
7867
digitalWrite(LED, LEDstate);
@@ -81,14 +70,11 @@ void loop() {
8170

8271
// print to all CDC ports
8372
void printAll(int ch) {
84-
// print as it is
85-
Serial.write(ch);
86-
8773
// always lower case
88-
USBSer1.write(tolower(ch));
74+
Serial.write(tolower(ch));
8975

9076
// always upper case
91-
USBSer2.write(toupper(ch));
77+
USBSer1.write(toupper(ch));
9278
}
9379

9480
// Helper: non-blocking "delay" alternative.

examples/Composite/hid_generic_inout_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/Composite/mouse_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/MassStorage/msc_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/MassStorage/msc_ramdisk_dual/ramdisk.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
// LUN 0
3030
//--------------------------------------------------------------------+
3131
#define README0_CONTENTS \
32-
"LUN0: This is tinyusb's MassStorage Class demo.\r\n\r\n\
33-
If you find any bugs or get any questions, feel free to file an\r\n\
34-
issue at github.com/hathach/tinyusb"
32+
"LUN0: This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3533

3634
uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3735
//------------- Block0: Boot Sector -------------//
@@ -119,9 +117,7 @@ uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
119117
// LUN 1
120118
//--------------------------------------------------------------------+
121119
#define README1_CONTENTS \
122-
"LUN1: This is tinyusb's MassStorage Class demo.\r\n\r\n\
123-
If you find any bugs or get any questions, feel free to file an\r\n\
124-
issue at github.com/hathach/tinyusb"
120+
"LUN1: This is TinyUSB MassStorage device demo for Arduino on RAM disk."
125121

126122
uint8_t msc_disk1[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
127123
//------------- Block0: Boot Sector -------------//

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit TinyUSB Library
2-
version=1.1.0
2+
version=1.2.0
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=TinyUSB library for Arduino

src/Adafruit_TinyUSB.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
#ifndef ADAFRUIT_TINYUSB_H_
2626
#define ADAFRUIT_TINYUSB_H_
2727

28-
// Give warning for Core that must select TinyUSB via menu
29-
#if (defined ARDUINO_ARCH_SAMD || defined ARDUINO_ARCH_RP2040) && !defined USE_TINYUSB
30-
#error TinyUSB is not selected, please select it in Tools->Menu->USB Stack
28+
// Error message for Core that must select TinyUSB via menu
29+
#if !defined(USE_TINYUSB) && ( defined(ARDUINO_ARCH_SAMD) || \
30+
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)) || \
31+
defined(ARDUINO_ARCH_ESP32) )
32+
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
3133
#endif
3234

3335
#include "tusb_option.h"

src/arduino/Adafruit_TinyUSB_API.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ void TinyUSB_Device_Task(void) {
4646
}
4747
#endif
4848

49-
// TODO should use getInstanceCount() API (when closed to BSP release cycle)
50-
// from Adafruit_USBD_CDC.cpp
51-
extern uint8_t _cdc_instance_count;
52-
5349
void TinyUSB_Device_FlushCDC(void) {
54-
for (uint8_t instance = 0; instance < _cdc_instance_count; instance++) {
50+
uint8_t const cdc_instance = Adafruit_USBD_CDC::getInstanceCount();
51+
for (uint8_t instance = 0; instance < cdc_instance; instance++) {
5552
tud_cdc_n_write_flush(instance);
5653
}
5754
}

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
#define EPOUT 0x00
3838
#define EPIN 0x80
3939

40-
uint8_t _cdc_instance_count = 0;
40+
// SerialTinyUSB can be macro expanding to "Serial" on supported cores
41+
Adafruit_USBD_CDC SerialTinyUSB;
4142

42-
Adafruit_USBD_CDC Serial;
43+
//------------- Static member -------------//
44+
uint8_t Adafruit_USBD_CDC::_instance_count = 0;
4345

44-
Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) {
45-
_begun = false;
46-
_itf = 0;
47-
}
46+
uint8_t Adafruit_USBD_CDC::getInstanceCount(void) { return _instance_count; }
47+
48+
Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) { _instance = INVALID_INSTANCE; }
4849

4950
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
5051
uint16_t bufsize) {
@@ -65,17 +66,17 @@ uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
6566
void Adafruit_USBD_CDC::begin(uint32_t baud) {
6667
(void)baud;
6768

68-
if (_begun) {
69+
// already called begin()
70+
if (isValid()) {
6971
return;
7072
}
7173

7274
// too many instances
73-
if (!(_cdc_instance_count < CFG_TUD_CDC)) {
75+
if (!(_instance_count < CFG_TUD_CDC)) {
7476
return;
7577
}
7678

77-
_begun = true;
78-
_itf = _cdc_instance_count++;
79+
_instance = _instance_count++;
7980
this->setStringDescriptor("TinyUSB Serial");
8081
USBDevice.addInterface(*this);
8182
}
@@ -86,69 +87,69 @@ void Adafruit_USBD_CDC::begin(uint32_t baud, uint8_t config) {
8687
}
8788

8889
void Adafruit_USBD_CDC::end(void) {
89-
// Resset configuration descriptor without Serial as CDC
90+
// Reset configuration descriptor without Serial as CDC
9091
USBDevice.clearConfiguration();
91-
_cdc_instance_count = 0;
92+
_instance_count = 0;
9293
}
9394

9495
uint32_t Adafruit_USBD_CDC::baud(void) {
95-
if (!_begun) {
96+
if (!isValid()) {
9697
return 0;
9798
}
9899

99100
cdc_line_coding_t coding;
100-
tud_cdc_n_get_line_coding(_itf, &coding);
101+
tud_cdc_n_get_line_coding(_instance, &coding);
101102

102103
return coding.bit_rate;
103104
}
104105

105106
uint8_t Adafruit_USBD_CDC::stopbits(void) {
106-
if (!_begun) {
107+
if (!isValid()) {
107108
return 0;
108109
}
109110

110111
cdc_line_coding_t coding;
111-
tud_cdc_n_get_line_coding(_itf, &coding);
112+
tud_cdc_n_get_line_coding(_instance, &coding);
112113

113114
return coding.stop_bits;
114115
}
115116

116117
uint8_t Adafruit_USBD_CDC::paritytype(void) {
117-
if (!_begun) {
118+
if (!isValid()) {
118119
return 0;
119120
}
120121

121122
cdc_line_coding_t coding;
122-
tud_cdc_n_get_line_coding(_itf, &coding);
123+
tud_cdc_n_get_line_coding(_instance, &coding);
123124

124125
return coding.parity;
125126
}
126127

127128
uint8_t Adafruit_USBD_CDC::numbits(void) {
128-
if (!_begun) {
129+
if (!isValid()) {
129130
return 0;
130131
}
131132

132133
cdc_line_coding_t coding;
133-
tud_cdc_n_get_line_coding(_itf, &coding);
134+
tud_cdc_n_get_line_coding(_instance, &coding);
134135

135136
return coding.data_bits;
136137
}
137138

138139
int Adafruit_USBD_CDC::dtr(void) {
139-
if (!_begun) {
140+
if (!isValid()) {
140141
return 0;
141142
}
142143

143-
return tud_cdc_n_connected(_itf);
144+
return tud_cdc_n_connected(_instance);
144145
}
145146

146147
Adafruit_USBD_CDC::operator bool() {
147-
if (!_begun) {
148+
if (!isValid()) {
148149
return false;
149150
}
150151

151-
bool ret = tud_cdc_n_connected(_itf);
152+
bool ret = tud_cdc_n_connected(_instance);
152153

153154
// Add an yield to run usb background in case sketch block wait as follows
154155
// while( !Serial ) {}
@@ -159,11 +160,11 @@ Adafruit_USBD_CDC::operator bool() {
159160
}
160161

161162
int Adafruit_USBD_CDC::available(void) {
162-
if (!_begun) {
163+
if (!isValid()) {
163164
return 0;
164165
}
165166

166-
uint32_t count = tud_cdc_n_available(_itf);
167+
uint32_t count = tud_cdc_n_available(_instance);
167168

168169
// Add an yield to run usb background in case sketch block wait as follows
169170
// while( !Serial.available() ) {}
@@ -175,39 +176,39 @@ int Adafruit_USBD_CDC::available(void) {
175176
}
176177

177178
int Adafruit_USBD_CDC::peek(void) {
178-
if (!_begun) {
179+
if (!isValid()) {
179180
return -1;
180181
}
181182

182183
uint8_t ch;
183-
return tud_cdc_n_peek(_itf, &ch) ? (int)ch : -1;
184+
return tud_cdc_n_peek(_instance, &ch) ? (int)ch : -1;
184185
}
185186

186187
int Adafruit_USBD_CDC::read(void) {
187-
if (!_begun) {
188+
if (!isValid()) {
188189
return -1;
189190
}
190-
return (int)tud_cdc_n_read_char(_itf);
191+
return (int)tud_cdc_n_read_char(_instance);
191192
}
192193

193194
void Adafruit_USBD_CDC::flush(void) {
194-
if (!_begun) {
195+
if (!isValid()) {
195196
return;
196197
}
197198

198-
tud_cdc_n_write_flush(_itf);
199+
tud_cdc_n_write_flush(_instance);
199200
}
200201

201202
size_t Adafruit_USBD_CDC::write(uint8_t ch) { return write(&ch, 1); }
202203

203204
size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
204-
if (!_begun) {
205+
if (!isValid()) {
205206
return 0;
206207
}
207208

208209
size_t remain = size;
209-
while (remain && tud_cdc_n_connected(_itf)) {
210-
size_t wrcount = tud_cdc_n_write(_itf, buffer, remain);
210+
while (remain && tud_cdc_n_connected(_instance)) {
211+
size_t wrcount = tud_cdc_n_write(_instance, buffer, remain);
211212
remain -= wrcount;
212213
buffer += wrcount;
213214

@@ -221,10 +222,10 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
221222
}
222223

223224
int Adafruit_USBD_CDC::availableForWrite(void) {
224-
if (!_begun) {
225+
if (!isValid()) {
225226
return 0;
226227
}
227-
return tud_cdc_n_write_available(_itf);
228+
return tud_cdc_n_write_available(_instance);
228229
}
229230

230231
extern "C" {

0 commit comments

Comments
 (0)