Skip to content

Commit aff9d95

Browse files
add usb hid
1 parent 46791e8 commit aff9d95

File tree

3 files changed

+139
-10
lines changed

3 files changed

+139
-10
lines changed

ports/esp32/boards/FireBeetle_2_ESP32_P4/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#define HIL_DEVICE_HOST_MUX_PIN 47
1616
#define HIL_DEVICE_STATE 1
1717
#define MICROPY_PY_ESPNOW (0)
18-
#define MICROPY_HW_USB_HOST (1)
19-
#define CFG_TUH_ENABLED (1)
18+
#define MICROPY_HW_USB_MSC (1)
2019

2120
#define MICROPY_PY_MACHINE_ADC (1)
2221
#define MP_SD_LDO (1)

shared/tinyusb/mp_usbd.c

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,94 @@
3232
#ifndef NO_QSTR
3333
#include "device/dcd.h"
3434
#endif
35+
#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_HID * TUD_HID_DESC_LEN)
36+
// Some MCU doesn't have enough 8KB SRAM to store the whole disk
37+
// We will use Flash as read-only disk with board that has
38+
// CFG_EXAMPLE_MSC_READONLY defined
3539

36-
#if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
40+
// whether host does safe-eject
41+
static bool ejected = false;
42+
#define README_CONTENTS \
43+
"This is tinyusb's MassStorage Class demo.\r\n\r\n\
44+
If you find any bugs or get any questions, feel free to file an\r\n\
45+
issue at github.com/hathach/tinyusb"
46+
47+
enum {
48+
DISK_BLOCK_NUM = 8192,// 8KB is the smallest size that windows allow to mount
49+
DISK_BLOCK_SIZE = 512
50+
};
51+
52+
#ifdef CFG_EXAMPLE_MSC_READONLY
53+
const
54+
#endif
55+
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
56+
//------------- Block0: Boot Sector -------------//
57+
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
58+
// sector_per_cluster = 1; reserved_sectors = 1;
59+
// fat_num = 1; fat12_root_entry_num = 16;
60+
// sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
61+
// drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
62+
// filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
63+
// FAT magic code at offset 510-511
64+
{
65+
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
66+
0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
67+
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T', 'i', 'n', 'y', 'U',
68+
'S', 'B', ' ', 'M', 'S', 'C', 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
69+
70+
// Zero up to 2 last bytes of FAT magic code
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+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x00, 0x00,
79+
80+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3788

89+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97+
98+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA},
102+
103+
//------------- Block1: FAT12 Table -------------//
104+
{
105+
0xF8, 0xFF, 0xFF, 0xFF, 0x0F// // first 2 entries must be F8FF, third entry is cluster end of readme file
106+
},
107+
108+
//------------- Block2: Root Directory -------------//
109+
{
110+
// first entry is volume label
111+
'T', 'i', 'n', 'y', 'U', 'S', 'B', ' ', 'M', 'S', 'C', 0x08, 0x00, 0x00, 0x00, 0x00,
112+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113+
// second entry is readme file
114+
'R', 'E', 'A', 'D', 'M', 'E', ' ', ' ', 'T', 'X', 'T', 0x20, 0x00, 0xC6, 0x52, 0x6D,
115+
0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
116+
sizeof(README_CONTENTS) - 1, 0x00, 0x00, 0x00// readme's files size (4 Bytes)
117+
},
118+
119+
//------------- Block3: Readme Content -------------//
120+
README_CONTENTS
121+
};
122+
#if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
38123
void mp_usbd_task(void) {
39124
tud_task_ext(0, false);
40125
}
@@ -71,5 +156,51 @@ void mp_usbd_hex_str(char *out_str, const uint8_t *bytes, size_t bytes_len) {
71156
}
72157
out_str[hex_len] = 0;
73158
}
159+
const uint8_t hid_report_descriptor[] = {
160+
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_ITF_PROTOCOL_KEYBOARD)),
161+
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(HID_ITF_PROTOCOL_MOUSE))
162+
};
163+
164+
/**
165+
* @brief String descriptor
166+
*/
167+
const char* hid_string_descriptor[5] = {
168+
// array of pointer to string descriptors
169+
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
170+
"TinyUSB", // 1: Manufacturer
171+
"TinyUSB Device", // 2: Product
172+
"123456", // 3: Serials, should use chip ID
173+
"Example HID interface", // 4: HID
174+
};
175+
176+
/********* TinyUSB HID callbacks ***************/
177+
178+
// Invoked when received GET HID REPORT DESCRIPTOR request
179+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
180+
uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance)
181+
{
182+
// We use only one interface and one HID report descriptor, so we can ignore parameter 'instance'
183+
return hid_report_descriptor;
184+
}
185+
186+
// Invoked when received GET_REPORT control request
187+
// Application must fill buffer report's content and return its length.
188+
// Return zero will cause the stack to STALL request
189+
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
190+
{
191+
(void) instance;
192+
(void) report_id;
193+
(void) report_type;
194+
(void) buffer;
195+
(void) reqlen;
196+
197+
return 0;
198+
}
199+
200+
// Invoked when received SET_REPORT control request or
201+
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
202+
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
203+
{
204+
}
74205

75206
#endif // MICROPY_HW_ENABLE_USBDEV

shared/tinyusb/tusb_config.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,16 @@
6262
#ifndef CFG_TUSB_RHPORT0_MODE
6363
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
6464
#endif
65-
65+
#define CFG_TUD_ENABLED 1
6666
#if MICROPY_HW_USB_CDC
6767
#define CFG_TUD_CDC (1)
6868
#else
6969
#define CFG_TUD_CDC (0)
7070
#endif
7171

72-
#if MICROPY_HW_USB_MSC
73-
#define CFG_TUD_MSC (1)
74-
#else
75-
#define CFG_TUD_MSC (0)
76-
#endif
77-
#define CFG_TUD_HID MICROPY_HW_USB_HID
72+
#define CFG_TUD_MSC 0
73+
74+
#define CFG_TUD_HID 1
7875
// CDC Configuration
7976
#if CFG_TUD_CDC
8077
#ifndef CFG_TUD_CDC_RX_BUFSIZE
@@ -93,6 +90,8 @@
9390
// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
9491
#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
9592
#endif
93+
// MSC Buffer size of Device Mass storage
94+
#define CFG_TUD_MSC_EP_BUFSIZE 512
9695

9796
#define USBD_RHPORT (0) // Currently only one port is supported
9897

0 commit comments

Comments
 (0)