Skip to content

Commit b7b834b

Browse files
committed
add mouse_ramdisk example
1 parent f21521d commit b7b834b

File tree

3 files changed

+218
-3
lines changed

3 files changed

+218
-3
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// The MIT License (MIT)
2+
// Copyright (c) 2019 Ha Thach for Adafruit Industries
3+
4+
/* This sketch demonstrates USB Mass Storage and HID mouse (and CDC)
5+
* - Enumerated as 8KB flash disk
6+
* - Press button pin will move mouse toward bottom right of monitor
7+
*/
8+
9+
#include "Adafruit_TinyUSB.h"
10+
11+
// 8KB is the smallest size that windows allow to mount
12+
#define DISK_BLOCK_NUM 16
13+
#define DISK_BLOCK_SIZE 512
14+
#include "ramdisk.h"
15+
16+
// HID report descriptor using TinyUSB's template
17+
// Single Report (no ID) descriptor
18+
uint8_t const desc_hid_report[] =
19+
{
20+
TUD_HID_REPORT_DESC_MOUSE()
21+
};
22+
23+
Adafruit_USBD_HID usb_hid;
24+
Adafruit_USBD_MSC usb_msc;
25+
26+
const int pin = 7;
27+
28+
// the setup function runs once when you press reset or power the board
29+
void setup()
30+
{
31+
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
32+
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
33+
34+
// Set disk size
35+
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
36+
37+
// Set callback
38+
usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb);
39+
40+
// Set Lun ready (RAM disk is always ready)
41+
usb_msc.setUnitReady(true);
42+
43+
usb_msc.begin();
44+
45+
// Set up button
46+
pinMode(pin, INPUT_PULLUP);
47+
48+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
49+
usb_hid.begin();
50+
51+
Serial.begin(115200);
52+
while ( !Serial ) delay(10); // wait for native usb
53+
54+
Serial.println("Adafruit TinyUSB Mass Storage RAM Disk example");
55+
}
56+
57+
void loop()
58+
{
59+
// poll gpio once each 10 ms
60+
delay(10);
61+
62+
// button is active low
63+
uint32_t const btn = 1 - digitalRead(pin);
64+
65+
// Remote wakeup
66+
if ( tud_suspended() && btn )
67+
{
68+
// Wake up host if we are in suspend mode
69+
// and REMOTE_WAKEUP feature is enabled by host
70+
tud_remote_wakeup();
71+
}
72+
73+
/*------------- Mouse -------------*/
74+
if ( usb_hid.ready() )
75+
{
76+
if ( btn )
77+
{
78+
int8_t const delta = 5;
79+
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
80+
81+
// delay a bit before attempt to send keyboard report
82+
delay(10);
83+
}
84+
}
85+
}
86+
87+
// Callback invoked when received READ10 command.
88+
// Copy disk's data to buffer (up to bufsize) and
89+
// return number of copied bytes (must be multiple of block size)
90+
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
91+
{
92+
uint8_t const* addr = msc_disk[lba];
93+
memcpy(buffer, addr, bufsize);
94+
95+
return bufsize;
96+
}
97+
98+
// Callback invoked when received WRITE10 command.
99+
// Process data in buffer to disk's storage and
100+
// return number of written bytes (must be multiple of block size)
101+
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
102+
{
103+
uint8_t* addr = msc_disk[lba];
104+
memcpy(addr, buffer, bufsize);
105+
106+
return bufsize;
107+
}
108+
109+
// Callback invoked when WRITE10 command is completed (status received and accepted by host).
110+
// used to flush any pending cache.
111+
void msc_flush_cb (void)
112+
{
113+
// nothing to do
114+
}
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_mouse/hid_mouse.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ const int pin = 7;
2222
// the setup function runs once when you press reset or power the board
2323
void setup()
2424
{
25+
// Set up button
26+
pinMode(pin, INPUT_PULLUP);
27+
2528
usb_hid.setPollInterval(2);
2629
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
2730

2831
usb_hid.begin();
2932

30-
// Set up button
31-
pinMode(pin, INPUT_PULLUP);
32-
3333
Serial.begin(115200);
3434
while ( !Serial ) delay(10); // wait for native usb
3535

0 commit comments

Comments
 (0)