Skip to content

Commit

Permalink
target/atmega32u4-generic: DRAFT configure usb descriptors and endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Silva <[email protected]>
  • Loading branch information
perigoso committed May 15, 2022
1 parent 09deaf3 commit 8d74fc7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 50 deletions.
134 changes: 117 additions & 17 deletions src/platform/avr/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "protocol/reports.h"
#include "usb.h"
#include "util/hid_descriptors.h"
#include "util/types.h"

#include <USB.h>
Expand All @@ -27,6 +28,75 @@ void usb_init()
void usb_task()
{
USB_USBTask();

/* openinput IN */
Endpoint_SelectEndpoint(0x81);

/* Check if Endpoint Ready for Read/Write */
if (Endpoint_IsReadWriteAllowed()) {
/* Write Report Data */
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);

/* Finalize the stream transfer to send the last packet */
// Endpoint_ClearIN();

/* Clear the report data afterwards */
// memset(&report_data, 0, sizeof(report_data));
}

/* openinput OUT */
Endpoint_SelectEndpoint(0x02);

/* Check if Endpoint Ready for Read/Write */
if (Endpoint_IsReadWriteAllowed()) {
/* Write Report Data */
// Keyboard_ProcessLEDReport(Endpoint_Read_8());

/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
Endpoint_ClearOUT();
}

/* mouse IN */
Endpoint_SelectEndpoint(0x83);

/* Check if Endpoint Ready for Read/Write */
if (Endpoint_IsReadWriteAllowed()) {
/* Write Report Data */
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);

/* Finalize the stream transfer to send the last packet */
// Endpoint_ClearIN();

/* Clear the report data afterwards */
// memset(&report_data, 0, sizeof(report_data));
}

/* keyboard IN */
Endpoint_SelectEndpoint(0x84);

/* Check if Endpoint Ready for Read/Write */
if (Endpoint_IsReadWriteAllowed()) {
/* Write Report Data */
// Endpoint_Write_Stream_LE(&report_data, sizeof(report_data), NULL);

/* Finalize the stream transfer to send the last packet */
// Endpoint_ClearIN();

/* Clear the report data afterwards */
// memset(&report_data, 0, sizeof(report_data));
}

/* keyboard OUT */
Endpoint_SelectEndpoint(0x05);

/* Check if Endpoint Ready for Read/Write */
if (Endpoint_IsReadWriteAllowed()) {
/* Write Report Data */
// Keyboard_ProcessLEDReport(Endpoint_Read_8());

/* Handshake the OUT Endpoint - clear endpoint and ready for next report */
Endpoint_ClearOUT();
}
}

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

struct oi_report_t oi_rep;
struct mouse_report mouse_rep;
struct keyboard_report keyb_rep;

/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
* the device from the USB host before passing along unhandled control requests to the library for processing
* internally.
Expand All @@ -59,14 +133,20 @@ void EVENT_USB_Device_ControlRequest(void)
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
Endpoint_ClearSETUP();

// /* Determine if it is the mouse or the keyboard data that is being requested */
// if (USB_ControlRequest.wIndex == 0) { // openinput
// } else if (USB_ControlRequest.wIndex == 1) { // mouse
// } else if (USB_ControlRequest.wIndex == 2) { // keyboard
// }
/* Determine if it is the mouse or the keyboard data that is being requested */
if (USB_ControlRequest.wIndex == 0) { // openinput
ReportData = (uint8_t *) &oi_rep;
ReportSize = sizeof(struct oi_report_t);
} else if (USB_ControlRequest.wIndex == 1) { // mouse
ReportData = (uint8_t *) &mouse_rep;
ReportSize = sizeof(struct mouse_report);
} else if (USB_ControlRequest.wIndex == 2) { // keyboard
ReportData = (uint8_t *) &keyb_rep;
ReportSize = sizeof(struct keyboard_report);
}

// /* Write the report data to the control endpoint */
// Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
/* Write the report data to the control endpoint */
Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
Endpoint_ClearOUT();
}

Expand All @@ -81,17 +161,20 @@ void EVENT_USB_Device_ControlRequest(void)
return;
}

