CherryUSB框架

USB CDC 功能实现

USB HID 功能实现

如果HID设备能正常枚举,但是无法打开设备进行通信,请检查HID报告描述符! 下边例子会在WIN10的电脑上枚举出一个<符合HID标准的条形码标记读取器>

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "main.h"
#include "usbd_core.h"
#include "usbd_hid.h"

/*!< hidraw in endpoint */
#define HIDRAW_IN_EP 0x81
#define HIDRAW_IN_EP_SIZE 64
#define HIDRAW_IN_INTERVAL 10

/*!< hidraw out endpoint */
#define HIDRAW_OUT_EP 0x02
#define HIDRAW_OUT_EP_SIZE 64
#define HIDRAW_OUT_EP_INTERVAL 10

#define USBD_VID 0x0480
#define USBD_PID 0x5850
#define USBD_MAX_POWER 100
#define USBD_LANGID_STRING 1033

/*!< config descriptor size */
#define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)

/*!< custom hid report descriptor size */
#define HID_CUSTOM_REPORT_DESC_SIZE 33

/*!< global descriptor */
static const uint8_t hid_descriptor[] = {USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
/************** Descriptor of Custom interface *****************/
0x09, /* bLength: Interface Descriptor size */
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/******************** Descriptor of Custom HID ********************/
0x09, /* bLength: HID Descriptor size */
HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
0x11, /* bcdHID: HID Class Spec release number */
0x01, 0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
0x00,
/******************** Descriptor of Custom in endpoint ********************/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
WBVAL(HIDRAW_IN_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */
/******************** Descriptor of Custom out endpoint ********************/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
HIDRAW_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
/* 73 */
/*
* string0 descriptor
*/
USB_LANGID_INIT(USBD_LANGID_STRING),
/*
* string1 descriptor
*/
0x1a, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'S', 0x00, /* wcChar0 */
'O', 0x00, /* wcChar1 */
'N', 0x00, /* wcChar2 */
'A', 0x00, /* wcChar3 */
'V', 0x00, /* wcChar4 */
'O', 0x00, /* wcChar5 */
'X', 0x00, /* wcChar6 */
' ', 0x00, /* wcChar7 */
'M', 0x00, /* wcChar8 */
'S', 0x00, /* wcChar9 */
'C', 0x00, /* wcChar10 */
'S', 0x00, /* wcChar11 */
/*
* string2 descriptor
*/
0x24, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'A', 0x00, /* wcChar0 */
'T', 0x00, /* wcChar1 */
'L', 0x00, /* wcChar2 */
'A', 0x00, /* wcChar3 */
'S', 0x00, /* wcChar4 */
' ', 0x00, /* wcChar13 */
'V', 0x00, /* wcChar14 */
'O', 0x00, /* wcChar15 */
'L', 0x00, /* wcChar16 */
'T', 0x00, /* wcChar17 */
'A', 0x00, /* wcChar18 */
'G', 0x00, /* wcChar19 */
'E', 0x00, /* wcChar20 */
' ', 0x00, /* wcChar21 */
'R', 0x00, /* wcChar22 */
'E', 0x00, /* wcChar23 */
'C', 0x00, /* wcChar24 */
/*
* string3 descriptor
*/
0x16, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'2', 0x00, /* wcChar0 */
'0', 0x00, /* wcChar1 */
'2', 0x00, /* wcChar2 */
'3', 0x00, /* wcChar3 */
'/', 0x00, /* wcChar3 */
'1', 0x00, /* wcChar4 */
'2', 0x00, /* wcChar5 */
'/', 0x00, /* wcChar6 */
'1', 0x00, /* wcChar7 */
'5', 0x00, /* wcChar8 */
#ifdef CONFIG_USB_HS
/*
* device qualifier descriptor
*/
0x0a, USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00,
#endif
0x00};
/*!< custom hid report descriptor */
static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
/* USER CODE BEGIN 0 */
0x05, 0x8c, /* USAGE_PAGE (Vendor Defined Page 1) */
0x09, 0x01, /* USAGE (Vendor Usage 1) */
0xa1, 0x01, /* COLLECTION (Application) */
// <___________________________________________________>
// 0x85, 0x02, /* REPORT ID (0x02) */
0x09, 0x03, /* USAGE (Vendor Usage 1) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x40, /* REPORT_COUNT (64) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
//<___________________________________________________>
// 0x85, 0x02, /* REPORT ID (0x02) */
0x09, 0x04, /* USAGE (Vendor Usage 1) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x40, /* REPORT_COUNT (64) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
/* USER CODE END 0 */
0xC0 /* END_COLLECTION */
};

USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE];
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HIDRAW_IN_EP_SIZE];

#define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1

/*!< hid state ! Data can be sent only when state is idle */
static volatile uint8_t custom_state;

void usbd_event_handler(uint8_t event)
{
switch (event)
{
case USBD_EVENT_RESET:
break;
case USBD_EVENT_CONNECTED:
USB_LOG_RAW("Host Connected\n");
break;
case USBD_EVENT_DISCONNECTED:
USB_LOG_RAW("Host DisConnected\n");
dev_rec.start = 0;
break;
case USBD_EVENT_RESUME:
break;
case USBD_EVENT_SUSPEND:
break;
case USBD_EVENT_CONFIGURED:
/* setup first out ep read transfer */
usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, HIDRAW_OUT_EP_SIZE);
break;
case USBD_EVENT_SET_REMOTE_WAKEUP:
break;
case USBD_EVENT_CLR_REMOTE_WAKEUP:
break;

default:
break;
}
}

static void usbd_hid_custom_in_callback(uint8_t ep, uint32_t nbytes)
{
USB_LOG_RAW("actual in len:%d\r\n", nbytes);
custom_state = HID_STATE_IDLE;
}

// 收到的数据中不包含Report ID,是实际长度 即64字节
static void usbd_hid_custom_out_callback(uint8_t ep, uint32_t nbytes)
{
USB_LOG_RAW("actual out len:%d\r\n", nbytes);
usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, 64);
for (uint8_t i = 0; i < 64; i++)
{
rt_kprintf("%02x ", read_buffer[i]);
}
rt_kprintf("\r\n");
dev_rec.cmd = read_buffer[1];
switch (dev_rec.cmd)
{
case SET_RELAY_VALUE:
set_relay(read_buffer[2]);
break;
case GET_INFO_INDS:
case SET_AC_VOLTAGE:
case SET_DC_VOLTAGE:
case GET_MES_VALUE: // 读取测量值
// char *cmd = ":READ?\r\n";
// uart_send_data(cmd, strlen(cmd));
// break;
uart_send_data(&read_buffer[2], read_buffer[0]);
break;
case SET_PARAM_VALUE: // 设置参数
dev_rec.period = read_buffer[2] * 256 + read_buffer[3];
dev_rec.ch = read_buffer[4];
dev_rec.cmd = 0;
// timer_set_timeout(dev_rec.period);
break;
case SET_START_REC: // 开始或者停止采集
case SET_STOP_REC:
dev_rec.start = read_buffer[2];
dev_rec.start ? timer_set_timeout(dev_rec.period) : timer_stop();
break;
default:
break;
}

// rt_kprintf("\n");
// usbd_ep_start_write(HIDRAW_IN_EP, read_buffer, nbytes);
}

static struct usbd_endpoint custom_in_ep = {.ep_cb = usbd_hid_custom_in_callback, .ep_addr = HIDRAW_IN_EP};

static struct usbd_endpoint custom_out_ep = {.ep_cb = usbd_hid_custom_out_callback, .ep_addr = HIDRAW_OUT_EP};

/* function ------------------------------------------------------------------*/
/**
* @brief hid custom init
* @pre none
* @param[in] none
* @retval none
*/
struct usbd_interface intf0;

void hid_custom_init(void)
{
usbd_desc_register(hid_descriptor);
usbd_add_interface(usbd_hid_init_intf(&intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
usbd_add_endpoint(&custom_in_ep);
usbd_add_endpoint(&custom_out_ep);

usbd_initialize();
}
// 发送时 会自动把id放在最前边 默认是0 上位机接收的数据长度是65
void hid_send_data(uint8_t *data, uint8_t len)
{
uint8_t buf[64];
memset(buf, 0, len);
memcpy(buf, data, len);
// for (uint8_t i = 0; i < len; i++)
// {
// rt_kprintf("%02x ", data[i]);
// }
// rt_kprintf("\r\n");
int ret = usbd_ep_start_write(HIDRAW_IN_EP, buf, 64);
if (ret < 0)
{
return;
}
}
// MSH_CMD_EXPORT(hid_custom_test, start hid_custom_test);

原理图

DMM_VOLTAGE_MES_F103.pdf