Skip to content

Commit 13e841a

Browse files
committed
target/atmega32u4-generic: configure usb descriptors and endpoints
Signed-off-by: Rafael Silva <[email protected]>
1 parent f708a84 commit 13e841a

File tree

2 files changed

+116
-43
lines changed

2 files changed

+116
-43
lines changed

src/platform/avr/usb.c

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "protocol/reports.h"
99
#include "usb.h"
10+
#include "util/hid_descriptors.h"
1011
#include "util/types.h"
1112

1213
#include <USB.h>
@@ -27,6 +28,66 @@ void usb_init()
2728
void usb_task()
2829
{
2930
USB_USBTask();
31+
32+
/* openinput IN */
33+
Endpoint_SelectEndpoint(0x81);
34+
35+
/* Check if Endpoint Ready for Read/Write */
36+
if (Endpoint_IsReadWriteAllowed()) {
37+
/* Write Report Data */
38+
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);
39+
40+
/* Finalize the stream transfer to send the last packet */
41+
// Endpoint_ClearIN();
42+
}
43+
44+
/* openinput OUT */
45+
Endpoint_SelectEndpoint(0x02);
46+
47+
/* Check if Endpoint Ready for Read/Write */
48+
if (Endpoint_IsReadWriteAllowed()) {
49+
/* Write Report Data */
50+
// Keyboard_ProcessLEDReport(Endpoint_Read_8());
51+
52+
/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
53+
Endpoint_ClearOUT();
54+
}
55+
56+
/* mouse IN */
57+
Endpoint_SelectEndpoint(0x83);
58+
59+
/* Check if Endpoint Ready for Read/Write */
60+
if (Endpoint_IsReadWriteAllowed()) {
61+
/* Write Report Data */
62+
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);
63+
64+
/* Finalize the stream transfer to send the last packet */
65+
// Endpoint_ClearIN();
66+
}
67+
68+
/* keyboard IN */
69+
Endpoint_SelectEndpoint(0x84);
70+
71+
/* Check if Endpoint Ready for Read/Write */
72+
if (Endpoint_IsReadWriteAllowed()) {
73+
/* Write Report Data */
74+
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);
75+
76+
/* Finalize the stream transfer to send the last packet */
77+
// Endpoint_ClearIN();
78+
}
79+
80+
/* keyboard OUT */
81+
Endpoint_SelectEndpoint(0x05);
82+
83+
/* Check if Endpoint Ready for Read/Write */
84+
if (Endpoint_IsReadWriteAllowed()) {
85+
/* Write Report Data */
86+
// Keyboard_ProcessLEDReport(Endpoint_Read_8());
87+
88+
/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
89+
Endpoint_ClearOUT();
90+
}
3091
}
3192

3293
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
@@ -43,6 +104,10 @@ void EVENT_USB_Device_Disconnect(void)
43104
{
44105
}
45106

107+
struct oi_report_t oi_rep;
108+
struct mouse_report mouse_rep;
109+
struct keyboard_report keyb_rep;
110+
46111
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
47112
* the device from the USB host before passing along unhandled control requests to the library for processing
48113
* internally.
@@ -61,8 +126,14 @@ void EVENT_USB_Device_ControlRequest(void)
61126

62127
// /* Determine if it is the mouse or the keyboard data that is being requested */
63128
// if (USB_ControlRequest.wIndex == 0) { // openinput
129+
// ReportData = (uint8_t *) &oi_rep;
130+
// ReportSize = sizeof(struct oi_report_t);
64131
// } else if (USB_ControlRequest.wIndex == 1) { // mouse
132+
// ReportData = (uint8_t *) &mouse_rep;
133+
// ReportSize = sizeof(struct mouse_report);
65134
// } else if (USB_ControlRequest.wIndex == 2) { // keyboard
135+
// ReportData = (uint8_t *) &keyb_rep;
136+
// ReportSize = sizeof(struct keyboard_report);
66137
// }
67138

68139
// /* Write the report data to the control endpoint */
@@ -81,17 +152,20 @@ void EVENT_USB_Device_ControlRequest(void)
81152
return;
82153
}
83154

84-
if (USB_ControlRequest.wIndex == 0) {
85-
// for (size_t i = 0; i < USB_ControlRequest.wLength; i++)
86-
// {
87-
// ((uint8_t *)(&oi_report))[i] = Endpoint_Read_8();
88-
// }
89-
// protocol_dispatch(protocol_config, (uint8_t *)(&oi_report), USB_ControlRequest.wLength);
155+
// if (USB_ControlRequest.wIndex == 0) {
156+
// // for (size_t i = 0; i < USB_ControlRequest.wLength; i++)
157+
// // {
158+
// // ((uint8_t *)(&oi_report))[i] = Endpoint_Read_8();
159+
// // }
160+
// // protocol_dispatch(protocol_config, (uint8_t *)(&oi_report), USB_ControlRequest.wLength);
90161

91-
} else if (USB_ControlRequest.wIndex == 2) {
92-
/* Read in the LED report from the host */
93-
uint8_t LEDStatus = Endpoint_Read_8();
94-
}
162+
// } else if (USB_ControlRequest.wIndex == 2) {
163+
// /* Read in the LED report from the host */
164+
// uint8_t LEDStatus = Endpoint_Read_8();
165+
// }
166+
167+
/* Read in the LED report from the host */
168+
uint8_t LEDStatus = Endpoint_Read_8();
95169

