Keyboard firmwares for Atmel AVR and Cortex-M
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.

usb_keyboard.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_keyboard.html
  3. * Copyright (c) 2009 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/interrupt.h>
  24. #include <avr/pgmspace.h>
  25. #include "keycode.h"
  26. #include "usb_keyboard.h"
  27. #include "print.h"
  28. #include "debug.h"
  29. #include "util.h"
  30. #include "host.h"
  31. // protocol setting from the host. We use exactly the same report
  32. // either way, so this variable only stores the setting since we
  33. // are required to be able to report which setting is in use.
  34. uint8_t keyboard_protocol=1;
  35. // the idle configuration, how often we send the report to the
  36. // host (ms * 4) even when it hasn't changed
  37. // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
  38. uint8_t keyboard_idle=125;
  39. // count until idle timeout
  40. uint8_t usb_keyboard_idle_count=0;
  41. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  42. volatile uint8_t usb_keyboard_leds=0;
  43. static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
  44. int8_t usb_keyboard_send_report(report_keyboard_t *report)
  45. {
  46. int8_t result = 0;
  47. #ifdef NKRO_ENABLE
  48. if (keyboard_nkro)
  49. result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
  50. else
  51. #endif
  52. {
  53. result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
  54. }
  55. if (result) return result;
  56. usb_keyboard_idle_count = 0;
  57. usb_keyboard_print_report(report);
  58. return 0;
  59. }
  60. void usb_keyboard_print_report(report_keyboard_t *report)
  61. {
  62. if (!debug_keyboard) return;
  63. print("keys: ");
  64. for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
  65. print(" mods: "); phex(report->mods); print("\n");
  66. }
  67. static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
  68. {
  69. uint8_t intr_state, timeout;
  70. if (!usb_configured()) return -1;
  71. intr_state = SREG;
  72. cli();
  73. UENUM = endpoint;
  74. timeout = UDFNUML + 50;
  75. while (1) {
  76. // are we ready to transmit?
  77. if (UEINTX & (1<<RWAL)) break;
  78. SREG = intr_state;
  79. // has the USB gone offline?
  80. if (!usb_configured()) return -1;
  81. // have we waited too long?
  82. if (UDFNUML == timeout) return -1;
  83. // get ready to try checking again
  84. intr_state = SREG;
  85. cli();
  86. UENUM = endpoint;
  87. }
  88. for (uint8_t i = keys_start; i < keys_end; i++) {
  89. UEDATX = report->raw[i];
  90. }
  91. UEINTX = 0x3A;
  92. SREG = intr_state;
  93. return 0;
  94. }