usb_keyboard.c 3.7 KB

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