hid_msc_template.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "usbd_core.h"
  2. #include "usbd_msc.h"
  3. #include "usbd_hid.h"
  4. #define MSC_IN_EP 0x81
  5. #define MSC_OUT_EP 0x02
  6. /*!< endpoint address */
  7. #define HID_INT_EP 0x83
  8. #define HID_INT_EP_SIZE 8
  9. #define HID_INT_EP_INTERVAL 10
  10. #define USBD_VID 0xFFFF
  11. #define USBD_PID 0xFFFF
  12. #define USBD_MAX_POWER 100
  13. #define USBD_LANGID_STRING 1033
  14. /*!< report descriptor size */
  15. #define HID_MOUSE_REPORT_DESC_SIZE 74
  16. #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN + 25)
  17. const uint8_t hid_msc_descriptor[] = {
  18. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  19. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  20. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, 0x02),
  21. /************** Descriptor of Joystick Mouse interface ****************/
  22. /* 09 */
  23. 0x09, /* bLength: Interface Descriptor size */
  24. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  25. 0x01, /* bInterfaceNumber: Number of Interface */
  26. 0x00, /* bAlternateSetting: Alternate setting */
  27. 0x01, /* bNumEndpoints */
  28. 0x03, /* bInterfaceClass: HID */
  29. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  30. 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  31. 0, /* iInterface: Index of string descriptor */
  32. /******************** Descriptor of Joystick Mouse HID ********************/
  33. /* 18 */
  34. 0x09, /* bLength: HID Descriptor size */
  35. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  36. 0x11, /* bcdHID: HID Class Spec release number */
  37. 0x01,
  38. 0x00, /* bCountryCode: Hardware target country */
  39. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  40. 0x22, /* bDescriptorType */
  41. HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  42. 0x00,
  43. /******************** Descriptor of Mouse endpoint ********************/
  44. /* 27 */
  45. 0x07, /* bLength: Endpoint Descriptor size */
  46. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  47. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  48. 0x03, /* bmAttributes: Interrupt endpoint */
  49. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  50. 0x00,
  51. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  52. ///////////////////////////////////////
  53. /// string0 descriptor
  54. ///////////////////////////////////////
  55. USB_LANGID_INIT(USBD_LANGID_STRING),
  56. ///////////////////////////////////////
  57. /// string1 descriptor
  58. ///////////////////////////////////////
  59. 0x14, /* bLength */
  60. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  61. 'C', 0x00, /* wcChar0 */
  62. 'h', 0x00, /* wcChar1 */
  63. 'e', 0x00, /* wcChar2 */
  64. 'r', 0x00, /* wcChar3 */
  65. 'r', 0x00, /* wcChar4 */
  66. 'y', 0x00, /* wcChar5 */
  67. 'U', 0x00, /* wcChar6 */
  68. 'S', 0x00, /* wcChar7 */
  69. 'B', 0x00, /* wcChar8 */
  70. ///////////////////////////////////////
  71. /// string2 descriptor
  72. ///////////////////////////////////////
  73. 0x26, /* bLength */
  74. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  75. 'C', 0x00, /* wcChar0 */
  76. 'h', 0x00, /* wcChar1 */
  77. 'e', 0x00, /* wcChar2 */
  78. 'r', 0x00, /* wcChar3 */
  79. 'r', 0x00, /* wcChar4 */
  80. 'y', 0x00, /* wcChar5 */
  81. 'U', 0x00, /* wcChar6 */
  82. 'S', 0x00, /* wcChar7 */
  83. 'B', 0x00, /* wcChar8 */
  84. ' ', 0x00, /* wcChar9 */
  85. 'H', 0x00, /* wcChar10 */
  86. '-', 0x00, /* wcChar11 */
  87. 'M', 0x00, /* wcChar12 */
  88. ' ', 0x00, /* wcChar13 */
  89. 'D', 0x00, /* wcChar14 */
  90. 'E', 0x00, /* wcChar15 */
  91. 'M', 0x00, /* wcChar16 */
  92. 'O', 0x00, /* wcChar17 */
  93. ///////////////////////////////////////
  94. /// string3 descriptor
  95. ///////////////////////////////////////
  96. 0x16, /* bLength */
  97. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  98. '2', 0x00, /* wcChar0 */
  99. '0', 0x00, /* wcChar1 */
  100. '2', 0x00, /* wcChar2 */
  101. '2', 0x00, /* wcChar3 */
  102. '1', 0x00, /* wcChar4 */
  103. '2', 0x00, /* wcChar5 */
  104. '3', 0x00, /* wcChar6 */
  105. '4', 0x00, /* wcChar7 */
  106. '5', 0x00, /* wcChar8 */
  107. '6', 0x00, /* wcChar9 */
  108. #ifdef CONFIG_USB_HS
  109. ///////////////////////////////////////
  110. /// device qualifier descriptor
  111. ///////////////////////////////////////
  112. 0x0a,
  113. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  114. 0x00,
  115. 0x02,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x40,
  120. 0x01,
  121. 0x00,
  122. #endif
  123. 0x00
  124. };
  125. #define BLOCK_SIZE 512
  126. #define BLOCK_COUNT 10
  127. typedef struct
  128. {
  129. uint8_t BlockSpace[BLOCK_SIZE];
  130. } BLOCK_TYPE;
  131. BLOCK_TYPE mass_block[BLOCK_COUNT];
  132. void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
  133. {
  134. *block_num = 1000; //Pretend having so many buffer,not has actually.
  135. *block_size = BLOCK_SIZE;
  136. }
  137. int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
  138. {
  139. if (sector < 10)
  140. memcpy(buffer, mass_block[sector].BlockSpace, length);
  141. return 0;
  142. }
  143. int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
  144. {
  145. if (sector < 10)
  146. memcpy(mass_block[sector].BlockSpace, buffer, length);
  147. return 0;
  148. }
  149. /*!< hid mouse report descriptor */
  150. static const uint8_t hid_mouse_report_desc[HID_MOUSE_REPORT_DESC_SIZE] = {
  151. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  152. 0x09, 0x02, // USAGE (Mouse)
  153. 0xA1, 0x01, // COLLECTION (Application)
  154. 0x09, 0x01, // USAGE (Pointer)
  155. 0xA1, 0x00, // COLLECTION (Physical)
  156. 0x05, 0x09, // USAGE_PAGE (Button)
  157. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  158. 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
  159. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  160. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  161. 0x95, 0x03, // REPORT_COUNT (3)
  162. 0x75, 0x01, // REPORT_SIZE (1)
  163. 0x81, 0x02, // INPUT (Data,Var,Abs)
  164. 0x95, 0x01, // REPORT_COUNT (1)
  165. 0x75, 0x05, // REPORT_SIZE (5)
  166. 0x81, 0x01, // INPUT (Cnst,Var,Abs)
  167. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  168. 0x09, 0x30, // USAGE (X)
  169. 0x09, 0x31, // USAGE (Y)
  170. 0x09, 0x38,
  171. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  172. 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
  173. 0x75, 0x08, // REPORT_SIZE (8)
  174. 0x95, 0x03, // REPORT_COUNT (2)
  175. 0x81, 0x06, // INPUT (Data,Var,Rel)
  176. 0xC0, 0x09,
  177. 0x3c, 0x05,
  178. 0xff, 0x09,
  179. 0x01, 0x15,
  180. 0x00, 0x25,
  181. 0x01, 0x75,
  182. 0x01, 0x95,
  183. 0x02, 0xb1,
  184. 0x22, 0x75,
  185. 0x06, 0x95,
  186. 0x01, 0xb1,
  187. 0x01, 0xc0 // END_COLLECTION
  188. };
  189. /*!< mouse report struct */
  190. struct hid_mouse {
  191. uint8_t buttons;
  192. int8_t x;
  193. int8_t y;
  194. int8_t wheel;
  195. };
  196. /*!< class */
  197. static usbd_class_t hid_class;
  198. /*!< interface */
  199. static usbd_interface_t hid_intf;
  200. /*!< mouse report */
  201. static struct hid_mouse mouse_cfg;
  202. #define HID_STATE_IDLE 0
  203. #define HID_STATE_BUSY 1
  204. /*!< hid state ! Data can be sent only when state is idle */
  205. uint8_t hid_state = HID_STATE_IDLE;
  206. /* function ------------------------------------------------------------------*/
  207. static void usbd_hid_int_callback(uint8_t ep)
  208. {
  209. /*!< endpoint call back */
  210. /*!< transfer successfully */
  211. if (hid_state == HID_STATE_BUSY) {
  212. /*!< update the state */
  213. hid_state = HID_STATE_IDLE;
  214. }
  215. }
  216. /*!< endpoint call back */
  217. static usbd_endpoint_t hid_in_ep = {
  218. .ep_cb = usbd_hid_int_callback,
  219. .ep_addr = HID_INT_EP
  220. };
  221. /* function ------------------------------------------------------------------*/
  222. /**
  223. * @brief msc ram init
  224. * @pre none
  225. * @param[in] none
  226. * @retval none
  227. */
  228. void hid_msc_descriptor_init(void)
  229. {
  230. usbd_desc_register(hid_msc_descriptor);
  231. usbd_msc_class_init(MSC_OUT_EP, MSC_IN_EP);
  232. /*!< add interface */
  233. usbd_hid_add_interface(&hid_class, &hid_intf);
  234. /*!< interface add endpoint */
  235. usbd_interface_add_endpoint(&hid_intf, &hid_in_ep);
  236. /*!< register report descriptor */
  237. usbd_hid_report_descriptor_register(1, hid_mouse_report_desc, HID_MOUSE_REPORT_DESC_SIZE);
  238. /*!< init mouse report data */
  239. mouse_cfg.buttons = 0;
  240. mouse_cfg.wheel = 0;
  241. mouse_cfg.x = 0;
  242. mouse_cfg.y = 0;
  243. usbd_initialize();
  244. }
  245. /**
  246. * @brief device send report to host
  247. * @pre none
  248. * @param[in] ep endpoint address
  249. * @param[in] data Points to the data buffer waiting to be sent
  250. * @param[in] len Length of data to be sent
  251. * @retval none
  252. */
  253. void hid_mouse_send_report(uint8_t ep, uint8_t *data, uint8_t len)
  254. {
  255. if (usb_device_is_configured()) {
  256. if (hid_state == HID_STATE_IDLE) {
  257. /*!< updata the state */
  258. hid_state = HID_STATE_BUSY;
  259. /*!< write buffer */
  260. usbd_ep_write(ep, data, len, NULL);
  261. }
  262. }
  263. }
  264. /**
  265. * @brief hid mouse test
  266. * @pre none
  267. * @param[in] none
  268. * @retval none
  269. */
  270. void hid_mouse_send_report_test(void)
  271. {
  272. /*!< remove mouse pointer */
  273. mouse_cfg.x += 10;
  274. mouse_cfg.y = 0;
  275. /*!< send repotr to host */
  276. hid_mouse_send_report(HID_INT_EP, (uint8_t *)&mouse_cfg, 4);
  277. /*!< delay 1000ms */
  278. }