Skip to content

Commit 7759332

Browse files
committed
improve port API with TinyUSB_Port_GetSerialNumber
1 parent a106cbc commit 7759332

File tree

5 files changed

+80
-56
lines changed

5 files changed

+80
-56
lines changed

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@
1919
#include "Adafruit_SPIFlash.h"
2020
#include "Adafruit_TinyUSB.h"
2121

22-
#if defined(__SAMD51__) || defined(NRF52840_XXAA)
23-
Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
22+
// Uncomment to run example with FRAM
23+
// #define FRAM_CS A5
24+
// #define FRAM_SPI SPI
25+
26+
#if defined(FRAM_CS) && defined(FRAM_SPI)
27+
Adafruit_FlashTransport_SPI flashTransport(FRAM_CS, FRAM_SPI);
28+
2429
#else
25-
#if (SPI_INTERFACES_COUNT == 1)
26-
Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
30+
// On-board external flash (QSPI or SPI) macros should already
31+
// defined in your board variant if supported
32+
// - EXTERNAL_FLASH_USE_QSPI
33+
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
34+
#if defined(EXTERNAL_FLASH_USE_QSPI)
35+
Adafruit_FlashTransport_QSPI flashTransport;
36+
37+
#elif defined(EXTERNAL_FLASH_USE_SPI)
38+
Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, EXTERNAL_FLASH_USE_SPI);
39+
2740
#else
28-
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
41+
#error No QSPI/SPI flash are defined on your board variant.h !
2942
#endif
3043
#endif
3144

@@ -82,7 +95,7 @@ void setup()
8295
usb_hid.begin();
8396

8497
Serial.begin(115200);
85-
while ( !Serial ) delay(10); // wait for native usb
98+
//while ( !Serial ) delay(10); // wait for native usb
8699

87100
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (external flash) example");
88101
}
@@ -143,4 +156,3 @@ void msc_flush_cb (void)
143156
{
144157
flash.syncBlocks();
145158
}
146-

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ void Adafruit_USBD_Device::setProductDescriptor(const char *s)
208208
_desc_str_arr[STRID_PRODUCT] = s;
209209
}
210210