96170
Endpoint_ClearOUT();
97171
Endpoint_ClearStatusStage();
@@ -100,3 +174,20 @@ void EVENT_USB_Device_ControlRequest(void)
100174
break;
101175
}
102176
}
177+
178+
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
179+
* of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
180+
*/
181+
void EVENT_USB_Device_ConfigurationChanged(void)
182+
{
183+
/* Setup Openinput Report Endpoints */
184+
Endpoint_ConfigureEndpoint(0x81, EP_TYPE_INTERRUPT, 0x40, 1); // IN
185+
Endpoint_ConfigureEndpoint(0x02, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
186+
187+
/* Setup Mouse HID Report Endpoint */
188+
Endpoint_ConfigureEndpoint(0x83, EP_TYPE_INTERRUPT, 0x40, 1); // IN
189+
190+
/* Setup Keyboard HID Report Endpoints */
191+
Endpoint_ConfigureEndpoint(0x84, EP_TYPE_INTERRUPT, 0x40, 1); // IN
192+
Endpoint_ConfigureEndpoint(0x05, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
193+
}

src/platform/avr/usb_descriptors.c

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "util/types.h"
1212

1313
/* Device descriptor */
14-
const u8 PROGMEM desc_device[] = {
14+
const u8 __attribute__((__progmem__)) desc_device[] = {
1515
/* clang-format off */
1616
0x12, /* LENGTH (18) */
1717
0x01, /* DESCRIPTOR TYPE (Device) */
@@ -30,7 +30,7 @@ const u8 PROGMEM desc_device[] = {
3030
/* clang-format on */
3131
};
3232

33-
const u8 PROGMEM oi_rdesc[] = {
33+
const u8 __attribute__((__progmem__)) oi_rdesc[] = {
3434
/* clang-format off */
3535
/* short report */
3636
0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Page) */
@@ -64,7 +64,7 @@ const u8 PROGMEM oi_rdesc[] = {
6464
};
6565

6666
/* HID Mouse report descriptor */
67-
const u8 PROGMEM desc_hid_mouse_report[] = {
67+
const u8 __attribute__((__progmem__)) desc_hid_mouse_report[] = {
6868
/* clang-format off */
6969
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
7070
0x09, 0x02, /* USAGE (Mouse) */
@@ -98,7 +98,7 @@ const u8 PROGMEM desc_hid_mouse_report[] = {
9898
};
9999

100100
/* HID keyboard report descriptor */
101-
const u8 PROGMEM desc_hid_keyboard_report[] = {
101+
const u8 __attribute__((__progmem__)) desc_hid_keyboard_report[] = {
102102
/* clang-format off */
103103
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
104104
0x09, 0x06, /* USAGE (keyboard) */
@@ -142,7 +142,7 @@ const u8 PROGMEM desc_hid_keyboard_report[] = {
142142
/* clang-format on */
143143
};
144144

145-
const u8 PROGMEM oi_hid_desc[] = {
145+
const u8 __attribute__((__progmem__)) oi_hid_desc[] = {
146146
/* HID */
147147
0x09, /* LENGTH */
148148
0x21, /* DESCRIPTOR TYPE (hid) */
@@ -155,7 +155,7 @@ const u8 PROGMEM oi_hid_desc[] = {
155155
0x00, /* DESCRIPTOR LENGTH () */
156156
};
157157

158-
const u8 PROGMEM mouse_hid_desc[] = {
158+
const u8 __attribute__((__progmem__)) mouse_hid_desc[] = {
159159
/* HID */
160160
0x09, /* LENGTH */
161161
0x21, /* DESCRIPTOR TYPE (hid) */
@@ -168,7 +168,7 @@ const u8 PROGMEM mouse_hid_desc[] = {
168168
0x00, /* DESCRIPTOR LENGTH () */
169169
};
170170

171-
const u8 PROGMEM keyboard_hid_desc[] = {
171+
const u8 __attribute__((__progmem__)) keyboard_hid_desc[] = {
172172
/* HID */
173173
0x09, /* LENGTH */
174174
0x21, /* DESCRIPTOR TYPE (hid) */
@@ -182,7 +182,7 @@ const u8 PROGMEM keyboard_hid_desc[] = {
182182
};
183183

184184
/* Configuration Descriptor */
185-
const u8 PROGMEM desc_configuration[] = {
185+
const u8 __attribute__((__progmem__)) desc_configuration[] = {
186186
/* clang-format off */
187187
/* configuration */
188188
0x09, /* LENGTH */
@@ -223,12 +223,11 @@ const u8 PROGMEM desc_configuration[] = {
223223
/* Endpoint out */
224224
0x07, /* LENGTH */
225225
0x05, /* DESCRIPTOR TYPE (Endpoint) */
226-
0x01, /* ENDPOINT ADDRESS (Endpoint 1, OUT) */
226+
0x02, /* ENDPOINT ADDRESS (Endpoint 2, OUT) */
227227
0x03, /* ATTRIBUTES (Interrupt) */
228228
0x40, 0x00, /* MAX PACKET SIZE (64) */
229229
0x0A, /* POLLING INTERVAL (100Hz) */
230230

231-
232231
/* Interface 1 - HID Mouse */
233232
0x09, /* LENGTH */
234233
0x04, /* DESCRIPTOR TYPE (Interface) */
@@ -251,7 +250,7 @@ const u8 PROGMEM desc_configuration[] = {
251250
/* Endpoint in */
252251
0x07, /* LENGTH */
253252
0x05, /* DESCRIPTOR TYPE (Endpoint) */
254-
0x82, /* ENDPOINT ADDRESS (Endpoint 2, IN) */
253+
0x83, /* ENDPOINT ADDRESS (Endpoint 3, IN) */
255254
0x03, /* ATTRIBUTES (Interrupt) */
256255
0x40, 0x00, /* MAX PACKET SIZE (64) */
257256
0x01, /* POLLING INTERVAL (1000Hz) */
@@ -279,24 +278,24 @@ const u8 PROGMEM desc_configuration[] = {
279278
/* Endpoint in */
280279
0x07, /* LENGTH */
281280
0x05, /* DESCRIPTOR TYPE (Endpoint) */
282-
0x83, /* ENDPOINT ADDRESS (Endpoint 3, IN) */
281+
0x84, /* ENDPOINT ADDRESS (Endpoint 4, IN) */
283282
0x03, /* ATTRIBUTES (Interrupt) */
284283
0x40, 0x00, /* MAX PACKET SIZE (64) */
285284
0x0A, /* POLLING INTERVAL (100Hz) */
286285
/* Endpoint out */
287286
0x07, /* LENGTH */
288287
0x05, /* DESCRIPTOR TYPE (Endpoint) */
289-
0x03, /* ENDPOINT ADDRESS (Endpoint 3, OUT) */
288+
0x05, /* ENDPOINT ADDRESS (Endpoint 5, OUT) */
290289
0x03, /* ATTRIBUTES (Interrupt) */
291290
0x40, 0x00, /* MAX PACKET SIZE (64) */
292291
0x0A, /* POLLING INTERVAL (100Hz) */
293292
/* clang-format on */
294293
};
295294

296295
/* String Descriptors */
297-
const USB_Descriptor_String_t PROGMEM lang_str = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
298-
const USB_Descriptor_String_t PROGMEM manu_str = USB_STRING_DESCRIPTOR(L"Openinput");
299-
const USB_Descriptor_String_t PROGMEM prod_str = USB_STRING_DESCRIPTOR(L"Openinput Device");
296+
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM lang_str = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
297+
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM manu_str = USB_STRING_DESCRIPTOR(L"Openinput");
298+
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM prod_str = USB_STRING_DESCRIPTOR(L"Openinput Device");
300299

301300
/**
302301
* This function is called by the library when in device mode, and must be overridden (see LUFA library "USB Descriptors"
@@ -380,20 +379,3 @@ u16 CALLBACK_USB_GetDescriptor(const u16 w_value, const u16 w_index, const void
380379
*descriptor_address = addr;
381380
return size;
382381
}
383-
384-
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
385-
* of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
386-
*/
387-
void EVENT_USB_Device_ConfigurationChanged(void)
388-
{
389-
/* Setup Openinput Report Endpoints */
390-
Endpoint_ConfigureEndpoint(0x81, EP_TYPE_INTERRUPT, 0x40, 1); // IN
391-
Endpoint_ConfigureEndpoint(0x01, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
392-
393-
/* Setup Mouse HID Report Endpoint */
394-
Endpoint_ConfigureEndpoint(0x82, EP_TYPE_INTERRUPT, 0x40, 1); // IN
395-
396-
/* Setup Keyboard HID Report Endpoints */
397-
Endpoint_ConfigureEndpoint(0x83, EP_TYPE_INTERRUPT, 0x40, 1); // IN
398-
Endpoint_ConfigureEndpoint(0x03, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
399-
}

0 commit comments

Comments
 (0)