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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define MAX_ENDPOINT 4
  47. #define LSB(n) (n & 255)
  48. #define MSB(n) ((n >> 8) & 255)
  49. #if defined(__AVR_AT90USB162__)
  50. #define HW_CONFIG()
  51. #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
  52. #define USB_CONFIG() (USBCON = (1<<USBE))
  53. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  54. #elif defined(__AVR_ATmega32U4__)
  55. #define HW_CONFIG() (UHWCON = 0x01)
  56. #define PLL_CONFIG() (PLLCSR = 0x12)
  57. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  58. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  59. #elif defined(__AVR_AT90USB646__)
  60. #define HW_CONFIG() (UHWCON = 0x81)
  61. #define PLL_CONFIG() (PLLCSR = 0x1A)
  62. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  63. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  64. #elif defined(__AVR_AT90USB1286__)
  65. #define HW_CONFIG() (UHWCON = 0x81)
  66. #define PLL_CONFIG() (PLLCSR = 0x16)
  67. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  68. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  69. #endif
  70. // standard control endpoint request types
  71. #define GET_STATUS 0
  72. #define CLEAR_FEATURE 1
  73. #define SET_FEATURE 3
  74. #define SET_ADDRESS 5
  75. #define GET_DESCRIPTOR 6
  76. #define GET_CONFIGURATION 8
  77. #define SET_CONFIGURATION 9
  78. #define GET_INTERFACE 10
  79. #define SET_INTERFACE 11
  80. // HID (human interface device)
  81. #define HID_GET_REPORT 1
  82. #define HID_GET_IDLE 2
  83. #define HID_GET_PROTOCOL 3
  84. #define HID_SET_REPORT 9
  85. #define HID_SET_IDLE 10
  86. #define HID_SET_PROTOCOL 11
  87. #define HID_REPORT_INPUT 1
  88. #define HID_REPORT_OUTPUT 2
  89. #define HID_REPORT_FEATURE 3
  90. // CDC (communication class device)
  91. #define CDC_SET_LINE_CODING 0x20
  92. #define CDC_GET_LINE_CODING 0x21
  93. #define CDC_SET_CONTROL_LINE_STATE 0x22
  94. // HID feature selectors
  95. #define DEVICE_REMOTE_WAKEUP 1
  96. #define ENDPOINT_HALT 0
  97. #define TEST_MODE 2
  98. /*------------------------------------------------------------------*
  99. * Keyboard descriptor setting
  100. *------------------------------------------------------------------*/
  101. #define KBD_INTERFACE 0
  102. #define KBD_ENDPOINT 1
  103. #define KBD_SIZE 8
  104. #define KBD_BUFFER EP_DOUBLE_BUFFER
  105. #define KBD_REPORT_KEYS (KBD_SIZE - 2)
  106. // secondary keyboard
  107. #ifdef USB_NKRO_ENABLE
  108. #define KBD2_INTERFACE 4
  109. #define KBD2_ENDPOINT 5
  110. #define KBD2_SIZE 16
  111. #define KBD2_BUFFER EP_DOUBLE_BUFFER
  112. #define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
  113. #endif
  114. #endif