if (USB_ControlRequest.wIndex == 0) {
// for (size_t i = 0; i < USB_ControlRequest.wLength; i++)
// {
// ((uint8_t *)(&oi_report))[i] = Endpoint_Read_8();
// }
// protocol_dispatch(protocol_config, (uint8_t *)(&oi_report), USB_ControlRequest.wLength);
// if (USB_ControlRequest.wIndex == 0) {
// // for (size_t i = 0; i < USB_ControlRequest.wLength; i++)
// // {
// // ((uint8_t *)(&oi_report))[i] = Endpoint_Read_8();
// // }
// // protocol_dispatch(protocol_config, (uint8_t *)(&oi_report), USB_ControlRequest.wLength);

} else if (USB_ControlRequest.wIndex == 2) {
/* Read in the LED report from the host */
uint8_t LEDStatus = Endpoint_Read_8();
}
// } else if (USB_ControlRequest.wIndex == 2) {
// /* Read in the LED report from the host */
// uint8_t LEDStatus = Endpoint_Read_8();
// }

/* Read in the LED report from the host */
uint8_t LEDStatus = Endpoint_Read_8();

Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
Expand All @@ -100,3 +183,20 @@ void EVENT_USB_Device_ControlRequest(void)
break;
}
}

/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
* of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
*/
void EVENT_USB_Device_ConfigurationChanged(void)
{
/* Setup Openinput Report Endpoints */
Endpoint_ConfigureEndpoint(0x81, EP_TYPE_INTERRUPT, 0x40, 1); // IN
Endpoint_ConfigureEndpoint(0x02, EP_TYPE_INTERRUPT, 0x40, 1); // OUT

/* Setup Mouse HID Report Endpoint */
Endpoint_ConfigureEndpoint(0x83, EP_TYPE_INTERRUPT, 0x40, 1); // IN

/* Setup Keyboard HID Report Endpoints */
Endpoint_ConfigureEndpoint(0x84, EP_TYPE_INTERRUPT, 0x40, 1); // IN
Endpoint_ConfigureEndpoint(0x05, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
}
48 changes: 15 additions & 33 deletions src/platform/avr/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "util/types.h"

/* Device descriptor */
const u8 PROGMEM desc_device[] = {
const u8 __attribute__((__progmem__)) desc_device[] = {
/* clang-format off */
0x12, /* LENGTH (18) */
0x01, /* DESCRIPTOR TYPE (Device) */
Expand All @@ -30,7 +30,7 @@ const u8 PROGMEM desc_device[] = {
/* clang-format on */
};

const u8 PROGMEM oi_rdesc[] = {
const u8 __attribute__((__progmem__)) oi_rdesc[] = {
/* clang-format off */
/* short report */
0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Page) */
Expand Down Expand Up @@ -64,7 +64,7 @@ const u8 PROGMEM oi_rdesc[] = {
};

/* HID Mouse report descriptor */
const u8 PROGMEM desc_hid_mouse_report[] = {
const u8 __attribute__((__progmem__)) desc_hid_mouse_report[] = {
/* clang-format off */
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x02, /* USAGE (Mouse) */
Expand Down Expand Up @@ -98,7 +98,7 @@ const u8 PROGMEM desc_hid_mouse_report[] = {
};

/* HID keyboard report descriptor */
const u8 PROGMEM desc_hid_keyboard_report[] = {
const u8 __attribute__((__progmem__)) desc_hid_keyboard_report[] = {
/* clang-format off */
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x06, /* USAGE (keyboard) */
Expand Down Expand Up @@ -142,7 +142,7 @@ const u8 PROGMEM desc_hid_keyboard_report[] = {
/* clang-format on */
};

const u8 PROGMEM oi_hid_desc[] = {
const u8 __attribute__((__progmem__)) oi_hid_desc[] = {
/* HID */
0x09, /* LENGTH */
0x21, /* DESCRIPTOR TYPE (hid) */
Expand All @@ -155,7 +155,7 @@ const u8 PROGMEM oi_hid_desc[] = {
0x00, /* DESCRIPTOR LENGTH () */
};

const u8 PROGMEM mouse_hid_desc[] = {
const u8 __attribute__((__progmem__)) mouse_hid_desc[] = {
/* HID */
0x09, /* LENGTH */
0x21, /* DESCRIPTOR TYPE (hid) */
Expand All @@ -168,7 +168,7 @@ const u8 PROGMEM mouse_hid_desc[] = {
0x00, /* DESCRIPTOR LENGTH () */
};

const u8 PROGMEM keyboard_hid_desc[] = {
const u8 __attribute__((__progmem__)) keyboard_hid_desc[] = {
/* HID */
0x09, /* LENGTH */
0x21, /* DESCRIPTOR TYPE (hid) */
Expand All @@ -182,7 +182,7 @@ const u8 PROGMEM keyboard_hid_desc[] = {
};

/* Configuration Descriptor */
const u8 PROGMEM desc_configuration[] = {
const u8 __attribute__((__progmem__)) desc_configuration[] = {
/* clang-format off */
/* configuration */
0x09, /* LENGTH */
Expand Down Expand Up @@ -223,12 +223,11 @@ const u8 PROGMEM desc_configuration[] = {
/* Endpoint out */
0x07, /* LENGTH */
0x05, /* DESCRIPTOR TYPE (Endpoint) */
0x01, /* ENDPOINT ADDRESS (Endpoint 1, OUT) */
0x02, /* ENDPOINT ADDRESS (Endpoint 2, OUT) */
0x03, /* ATTRIBUTES (Interrupt) */
0x40, 0x00, /* MAX PACKET SIZE (64) */
0x0A, /* POLLING INTERVAL (100Hz) */


/* Interface 1 - HID Mouse */
0x09, /* LENGTH */
0x04, /* DESCRIPTOR TYPE (Interface) */
Expand All @@ -251,7 +250,7 @@ const u8 PROGMEM desc_configuration[] = {
/* Endpoint in */
0x07, /* LENGTH */
0x05, /* DESCRIPTOR TYPE (Endpoint) */
0x82, /* ENDPOINT ADDRESS (Endpoint 2, IN) */
0x83, /* ENDPOINT ADDRESS (Endpoint 3, IN) */
0x03, /* ATTRIBUTES (Interrupt) */
0x40, 0x00, /* MAX PACKET SIZE (64) */
0x01, /* POLLING INTERVAL (1000Hz) */
Expand Down Expand Up @@ -279,24 +278,24 @@ const u8 PROGMEM desc_configuration[] = {
/* Endpoint in */
0x07, /* LENGTH */
0x05, /* DESCRIPTOR TYPE (Endpoint) */
0x83, /* ENDPOINT ADDRESS (Endpoint 3, IN) */
0x84, /* ENDPOINT ADDRESS (Endpoint 4, IN) */
0x03, /* ATTRIBUTES (Interrupt) */
0x40, 0x00, /* MAX PACKET SIZE (64) */
0x0A, /* POLLING INTERVAL (100Hz) */
/* Endpoint out */
0x07, /* LENGTH */
0x05, /* DESCRIPTOR TYPE (Endpoint) */
0x03, /* ENDPOINT ADDRESS (Endpoint 3, OUT) */
0x05, /* ENDPOINT ADDRESS (Endpoint 5, OUT) */
0x03, /* ATTRIBUTES (Interrupt) */
0x40, 0x00, /* MAX PACKET SIZE (64) */
0x0A, /* POLLING INTERVAL (100Hz) */
/* clang-format on */
};

/* String Descriptors */
const USB_Descriptor_String_t PROGMEM lang_str = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
const USB_Descriptor_String_t PROGMEM manu_str = USB_STRING_DESCRIPTOR(L"Openinput");
const USB_Descriptor_String_t PROGMEM prod_str = USB_STRING_DESCRIPTOR(L"Openinput Device");
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM lang_str = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM manu_str = USB_STRING_DESCRIPTOR(L"Openinput");
const USB_Descriptor_String_t __attribute__((__progmem__)) PROGMEM prod_str = USB_STRING_DESCRIPTOR(L"Openinput Device");

/**
* This function is called by the library when in device mode, and must be overridden (see LUFA library "USB Descriptors"
Expand Down Expand Up @@ -380,20 +379,3 @@ u16 CALLBACK_USB_GetDescriptor(const u16 w_value, const u16 w_index, const void
*descriptor_address = addr;
return size;
}

/** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
* of the USB device after enumeration, and configures the keyboard and mouse device endpoints.
*/
void EVENT_USB_Device_ConfigurationChanged(void)
{
/* Setup Openinput Report Endpoints */
Endpoint_ConfigureEndpoint(0x81, EP_TYPE_INTERRUPT, 0x40, 1); // IN
Endpoint_ConfigureEndpoint(0x01, EP_TYPE_INTERRUPT, 0x40, 1); // OUT

/* Setup Mouse HID Report Endpoint */
Endpoint_ConfigureEndpoint(0x82, EP_TYPE_INTERRUPT, 0x40, 1); // IN

/* Setup Keyboard HID Report Endpoints */
Endpoint_ConfigureEndpoint(0x83, EP_TYPE_INTERRUPT, 0x40, 1); // IN
Endpoint_ConfigureEndpoint(0x03, EP_TYPE_INTERRUPT, 0x40, 1); // OUT
}

0 comments on commit 8d74fc7

Please sign in to comment.