Skip to content

Commit f708a84

Browse files
committed
target/atmega32u4-generic: configure lufa usb stack
Signed-off-by: Rafael Silva <[email protected]>
1 parent b4848cf commit f708a84

File tree

7 files changed

+610
-1
lines changed

7 files changed

+610
-1
lines changed

config/families/avr.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ld_flags = [
1313
'-Wl,--gc-sections',
1414
]
1515
source = [
16+
'boot.c',
17+
'usb.c',
18+
'usb_descriptors.c',
1619
]
1720

1821
[dependencies]

config/targets/atmega32u4-generic.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ family = 'avr'
22
has-config = true
33
c_flags = [
44
'-mmcu=atmega32u4',
5+
'-DUSE_LUFA_CONFIG_HEADER',
56
]
67
ld_flags = [
78
'-mmcu=atmega32u4',

src/platform/avr/usb.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]>
4+
*/
5+
6+
#include <avr/interrupt.h>
7+
8+
#include "protocol/reports.h"
9+
#include "usb.h"
10+
#include "util/types.h"
11+
12+
#include <USB.h>
13+
14+
static struct protocol_config_t protocol_config;
15+
16+
void usb_attach_protocol_config(struct protocol_config_t config)
17+
{
18+
protocol_config = config;
19+
}
20+
21+
void usb_init()
22+
{
23+
/* Init USB stack */
24+
USB_Init();
25+
}
26+
27+
void usb_task()
28+
{
29+
USB_USBTask();
30+
}
31+
32+
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
33+
* starts the library USB task to begin the enumeration and USB management process.
34+
*/
35+
void EVENT_USB_Device_Connect(void)
36+
{
37+
}
38+
39+
/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
40+
* the status LEDs and stops the USB management task.
41+
*/
42+
void EVENT_USB_Device_Disconnect(void)
43+
{
44+
}
45+
46+
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
47+
* the device from the USB host before passing along unhandled control requests to the library for processing
48+
* internally.
49+
*/
50+
void EVENT_USB_Device_ControlRequest(void)
51+
{
52+
struct oi_report_t oi_report;
53+
uint8_t *ReportData;
54+
uint8_t ReportSize;
55+
56+
/* Handle HID Class specific requests */
57+
switch (USB_ControlRequest.bRequest) {
58+
case HID_REQ_GetReport:
59+
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
60+
Endpoint_ClearSETUP();
61+
62+
// /* Determine if it is the mouse or the keyboard data that is being requested */
63+
// if (USB_ControlRequest.wIndex == 0) { // openinput
64+
// } else if (USB_ControlRequest.wIndex == 1) { // mouse
65+
// } else if (USB_ControlRequest.wIndex == 2) { // keyboard
66+
// }
67+
68+
// /* Write the report data to the control endpoint */
69+
// Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
70+
Endpoint_ClearOUT();
71+
}
72+
73+
break;
74+
case HID_REQ_SetReport:
75+
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
76+
Endpoint_ClearSETUP();
77+
78+
/* Wait until the report has been sent by the host */
79+
while (!(Endpoint_IsOUTReceived())) {
80+
if (USB_DeviceState == DEVICE_STATE_Unattached)
81+
return;
82+
}
83+
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);
90+
91+
} else if (USB_ControlRequest.wIndex == 2) {
92+
/* Read in the LED report from the host */
93+
uint8_t LEDStatus = Endpoint_Read_8();
94+
}
95+
96+
Endpoint_ClearOUT();
97+
Endpoint_ClearStatusStage();
98+
}
99+
100+
break;
101+
}
102+
}

src/platform/avr/usb.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]>
4+
*/
5+
6+
#pragma once
7+
8+
#include "protocol/protocol.h"
9+
10+
void usb_init();
11+
void usb_task();
12+
void usb_attach_protocol_config(struct protocol_config_t config);

0 commit comments

Comments
 (0)