PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

154 lines
5.5KB

  1. #ifndef usb_common_h__
  2. #define usb_common_h__
  3. #include <stdint.h>
  4. #include <avr/io.h>
  5. #include <avr/pgmspace.h>
  6. #include <avr/interrupt.h>
  7. #ifdef __cplusplus
  8. extern "C"{
  9. #endif
  10. #define MAX_ENDPOINT 6
  11. #define LSB(n) (n & 255)
  12. #define MSB(n) ((n >> 8) & 255)
  13. // constants corresponding to the various serial parameters
  14. #define USB_SERIAL_DTR 0x01
  15. #define USB_SERIAL_RTS 0x02
  16. #define USB_SERIAL_1_STOP 0
  17. #define USB_SERIAL_1_5_STOP 1
  18. #define USB_SERIAL_2_STOP 2
  19. #define USB_SERIAL_PARITY_NONE 0
  20. #define USB_SERIAL_PARITY_ODD 1
  21. #define USB_SERIAL_PARITY_EVEN 2
  22. #define USB_SERIAL_PARITY_MARK 3
  23. #define USB_SERIAL_PARITY_SPACE 4
  24. #define USB_SERIAL_DCD 0x01
  25. #define USB_SERIAL_DSR 0x02
  26. #define USB_SERIAL_BREAK 0x04
  27. #define USB_SERIAL_RI 0x08
  28. #define USB_SERIAL_FRAME_ERR 0x10
  29. #define USB_SERIAL_PARITY_ERR 0x20
  30. #define USB_SERIAL_OVERRUN_ERR 0x40
  31. #define EP_TYPE_CONTROL 0x00
  32. #define EP_TYPE_BULK_IN 0x81
  33. #define EP_TYPE_BULK_OUT 0x80
  34. #define EP_TYPE_INTERRUPT_IN 0xC1
  35. #define EP_TYPE_INTERRUPT_OUT 0xC0
  36. #define EP_TYPE_ISOCHRONOUS_IN 0x41
  37. #define EP_TYPE_ISOCHRONOUS_OUT 0x40
  38. #define EP_SINGLE_BUFFER 0x02
  39. #define EP_DOUBLE_BUFFER 0x06
  40. #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
  41. ((s) == 32 ? 0x20 : \
  42. ((s) == 16 ? 0x10 : \
  43. 0x00)))
  44. #if defined(__AVR_AT90USB162__)
  45. #define HW_CONFIG()
  46. #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
  47. #define USB_CONFIG() (USBCON = (1<<USBE))
  48. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  49. #elif defined(__AVR_ATmega32U4__)
  50. #define HW_CONFIG() (UHWCON = 0x01)
  51. #define PLL_CONFIG() (PLLCSR = 0x12)
  52. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  53. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  54. #elif defined(__AVR_AT90USB646__)
  55. #define HW_CONFIG() (UHWCON = 0x81)
  56. #define PLL_CONFIG() (PLLCSR = 0x1A)
  57. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  58. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  59. #elif defined(__AVR_AT90USB1286__)
  60. #define HW_CONFIG() (UHWCON = 0x81)
  61. #define PLL_CONFIG() (PLLCSR = 0x16)
  62. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  63. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  64. #endif
  65. // standard control endpoint request types
  66. #define GET_STATUS 0
  67. #define CLEAR_FEATURE 1
  68. #define SET_FEATURE 3
  69. #define SET_ADDRESS 5
  70. #define GET_DESCRIPTOR 6
  71. #define GET_CONFIGURATION 8
  72. #define SET_CONFIGURATION 9
  73. #define GET_INTERFACE 10
  74. #define SET_INTERFACE 11
  75. // CDC (communication class device)
  76. #define CDC_SET_LINE_CODING 0x20
  77. #define CDC_GET_LINE_CODING 0x21
  78. #define CDC_SET_CONTROL_LINE_STATE 0x22
  79. #define CDC_SEND_BREAK 0x23
  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. // Mass Storage
  88. #define MS_BULK_ONLY_RESET 0xFF
  89. #define MS_GET_MAX_LUN 0xFE /* stall = 0 */
  90. #define pgm_read_byte_postinc(val, addr) \
  91. asm ("lpm %0, Z+\n" : "=r" (val), "+z" (addr) : )
  92. #define pgm_read_word_postinc(val, addr) \
  93. asm ("lpm %A0, Z+\n\tlpm %B0, Z+\n" : "=r" (val), "+z" (addr) : )
  94. #define read_word_lsbfirst(val, reg) \
  95. asm volatile( \
  96. "lds %A0, %1\n\tlds %B0, %1\n" \
  97. : "=r" (val) : "M" ((int)(&reg)) )
  98. #define read_word_msbfirst(val, reg) \
  99. asm volatile( \
  100. "lds %B0, %1\n\tlds %A0, %1\n" \
  101. : "=r" (val) : "M" ((int)(&reg)) )
  102. #define read_dword_lsbfirst(val, reg) \
  103. asm volatile( \
  104. "lds %A0, %1\n\tlds %B0, %1\n\t" \
  105. "lds %C0, %1\n\tlds %D0, %1\n" \
  106. : "=r" (val) : "M" ((int)(&reg)) )
  107. #define read_dword_msbfirst(val, reg) \
  108. asm volatile( \
  109. "lds %D0, %1\n\tlds %C0, %1\n\t" \
  110. "lds %B0, %1\n\tlds %A0, %1\n" \
  111. : "=r" (val) : "M" ((int)(&reg)) )
  112. #define write_word_lsbfirst(val, reg) \
  113. asm volatile( \
  114. "sts %1, %A0\n\tsts %1, %B0\n" \
  115. : : "r" (val) , "M" ((int)(&reg)) )
  116. #define write_word_msbfirst(val, reg) \
  117. asm volatile( \
  118. "sts %1, %B0\n\tsts %1, %A0\n" \
  119. : : "r" (val) , "M" ((int)(&reg)) )
  120. #define write_dword_lsbfirst(val, reg) \
  121. asm volatile( \
  122. "sts %1, %A0\n\tsts %1, %B0\n\t" \
  123. "sts %1, %C0\n\tsts %1, %D0\n" \
  124. : : "r" (val) , "M" ((int)(&reg)) )
  125. #define write_dword_msbfirst(val, reg) \
  126. asm volatile( \
  127. "sts %1, %D0\n\tsts %1, %C0\n\t" \
  128. "sts %1, %B0\n\tsts %1, %A0\n" \
  129. : : "r" (val) , "M" ((int)(&reg)) )
  130. #define USBSTATE __attribute__ ((section (".noinit")))
  131. extern void _reboot_Teensyduino_(void) __attribute__((noreturn));
  132. extern void _restart_Teensyduino_(void) __attribute__((noreturn));
  133. #ifdef __cplusplus
  134. } // extern "C"
  135. #endif
  136. #endif