• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "usbd_core.h"
2 #include "usbd_hid.h"
3 
4 #define USBD_VID           0xffff
5 #define USBD_PID           0xffff
6 #define USBD_MAX_POWER     100
7 #define USBD_LANGID_STRING 1033
8 
9 #define HID_INT_EP          0x81
10 #define HID_INT_EP_SIZE     8
11 #define HID_INT_EP_INTERVAL 10
12 
13 #define USB_HID_CONFIG_DESC_SIZ       34
14 #define HID_KEYBOARD_REPORT_DESC_SIZE 63
15 
16 static const uint8_t hid_descriptor[] = {
17     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
18     USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
19 
20     /************** Descriptor of Joystick Mouse interface ****************/
21     /* 09 */
22     0x09,                          /* bLength: Interface Descriptor size */
23     USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
24     0x00,                          /* bInterfaceNumber: Number of Interface */
25     0x00,                          /* bAlternateSetting: Alternate setting */
26     0x01,                          /* bNumEndpoints */
27     0x03,                          /* bInterfaceClass: HID */
28     0x01,                          /* bInterfaceSubClass : 1=BOOT, 0=no boot */
29     0x01,                          /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
30     0,                             /* iInterface: Index of string descriptor */
31     /******************** Descriptor of Joystick Mouse HID ********************/
32     /* 18 */
33     0x09,                    /* bLength: HID Descriptor size */
34     HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
35     0x11,                    /* bcdHID: HID Class Spec release number */
36     0x01,
37     0x00,                          /* bCountryCode: Hardware target country */
38     0x01,                          /* bNumDescriptors: Number of HID class descriptors to follow */
39     0x22,                          /* bDescriptorType */
40     HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
41     0x00,
42     /******************** Descriptor of Mouse endpoint ********************/
43     /* 27 */
44     0x07,                         /* bLength: Endpoint Descriptor size */
45     USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
46     HID_INT_EP,                   /* bEndpointAddress: Endpoint Address (IN) */
47     0x03,                         /* bmAttributes: Interrupt endpoint */
48     HID_INT_EP_SIZE,              /* wMaxPacketSize: 4 Byte max */
49     0x00,
50     HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
51     /* 34 */
52     ///////////////////////////////////////
53     /// string0 descriptor
54     ///////////////////////////////////////
55     USB_LANGID_INIT(USBD_LANGID_STRING),
56     ///////////////////////////////////////
57     /// string1 descriptor
58     ///////////////////////////////////////
59     0x14,                       /* bLength */
60     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
61     'C', 0x00,                  /* wcChar0 */
62     'h', 0x00,                  /* wcChar1 */
63     'e', 0x00,                  /* wcChar2 */
64     'r', 0x00,                  /* wcChar3 */
65     'r', 0x00,                  /* wcChar4 */
66     'y', 0x00,                  /* wcChar5 */
67     'U', 0x00,                  /* wcChar6 */
68     'S', 0x00,                  /* wcChar7 */
69     'B', 0x00,                  /* wcChar8 */
70     ///////////////////////////////////////
71     /// string2 descriptor
72     ///////////////////////////////////////
73     0x26,                       /* bLength */
74     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
75     'C', 0x00,                  /* wcChar0 */
76     'h', 0x00,                  /* wcChar1 */
77     'e', 0x00,                  /* wcChar2 */
78     'r', 0x00,                  /* wcChar3 */
79     'r', 0x00,                  /* wcChar4 */
80     'y', 0x00,                  /* wcChar5 */
81     'U', 0x00,                  /* wcChar6 */
82     'S', 0x00,                  /* wcChar7 */
83     'B', 0x00,                  /* wcChar8 */
84     ' ', 0x00,                  /* wcChar9 */
85     'H', 0x00,                  /* wcChar10 */
86     'I', 0x00,                  /* wcChar11 */
87     'D', 0x00,                  /* wcChar12 */
88     ' ', 0x00,                  /* wcChar13 */
89     'D', 0x00,                  /* wcChar14 */
90     'E', 0x00,                  /* wcChar15 */
91     'M', 0x00,                  /* wcChar16 */
92     'O', 0x00,                  /* wcChar17 */
93     ///////////////////////////////////////
94     /// string3 descriptor
95     ///////////////////////////////////////
96     0x16,                       /* bLength */
97     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
98     '2', 0x00,                  /* wcChar0 */
99     '0', 0x00,                  /* wcChar1 */
100     '2', 0x00,                  /* wcChar2 */
101     '2', 0x00,                  /* wcChar3 */
102     '1', 0x00,                  /* wcChar4 */
103     '2', 0x00,                  /* wcChar5 */
104     '3', 0x00,                  /* wcChar6 */
105     '4', 0x00,                  /* wcChar7 */
106     '5', 0x00,                  /* wcChar8 */
107     '6', 0x00,                  /* wcChar9 */
108 #ifdef CONFIG_USB_HS
109     ///////////////////////////////////////
110     /// device qualifier descriptor
111     ///////////////////////////////////////
112     0x0a,
113     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
114     0x00,
115     0x02,
116     0x00,
117     0x00,
118     0x00,
119     0x40,
120     0x01,
121     0x00,
122 #endif
123     0x00
124 };
125 
126 /* USB HID device Configuration Descriptor */
127 static uint8_t hid_desc[9] __ALIGN_END = {
128     /* 18 */
129     0x09,                    /* bLength: HID Descriptor size */
130     HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
131     0x11,                    /* bcdHID: HID Class Spec release number */
132     0x01,
133     0x00,                          /* bCountryCode: Hardware target country */
134     0x01,                          /* bNumDescriptors: Number of HID class descriptors to follow */
135     0x22,                          /* bDescriptorType */
136     HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
137     0x00,
138 };
139 
140 static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
141     0x05, 0x01, // USAGE_PAGE (Generic Desktop)
142     0x09, 0x06, // USAGE (Keyboard)
143     0xa1, 0x01, // COLLECTION (Application)
144     0x05, 0x07, // USAGE_PAGE (Keyboard)
145     0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
146     0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
147     0x15, 0x00, // LOGICAL_MINIMUM (0)
148     0x25, 0x01, // LOGICAL_MAXIMUM (1)
149     0x75, 0x01, // REPORT_SIZE (1)
150     0x95, 0x08, // REPORT_COUNT (8)
151     0x81, 0x02, // INPUT (Data,Var,Abs)
152     0x95, 0x01, // REPORT_COUNT (1)
153     0x75, 0x08, // REPORT_SIZE (8)
154     0x81, 0x03, // INPUT (Cnst,Var,Abs)
155     0x95, 0x05, // REPORT_COUNT (5)
156     0x75, 0x01, // REPORT_SIZE (1)
157     0x05, 0x08, // USAGE_PAGE (LEDs)
158     0x19, 0x01, // USAGE_MINIMUM (Num Lock)
159     0x29, 0x05, // USAGE_MAXIMUM (Kana)
160     0x91, 0x02, // OUTPUT (Data,Var,Abs)
161     0x95, 0x01, // REPORT_COUNT (1)
162     0x75, 0x03, // REPORT_SIZE (3)
163     0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
164     0x95, 0x06, // REPORT_COUNT (6)
165     0x75, 0x08, // REPORT_SIZE (8)
166     0x15, 0x00, // LOGICAL_MINIMUM (0)
167     0x25, 0xFF, // LOGICAL_MAXIMUM (255)
168     0x05, 0x07, // USAGE_PAGE (Keyboard)
169     0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
170     0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
171     0x81, 0x00, // INPUT (Data,Ary,Abs)
172     0xc0        // END_COLLECTION
173 };
174 
usbd_event_handler(uint8_t event)175 void usbd_event_handler(uint8_t event)
176 {
177     switch (event) {
178         case USBD_EVENT_RESET:
179             break;
180         case USBD_EVENT_CONNECTED:
181             break;
182         case USBD_EVENT_DISCONNECTED:
183             break;
184         case USBD_EVENT_RESUME:
185             break;
186         case USBD_EVENT_SUSPEND:
187             break;
188         case USBD_EVENT_CONFIGURED:
189             break;
190         case USBD_EVENT_SET_REMOTE_WAKEUP:
191             break;
192         case USBD_EVENT_CLR_REMOTE_WAKEUP:
193             break;
194 
195         default:
196             break;
197     }
198 }
199 
200 #define HID_STATE_IDLE 0
201 #define HID_STATE_BUSY 1
202 
203 /*!< hid state ! Data can be sent only when state is idle  */
204 static volatile uint8_t hid_state = HID_STATE_IDLE;
205 
usbd_hid_int_callback(uint8_t ep,uint32_t nbytes)206 void usbd_hid_int_callback(uint8_t ep, uint32_t nbytes)
207 {
208     hid_state = HID_STATE_IDLE;
209 }
210 
211 static struct usbd_endpoint hid_in_ep = {
212     .ep_cb = usbd_hid_int_callback,
213     .ep_addr = HID_INT_EP
214 };
215 
216 struct usbd_interface intf0;
217 
hid_keyboard_init(void)218 void hid_keyboard_init(void)
219 {
220     usbd_desc_register(hid_descriptor);
221     usbd_add_interface(usbd_hid_init_intf(&intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
222     usbd_add_endpoint(&hid_in_ep);
223 
224     usbd_initialize();
225 }
226 
227 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
228 
hid_keyboard_test(void)229 void hid_keyboard_test(void)
230 {
231     const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
232 
233     memcpy(write_buffer, sendbuffer, 8);
234     int ret = usbd_ep_start_write(HID_INT_EP, write_buffer, 8);
235     if (ret < 0) {
236         return;
237     }
238     hid_state = HID_STATE_BUSY;
239     while (hid_state == HID_STATE_BUSY) {
240     }
241 }
242