usb_keyboard.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifdef NKRO_ENABLE
  32. # include "keycode_config.h"
  33. extern keymap_config_t keymap_config;
  34. #endif
  35. // protocol setting from the host. We use exactly the same report
  36. // either way, so this variable only stores the setting since we
  37. // are required to be able to report which setting is in use.
  38. uint8_t keyboard_protocol = 1;
  39. // the idle configuration, how often we send the report to the
  40. // host (ms * 4) even when it hasn't changed
  41. // Windows and Linux set 0 while OS X sets 6(24ms) by SET_IDLE request.
  42. uint8_t keyboard_idle = 125;
  43. // count until idle timeout
  44. uint8_t usb_keyboard_idle_count = 0;
  45. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  46. volatile uint8_t usb_keyboard_leds = 0;
  47. static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
  48. int8_t usb_keyboard_send_report(report_keyboard_t *report) {
  49. int8_t result = 0;
  50. #ifdef NKRO_ENABLE
  51. if (keymap_config.nkro)
  52. result = send_report(report, KBD2_ENDPOINT, 0, KBD2_SIZE);
  53. else
  54. #endif
  55. {
  56. result = send_report(report, KBD_ENDPOINT, 0, KBD_SIZE);
  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. if (!debug_keyboard) return;
  65. print("keys: ");
  66. for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  67. phex(report->keys[i]);
  68. print(" ");
  69. }
  70. print(" mods: ");
  71. phex(report->mods);
  72. print("\n");
  73. }
  74. static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end) {
  75. uint8_t intr_state, timeout;
  76. if (!usb_configured()) return -1;
  77. intr_state = SREG;
  78. cli();
  79. UENUM = endpoint;
  80. timeout = UDFNUML + 50;
  81. while (1) {
  82. // are we ready to transmit?
  83. if (UEINTX & (1 << RWAL)) break;
  84. SREG = intr_state;
  85. // has the USB gone offline?
  86. if (!usb_configured()) return -1;
  87. // have we waited too long?
  88. if (UDFNUML == timeout) return -1;
  89. // get ready to try checking again
  90. intr_state = SREG;
  91. cli();
  92. UENUM = endpoint;
  93. }
  94. for (uint8_t i = keys_start; i < keys_end; i++) {
  95. UEDATX = report->raw[i];
  96. }
  97. UEINTX = 0x3A;
  98. SREG = intr_state;
  99. return 0;
  100. }