You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1085 lines
31 KiB

Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
8 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
8 years ago
8 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
8 years ago
8 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
8 years ago
8 years ago
  1. /*
  2. * Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. * This file is based on:
  4. * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
  5. * LUFA-120219/Demos/Device/Lowlevel/GenericHID
  6. */
  7. /*
  8. LUFA Library
  9. Copyright (C) Dean Camera, 2012.
  10. dean [at] fourwalledcubicle [dot] com
  11. www.lufa-lib.org
  12. */
  13. /*
  14. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  15. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  16. Permission to use, copy, modify, distribute, and sell this
  17. software and its documentation for any purpose is hereby granted
  18. without fee, provided that the above copyright notice appear in
  19. all copies and that both that the copyright notice and this
  20. permission notice and warranty disclaimer appear in supporting
  21. documentation, and that the name of the author not be used in
  22. advertising or publicity pertaining to distribution of the
  23. software without specific, written prior permission.
  24. The author disclaim all warranties with regard to this
  25. software, including all implied warranties of merchantability
  26. and fitness. In no event shall the author be liable for any
  27. special, indirect or consequential damages or any damages
  28. whatsoever resulting from loss of use, data or profits, whether
  29. in an action of contract, negligence or other tortious action,
  30. arising out of or in connection with the use or performance of
  31. this software.
  32. */
  33. #include "report.h"
  34. #include "host.h"
  35. #include "host_driver.h"
  36. #include "keyboard.h"
  37. #include "action.h"
  38. #include "led.h"
  39. #include "sendchar.h"
  40. #include "debug.h"
  41. #ifdef SLEEP_LED_ENABLE
  42. # include "sleep_led.h"
  43. #endif
  44. #include "suspend.h"
  45. #include "usb_descriptor.h"
  46. #include "lufa.h"
  47. #include "quantum.h"
  48. #include <util/atomic.h>
  49. #ifdef NKRO_ENABLE
  50. # include "keycode_config.h"
  51. extern keymap_config_t keymap_config;
  52. #endif
  53. #ifdef AUDIO_ENABLE
  54. # include <audio.h>
  55. #endif
  56. #ifdef BLUETOOTH_ENABLE
  57. # include "outputselect.h"
  58. # ifdef MODULE_ADAFRUIT_BLE
  59. # include "adafruit_ble.h"
  60. # else
  61. # include "bluetooth.h"
  62. # endif
  63. #endif
  64. #ifdef VIRTSER_ENABLE
  65. # include "virtser.h"
  66. #endif
  67. #ifdef MIDI_ENABLE
  68. # include "qmk_midi.h"
  69. #endif
  70. #ifdef RAW_ENABLE
  71. # include "raw_hid.h"
  72. #endif
  73. #ifdef JOYSTICK_ENABLE
  74. # include "joystick.h"
  75. #endif
  76. uint8_t keyboard_idle = 0;
  77. /* 0: Boot Protocol, 1: Report Protocol(default) */
  78. uint8_t keyboard_protocol = 1;
  79. static uint8_t keyboard_led_state = 0;
  80. static report_keyboard_t keyboard_report_sent;
  81. /* Host driver */
  82. static uint8_t keyboard_leds(void);
  83. static void send_keyboard(report_keyboard_t *report);
  84. static void send_mouse(report_mouse_t *report);
  85. static void send_system(uint16_t data);
  86. static void send_consumer(uint16_t data);
  87. host_driver_t lufa_driver = {
  88. keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer,
  89. };
  90. #ifdef VIRTSER_ENABLE
  91. // clang-format off
  92. USB_ClassInfo_CDC_Device_t cdc_device = {
  93. .Config = {
  94. .ControlInterfaceNumber = CCI_INTERFACE,
  95. .DataINEndpoint = {
  96. .Address = (CDC_IN_EPNUM | ENDPOINT_DIR_IN),
  97. .Size = CDC_EPSIZE,
  98. .Banks = 1
  99. },
  100. .DataOUTEndpoint = {
  101. .Address = (CDC_OUT_EPNUM | ENDPOINT_DIR_OUT),
  102. .Size = CDC_EPSIZE,
  103. .Banks = 1
  104. },
  105. .NotificationEndpoint = {
  106. .Address = (CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN),
  107. .Size = CDC_NOTIFICATION_EPSIZE,
  108. .Banks = 1
  109. }
  110. }
  111. };
  112. // clang-format on
  113. #endif
  114. #ifdef RAW_ENABLE
  115. /** \brief Raw HID Send
  116. *
  117. * FIXME: Needs doc
  118. */
  119. void raw_hid_send(uint8_t *data, uint8_t length) {
  120. // TODO: implement variable size packet
  121. if (length != RAW_EPSIZE) {
  122. return;
  123. }
  124. if (USB_DeviceState != DEVICE_STATE_Configured) {
  125. return;
  126. }
  127. // TODO: decide if we allow calls to raw_hid_send() in the middle
  128. // of other endpoint usage.
  129. uint8_t ep = Endpoint_GetCurrentEndpoint();
  130. Endpoint_SelectEndpoint(RAW_IN_EPNUM);
  131. // Check to see if the host is ready to accept another packet
  132. if (Endpoint_IsINReady()) {
  133. // Write data
  134. Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
  135. // Finalize the stream transfer to send the last packet
  136. Endpoint_ClearIN();
  137. }
  138. Endpoint_SelectEndpoint(ep);
  139. }
  140. /** \brief Raw HID Receive
  141. *
  142. * FIXME: Needs doc
  143. */
  144. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  145. // Users should #include "raw_hid.h" in their own code
  146. // and implement this function there. Leave this as weak linkage
  147. // so users can opt to not handle data coming in.
  148. }
  149. /** \brief Raw HID Task
  150. *
  151. * FIXME: Needs doc
  152. */
  153. static void raw_hid_task(void) {
  154. // Create a temporary buffer to hold the read in data from the host
  155. uint8_t data[RAW_EPSIZE];
  156. bool data_read = false;
  157. // Device must be connected and configured for the task to run
  158. if (USB_DeviceState != DEVICE_STATE_Configured) return;
  159. Endpoint_SelectEndpoint(RAW_OUT_EPNUM);
  160. // Check to see if a packet has been sent from the host
  161. if (Endpoint_IsOUTReceived()) {
  162. // Check to see if the packet contains data
  163. if (Endpoint_IsReadWriteAllowed()) {
  164. /* Read data */
  165. Endpoint_Read_Stream_LE(data, sizeof(data), NULL);
  166. data_read = true;
  167. }
  168. // Finalize the stream transfer to receive the last packet
  169. Endpoint_ClearOUT();
  170. if (data_read) {
  171. raw_hid_receive(data, sizeof(data));
  172. }
  173. }
  174. }
  175. #endif
  176. /*******************************************************************************
  177. * Console
  178. ******************************************************************************/
  179. #ifdef CONSOLE_ENABLE
  180. /** \brief Console Task
  181. *
  182. * FIXME: Needs doc
  183. */
  184. static void Console_Task(void) {
  185. /* Device must be connected and configured for the task to run */
  186. if (USB_DeviceState != DEVICE_STATE_Configured) return;
  187. uint8_t ep = Endpoint_GetCurrentEndpoint();
  188. # if 0
  189. // TODO: impl receivechar()/recvchar()
  190. Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM);
  191. /* Check to see if a packet has been sent from the host */
  192. if (Endpoint_IsOUTReceived())
  193. {
  194. /* Check to see if the packet contains data */
  195. if (Endpoint_IsReadWriteAllowed())
  196. {
  197. /* Create a temporary buffer to hold the read in report from the host */
  198. uint8_t ConsoleData[CONSOLE_EPSIZE];
  199. /* Read Console Report Data */
  200. Endpoint_Read_Stream_LE(&ConsoleData, sizeof(ConsoleData), NULL);
  201. /* Process Console Report Data */
  202. //ProcessConsoleHIDReport(ConsoleData);
  203. }
  204. /* Finalize the stream transfer to send the last packet */
  205. Endpoint_ClearOUT();
  206. }
  207. # endif
  208. /* IN packet */
  209. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  210. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  211. Endpoint_SelectEndpoint(ep);
  212. return;
  213. }
  214. // fill empty bank
  215. while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0);
  216. // flush sendchar packet
  217. if (Endpoint_IsINReady()) {
  218. Endpoint_ClearIN();
  219. }
  220. Endpoint_SelectEndpoint(ep);
  221. }
  222. #endif
  223. /*******************************************************************************
  224. * Joystick
  225. ******************************************************************************/
  226. #ifdef JOYSTICK_ENABLE
  227. void send_joystick_packet(joystick_t *joystick) {
  228. uint8_t timeout = 255;
  229. joystick_report_t r = {
  230. # if JOYSTICK_AXES_COUNT > 0
  231. .axes = {joystick->axes[0],
  232. # if JOYSTICK_AXES_COUNT >= 2
  233. joystick->axes[1],
  234. # endif
  235. # if JOYSTICK_AXES_COUNT >= 3
  236. joystick->axes[2],
  237. # endif
  238. # if JOYSTICK_AXES_COUNT >= 4
  239. joystick->axes[3],
  240. # endif
  241. # if JOYSTICK_AXES_COUNT >= 5
  242. joystick->axes[4],
  243. # endif
  244. # if JOYSTICK_AXES_COUNT >= 6
  245. joystick->axes[5],
  246. # endif
  247. },
  248. # endif // JOYSTICK_AXES_COUNT>0
  249. # if JOYSTICK_BUTTON_COUNT > 0
  250. .buttons = {joystick->buttons[0],
  251. # if JOYSTICK_BUTTON_COUNT > 8
  252. joystick->buttons[1],
  253. # endif
  254. # if JOYSTICK_BUTTON_COUNT > 16
  255. joystick->buttons[2],
  256. # endif
  257. # if JOYSTICK_BUTTON_COUNT > 24
  258. joystick->buttons[3],
  259. # endif
  260. }
  261. # endif // JOYSTICK_BUTTON_COUNT>0
  262. };
  263. /* Select the Joystick Report Endpoint */
  264. Endpoint_SelectEndpoint(JOYSTICK_IN_EPNUM);
  265. /* Check if write ready for a polling interval around 10ms */
  266. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  267. if (!Endpoint_IsReadWriteAllowed()) return;
  268. /* Write Joystick Report Data */
  269. Endpoint_Write_Stream_LE(&r, sizeof(joystick_report_t), NULL);
  270. /* Finalize the stream transfer to send the last packet */
  271. Endpoint_ClearIN();
  272. }
  273. #endif
  274. /*******************************************************************************
  275. * USB Events
  276. ******************************************************************************/
  277. /*
  278. * Event Order of Plug in:
  279. * 0) EVENT_USB_Device_Connect
  280. * 1) EVENT_USB_Device_Suspend
  281. * 2) EVENT_USB_Device_Reset
  282. * 3) EVENT_USB_Device_Wake
  283. */
  284. /** \brief Event USB Device Connect
  285. *
  286. * FIXME: Needs doc
  287. */
  288. void EVENT_USB_Device_Connect(void) {
  289. print("[C]");
  290. /* For battery powered device */
  291. if (!USB_IsInitialized) {
  292. USB_Disable();
  293. USB_Init();
  294. USB_Device_EnableSOFEvents();
  295. }
  296. }
  297. /** \brief Event USB Device Connect
  298. *
  299. * FIXME: Needs doc
  300. */
  301. void EVENT_USB_Device_Disconnect(void) {
  302. print("[D]");
  303. /* For battery powered device */
  304. USB_IsInitialized = false;
  305. /* TODO: This doesn't work. After several plug in/outs can not be enumerated.
  306. if (USB_IsInitialized) {
  307. USB_Disable(); // Disable all interrupts
  308. USB_Controller_Enable();
  309. USB_INT_Enable(USB_INT_VBUSTI);
  310. }
  311. */
  312. }
  313. /** \brief Event USB Device Connect
  314. *
  315. * FIXME: Needs doc
  316. */
  317. void EVENT_USB_Device_Reset(void) { print("[R]"); }
  318. /** \brief Event USB Device Connect
  319. *
  320. * FIXME: Needs doc
  321. */
  322. void EVENT_USB_Device_Suspend() {
  323. print("[S]");
  324. #ifdef SLEEP_LED_ENABLE
  325. sleep_led_enable();
  326. #endif
  327. }
  328. /** \brief Event USB Device Connect
  329. *
  330. * FIXME: Needs doc
  331. */
  332. void EVENT_USB_Device_WakeUp() {
  333. print("[W]");
  334. suspend_wakeup_init();
  335. #ifdef SLEEP_LED_ENABLE
  336. sleep_led_disable();
  337. // NOTE: converters may not accept this
  338. led_set(host_keyboard_leds());
  339. #endif
  340. }
  341. #ifdef CONSOLE_ENABLE
  342. static bool console_flush = false;
  343. # define CONSOLE_FLUSH_SET(b) \
  344. do { \
  345. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { console_flush = b; } \
  346. } while (0)
  347. /** \brief Event USB Device Start Of Frame
  348. *
  349. * FIXME: Needs doc
  350. * called every 1ms
  351. */
  352. void EVENT_USB_Device_StartOfFrame(void) {
  353. static uint8_t count;
  354. if (++count % 50) return;
  355. count = 0;
  356. if (!console_flush) return;
  357. Console_Task();
  358. console_flush = false;
  359. }
  360. #endif
  361. /** \brief Event handler for the USB_ConfigurationChanged event.
  362. *
  363. * This is fired when the host sets the current configuration of the USB device after enumeration.
  364. *
  365. * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4,
  366. * it is safe to use single bank for all endpoints.
  367. */
  368. void EVENT_USB_Device_ConfigurationChanged(void) {
  369. bool ConfigSuccess = true;
  370. #ifndef KEYBOARD_SHARED_EP
  371. /* Setup keyboard report endpoint */
  372. ConfigSuccess &= Endpoint_ConfigureEndpoint((KEYBOARD_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1);
  373. #endif
  374. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  375. /* Setup mouse report endpoint */
  376. ConfigSuccess &= Endpoint_ConfigureEndpoint((MOUSE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, MOUSE_EPSIZE, 1);
  377. #endif
  378. #ifdef SHARED_EP_ENABLE
  379. /* Setup shared report endpoint */
  380. ConfigSuccess &= Endpoint_ConfigureEndpoint((SHARED_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, SHARED_EPSIZE, 1);
  381. #endif
  382. #ifdef RAW_ENABLE
  383. /* Setup raw HID endpoints */
  384. ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
  385. ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1);
  386. #endif
  387. #ifdef CONSOLE_ENABLE
  388. /* Setup console endpoint */
  389. ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
  390. # if 0
  391. ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1);
  392. # endif
  393. #endif
  394. #ifdef MIDI_ENABLE
  395. /* Setup MIDI stream endpoints */
  396. ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
  397. ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
  398. #endif
  399. #ifdef VIRTSER_ENABLE
  400. /* Setup virtual serial endpoints */
  401. ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
  402. ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1);
  403. ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1);
  404. #endif
  405. #ifdef JOYSTICK_ENABLE
  406. ConfigSuccess &= ENDPOINT_CONFIG(JOYSTICK_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, ENDPOINT_BANK_SINGLE);
  407. #endif
  408. }
  409. /* FIXME: Expose this table in the docs somehow
  410. Appendix G: HID Request Support Requirements
  411. The following table enumerates the requests that need to be supported by various types of HID class devices.
  412. Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  413. ------------------------------------------------------------------------------------------
  414. Boot Mouse Required Optional Optional Optional Required Required
  415. Non-Boot Mouse Required Optional Optional Optional Optional Optional
  416. Boot Keyboard Required Optional Required Required Required Required
  417. Non-Boot Keybrd Required Optional Required Required Optional Optional
  418. Other Device Required Optional Optional Optional Optional Optional
  419. */
  420. /** \brief Event handler for the USB_ControlRequest event.
  421. *
  422. * This is fired before passing along unhandled control requests to the library for processing internally.
  423. */
  424. void EVENT_USB_Device_ControlRequest(void) {
  425. uint8_t *ReportData = NULL;
  426. uint8_t ReportSize = 0;
  427. /* Handle HID Class specific requests */
  428. switch (USB_ControlRequest.bRequest) {
  429. case HID_REQ_GetReport:
  430. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
  431. Endpoint_ClearSETUP();
  432. // Interface
  433. switch (USB_ControlRequest.wIndex) {
  434. case KEYBOARD_INTERFACE:
  435. // TODO: test/check
  436. ReportData = (uint8_t *)&keyboard_report_sent;
  437. ReportSize = sizeof(keyboard_report_sent);
  438. break;
  439. }
  440. /* Write the report data to the control endpoint */
  441. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  442. Endpoint_ClearOUT();
  443. }
  444. break;
  445. case HID_REQ_SetReport:
  446. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
  447. // Interface
  448. switch (USB_ControlRequest.wIndex) {
  449. case KEYBOARD_INTERFACE:
  450. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  451. case SHARED_INTERFACE:
  452. #endif
  453. Endpoint_ClearSETUP();
  454. while (!(Endpoint_IsOUTReceived())) {
  455. if (USB_DeviceState == DEVICE_STATE_Unattached) return;
  456. }
  457. if (Endpoint_BytesInEndpoint() == 2) {
  458. uint8_t report_id = Endpoint_Read_8();
  459. if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) {
  460. keyboard_led_state = Endpoint_Read_8();
  461. }
  462. } else {
  463. keyboard_led_state = Endpoint_Read_8();
  464. }
  465. Endpoint_ClearOUT();
  466. Endpoint_ClearStatusStage();
  467. break;
  468. }
  469. }
  470. break;
  471. case HID_REQ_GetProtocol:
  472. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
  473. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  474. Endpoint_ClearSETUP();
  475. while (!(Endpoint_IsINReady()))
  476. ;
  477. Endpoint_Write_8(keyboard_protocol);
  478. Endpoint_ClearIN();
  479. Endpoint_ClearStatusStage();
  480. }
  481. }
  482. break;
  483. case HID_REQ_SetProtocol:
  484. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
  485. if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) {
  486. Endpoint_ClearSETUP();
  487. Endpoint_ClearStatusStage();
  488. keyboard_protocol = (USB_ControlRequest.wValue & 0xFF);
  489. clear_keyboard();
  490. }
  491. }
  492. break;
  493. case HID_REQ_SetIdle:
  494. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) {
  495. Endpoint_ClearSETUP();
  496. Endpoint_ClearStatusStage();
  497. keyboard_idle = ((USB_ControlRequest.wValue & 0xFF00) >> 8);
  498. }
  499. break;
  500. case HID_REQ_GetIdle:
  501. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
  502. Endpoint_ClearSETUP();
  503. while (!(Endpoint_IsINReady()))
  504. ;
  505. Endpoint_Write_8(keyboard_idle);
  506. Endpoint_ClearIN();
  507. Endpoint_ClearStatusStage();
  508. }
  509. break;
  510. }
  511. #ifdef VIRTSER_ENABLE
  512. CDC_Device_ProcessControlRequest(&cdc_device);
  513. #endif
  514. }
  515. /*******************************************************************************
  516. * Host driver
  517. ******************************************************************************/
  518. /** \brief Keyboard LEDs
  519. *
  520. * FIXME: Needs doc
  521. */
  522. static uint8_t keyboard_leds(void) { return keyboard_led_state; }
  523. /** \brief Send Keyboard
  524. *
  525. * FIXME: Needs doc
  526. */
  527. static void send_keyboard(report_keyboard_t *report) {
  528. uint8_t timeout = 255;
  529. #ifdef BLUETOOTH_ENABLE
  530. uint8_t where = where_to_send();
  531. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  532. # ifdef MODULE_ADAFRUIT_BLE
  533. adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
  534. # elif MODULE_RN42
  535. bluefruit_serial_send(0xFD);
  536. bluefruit_serial_send(0x09);
  537. bluefruit_serial_send(0x01);
  538. bluefruit_serial_send(report->mods);
  539. bluefruit_serial_send(report->reserved);
  540. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  541. bluefruit_serial_send(report->keys[i]);
  542. }
  543. # else
  544. bluefruit_serial_send(0xFD);
  545. bluefruit_serial_send(report->mods);
  546. bluefruit_serial_send(report->reserved);
  547. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  548. bluefruit_serial_send(report->keys[i]);
  549. }
  550. # endif
  551. }
  552. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  553. return;
  554. }
  555. #endif
  556. /* Select the Keyboard Report Endpoint */
  557. uint8_t ep = KEYBOARD_IN_EPNUM;
  558. uint8_t size = KEYBOARD_REPORT_SIZE;
  559. #ifdef NKRO_ENABLE
  560. if (keyboard_protocol && keymap_config.nkro) {
  561. ep = SHARED_IN_EPNUM;
  562. size = sizeof(struct nkro_report);
  563. }
  564. #endif
  565. Endpoint_SelectEndpoint(ep);
  566. /* Check if write ready for a polling interval around 10ms */
  567. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  568. if (!Endpoint_IsReadWriteAllowed()) return;
  569. /* If we're in Boot Protocol, don't send any report ID or other funky fields */
  570. if (!keyboard_protocol) {
  571. Endpoint_Write_Stream_LE(&report->mods, 8, NULL);
  572. } else {
  573. Endpoint_Write_Stream_LE(report, size, NULL);
  574. }
  575. /* Finalize the stream transfer to send the last packet */
  576. Endpoint_ClearIN();
  577. keyboard_report_sent = *report;
  578. }
  579. /** \brief Send Mouse
  580. *
  581. * FIXME: Needs doc
  582. */
  583. static void send_mouse(report_mouse_t *report) {
  584. #ifdef MOUSE_ENABLE
  585. uint8_t timeout = 255;
  586. # ifdef BLUETOOTH_ENABLE
  587. uint8_t where = where_to_send();
  588. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  589. # ifdef MODULE_ADAFRUIT_BLE
  590. // FIXME: mouse buttons
  591. adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
  592. # else
  593. bluefruit_serial_send(0xFD);
  594. bluefruit_serial_send(0x00);
  595. bluefruit_serial_send(0x03);
  596. bluefruit_serial_send(report->buttons);
  597. bluefruit_serial_send(report->x);
  598. bluefruit_serial_send(report->y);
  599. bluefruit_serial_send(report->v); // should try sending the wheel v here
  600. bluefruit_serial_send(report->h); // should try sending the wheel h here
  601. bluefruit_serial_send(0x00);
  602. # endif
  603. }
  604. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  605. return;
  606. }
  607. # endif
  608. /* Select the Mouse Report Endpoint */
  609. Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
  610. /* Check if write ready for a polling interval around 10ms */
  611. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  612. if (!Endpoint_IsReadWriteAllowed()) return;
  613. /* Write Mouse Report Data */
  614. Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
  615. /* Finalize the stream transfer to send the last packet */
  616. Endpoint_ClearIN();
  617. #endif
  618. }
  619. /** \brief Send Extra
  620. *
  621. * FIXME: Needs doc
  622. */
  623. #ifdef EXTRAKEY_ENABLE
  624. static void send_extra(uint8_t report_id, uint16_t data) {
  625. uint8_t timeout = 255;
  626. if (USB_DeviceState != DEVICE_STATE_Configured) return;
  627. report_extra_t r = {.report_id = report_id, .usage = data};
  628. Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
  629. /* Check if write ready for a polling interval around 10ms */
  630. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  631. if (!Endpoint_IsReadWriteAllowed()) return;
  632. Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL);
  633. Endpoint_ClearIN();
  634. }
  635. #endif
  636. /** \brief Send System
  637. *
  638. * FIXME: Needs doc
  639. */
  640. static void send_system(uint16_t data) {
  641. #ifdef EXTRAKEY_ENABLE
  642. send_extra(REPORT_ID_SYSTEM, data);
  643. #endif
  644. }
  645. /** \brief Send Consumer
  646. *
  647. * FIXME: Needs doc
  648. */
  649. static void send_consumer(uint16_t data) {
  650. #ifdef EXTRAKEY_ENABLE
  651. # ifdef BLUETOOTH_ENABLE
  652. uint8_t where = where_to_send();
  653. if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
  654. # ifdef MODULE_ADAFRUIT_BLE
  655. adafruit_ble_send_consumer_key(data, 0);
  656. # elif MODULE_RN42
  657. static uint16_t last_data = 0;
  658. if (data == last_data) return;
  659. last_data = data;
  660. uint16_t bitmap = CONSUMER2RN42(data);
  661. bluefruit_serial_send(0xFD);
  662. bluefruit_serial_send(0x03);
  663. bluefruit_serial_send(0x03);
  664. bluefruit_serial_send(bitmap & 0xFF);
  665. bluefruit_serial_send((bitmap >> 8) & 0xFF);
  666. # else
  667. static uint16_t last_data = 0;
  668. if (data == last_data) return;
  669. last_data = data;
  670. uint16_t bitmap = CONSUMER2BLUEFRUIT(data);
  671. bluefruit_serial_send(0xFD);
  672. bluefruit_serial_send(0x00);
  673. bluefruit_serial_send(0x02);
  674. bluefruit_serial_send(bitmap & 0xFF);
  675. bluefruit_serial_send((bitmap >> 8) & 0xFF);
  676. bluefruit_serial_send(0x00);
  677. bluefruit_serial_send(0x00);
  678. bluefruit_serial_send(0x00);
  679. bluefruit_serial_send(0x00);
  680. # endif
  681. }
  682. if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
  683. return;
  684. }
  685. # endif
  686. send_extra(REPORT_ID_CONSUMER, data);
  687. #endif
  688. }
  689. /*******************************************************************************
  690. * sendchar
  691. ******************************************************************************/
  692. #ifdef CONSOLE_ENABLE
  693. # define SEND_TIMEOUT 5
  694. /** \brief Send Char
  695. *
  696. * FIXME: Needs doc
  697. */
  698. int8_t sendchar(uint8_t c) {
  699. // Not wait once timeouted.
  700. // Because sendchar() is called so many times, waiting each call causes big lag.
  701. static bool timeouted = false;
  702. // prevents Console_Task() from running during sendchar() runs.
  703. // or char will be lost. These two function is mutually exclusive.
  704. CONSOLE_FLUSH_SET(false);
  705. if (USB_DeviceState != DEVICE_STATE_Configured) return -1;
  706. uint8_t ep = Endpoint_GetCurrentEndpoint();
  707. Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
  708. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  709. goto ERROR_EXIT;
  710. }
  711. if (timeouted && !Endpoint_IsReadWriteAllowed()) {
  712. goto ERROR_EXIT;
  713. }
  714. timeouted = false;
  715. uint8_t timeout = SEND_TIMEOUT;
  716. while (!Endpoint_IsReadWriteAllowed()) {
  717. if (USB_DeviceState != DEVICE_STATE_Configured) {
  718. goto ERROR_EXIT;
  719. }
  720. if (Endpoint_IsStalled()) {
  721. goto ERROR_EXIT;
  722. }
  723. if (!(timeout--)) {
  724. timeouted = true;
  725. goto ERROR_EXIT;
  726. }
  727. _delay_ms(1);
  728. }
  729. Endpoint_Write_8(c);
  730. // send when bank is full
  731. if (!Endpoint_IsReadWriteAllowed()) {
  732. while (!(Endpoint_IsINReady()))
  733. ;
  734. Endpoint_ClearIN();
  735. } else {
  736. CONSOLE_FLUSH_SET(true);
  737. }
  738. Endpoint_SelectEndpoint(ep);
  739. return 0;
  740. ERROR_EXIT:
  741. Endpoint_SelectEndpoint(ep);
  742. return -1;
  743. }
  744. #endif
  745. /*******************************************************************************
  746. * MIDI
  747. ******************************************************************************/
  748. #ifdef MIDI_ENABLE
  749. // clang-format off
  750. USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = {
  751. .Config = {
  752. .StreamingInterfaceNumber = AS_INTERFACE,
  753. .DataINEndpoint = {
  754. .Address = (MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN),
  755. .Size = MIDI_STREAM_EPSIZE,
  756. .Banks = 1
  757. },
  758. .DataOUTEndpoint = {
  759. .Address = (MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_OUT),
  760. .Size = MIDI_STREAM_EPSIZE,
  761. .Banks = 1
  762. }
  763. }
  764. };
  765. // clang-format on
  766. void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); }
  767. bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); }
  768. #endif
  769. /*******************************************************************************
  770. * VIRTUAL SERIAL
  771. ******************************************************************************/
  772. #ifdef VIRTSER_ENABLE
  773. /** \brief Virtual Serial Init
  774. *
  775. * FIXME: Needs doc
  776. */
  777. void virtser_init(void) {
  778. cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR;
  779. CDC_Device_SendControlLineStateChange(&cdc_device);
  780. }
  781. /** \brief Virtual Serial Receive
  782. *
  783. * FIXME: Needs doc
  784. */
  785. void virtser_recv(uint8_t c) __attribute__((weak));
  786. void virtser_recv(uint8_t c) {
  787. // Ignore by default
  788. }
  789. /** \brief Virtual Serial Task
  790. *
  791. * FIXME: Needs doc
  792. */
  793. void virtser_task(void) {
  794. uint16_t count = CDC_Device_BytesReceived(&cdc_device);
  795. uint8_t ch;
  796. for (; count; --count) {
  797. ch = CDC_Device_ReceiveByte(&cdc_device);
  798. virtser_recv(ch);
  799. }
  800. }
  801. /** \brief Virtual Serial Send
  802. *
  803. * FIXME: Needs doc
  804. */
  805. void virtser_send(const uint8_t byte) {
  806. uint8_t timeout = 255;
  807. uint8_t ep = Endpoint_GetCurrentEndpoint();
  808. if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) {
  809. /* IN packet */
  810. Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
  811. if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
  812. Endpoint_SelectEndpoint(ep);
  813. return;
  814. }
  815. while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
  816. Endpoint_Write_8(byte);
  817. CDC_Device_Flush(&cdc_device);
  818. if (Endpoint_IsINReady()) {
  819. Endpoint_ClearIN();
  820. }
  821. Endpoint_SelectEndpoint(ep);
  822. }
  823. }
  824. #endif
  825. /*******************************************************************************
  826. * main
  827. ******************************************************************************/
  828. /** \brief Setup MCU
  829. *
  830. * FIXME: Needs doc
  831. */
  832. static void setup_mcu(void) {
  833. /* Disable watchdog if enabled by bootloader/fuses */
  834. MCUSR &= ~_BV(WDRF);
  835. wdt_disable();
  836. /* Disable clock division */
  837. clock_prescale_set(clock_div_1);
  838. }
  839. /** \brief Setup USB
  840. *
  841. * FIXME: Needs doc
  842. */
  843. static void setup_usb(void) {
  844. // Leonardo needs. Without this USB device is not recognized.
  845. USB_Disable();
  846. USB_Init();
  847. // for Console_Task
  848. USB_Device_EnableSOFEvents();
  849. print_set_sendchar(sendchar);
  850. }
  851. /** \brief Main
  852. *
  853. * FIXME: Needs doc
  854. */
  855. int main(void) __attribute__((weak));
  856. int main(void) {
  857. #ifdef MIDI_ENABLE
  858. setup_midi();
  859. #endif
  860. setup_mcu();
  861. keyboard_setup();
  862. setup_usb();
  863. sei();
  864. #if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42)
  865. serial_init();
  866. #endif
  867. /* wait for USB startup & debug output */
  868. #ifdef WAIT_FOR_USB
  869. while (USB_DeviceState != DEVICE_STATE_Configured) {
  870. # if defined(INTERRUPT_CONTROL_ENDPOINT)
  871. ;
  872. # else
  873. USB_USBTask();
  874. # endif
  875. }
  876. print("USB configured.\n");
  877. #else
  878. USB_USBTask();
  879. #endif
  880. /* init modules */
  881. keyboard_init();
  882. host_set_driver(&lufa_driver);
  883. #ifdef SLEEP_LED_ENABLE
  884. sleep_led_init();
  885. #endif
  886. #ifdef VIRTSER_ENABLE
  887. virtser_init();
  888. #endif
  889. print("Keyboard start.\n");
  890. while (1) {
  891. #if !defined(NO_USB_STARTUP_CHECK)
  892. while (USB_DeviceState == DEVICE_STATE_Suspended) {
  893. print("[s]");
  894. suspend_power_down();
  895. if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
  896. USB_Device_SendRemoteWakeup();
  897. }
  898. }
  899. #endif
  900. keyboard_task();
  901. #ifdef MIDI_ENABLE
  902. MIDI_Device_USBTask(&USB_MIDI_Interface);
  903. #endif
  904. #ifdef MODULE_ADAFRUIT_BLE
  905. adafruit_ble_task();
  906. #endif
  907. #ifdef VIRTSER_ENABLE
  908. virtser_task();
  909. CDC_Device_USBTask(&cdc_device);
  910. #endif
  911. #ifdef RAW_ENABLE
  912. raw_hid_task();
  913. #endif
  914. #if !defined(INTERRUPT_CONTROL_ENDPOINT)
  915. USB_USBTask();
  916. #endif
  917. }
  918. }
  919. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, const void **const DescriptorAddress) { return get_usb_descriptor(wValue, wIndex, DescriptorAddress); }