Skip to content

Commit b3116c7

Browse files
authored
Merge pull request #7 from adafruit/develop
usb update
2 parents 67fbbc9 + 2ac0b69 commit b3116c7

File tree

20 files changed

+1218
-165
lines changed

20 files changed

+1218
-165
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ before_install:
2626
- source install.sh
2727

2828
install:
29-
- arduino --install-library "Adafruit SPIFlash","Adafruit QSPI"
29+
- arduino --install-library "Adafruit SPIFlash","Adafruit QSPI","SdFat"
3030
- pip3 install --user adafruit-nrfutil
3131

3232
script:
33-
- build_m4_platforms
33+
- build_samd_platforms
3434
- build_nrf5x_platforms
3535

3636
# Generate and deploy documentation
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 sketch demonstrates USB Mass Storage and HID mouse (and CDC)
13+
* - Enumerated as disk using on-board external flash
14+
* - Press button pin will move mouse toward bottom right of monitor
15+
*/
16+
17+
#include "SPI.h"
18+
#include "SdFat.h"
19+
#include "Adafruit_SPIFlash.h"
20+
#include "Adafruit_TinyUSB.h"
21+
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);
24+
#else
25+
#if (SPI_INTERFACES_COUNT == 1)
26+
Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
27+
#else
28+
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
29+
#endif
30+
#endif
31+
32+
Adafruit_SPIFlash flash(&flashTransport);
33+
34+
Adafruit_USBD_MSC usb_msc;
35+
36+
// HID report descriptor using TinyUSB's template
37+
// Single Report (no ID) descriptor
38+
uint8_t const desc_hid_report[] =
39+
{
40+
TUD_HID_REPORT_DESC_MOUSE()
41+
};
42+
43+
Adafruit_USBD_HID usb_hid;
44+
45+
const int pin = 7;
46+
47+
// the setup function runs once when you press reset or power the board
48+
void setup()
49+
{
50+
flash.begin();
51+
52+
pinMode(LED_BUILTIN, OUTPUT);
53+
54+
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
55+
usb_msc.setID("Adafruit", "External Flash", "1.0");
56+
57+
// Set callback
58+
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
59+
60+
// Set disk size, block size should be 512 regardless of spi flash page size
61+
usb_msc.setCapacity(flash.pageSize()*flash.numPages()/512, 512);
62+
63+
// MSC is ready for read/write
64+
usb_msc.setUnitReady(true);
65+
66+
usb_msc.begin();
67+
68+
69+
// Set up button
70+
pinMode(pin, INPUT_PULLUP);
71+
72+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
73+
usb_hid.begin();
74+
75+
Serial.begin(115200);
76+
while ( !Serial ) delay(10); // wait for native usb
77+
78+
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (external flash) example");
79+
}
80+
81+
void loop()
82+
{
83+
// poll gpio once each 10 ms
84+
delay(10);
85+
86+
// button is active low
87+
uint32_t const btn = 1 - digitalRead(pin);
88+
89+
// Remote wakeup
90+
if ( tud_suspended() && btn )
91+
{
92+
// Wake up host if we are in suspend mode
93+
// and REMOTE_WAKEUP feature is enabled by host
94+
tud_remote_wakeup();
95+
}
96+
97+
/*------------- Mouse -------------*/
98+
if ( usb_hid.ready() )
99+
{
100+
if ( btn )
101+
{
102+
int8_t const delta = 5;
103+
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
104+
105+
// delay a bit before attempt to send keyboard report
106+
delay(10);
107+
}
108+
}
109+
}
110+
111+
// Callback invoked when received READ10 command.
112+
// Copy disk's data to buffer (up to bufsize) and
113+
// return number of copied bytes (must be multiple of block size)
114+
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
115+
{
116+
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
117+
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
118+
return flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
119+
}
120+
121+
// Callback invoked when received WRITE10 command.
122+
// Process data in buffer to disk's storage and
123+
// return number of written bytes (must be multiple of block size)
124+
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
125+
{
126+
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
127+
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
128+
return flash.writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1;
129+
}
130+
131+
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
132+
// used to flush any pending cache.
133+
void msc_flush_cb (void)
134+
{
135+
flash.syncBlocks();
136+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 sketch demonstrates USB Mass Storage and HID mouse (and CDC)
13+
* - Enumerated as 8KB flash disk
14+
* - Press button pin will move mouse toward bottom right of monitor
15+
*/
16+
17+
#include "Adafruit_TinyUSB.h"
18+
19+
// 8KB is the smallest size that windows allow to mount
20+
#define DISK_BLOCK_NUM 16
21+
#define DISK_BLOCK_SIZE 512
22+
#include "ramdisk.h"
23+
24+
// HID report descriptor using TinyUSB's template
25+
// Single Report (no ID) descriptor
26+
uint8_t const desc_hid_report[] =
27+
{
28+
TUD_HID_REPORT_DESC_MOUSE()
29+
};
30+
31+
Adafruit_USBD_HID usb_hid;
32+
Adafruit_USBD_MSC usb_msc;
33+
34+
const int pin = 7;
35+
36+
// the setup function runs once when you press reset or power the board
37+
void setup()
38+
{
39+
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
40+
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
41+
42+
// Set disk size
43+
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
44+
45+
// Set callback
46+
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
47+
48+
// Set Lun ready (RAM disk is always ready)
49+
usb_msc.setUnitReady(true);
50+
51+
usb_msc.begin();
52+
53+
// Set up button
54+
pinMode(pin, INPUT_PULLUP);
55+
56+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
57+
usb_hid.begin();
58+
59+
Serial.begin(115200);
60+
while ( !Serial ) delay(10); // wait for native usb
61+
62+
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
63+
}
64+
65+
void loop()
66+
{
67+
// poll gpio once each 10 ms
68+
delay(10);
69+
70+
// button is active low
71+
uint32_t const btn = 1 - digitalRead(pin);
72+
73+
// Remote wakeup
74+
if ( tud_suspended() && btn )
75+
{
76+
// Wake up host if we are in suspend mode
77+
// and REMOTE_WAKEUP feature is enabled by host
78+
tud_remote_wakeup();
79+
}
80+
81+
/*------------- Mouse -------------*/
82+
if ( usb_hid.ready() )
83+
{
84+
if ( btn )
85+
{
86+
int8_t const delta = 5;
87+
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
88+
89+
// delay a bit before attempt to send keyboard report
90+
delay(10);
91+
}
92+
}
93+
}
94+
95+
// Callback invoked when received READ10 command.
96+
// Copy disk's data to buffer (up to bufsize) and
97+
// return number of copied bytes (must be multiple of block size)
98+
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
99+
{
100+
uint8_t const* addr = msc_disk[lba];
101+
memcpy(buffer, addr, bufsize);
102+
103+
return bufsize;
104+
}
105+
106+
// Callback invoked when received WRITE10 command.
107+
// Process data in buffer to disk's storage and
108+
// return number of written bytes (must be multiple of block size)
109+
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
110+
{
111+
uint8_t* addr = msc_disk[lba];
112+
memcpy(addr, buffer, bufsize);
113+
114+
return bufsize;
115+
}
116+
117+
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
118+
// used to flush any pending cache.
119+
void msc_flush_cb (void)
120+
{
121+
// nothing to do
122+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 RAMDISK_H_
26+
#define RAMDISK_H_
27+
28+
#define README_CONTENTS \
29+
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
30+
31+
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
32+
{
33+
//------------- Block0: Boot Sector -------------//
34+
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
35+
// sector_per_cluster = 1; reserved_sectors = 1;
36+
// fat_num = 1; fat12_root_entry_num = 16;
37+
// sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
38+
// drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
39+
// filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
40+
// FAT magic code at offset 510-511
41+
{
42+
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
43+
0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
44+
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T' , 'i' , 'n' , 'y' , 'U' ,
45+
'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
46+
47+
// Zero up to 2 last bytes of FAT magic code
48+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56+
57+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65+
66+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74+
75+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
79+
},
80+
81+
//------------- Block1: FAT12 Table -------------//
82+
{
83+
0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
84+
},
85+
86+
//------------- Block2: Root Directory -------------//
87+
{
88+
// first entry is volume label
89+
'T' , 'i' , 'n' , 'y' , 'U' , 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x08, 0x00, 0x00, 0x00, 0x00,
90+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91+
// second entry is readme file
92+
'R' , 'E' , 'A' , 'D' , 'M' , 'E' , ' ' , ' ' , 'T' , 'X' , 'T' , 0x20, 0x00, 0xC6, 0x52, 0x6D,
93+
0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
94+
sizeof(README_CONTENTS)-1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
95+
},
96+
97+
//------------- Block3: Readme Content -------------//
98+
README_CONTENTS
99+
};
100+
101+
#endif /* RAMDISK_H_ */

examples/HID/hid_composite/hid_composite.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
// The MIT License (MIT)
2-
// Copyright (c) 2019 Ha Thach for Adafruit Industries
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+
*********************************************************************/
311

412
#include "Adafruit_TinyUSB.h"
513

0 commit comments

Comments
 (0)