usb.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2016, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef _USB_H_
  29. #define _USB_H_
  30. /*- Includes ----------------------------------------------------------------*/
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #include "utils.h"
  34. /*- Definitions -------------------------------------------------------------*/
  35. enum
  36. {
  37. USB_GET_STATUS = 0,
  38. USB_CLEAR_FEATURE = 1,
  39. USB_SET_FEATURE = 3,
  40. USB_SET_ADDRESS = 5,
  41. USB_GET_DESCRIPTOR = 6,
  42. USB_SET_DESCRIPTOR = 7,
  43. USB_GET_CONFIGURATION = 8,
  44. USB_SET_CONFIGURATION = 9,
  45. USB_GET_INTERFACE = 10,
  46. USB_SET_INTERFACE = 11,
  47. USB_SYNCH_FRAME = 12,
  48. };
  49. enum
  50. {
  51. USB_DEVICE_DESCRIPTOR = 1,
  52. USB_CONFIGURATION_DESCRIPTOR = 2,
  53. USB_STRING_DESCRIPTOR = 3,
  54. USB_INTERFACE_DESCRIPTOR = 4,
  55. USB_ENDPOINT_DESCRIPTOR = 5,
  56. USB_DEVICE_QUALIFIER_DESCRIPTOR = 6,
  57. USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR = 7,
  58. USB_INTERFACE_POWER_DESCRIPTOR = 8,
  59. USB_OTG_DESCRIPTOR = 9,
  60. USB_DEBUG_DESCRIPTOR = 10,
  61. USB_INTERFACE_ASSOCIATION_DESCRIPTOR = 11,
  62. USB_BINARY_OBJECT_STORE_DESCRIPTOR = 15,
  63. USB_DEVICE_CAPABILITY_DESCRIPTOR = 16,
  64. };
  65. enum
  66. {
  67. USB_DEVICE_RECIPIENT = 0,
  68. USB_INTERFACE_RECIPIENT = 1,
  69. USB_ENDPOINT_RECIPIENT = 2,
  70. USB_OTHER_RECIPIENT = 3,
  71. };
  72. enum
  73. {
  74. USB_STANDARD_REQUEST = 0,
  75. USB_CLASS_REQUEST = 1,
  76. USB_VENDOR_REQUEST = 2,
  77. };
  78. enum
  79. {
  80. USB_OUT_TRANSFER = 0,
  81. USB_IN_TRANSFER = 1,
  82. };
  83. enum
  84. {
  85. USB_IN_ENDPOINT = 0x80,
  86. USB_OUT_ENDPOINT = 0x00,
  87. USB_INDEX_MASK = 0x7f,
  88. USB_DIRECTION_MASK = 0x80,
  89. };
  90. enum
  91. {
  92. USB_CONTROL_ENDPOINT = 0 << 0,
  93. USB_ISOCHRONOUS_ENDPOINT = 1 << 0,
  94. USB_BULK_ENDPOINT = 2 << 0,
  95. USB_INTERRUPT_ENDPOINT = 3 << 0,
  96. USB_NO_SYNCHRONIZATION = 0 << 2,
  97. USB_ASYNCHRONOUS = 1 << 2,
  98. USB_ADAPTIVE = 2 << 2,
  99. USB_SYNCHRONOUS = 3 << 2,
  100. USB_DATA_ENDPOINT = 0 << 4,
  101. USB_FEEDBACK_ENDPOINT = 1 << 4,
  102. USB_IMP_FB_DATA_ENDPOINT = 2 << 4,
  103. };
  104. /*- Types -------------------------------------------------------------------*/
  105. typedef struct PACK
  106. {
  107. uint8_t bmRequestType;
  108. uint8_t bRequest;
  109. uint16_t wValue;
  110. uint16_t wIndex;
  111. uint16_t wLength;
  112. } usb_request_t;
  113. typedef struct PACK
  114. {
  115. uint8_t bLength;
  116. uint8_t bDescriptorType;
  117. } usb_descriptor_header_t;
  118. typedef struct PACK
  119. {
  120. uint8_t bLength;
  121. uint8_t bDescriptorType;
  122. uint16_t bcdUSB;
  123. uint8_t bDeviceClass;
  124. uint8_t bDeviceSubClass;
  125. uint8_t bDeviceProtocol;
  126. uint8_t bMaxPacketSize0;
  127. uint16_t idVendor;
  128. uint16_t idProduct;
  129. uint16_t bcdDevice;
  130. uint8_t iManufacturer;
  131. uint8_t iProduct;
  132. uint8_t iSerialNumber;
  133. uint8_t bNumConfigurations;
  134. } usb_device_descriptor_t;
  135. typedef struct PACK
  136. {
  137. uint8_t bLength;
  138. uint8_t bDescriptorType;
  139. uint16_t wTotalLength;
  140. uint8_t bNumInterfaces;
  141. uint8_t bConfigurationValue;
  142. uint8_t iConfiguration;
  143. uint8_t bmAttributes;
  144. uint8_t bMaxPower;
  145. } usb_configuration_descriptor_t;
  146. typedef struct PACK
  147. {
  148. uint8_t bLength;
  149. uint8_t bDescriptorType;
  150. uint8_t bInterfaceNumber;
  151. uint8_t bAlternateSetting;
  152. uint8_t bNumEndpoints;
  153. uint8_t bInterfaceClass;
  154. uint8_t bInterfaceSubClass;
  155. uint8_t bInterfaceProtocol;
  156. uint8_t iInterface;
  157. } usb_interface_descriptor_t;
  158. typedef struct PACK
  159. {
  160. uint8_t bLength;
  161. uint8_t bDescriptorType;
  162. uint8_t bEndpointAddress;
  163. uint8_t bmAttributes;
  164. uint16_t wMaxPacketSize;
  165. uint8_t bInterval;
  166. } usb_endpoint_descriptor_t;
  167. typedef struct PACK
  168. {
  169. uint8_t bLength;
  170. uint8_t bDescriptorType;
  171. uint16_t wLANGID;
  172. } usb_string_descriptor_zero_t;
  173. typedef struct PACK
  174. {
  175. uint8_t bLength;
  176. uint8_t bDescriptorType;
  177. uint16_t bString;
  178. } usb_string_descriptor_t;
  179. /*- Prototypes --------------------------------------------------------------*/
  180. void usb_init(void);
  181. void usb_send(int ep, uint8_t *data, int size, void (*callback)(void));
  182. void usb_recv(int ep, uint8_t *data, int size, void (*callback)(void));
  183. void usb_handle_standard_request(usb_request_t *request);
  184. void usb_configuration_callback(int config);
  185. #endif // _USB_H_