forked from sekigon-gonnoc/Pico-PIO-USB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_device.c
149 lines (117 loc) · 4.03 KB
/
usb_device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <string.h>
#include "hardware/clocks.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/bootrom.h"
#include "pio_usb.h"
// Use tinyUSB header to define USB descriptors
#include "device/usbd.h"
#include "class/hid/hid_device.h"
static usb_device_t *usb_device = NULL;
tusb_desc_device_t const desc_device = {.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0110,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64,
.idVendor = 0xCafe,
.idProduct = 0,
.bcdDevice = 0x0100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01};
enum {
ITF_NUM_KEYBOARD,
ITF_NUM_MOUSE,
ITF_NUM_TOTAL,
};
enum {
EPNUM_KEYBOARD = 0x81,
EPNUM_MOUSE = 0x82,
};
uint8_t const desc_hid_keyboard_report[] =
{
TUD_HID_REPORT_DESC_KEYBOARD()
};
uint8_t const desc_hid_mouse_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
};
const uint8_t *report_desc[] = {desc_hid_keyboard_report,
desc_hid_mouse_report};
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + 2*TUD_HID_DESC_LEN)
uint8_t const desc_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN,
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_HID_DESCRIPTOR(ITF_NUM_KEYBOARD, 0, HID_ITF_PROTOCOL_KEYBOARD,
sizeof(desc_hid_keyboard_report), EPNUM_KEYBOARD,
CFG_TUD_HID_EP_BUFSIZE, 10),
TUD_HID_DESCRIPTOR(ITF_NUM_MOUSE, 0, HID_ITF_PROTOCOL_MOUSE,
sizeof(desc_hid_mouse_report), EPNUM_MOUSE,
CFG_TUD_HID_EP_BUFSIZE, 10),
};
static_assert(sizeof(desc_device) == 18, "device desc size error");
const char *string_descriptors_base[] = {
[0] = (const char[]){0x09, 0x04},
[1] = "Pico PIO USB",
[2] = "Pico PIO USB Device",
[3] = "123456",
};
static string_descriptor_t str_desc[4];
static void init_string_desc(void) {
for (int idx = 0; idx < 4; idx++) {
uint8_t len = 0;
uint16_t *wchar_str = (uint16_t *)&str_desc[idx];
if (idx == 0) {
wchar_str[1] = string_descriptors_base[0][0] |
((uint16_t)string_descriptors_base[0][1] << 8);
len = 1;
} else if (idx <= 3) {
len = strnlen(string_descriptors_base[idx], 31);
for (int i = 0; i < len; i++) {
wchar_str[i + 1] = string_descriptors_base[idx][i];
}
} else {
len = 0;
}
wchar_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
}
}
static usb_descriptor_buffers_t desc = {
.device = (uint8_t *)&desc_device,
.config = desc_configuration,
.hid_report = report_desc,
.string = str_desc
};
void core1_main() {
sleep_ms(10);
static pio_usb_configuration_t config = PIO_USB_DEFAULT_CONFIG;
init_string_desc();
usb_device = pio_usb_device_init(&config, &desc);
while (true) {
pio_usb_device_task();
}
}
int main() {
// default 125MHz is not appropreate. Sysclock should be multiple of 12MHz.
set_sys_clock_khz(120000, true);
stdio_init_all();
printf("hello!");
sleep_ms(10);
multicore_reset_core1();
// all USB task run in core1
multicore_launch_core1(core1_main);
// move mouse pointer every 0.5s
while (true) {
if (usb_device != NULL) {
hid_mouse_report_t mouse_report = {0};
mouse_report.x = 1;
endpoint_t *ep = pio_usb_get_endpoint(usb_device, 2);
pio_usb_set_out_data(ep, (uint8_t *)&mouse_report, sizeof(mouse_report));
}
sleep_ms(500);
}
}