yc11xx_usb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright 2016, yichip Semiconductor
  3. * All Rights Reserved.
  4. *
  5. * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Yichip Semiconductor;
  6. * the contents of this file may not be disclosed to third parties, copied
  7. * or duplicated in any form, in whole or in part, without the prior
  8. * written permission of Yichip Semiconductor.
  9. */
  10. /**
  11. *@file yc11xx_usb.h
  12. *@brief USB support for application.
  13. */
  14. #ifndef _USB_H_
  15. #define _USB_H_
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "yc11xx.h"
  20. #include "yc_drv_common.h"
  21. //#include "systick.h"
  22. /**
  23. *@brief USB type.
  24. */
  25. #define SDK_DEBUG //Debug switch
  26. #define USB_TEST
  27. #define MS_REPORT_ID 0X01
  28. #define KB_NORMAL_REPORT_ID 0X00
  29. #define KB_MULTIKEY_REPORT_ID 0X03
  30. #define USB_EP1_KB 0x10
  31. #define USB_EP2_MS 0x20
  32. #define USB_EP2_MULTIKEY 0x21
  33. #define MAC_BOOT_MODE 0x01
  34. #define R_KEY 0x02
  35. #define LR_KEY 0x03
  36. /**
  37. *@brief USB request.
  38. */
  39. #define STANDARD_REQ 0x00
  40. #define CLASS_REQ 0x01
  41. #define HCI_CLASS_REQUEST_TYPE 0x20 /*!< SetReport HID Request */
  42. #define HID_CLASS_REQUEST_TYPE 0x21
  43. #define HID_TESTS_REQUEST_TYPE 0xa1 /*!< usb 2 cv : hid tests */
  44. #define CLEAR_PORT_REQUEST_TYPE 0x23
  45. #define GET_PORT_REQUEST_TYPE 0xa3
  46. #define ZERO_CLASS_REQUEST_TYPE 0X00
  47. /**
  48. *@brief Standard Request Codes.
  49. */
  50. #define GET_STATUS 0x00 /*!< Code for Get Status */
  51. #define CLEAR_FEATURE 0x01 /*!< Code for Clear Feature */
  52. #define SET_FEATURE 0x03 /*!< Code for Set Feature */
  53. #define SET_ADDRESS 0x05 /*!< Code for Set Address */
  54. #define GET_DESCRIPTOR 0x06 /*!< Code for Get Descriptor */
  55. #define SET_DESCRIPTOR 0x07 /*!< Code for Set Descriptor(not used) */
  56. #define GET_CONFIGURATION 0x08 /*!< Code for Get Configuration */
  57. #define SET_CONFIGURATION 0x09 /*!< Code for Set Configuration */
  58. #define GET_INTERFACE 0x0A /*!< Code for Get Interface */
  59. #define SET_INTERFACE 0x0B /*!< Code for Set Interface */
  60. #define SYNCH_FRAME 0x0C /*!< Code for Synch Frame(not used) */
  61. /**
  62. *@brief Standard Descriptor Types.
  63. */
  64. #define DSC_DEVICE 0x01 /*!< Device Descriptor */
  65. #define DSC_CONFIG 0x02 /*!< Configuration Descriptor */
  66. #define DSC_STRING 0x03 /*!< String Descriptor */
  67. #define DSC_INTERFACE 0x04 /*!< Interface Descriptor */
  68. #define DSC_ENDPOINT 0x05 /*!< Endpoint Descriptor */
  69. /**
  70. *@brief HID Descriptor Types.
  71. */
  72. #define DSC_HID 0x21 /*!< HID Class Descriptor */
  73. #define DSC_HID_REPORT 0x22 /*!< HID Report Descriptor */
  74. /**
  75. *@brief Define bmRequestType bitmaps.
  76. */
  77. #define IN_DEVICE 0x80 /*!< Request made to device, direction is IN*/
  78. #define OUT_DEVICE 0x00 /*!< Request made to device,direction is OUT */
  79. #define IN_INTERFACE 0x81 /*!< Request made to interface,direction is IN */
  80. #define OUT_INTERFACE 0x01 /*!< Request made to interface,direction is OUT */
  81. #define IN_ENDPOINT 0x82 /*!< Request made to endpoint, direction is IN*/
  82. #define OUT_ENDPOINT 0x02 /*!< Request made to endpoint */
  83. /**
  84. *@brief HID Request Codes.
  85. */
  86. #define GET_REPORT 0x01 /*!< Code for Get Report */
  87. #define GET_IDLE 0x02 /*!< Code for Get Idle */
  88. #define GET_PROTOCOL 0x03 /*!< Code for Get Protocol */
  89. #define SET_REPORT 0x09 /*!< Code for Set Report */
  90. #define SET_IDLE 0x0A /*!< Code for Set Idle */
  91. #define SET_PROTOCOL 0x0B /*!< Code for Set Protocol */
  92. #define HID_REPORT_ID 0xF2
  93. /**
  94. *@brief EP packet size.
  95. */
  96. #define EP0_PACKET_SIZE 0x40
  97. #define EP1_PACKET_SIZE 0x40
  98. #define EP2_PACKET_SIZE 0x40
  99. #define HID_REPORT_SIZE 16
  100. #define HID_REPORT_DESCRIPTOR_SIZE 0x001B
  101. /**
  102. *@brief USB state.
  103. */
  104. #define USB_STATUS_SETUP 0x10
  105. #define USB_STATUS_NAK 0x40
  106. /**
  107. *@brief USB event.
  108. */
  109. #define USB_STAY_BY 0
  110. #define USB_GOT_REPORT_REQ 1
  111. #define USB_CONNECTED 2
  112. #define USB_SLEEP 3
  113. #define USB_RESUME 4
  114. #define USB_SETIDLE_1 5
  115. /**
  116. *@brief Define device states.
  117. */
  118. #define DEV_ATTACHED 0X00 /*!< Device is in Attached State */
  119. #define DEV_POWERED 0X01 /*!< Device is in Powered State */
  120. #define DEV_DEFAULT 0X02 /*!< Device is in Default State */
  121. #define DEV_ADDRESS 0X03 /*!< Device is in Addressed State */
  122. #define DEV_CONFIGURED 0X04 /*!< Device is in Configured State */
  123. #define DEV_SUSPENDED 0X05 /*!< Device is in Suspended State */
  124. /**
  125. *@brief Define wIndex bitmaps.
  126. */
  127. #define IN_EP1 0x81 /*!< Index values used by Set and Clear */
  128. #define OUT_EP1 0x01 /*!< commands for Endpoint_Halt */
  129. #define IN_EP2 0x82
  130. #define OUT_EP2 0x02
  131. #define IN_EP3 0x83
  132. #define OUT_EP3 0x03
  133. /**
  134. *@brief Define wValue bitmaps for Standard Feature Selectors.
  135. */
  136. #define ENDPOINT_HALT 0x00 /*!< Endpoint_Halt feature selector */
  137. #define DEVICE_REMOTE_WAKEUP 0x01 /*!< Remote wakeup feature(not used) */
  138. #define BOOT_PROTOCOL 0x00
  139. #define REPORT_PROTOCOL 0x01
  140. #define USB_MAX_NUM_CFG 0x01
  141. /**
  142. *@brief PC report.
  143. */
  144. #define PC_GET_REPORT 0x01ba
  145. #define PC_SET_REPORT 0x02ba
  146. #define PC_SET_REPORT_bValue 0x0200
  147. #define PC_SET_REPORT_wIndex 0x0000
  148. #define PC_SET_REPORT_bLength 0x0001
  149. #define PC_REPORT_ID 0xba
  150. #ifndef MSB
  151. #define MSB 1
  152. #endif
  153. #ifndef LSB
  154. #define LSB 0
  155. #endif
  156. typedef union{uint16_t i;uint8_t c[2];} WORDER;
  157. /**
  158. *@brief USB set up.
  159. */
  160. typedef struct
  161. {
  162. uint8_t bmRequestType;
  163. uint8_t bRequest;
  164. WORDER bValue;
  165. WORDER wIndex;
  166. WORDER bLength;
  167. uint32_t redundant_buf1;
  168. uint32_t redundant_buf2;
  169. } usb_setup;
  170. /**
  171. *@brief Standard Device Descriptor Type Definition.
  172. */
  173. typedef /*code*/ struct
  174. {
  175. uint8_t bLength; /*!< Size of this Descriptor in Bytes */
  176. uint8_t bDescriptorType; /*!< Descriptor Type (=1) */
  177. uint16_t bcdUSB; /*!< USB Spec Release Number in BCD */
  178. uint8_t bDeviceClass; /*!< Device Class Code */
  179. uint8_t bDeviceSubClass; /*!< Device Subclass Code */
  180. uint8_t bDeviceProtocol; /*!< Device Protocol Code */
  181. uint8_t bMaxPacketSize0; /*!< Maximum Packet Size for EP0 */
  182. uint16_t idVendor; /*!< Vendor ID */
  183. uint16_t idProduct; /*!< Product ID */
  184. uint16_t bcdDevice; /*!< Device Release Number in BCD */
  185. uint8_t iManufacturer; /*!< Index of String Desc for Manufacturer */
  186. uint8_t iProduct; /*!< Index of String Desc for Product */
  187. uint8_t iSerialNumber; /*!< Index of String Desc for SerNo */
  188. uint8_t bNumConfigurations; /*!< Number of possible Configurations */
  189. } device_descriptor;
  190. /**
  191. *@brief Standard Configuration Descriptor Type Definition.
  192. */
  193. typedef /*code*/ struct
  194. {
  195. uint8_t bLength; /*!< Size of this Descriptor in uint8_ts */
  196. uint8_t bDescriptorType; /*!< Descriptor Type (=2) */
  197. uint16_t wTotalLength; /*!< Total Length of Data for this Conf */
  198. uint8_t bNumInterfaces; /*!< No of Interfaces supported by this */
  199. uint8_t bConfigurationValue; /*!< Designator Value for *this */
  200. uint8_t iConfiguration; /*!< Index of String Desc for this Conf */
  201. uint8_t bmAttributes; /*!< Configuration Characteristics (see below) */
  202. uint8_t bMaxPower; /*!< Configuration Characteristics (see below) */
  203. /*!< Conf (*2mA) */
  204. } configuration_descriptor; /*!< End of Configuration Descriptor Type */
  205. /**
  206. *@brief Standard Interface Descriptor Type Definition.
  207. */
  208. typedef /*code*/ struct
  209. {
  210. uint8_t bLength; /*!< Size of this Descriptor in uint8_ts */
  211. uint8_t bDescriptorType; /*!< Descriptor Type (=4) */
  212. uint8_t bInterfaceNumber; /*!< Number of *this* Interface (0..) */
  213. uint8_t bAlternateSetting; /*!< Alternative for this Interface (if any) */
  214. uint8_t bNumEndpoints; /*!< No of EPs used by this IF (excl. EP0) */
  215. uint8_t bInterfaceClass; /*!< Interface Class Code */
  216. uint8_t bInterfaceSubClass; /*!< Interface Subclass Code */
  217. uint8_t bInterfaceProtocol; /*!< Interface Protocol Code */
  218. uint8_t iInterface; /*!< Index of String Desc for this Interface */
  219. } interface_descriptor; /*!< End of Interface Descriptor Type */
  220. /**
  221. *@brief Standard Class Descriptor Type Definition.
  222. */
  223. typedef /*code */struct
  224. {
  225. uint8_t bLength; /*!< Size of this Descriptor in uint8_ts (=9) */
  226. uint8_t bDescriptorType; /*!< Descriptor Type (HID=0x21) */
  227. uint16_t bcdHID; /*!< HID Class Specification */
  228. // release number (=1.01)
  229. uint8_t bCountryCode; /*!< Localized country code */
  230. uint8_t bNumDescriptors; /*!< Number of class descriptors to follow */
  231. uint8_t bReportDescriptorType;/*!< Report descriptor type (HID=0x22) */
  232. uint16_t wItemLength; /*!< Total length of report descriptor table */
  233. } class_descriptor; /*!< End of Class Descriptor Type */
  234. /**
  235. *@brief Standard Endpoint Descriptor Type Definition.
  236. */
  237. typedef /*code*/ struct
  238. {
  239. uint8_t bLength; /*!< Size of this Descriptor in uint8_ts */
  240. uint8_t bDescriptorType; /*!< Descriptor Type (=5) */
  241. uint8_t bEndpointAddress; /*!< Endpoint Address (Number + Direction) */
  242. uint8_t bmAttributes; /*!< Endpoint Attributes (Transfer Type) */
  243. uint16_t wMaxPacketSize; /*!< Max. Endpoint Packet Size */
  244. uint8_t bInterval; /*!< Polling Interval (Interrupt) ms */
  245. } endpoint_descriptor;
  246. /**
  247. *@brief HID Configuration Descriptor Type Definition.
  248. */
  249. /**
  250. *From "USB Device Class Definition for Human Interface Devices (HID)".
  251. *Section 7.1:
  252. *"When a Get_Descriptor(Configuration) request is issued,
  253. *it returns the Configuration descriptor, all Interface descriptors,
  254. *all Endpoint descriptors, and the HID descriptor for each interface."
  255. */
  256. typedef /*code*/ struct {
  257. configuration_descriptor hid_configuration_descriptor;
  258. interface_descriptor hid_keyboard_interface_descriptor;
  259. class_descriptor hid_keyboard_descriptor;
  260. endpoint_descriptor hid_keyboard_endpoint_in_descriptor;
  261. interface_descriptor hid_ms_interface_descriptor;
  262. class_descriptor hid_ms_descriptor;
  263. endpoint_descriptor hid_ms_endpoint_in_descriptor;
  264. } hid_configuration_descriptor;
  265. #define HID_KB_RP_DESCRIPTOR_LEN 0x3f
  266. #define HID_MO_RP_DESCRIPTOR_LEN 0xc3
  267. #define HID_CONFIG_DESCRIPTOR_LEN 0x3b
  268. /* configuration
  269. const hid_configuration_descriptor hid_confdesc;
  270. #define confdesc (hid_confdesc.hid_configuration_descriptor)
  271. #define KeyInterfaceDesc (hid_confdesc.hid_keyboard_descriptor)
  272. #define KeyHidDesc (hid_confdesc.hid_keyboard_endpoint_in_descriptor)
  273. #define KeyEndpiontDesc (hid_confdesc.hid_keyboard_interface_descriptor)
  274. #define MsInterfaceDesc (hid_confdesc.hid_ms_descriptor)
  275. #define MsHidDesc (hid_confdesc.hid_ms_endpoint_in_descriptor)
  276. #define MsEndpiontDesc (hid_confdesc.hid_ms_interface_descriptor)
  277. */
  278. //const device_descriptor devicedesc;
  279. //const uint8_t hidreportdesc_kb[HID_KB_RP_DESCRIPTOR_LEN];
  280. //const uint8_t hidreportdesc_m[HID_MO_RP_DESCRIPTOR_LEN];
  281. //const uint8_t confdesc[HID_CONFIG_DESCRIPTOR_LEN];
  282. //const uint8_t* const string_table [4];
  283. /**
  284. *@brief USB initialization.
  285. *@param None.
  286. *@return None.
  287. */
  288. void usb_init();
  289. /**
  290. *@brief USB interrupt handler.
  291. *@param None.
  292. *@return None.
  293. */
  294. void USB_IRQHandler();
  295. #endif