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.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef USB_H
  24. #define USB_H 1
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <avr/io.h>
  28. extern bool remote_wakeup;
  29. extern bool suspend;
  30. void usb_init(void); // initialize everything
  31. uint8_t usb_configured(void); // is the USB port configured
  32. void usb_remote_wakeup(void);
  33. #define EP_TYPE_CONTROL 0x00
  34. #define EP_TYPE_BULK_IN 0x81
  35. #define EP_TYPE_BULK_OUT 0x80
  36. #define EP_TYPE_INTERRUPT_IN 0xC1
  37. #define EP_TYPE_INTERRUPT_OUT 0xC0
  38. #define EP_TYPE_ISOCHRONOUS_IN 0x41
  39. #define EP_TYPE_ISOCHRONOUS_OUT 0x40
  40. #define EP_SINGLE_BUFFER 0x02
  41. #define EP_DOUBLE_BUFFER 0x06
  42. #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
  43. ((s) == 32 ? 0x20 : \
  44. ((s) == 16 ? 0x10 : \
  45. 0x00)))
  46. #if defined (__AVR_AT90USB162__) || defined (__AVR_AT90USB82__)
  47. # define MAX_ENDPOINT 4
  48. # define UERST_MASK 0x1E
  49. #else
  50. # define MAX_ENDPOINT 6
  51. # define UERST_MASK 0x7E
  52. #endif
  53. #define LSB(n) (n & 255)
  54. #define MSB(n) ((n >> 8) & 255)
  55. #if defined(__AVR_AT90USB162__)
  56. #define HW_CONFIG()
  57. #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
  58. #define USB_CONFIG() (USBCON = (1<<USBE))
  59. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  60. #elif defined(__AVR_ATmega32U4__)
  61. #define HW_CONFIG() (UHWCON = 0x01)
  62. #define PLL_CONFIG() (PLLCSR = 0x12)
  63. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  64. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  65. #elif defined(__AVR_AT90USB646__)
  66. #define HW_CONFIG() (UHWCON = 0x81)
  67. #define PLL_CONFIG() (PLLCSR = 0x1A)
  68. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  69. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  70. #elif defined(__AVR_AT90USB1286__)
  71. #define HW_CONFIG() (UHWCON = 0x81)
  72. #define PLL_CONFIG() (PLLCSR = 0x16)
  73. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  74. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  75. #endif
  76. // standard control endpoint request types
  77. #define GET_STATUS 0
  78. #define CLEAR_FEATURE 1
  79. #define SET_FEATURE 3
  80. #define SET_ADDRESS 5
  81. #define GET_DESCRIPTOR 6
  82. #define GET_CONFIGURATION 8
  83. #define SET_CONFIGURATION 9
  84. #define GET_INTERFACE 10
  85. #define SET_INTERFACE 11
  86. // HID (human interface device)
  87. #define HID_GET_REPORT 1
  88. #define HID_GET_IDLE 2
  89. #define HID_GET_PROTOCOL 3
  90. #define HID_SET_REPORT 9
  91. #define HID_SET_IDLE 10
  92. #define HID_SET_PROTOCOL 11
  93. #define HID_REPORT_INPUT 1
  94. #define HID_REPORT_OUTPUT 2
  95. #define HID_REPORT_FEATURE 3
  96. // CDC (communication class device)
  97. #define CDC_SET_LINE_CODING 0x20
  98. #define CDC_GET_LINE_CODING 0x21
  99. #define CDC_SET_CONTROL_LINE_STATE 0x22
  100. // HID feature selectors
  101. #define DEVICE_REMOTE_WAKEUP 1
  102. #define ENDPOINT_HALT 0
  103. #define TEST_MODE 2
  104. /*------------------------------------------------------------------*
  105. * Keyboard descriptor setting
  106. *------------------------------------------------------------------*/
  107. #define KBD_INTERFACE 0
  108. #define KBD_ENDPOINT 1
  109. #define KBD_SIZE 8
  110. #define KBD_BUFFER EP_DOUBLE_BUFFER
  111. #define KBD_REPORT_KEYS (KBD_SIZE - 2)
  112. // secondary keyboard
  113. #ifdef NKRO_ENABLE
  114. #define KBD2_INTERFACE 4
  115. #define KBD2_ENDPOINT 5
  116. #define KBD2_SIZE 16
  117. #define KBD2_BUFFER EP_DOUBLE_BUFFER
  118. #define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
  119. #endif
  120. #endif