-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avr port #82
Draft
perigoso
wants to merge
16
commits into
openinput-fw:main
Choose a base branch
from
perigoso:avr-port
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Avr port #82
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
96942ac
config/families: add avr family
perigoso 15a08e6
targets: add atmega32u4 generic target
perigoso c9466c6
workflows: setub build for atmega32u4-generic target
perigoso 5b81cc4
targets/atmega32u4-generic: rename sample config to octupus, a mouse …
perigoso 2db37db
external: add lufa submodule, avr usb
perigoso 9785350
build_system/dependencies: add lufa dependency
perigoso 6b1bfba
external/lufa: checkout latest release
perigoso 6e7d01a
targets/octopus: rename to octomouse
perigoso 6ff36d0
workflow: rename avr target to octomouse
perigoso ed6045e
tools: add caterina bootloader tool
perigoso e82ab56
fixup! build_system/dependencies: add lufa dependency
perigoso 35caf4b
build_system/ninja: add local target directory to includes
perigoso b4848cf
platform/avr: add reset to bootloader and reset functions
perigoso f708a84
target/atmega32u4-generic: configure lufa usb stack
perigoso 13e841a
target/atmega32u4-generic: configure usb descriptors and endpoints
perigoso 6d3bfb0
platform/avr: implement lufa hid driver and hid hal
perigoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
toolchain = 'avr' | ||
has-linker = false | ||
c_flags = [ | ||
'-Os', | ||
'-fpack-struct', | ||
'-fshort-enums', | ||
'-ffunction-sections', | ||
'-fdata-sections', | ||
] | ||
ld_flags = [ | ||
'-fdata-sections', | ||
'-ffunction-sections', | ||
'-Wl,--gc-sections', | ||
] | ||
source = [ | ||
'boot.c', | ||
'usb.c', | ||
'usb_descriptors.c', | ||
'hal/hid.c', | ||
] | ||
|
||
[dependencies] | ||
lufa = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
family = 'avr' | ||
has-config = true | ||
c_flags = [ | ||
'-mmcu=atmega32u4', | ||
'-DUSE_LUFA_CONFIG_HEADER', | ||
] | ||
ld_flags = [ | ||
'-mmcu=atmega32u4', | ||
] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#include <avr/wdt.h> | ||
|
||
// Assuming the Caterina bootloader that comes with the 32u4 | ||
uint16_t bootKey = 0x7777; | ||
volatile uint16_t *const bootKeyPtr = (volatile uint16_t *) 0x0800; | ||
|
||
void reset() | ||
{ | ||
wdt_enable(WDTO_15MS); | ||
for (;;) { | ||
} | ||
} | ||
|
||
void bootloader() | ||
{ | ||
*bootKeyPtr = bootKey; | ||
|
||
reset(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
void reset() __attribute__((noreturn)); | ||
void bootloader() __attribute__((noreturn)); | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2021 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#include "platform/avr/hal/hid.h" | ||
#include "platform/avr/usb.h" | ||
|
||
int hid_hal_send(struct hid_hal_t interface, u8 *buffer, size_t buffer_size) | ||
{ | ||
usb_write_descriptor(0, buffer, buffer_size); | ||
} | ||
|
||
struct hid_hal_t hid_hal_init(void) | ||
{ | ||
struct hid_hal_t hal = { | ||
.send = hid_hal_send, | ||
.drv_data = NULL, | ||
}; | ||
return hal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2021 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "hal/hid.h" | ||
#include "util/types.h" | ||
|
||
struct hid_hal_t hid_hal_init(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#include <avr/interrupt.h> | ||
|
||
#include "protocol/reports.h" | ||
#include "usb.h" | ||
#include "util/hid_descriptors.h" | ||
#include "util/types.h" | ||
|
||
#include <USB.h> | ||
|
||
/** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */ | ||
static u8 openinput_hid_report_buff[sizeof(struct oi_report_t)]; | ||
static u8 mouse_hid_report_buff[sizeof(struct mouse_report)]; | ||
static u8 keyboard_hid_report_buff[sizeof(struct keyboard_report)]; | ||
|
||
static u8 new_oi_report; | ||
static struct oi_report_t oi_report; | ||
static u8 oi_report_size; | ||
|
||
static u8 new_mouse_report; | ||
static struct mouse_report mouse_report; | ||
static u8 mouse_report_size; | ||
|
||
static u8 new_keyboard_report; | ||
static struct keyboard_report keyboard_report; | ||
static u8 keyboard_report_size; | ||
|
||
static struct protocol_config_t protocol_config; | ||
|
||
/** LUFA HID Class driver interface configuration and state information. This structure is | ||
* passed to all HID Class driver functions, so that multiple instances of the same class | ||
* within a device can be differentiated from one another. | ||
*/ | ||
USB_ClassInfo_HID_Device_t openinput_hid_interface = { | ||
.Config = | ||
{ | ||
.InterfaceNumber = 0, | ||
.ReportINEndpoint = | ||
{ | ||
.Address = 0x81, | ||
.Size = 64, | ||
.Banks = 1, | ||
}, | ||
.PrevReportINBuffer = openinput_hid_report_buff, | ||
.PrevReportINBufferSize = sizeof(openinput_hid_report_buff), | ||
}, | ||
}; | ||
|
||
USB_ClassInfo_HID_Device_t mouse_hid_interface = { | ||
.Config = | ||
{ | ||
.InterfaceNumber = 1, | ||
.ReportINEndpoint = | ||
{ | ||
.Address = 0x83, | ||
.Size = 64, | ||
.Banks = 1, | ||
}, | ||
.PrevReportINBuffer = mouse_hid_report_buff, | ||
.PrevReportINBufferSize = sizeof(mouse_hid_report_buff), | ||
}, | ||
}; | ||
|
||
USB_ClassInfo_HID_Device_t keyboard_hid_interface = { | ||
.Config = | ||
{ | ||
.InterfaceNumber = 2, | ||
.ReportINEndpoint = | ||
{ | ||
.Address = 0x84, | ||
.Size = 64, | ||
.Banks = 1, | ||
}, | ||
.PrevReportINBuffer = keyboard_hid_report_buff, | ||
.PrevReportINBufferSize = sizeof(keyboard_hid_report_buff), | ||
}, | ||
}; | ||
|
||
void usb_init() | ||
{ | ||
/* Init USB stack */ | ||
USB_Init(); | ||
} | ||
|
||
void usb_task() | ||
{ | ||
USB_USBTask(); | ||
|
||
HID_Device_USBTask(&openinput_hid_interface); | ||
HID_Device_USBTask(&mouse_hid_interface); | ||
HID_Device_USBTask(&keyboard_hid_interface); | ||
} | ||
|
||
void usb_attach_protocol_config(struct protocol_config_t config) | ||
{ | ||
protocol_config = config; | ||
} | ||
|
||
void usb_write_descriptor(u8 interface, u8 *report_data, u16 report_size) | ||
{ | ||
if (interface == openinput_hid_interface.Config.InterfaceNumber) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use a |
||
memcpy(&oi_report, report_data, report_size); | ||
oi_report_size = report_size; | ||
new_oi_report = 1; | ||
} else if (interface == mouse_hid_interface.Config.InterfaceNumber) { | ||
memcpy(&mouse_report, report_data, report_size); | ||
mouse_report_size = report_size; | ||
new_mouse_report = 1; | ||
} else if (interface == keyboard_hid_interface.Config.InterfaceNumber) { | ||
memcpy(&keyboard_report, report_data, report_size); | ||
keyboard_report_size = report_size; | ||
new_keyboard_report = 1; | ||
} | ||
} | ||
|
||
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and | ||
* starts the library USB task to begin the enumeration and USB management process. | ||
*/ | ||
void EVENT_USB_Device_Connect(void) | ||
{ | ||
} | ||
|
||
/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via | ||
* the status LEDs and stops the USB management task. | ||
*/ | ||
void EVENT_USB_Device_Disconnect(void) | ||
{ | ||
} | ||
|
||
/** Event handler for the library USB Configuration Changed event. */ | ||
void EVENT_USB_Device_ConfigurationChanged(void) | ||
{ | ||
HID_Device_ConfigureEndpoints(&openinput_hid_interface); | ||
HID_Device_ConfigureEndpoints(&mouse_hid_interface); | ||
HID_Device_ConfigureEndpoints(&keyboard_hid_interface); | ||
} | ||
|
||
/** HID class driver callback function for the creation of HID reports to the host. | ||
* | ||
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced | ||
* \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID | ||
* \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature | ||
* \param[out] ReportData Pointer to a buffer where the created report should be stored | ||
* \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent) | ||
* | ||
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent | ||
*/ | ||
bool CALLBACK_HID_Device_CreateHIDReport( | ||
USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo, | ||
uint8_t *const ReportID, | ||
const uint8_t ReportType, | ||
void *ReportData, | ||
uint16_t *const ReportSize) | ||
{ | ||
/* Determine which interface must have its report generated */ | ||
if (HIDInterfaceInfo == &openinput_hid_interface) { | ||
if (new_oi_report == 1) { | ||
memcpy(ReportData, &oi_report, oi_report_size); | ||
*ReportSize = oi_report_size; | ||
new_oi_report = 0; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else if (HIDInterfaceInfo == &mouse_hid_interface) { | ||
if (new_mouse_report == 1) { | ||
memcpy(ReportData, &mouse_report, mouse_report_size); | ||
*ReportSize = mouse_report_size; | ||
new_mouse_report = 0; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else if (HIDInterfaceInfo == &keyboard_hid_interface) { | ||
if (new_keyboard_report == 1) { | ||
memcpy(ReportData, &keyboard_report, keyboard_report_size); | ||
*ReportSize = keyboard_report_size; | ||
new_keyboard_report = 0; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
/** HID class driver callback function for the processing of HID reports from the host. | ||
* | ||
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced | ||
* \param[in] ReportID Report ID of the received report from the host | ||
* \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature | ||
* \param[in] ReportData Pointer to a buffer where the received report has been stored | ||
* \param[in] ReportSize Size in bytes of the received HID report | ||
*/ | ||
void CALLBACK_HID_Device_ProcessHIDReport( | ||
USB_ClassInfo_HID_Device_t *const HIDInterfaceInfo, | ||
const uint8_t ReportID, | ||
const uint8_t ReportType, | ||
const void *ReportData, | ||
const uint16_t ReportSize) | ||
{ | ||
if (HIDInterfaceInfo == &openinput_hid_interface) { | ||
} else if (HIDInterfaceInfo == &keyboard_hid_interface) { | ||
protocol_dispatch(protocol_config, (u8 *) ReportData, ReportSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* SPDX-FileCopyrightText: 2022 Rafael Silva <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "protocol/protocol.h" | ||
|
||
void usb_init(); | ||
void usb_task(); | ||
void usb_attach_protocol_config(struct protocol_config_t config); | ||
void usb_write_descriptor(u8 interface, u8 *report_data, u16 report_size); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this aliased to
const
? I don't see changes to this memory so perhaps this can be on.data
section or other read-only memory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just me playing with lufa and committing to save progress, this is will get a clean up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The struct itself i don't think is written, so yes, although on avrs, consts are always fetched to ram anyway i believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, yeah, I'm not aware of those embedded cases. Godbolt https://godbolt.org/z/soo4eaaev tells me it goes to
.rodata
and the compiler can optimize it to immediate loads instead of an offset. This is not always the case although, as I believe the jump cost is a trade-off the compiler takes into consideration, depending on size of the data/non-natural alignment.