211+
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_utf16)
212+
{
213+
uint8_t serial_id[16] __attribute__((aligned(4)));
214+
uint8_t const serial_len = TinyUSB_Port_GetSerialNumber(serial_id);
215+
216+
for ( uint8_t i = 0; i < serial_len; i++ )
217+
{
218+
for ( uint8_t j = 0; j < 2; j++ )
219+
{
220+
const char nibble_to_hex[16] =
221+
{
222+
'0', '1', '2', '3', '4', '5', '6', '7',
223+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
224+
};
225+
226+
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
227+
serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
228+
}
229+
}
230+
231+
return 2*serial_len;
232+
}
233+
211234
bool Adafruit_USBD_Device::begin(uint8_t rhport)
212235
{
213236
Serial.setStringDescriptor("TinyUSB Serial");
@@ -259,20 +282,17 @@ uint16_t const* Adafruit_USBD_Device::descriptor_string_cb(uint8_t index, uint16
259282
{
260283
(void) langid;
261284

262-
// up to 32 unicode characters (header make it 33)
263-
static uint16_t _desc_str[33];
264285
uint8_t chr_count;
265286

266287
switch (index)
267288
{
268-
case 0:
289+
case STRID_LANGUAGE:
269290
_desc_str[1] = ((uint16_t) ((uint32_t) _desc_str_arr[STRID_LANGUAGE]));
270291
chr_count = 1;
271292
break;
272293

273-
case 3:
274-
// serial Number
275-
chr_count = this->getSerialDescriptor(_desc_str+1);
294+
case STRID_SERIAL:
295+
chr_count = getSerialDescriptor(_desc_str);
276296
break;
277297

278298
default:

src/arduino/Adafruit_USBD_Device.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ class Adafruit_USBD_Device
3333
private:
3434
enum { STRING_DESCRIPTOR_MAX = 8 };
3535

36+
// Device descriptor
3637
uint8_t _desc_device[18] __attribute__ ((aligned(4)));
3738

39+
// Configuration descriptor
3840
uint8_t* _desc_cfg;
3941
uint8_t _desc_cfg_buffer[256];
4042
uint16_t _desc_cfg_len;
@@ -45,8 +47,10 @@ class Adafruit_USBD_Device
4547
uint8_t _epin_count;
4648
uint8_t _epout_count;
4749

50+
// String descriptor
4851
const char* _desc_str_arr[STRING_DESCRIPTOR_MAX];
4952
uint8_t _desc_str_count;
53+
uint16_t _desc_str[32+1]; // up to 32 unicode characters with headers
5054

5155
public:
5256
Adafruit_USBD_Device(void);
@@ -60,10 +64,13 @@ class Adafruit_USBD_Device
6064
bool addInterface(Adafruit_USBD_Interface& itf);
6165
void setDescriptorBuffer(uint8_t* buf, uint32_t buflen);
6266

67+
// String descriptor
6368
void setLanguageDescriptor(uint16_t language_id);
6469
void setManufacturerDescriptor(const char *s);
6570
void setProductDescriptor(const char *s);
71+
uint8_t getSerialDescriptor(uint16_t* serial_utf16);
6672

73+
// Control
6774
bool begin(uint8_t rhport=0);
6875
void task(void);
6976

@@ -77,12 +84,6 @@ class Adafruit_USBD_Device
7784
bool detach (void);
7885
bool attach (void);
7986

80-
//------------- Platform Dependent APIs -------------//
81-
uint8_t getSerialDescriptor(uint16_t* serial_str);
82-
83-
private:
84-
void port_InitHardware(uint8_t rhport);
85-
void port_Touch1200(void);
8687

8788
private:
8889
uint16_t const* descriptor_string_cb(uint8_t index, uint16_t langid);

src/arduino/ports/Adafruit_TinyUSB_Port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ void TinyUSB_Port_EnterDFU(void);
3737
// Called by USBDevice.begin()
3838
void TinyUSB_Port_InitDeviceController(uint8_t rhport);
3939

40+
// Get unique serial number, needed for Serial String Descriptor
41+
// Fill serial_id (raw bytes) and return its length (limit to 16 bytes)
42+
// Note: Serial descriptor can be overwritten by user API
43+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]);
44+
4045
#endif

src/arduino/ports/Adafruit_TinyUSB_samd.cpp

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,28 @@
3535
extern "C"
3636
{
3737

38-
#if defined(__SAMD51__)
38+
#if CFG_TUSB_MCU == OPT_MCU_SAMD51 || CFG_TUSB_MCU == OPT_MCU_SAME5X
3939

40+
// SAMD51
4041
void USB_0_Handler (void) { tud_int_handler(0); }
4142
void USB_1_Handler (void) { tud_int_handler(0); }
4243
void USB_2_Handler (void) { tud_int_handler(0); }
4344
void USB_3_Handler (void) { tud_int_handler(0); }
4445

45-
#else
46+
#elif CFG_TUSB_MCU == OPT_MCU_SAMD21
4647

48+
// SAMD21
4749
void USB_Handler(void) { tud_int_handler(0); }
4850

4951
#endif
52+
53+
// run TinyUSB background task when yield()
54+
void yield(void)
55+
{
56+
tud_task();
57+
tud_cdc_write_flush();
58+
}
59+
5060
} // extern C
5161

5262

@@ -69,23 +79,7 @@ extern "C" int serial1_printf(const char *__restrict format, ...)
6979
#endif
7080

7181
//--------------------------------------------------------------------+
72-
// Core Init & Touch1200
73-
//--------------------------------------------------------------------+
74-
75-
void TinyUSB_Port_EnterDFU(void)
76-
{
77-
initiateReset(250);
78-
}
79-
80-
// run TinyUSB background task when yield()
81-
extern "C" void yield(void)
82-
{
83-
tud_task();
84-
tud_cdc_write_flush();
85-
}
86-
87-
//--------------------------------------------------------------------+
88-
// Adafruit_USBD_Device platform dependent
82+
// Porting API
8983
//--------------------------------------------------------------------+
9084
void TinyUSB_Port_InitDeviceController(uint8_t rhport)
9185
{
@@ -131,10 +125,13 @@ void TinyUSB_Port_InitDeviceController(uint8_t rhport)
131125
#endif
132126
}
133127

134-
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_str)
128+
void TinyUSB_Port_EnterDFU(void)
135129
{
136-
enum { SERIAL_BYTE_LEN = 16 };
130+
initiateReset(250);
131+
}
137132

133+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16])
134+
{
138135
#ifdef __SAMD51__
139136
uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010,
140137
(uint32_t *) 0x00806014, (uint32_t *) 0x00806018};
@@ -143,25 +140,14 @@ uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_str)
143140
(uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048};
144141
#endif
145142

146-
uint8_t raw_id[SERIAL_BYTE_LEN];
147-
148-
for (int i=0; i<4; i++) {
149-
for (int k=0; k<4; k++) {
150-
raw_id[4 * i + (3 - k)] = (*(id_addresses[i]) >> k * 8) & 0xff;
151-
}
152-
}
153-
154-
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
143+
uint32_t* serial_32 = (uint32_t*) serial_id;
155144

156-
for (unsigned int i = 0; i < sizeof(raw_id); i++) {
157-
for (int j = 0; j < 2; j++) {
158-
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
159-
// Strings are UTF-16-LE encoded.
160-
serial_str[i * 2 + (1 - j)] = nibble_to_hex[nibble];
161-
}
145+
for (int i=0; i<4; i++)
146+
{
147+
*serial_32++ = __builtin_bswap32(*id_addresses[i]);
162148
}
163149

164-
return sizeof(raw_id)*2;
150+
return 16;
165151
}
166152

167153
#endif // USE_TINYUSB

0 commit comments

Comments
 (0)