Skip to content

Commit 611608f

Browse files
committed
esp32s2 running well with lib API
- Device and Serial (CDC) uses arduino-esp32 core code - MSC work well using descriptor builder from core
1 parent 4044e9a commit 611608f

File tree

9 files changed

+112
-41
lines changed

9 files changed

+112
-41
lines changed

examples/MassStorage/msc_ramdisk/msc_ramdisk.ino

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ Adafruit_USBD_MSC usb_msc;
2020

2121
// the setup function runs once when you press reset or power the board
2222
void setup()
23-
{
23+
{
2424
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
25-
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
25+
// Manual begin() is required on core without built-in support for TinyUSB such as
26+
// - mbed rp2040
2627
TinyUSB_Device_Init(0);
2728
#endif
2829

@@ -33,7 +34,7 @@ void setup()
3334
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
3435

3536
// Set callback
36-
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
37+
usb_msc.setReadWriteCallback(msc_read_callback, msc_write_callback, msc_flush_callback);
3738

3839
// Set Lun ready (RAM disk is always ready)
3940
usb_msc.setUnitReady(true);
@@ -49,12 +50,14 @@ void setup()
4950
void loop()
5051
{
5152
// nothing to do
53+
Serial.println("Adafruit TinyUSB Mass Storage RAM Disk example");
54+
delay(1000);
5255
}
5356

5457
// Callback invoked when received READ10 command.
5558
// Copy disk's data to buffer (up to bufsize) and
5659
// return number of copied bytes (must be multiple of block size)
57-
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
60+
int32_t msc_read_callback (uint32_t lba, void* buffer, uint32_t bufsize)
5861
{
5962
uint8_t const* addr = msc_disk[lba];
6063
memcpy(buffer, addr, bufsize);
@@ -65,7 +68,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
6568
// Callback invoked when received WRITE10 command.
6669
// Process data in buffer to disk's storage and
6770
// return number of written bytes (must be multiple of block size)
68-
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
71+
int32_t msc_write_callback (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
6972
{
7073
uint8_t* addr = msc_disk[lba];
7174
memcpy(addr, buffer, bufsize);
@@ -75,7 +78,7 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
7578

7679
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
7780
// used to flush any pending cache.
78-
void msc_flush_cb (void)
81+
void msc_flush_callback (void)
7982
{
8083
// nothing to do
8184
}

src/arduino/Adafruit_TinyUSB_API.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
#include "tusb_option.h"
2626

27-
#if TUSB_OPT_DEVICE_ENABLED
27+
// ESP32 will use the arduino-esp32 core initialization and Serial
28+
#if TUSB_OPT_DEVICE_ENABLED && !defined(ARDUINO_ARCH_ESP32)
2829

2930
#include "Adafruit_TinyUSB.h"
3031

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include "tusb_option.h"
2626

27-
#if TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC
27+
#if TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC && !defined(ARDUINO_ARCH_ESP32)
2828

2929
#include "Arduino.h"
3030

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "Adafruit_TinyUSB_API.h"
2929

30-
#ifdef __cplusplus
30+
#if defined(__cplusplus) && !defined(ARDUINO_ARCH_ESP32)
3131

3232
#include "Adafruit_USBD_Interface.h"
3333
#include "Stream.h"

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
150150
// - Interface Number & string descriptor
151151
// - Endpoint address
152152
while (desc < desc_end) {
153-
if (desc[1] == TUSB_DESC_INTERFACE) {
153+
if (tu_desc_type(desc) == TUSB_DESC_INTERFACE) {
154154
tusb_desc_interface_t *desc_itf = (tusb_desc_interface_t *)desc;
155155
if (desc_itf->bAlternateSetting == 0) {
156156
_itf_count++;
@@ -163,7 +163,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
163163
desc_str = NULL;
164164
}
165165
}
166-
} else if (desc[1] == TUSB_DESC_ENDPOINT) {
166+
} else if (tu_desc_type(desc) == TUSB_DESC_ENDPOINT) {
167167
tusb_desc_endpoint_t *desc_ep = (tusb_desc_endpoint_t *)desc;
168168
desc_ep->bEndpointAddress |=
169169
(desc_ep->bEndpointAddress & 0x80) ? _epin_count++ : _epout_count++;
@@ -173,7 +173,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
173173
return false;
174174
}
175175

176-
desc += desc[0]; // next
176+
desc += tu_desc_len(desc);
177177
}
178178

179179
_desc_cfg_len += len;
@@ -250,10 +250,12 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
250250
_desc_device.bDeviceSubClass = MISC_SUBCLASS_COMMON;
251251
_desc_device.bDeviceProtocol = MISC_PROTOCOL_IAD;
252252

253+
#ifndef ARDUINO_ARCH_ESP32
253254
SerialTinyUSB.begin(115200);
254255

255256
// Init device hardware and call tusb_init()
256257
TinyUSB_Port_InitDevice(rhport);
258+
#endif
257259

258260
return true;
259261
}
@@ -272,6 +274,19 @@ bool Adafruit_USBD_Device::detach(void) { return tud_disconnect(); }
272274

273275
bool Adafruit_USBD_Device::attach(void) { return tud_connect(); }
274276

277+
#ifdef ARDUINO_ARCH_ESP32
278+
279+
// EPS32 use built-in core descriptor builder
280+
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
281+
uint16_t langid) {
282+
(void)index;
283+
(void)langid;
284+
285+
return NULL;
286+
}
287+
288+
#else
289+
275290
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
276291
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
277292
uint16_t langid) {
@@ -308,7 +323,7 @@ uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
308323
//--------------------------------------------------------------------+
309324
// TinyUSB stack callbacks
310325
//--------------------------------------------------------------------+
311-
#if !defined(ARDUINO_ARCH_ESP32)
326+
312327

313328
extern "C" {
314329

src/arduino/Adafruit_USBD_Device.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#include "Adafruit_USBD_Interface.h"
2929
#include "tusb.h"
3030

31+
#ifdef ARDUINO_ARCH_ESP32
32+
#include "esp32-hal-tinyusb.h"
33+
#endif
34+
3135
class Adafruit_USBD_Device {
3236
private:
3337
enum { STRING_DESCRIPTOR_MAX = 8 };

src/arduino/msc/Adafruit_USBD_MSC.cpp

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,39 @@
3434

3535
static Adafruit_USBD_MSC *_msc_dev = NULL;
3636

37+
#ifdef ARDUINO_ARCH_ESP32
38+
static uint16_t msc_load_descriptor(uint8_t * dst, uint8_t * itf)
39+
{
40+
// uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB MSC");
41+
uint8_t str_index = 0;
42+
uint8_t ep_num = tinyusb_get_free_duplex_endpoint();
43+
TU_VERIFY (ep_num != 0);
44+
45+
uint8_t const descriptor[TUD_MSC_DESC_LEN] = {
46+
// Interface number, string index, EP Out & EP In address, EP size
47+
TUD_MSC_DESCRIPTOR(*itf, str_index, ep_num, (uint8_t)(0x80 | ep_num), EPSIZE)
48+
};
49+
50+
*itf+=1;
51+
memcpy(dst, descriptor, TUD_MSC_DESC_LEN);
52+
return TUD_MSC_DESC_LEN;
53+
}
54+
#endif
55+
3756
Adafruit_USBD_MSC::Adafruit_USBD_MSC(void) {
3857
_maxlun = 1;
39-
memset(_lun, 0, sizeof(_lun));
58+
memset(_lun_info, 0, sizeof(_lun_info));
59+
60+
#ifdef ARDUINO_ARCH_ESP32
61+
// ESP32 requires setup configuration descriptor on declaration
62+
tinyusb_enable_interface(USB_INTERFACE_MSC, TUD_MSC_DESC_LEN, msc_load_descriptor);
63+
#endif
4064
}
4165

4266
uint16_t Adafruit_USBD_MSC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
4367
uint16_t bufsize) {
4468
// usb core will automatically update endpoint number
45-
uint8_t desc[] = {TUD_MSC_DESCRIPTOR(itfnum, 0, EPOUT, EPIN, EPSIZE)};
69+
uint8_t const desc[] = {TUD_MSC_DESCRIPTOR(itfnum, 0, EPOUT, EPIN, EPSIZE)};
4670
uint16_t const len = sizeof(desc);
4771

4872
if (bufsize < len) {
@@ -59,37 +83,40 @@ uint8_t Adafruit_USBD_MSC::getMaxLun(void) { return _maxlun; }
5983

6084
void Adafruit_USBD_MSC::setID(uint8_t lun, const char *vendor_id,
6185
const char *product_id, const char *product_rev) {
62-
_lun[lun]._inquiry_vid = vendor_id;
63-
_lun[lun]._inquiry_pid = product_id;
64-
_lun[lun]._inquiry_rev = product_rev;
86+
_lun_info[lun]._inquiry_vid = vendor_id;
87+
_lun_info[lun]._inquiry_pid = product_id;
88+
_lun_info[lun]._inquiry_rev = product_rev;
6589
}
6690

6791
void Adafruit_USBD_MSC::setCapacity(uint8_t lun, uint32_t block_count,
6892
uint16_t block_size) {
69-
_lun[lun].block_count = block_count;
70-
_lun[lun].block_size = block_size;
93+
_lun_info[lun].block_count = block_count;
94+
_lun_info[lun].block_size = block_size;
7195
}
7296

7397
void Adafruit_USBD_MSC::setUnitReady(uint8_t lun, bool ready) {
74-
_lun[lun].unit_ready = ready;
98+
_lun_info[lun].unit_ready = ready;
7599
}
76100

77101
void Adafruit_USBD_MSC::setReadWriteCallback(uint8_t lun, read_callback_t rd_cb,
78102
write_callback_t wr_cb,
79103
flush_callback_t fl_cb) {
80-
_lun[lun].rd_cb = rd_cb;
81-
_lun[lun].wr_cb = wr_cb;
82-
_lun[lun].fl_cb = fl_cb;
104+
_lun_info[lun].rd_cb = rd_cb;
105+
_lun_info[lun].wr_cb = wr_cb;
106+
_lun_info[lun].fl_cb = fl_cb;
83107
}
84108

85109
void Adafruit_USBD_MSC::setReadyCallback(uint8_t lun, ready_callback_t cb) {
86-
_lun[lun].ready_cb = cb;
110+
_lun_info[lun].ready_cb = cb;
87111
}
88112

89113
bool Adafruit_USBD_MSC::begin(void) {
114+
115+
#ifndef ARDUINO_ARCH_ESP32
90116
if (!TinyUSBDevice.addInterface(*this)) {
91117
return false;
92118
}
119+
#endif
93120

94121
_msc_dev = this;
95122
return true;
@@ -117,13 +144,13 @@ void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
117144

118145
// If not set use default ID "Adafruit - Mass Storage - 1.0"
119146
const char *vid =
120-
(_msc_dev->_lun[lun]._inquiry_vid ? _msc_dev->_lun[lun]._inquiry_vid
147+
(_msc_dev->_lun_info[lun]._inquiry_vid ? _msc_dev->_lun_info[lun]._inquiry_vid
121148
: "Adafruit");
122149
const char *pid =
123-
(_msc_dev->_lun[lun]._inquiry_pid ? _msc_dev->_lun[lun]._inquiry_pid
150+
(_msc_dev->_lun_info[lun]._inquiry_pid ? _msc_dev->_lun_info[lun]._inquiry_pid
124151
: "Mass Storage");
125152
const char *rev =
126-
(_msc_dev->_lun[lun]._inquiry_rev ? _msc_dev->_lun[lun]._inquiry_rev
153+
(_msc_dev->_lun_info[lun]._inquiry_rev ? _msc_dev->_lun_info[lun]._inquiry_rev
127154
: "1.0");
128155

129156
memcpy(vendor_id, vid, tu_min32(strlen(vid), 8));
@@ -138,11 +165,11 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
138165
return false;
139166
}
140167

141-
if (_msc_dev->_lun[lun].ready_cb) {
142-
_msc_dev->_lun[lun].unit_ready = _msc_dev->_lun[lun].ready_cb();
168+
if (_msc_dev->_lun_info[lun].ready_cb) {
169+
_msc_dev->_lun_info[lun].unit_ready = _msc_dev->_lun_info[lun].ready_cb();
143170
}
144171

145-
return _msc_dev->_lun[lun].unit_ready;
172+
return _msc_dev->_lun_info[lun].unit_ready;
146173
}
147174

148175
// Callback invoked to determine disk's size
@@ -152,8 +179,8 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count,
152179
return;
153180
}
154181

155-
*block_count = _msc_dev->_lun[lun].block_count;
156-
*block_size = _msc_dev->_lun[lun].block_size;
182+
*block_count = _msc_dev->_lun_info[lun].block_count;
183+
*block_size = _msc_dev->_lun_info[lun].block_size;
157184
}
158185

159186
// Callback invoked when received an SCSI command not in built-in list below
@@ -199,11 +226,11 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
199226
void *buffer, uint32_t bufsize) {
200227
(void)offset;
201228

202-
if (!(_msc_dev && _msc_dev->_lun[lun].rd_cb)) {
229+
if (!(_msc_dev && _msc_dev->_lun_info[lun].rd_cb)) {
203230
return -1;
204231
}
205232

206-
return _msc_dev->_lun[lun].rd_cb(lba, buffer, bufsize);
233+
return _msc_dev->_lun_info[lun].rd_cb(lba, buffer, bufsize);
207234
}
208235

209236
// Callback invoked when received WRITE10 command.
@@ -212,22 +239,22 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
212239
uint8_t *buffer, uint32_t bufsize) {
213240
(void)offset;
214241

215-
if (!(_msc_dev && _msc_dev->_lun[lun].wr_cb)) {
242+
if (!(_msc_dev && _msc_dev->_lun_info[lun].wr_cb)) {
216243
return -1;
217244
}
218245

219-
return _msc_dev->_lun[lun].wr_cb(lba, buffer, bufsize);
246+
return _msc_dev->_lun_info[lun].wr_cb(lba, buffer, bufsize);
220247
}
221248

222249
// Callback invoked when WRITE10 command is completed (status received and
223250
// accepted by host). used to flush any pending cache.
224251
void tud_msc_write10_complete_cb(uint8_t lun) {
225-
if (!(_msc_dev && _msc_dev->_lun[lun].fl_cb)) {
252+
if (!(_msc_dev && _msc_dev->_lun_info[lun].fl_cb)) {
226253
return;
227254
}
228255

229256
// flush pending cache when write10 is complete
230-
return _msc_dev->_lun[lun].fl_cb();
257+
return _msc_dev->_lun_info[lun].fl_cb();
231258
}
232259

233260
} // extern "C"

src/arduino/msc/Adafruit_USBD_MSC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Adafruit_USBD_MSC : public Adafruit_USBD_Interface {
9191
uint16_t block_size;
9292
bool unit_ready;
9393

94-
} _lun[MAX_LUN];
94+
} _lun_info[MAX_LUN];
9595

9696
uint8_t _maxlun;
9797

src/arduino/ports/esp32/Adafruit_TinyUSB_esp32.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@
2626

2727
#if defined(ARDUINO_ARCH_ESP32) && TUSB_OPT_DEVICE_ENABLED
2828

29+
#include <stdint.h>
30+
31+
// ESP32 will use the arduino-esp32 core initialization
32+
// These port API is empty to prevent compilation error
33+
34+
void TinyUSB_Port_EnterDFU(void) {}
35+
36+
void TinyUSB_Port_InitDevice(uint8_t rhport) {
37+
(void) rhport;
38+
}
39+
40+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) {
41+
(void) serial_id;
42+
}
43+
44+
#if 0
45+
46+
// This port implemented is not needed and left here for reference only
47+
2948
#include "sdkconfig.h"
3049

3150
#include "soc/soc.h"
@@ -109,6 +128,7 @@ static void usb_device_task(void *param) {
109128
}
110129
}
111130

131+
112132
void TinyUSB_Port_InitDevice(uint8_t rhport) {
113133
(void)rhport;
114134

@@ -162,5 +182,6 @@ extern "C" void yield(void) {
162182
TinyUSB_Device_FlushCDC();
163183
vPortYield();
164184
}
185+
#endif // commented out
165186

166-
#endif // USE_TINYUSB
187+
#endif

0 commit comments

Comments
 (0)