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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 usb_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 usb_keyboard_idle_config=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_REPORT_KEYS);
  50. else
  51. #endif
  52. {
  53. if (usb_keyboard_protocol)
  54. result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
  55. else
  56. result = send_report(report, KBD_ENDPOINT, 0, 6);
  57. }
  58. if (result) return result;
  59. usb_keyboard_idle_count = 0;
  60. usb_keyboard_print_report(report);
  61. return 0;
  62. }
  63. void usb_keyboard_print_report(report_keyboard_t *report)
  64. {
  65. if (!debug_keyboard) return;
  66. print("keys: ");
  67. for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
  68. print(" mods: "); phex(report->mods); print("\n");
  69. }
  70. static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
  71. {
  72. uint8_t intr_state, timeout;
  73. if (!usb_configured()) return -1;
  74. intr_state = SREG;
  75. cli();
  76. UENUM = endpoint;
  77. timeout = UDFNUML + 50;
  78. while (1) {
  79. // are we ready to transmit?
  80. if (UEINTX & (1<<RWAL)) break;
  81. SREG = intr_state;
  82. // has the USB gone offline?
  83. if (!usb_configured()) return -1;
  84. // have we waited too long?
  85. if (UDFNUML == timeout) return -1;
  86. // get ready to try checking again
  87. intr_state = SREG;
  88. cli();
  89. UENUM = endpoint;
  90. }
  91. UEDATX = report->mods;
  92. #ifdef NKRO_ENABLE
  93. if (!keyboard_nkro)
  94. UEDATX = 0;
  95. #else
  96. UEDATX = 0;
  97. #endif
  98. for (uint8_t i = keys_start; i < keys_end; i++) {
  99. UEDATX = report->keys[i];
  100. }
  101. UEINTX = 0x3A;
  102. SREG = intr_state;
  103. return 0;
  104. }