cdc_loop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include "stm32.h"
  19. #include "usb.h"
  20. #include "usb_cdc.h"
  21. #include "usb_hid.h"
  22. #include "hid_usage_desktop.h"
  23. #include "hid_usage_button.h"
  24. #define CDC_EP0_SIZE 0x08
  25. #define CDC_RXD_EP 0x01
  26. #define CDC_TXD_EP 0x81
  27. #define CDC_DATA_SZ 0x40
  28. #define CDC_NTF_EP 0x82
  29. #define CDC_NTF_SZ 0x08
  30. #define HID_RIN_EP 0x83
  31. #define HID_RIN_SZ 0x10
  32. #define CDC_LOOPBACK
  33. #define ENABLE_HID_COMBO
  34. //#define CDC_USE_IRQ /* uncomment to build interrupt-based demo */
  35. /* Declaration of the report descriptor */
  36. struct cdc_config {
  37. struct usb_config_descriptor config;
  38. struct usb_interface_descriptor comm;
  39. struct usb_cdc_header_desc cdc_hdr;
  40. struct usb_cdc_call_mgmt_desc cdc_mgmt;
  41. struct usb_cdc_acm_desc cdc_acm;
  42. struct usb_cdc_union_desc cdc_union;
  43. struct usb_endpoint_descriptor comm_ep;
  44. struct usb_interface_descriptor data;
  45. struct usb_endpoint_descriptor data_eprx;
  46. struct usb_endpoint_descriptor data_eptx;
  47. #ifdef ENABLE_HID_COMBO
  48. struct usb_interface_descriptor hid;
  49. struct usb_hid_descriptor hid_desc;
  50. struct usb_endpoint_descriptor hid_ep;
  51. #endif //ENABLE_HID_COMBO
  52. } __attribute__((packed));
  53. /* HID mouse report desscriptor. 2 axis 5 buttons */
  54. static const uint8_t hid_report_desc[] = {
  55. HID_USAGE_PAGE(HID_PAGE_DESKTOP),
  56. HID_USAGE(HID_DESKTOP_MOUSE),
  57. HID_COLLECTION(HID_APPLICATION_COLLECTION),
  58. HID_USAGE(HID_DESKTOP_POINTER),
  59. HID_COLLECTION(HID_PHYSICAL_COLLECTION),
  60. HID_USAGE(HID_DESKTOP_X),
  61. HID_USAGE(HID_DESKTOP_Y),
  62. HID_LOGICAL_MINIMUM(-127),
  63. HID_LOGICAL_MAXIMUM(127),
  64. HID_REPORT_SIZE(8),
  65. HID_REPORT_COUNT(2),
  66. HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE ),
  67. HID_USAGE_PAGE(HID_PAGE_BUTTON),
  68. HID_USAGE_MINIMUM(1),
  69. HID_USAGE_MAXIMUM(5),
  70. HID_LOGICAL_MINIMUM(0),
  71. HID_LOGICAL_MAXIMUM(1),
  72. HID_REPORT_SIZE(1),
  73. HID_REPORT_COUNT(5),
  74. HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE ),
  75. HID_END_COLLECTION,
  76. HID_END_COLLECTION,
  77. };
  78. /* Device descriptor */
  79. static const struct usb_device_descriptor device_desc = {
  80. .bLength = sizeof(struct usb_device_descriptor),
  81. .bDescriptorType = USB_DTYPE_DEVICE,
  82. .bcdUSB = VERSION_BCD(2,0,0),
  83. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  84. .bDeviceSubClass = USB_SUBCLASS_NONE,
  85. .bDeviceProtocol = USB_PROTO_NONE,
  86. .bMaxPacketSize0 = CDC_EP0_SIZE,
  87. .idVendor = 0x0483,
  88. .idProduct = 0x5740,
  89. .bcdDevice = VERSION_BCD(1,0,0),
  90. .iManufacturer = 1,
  91. .iProduct = 2,
  92. .iSerialNumber = INTSERIALNO_DESCRIPTOR,
  93. .bNumConfigurations = 1,
  94. };
  95. /* Device configuration descriptor */
  96. static const struct cdc_config config_desc = {
  97. .config = {
  98. .bLength = sizeof(struct usb_config_descriptor),
  99. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  100. .wTotalLength = sizeof(struct cdc_config),
  101. #ifdef ENABLE_HID_COMBO
  102. .bNumInterfaces = 3,
  103. #else
  104. .bNumInterfaces = 2,
  105. #endif //ENABLE_HID_COMBO
  106. .bConfigurationValue = 1,
  107. .iConfiguration = NO_DESCRIPTOR,
  108. .bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
  109. .bMaxPower = USB_CFG_POWER_MA(100),
  110. },
  111. .comm = {
  112. .bLength = sizeof(struct usb_interface_descriptor),
  113. .bDescriptorType = USB_DTYPE_INTERFACE,
  114. .bInterfaceNumber = 0,
  115. .bAlternateSetting = 0,
  116. .bNumEndpoints = 1,
  117. .bInterfaceClass = USB_CLASS_CDC,
  118. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  119. .bInterfaceProtocol = USB_CDC_PROTO_V25TER,
  120. .iInterface = NO_DESCRIPTOR,
  121. },
  122. .cdc_hdr = {
  123. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  124. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  125. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  126. .bcdCDC = VERSION_BCD(1,1,0),
  127. },
  128. .cdc_mgmt = {
  129. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  130. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  131. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  132. .bmCapabilities = 0,
  133. .bDataInterface = 1,
  134. },
  135. .cdc_acm = {
  136. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  137. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  138. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  139. .bmCapabilities = 0,
  140. },
  141. .cdc_union = {
  142. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  143. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  144. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  145. .bMasterInterface0 = 0,
  146. .bSlaveInterface0 = 1,
  147. },
  148. .comm_ep = {
  149. .bLength = sizeof(struct usb_endpoint_descriptor),
  150. .bDescriptorType = USB_DTYPE_ENDPOINT,
  151. .bEndpointAddress = CDC_NTF_EP,
  152. .bmAttributes = USB_EPTYPE_INTERRUPT,
  153. .wMaxPacketSize = CDC_NTF_SZ,
  154. .bInterval = 0xFF,
  155. },
  156. .data = {
  157. .bLength = sizeof(struct usb_interface_descriptor),
  158. .bDescriptorType = USB_DTYPE_INTERFACE,
  159. .bInterfaceNumber = 1,
  160. .bAlternateSetting = 0,
  161. .bNumEndpoints = 2,
  162. .bInterfaceClass = USB_CLASS_CDC_DATA,
  163. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  164. .bInterfaceProtocol = USB_PROTO_NONE,
  165. .iInterface = NO_DESCRIPTOR,
  166. },
  167. .data_eprx = {
  168. .bLength = sizeof(struct usb_endpoint_descriptor),
  169. .bDescriptorType = USB_DTYPE_ENDPOINT,
  170. .bEndpointAddress = CDC_RXD_EP,
  171. .bmAttributes = USB_EPTYPE_BULK,
  172. .wMaxPacketSize = CDC_DATA_SZ,
  173. .bInterval = 0x01,
  174. },
  175. .data_eptx = {
  176. .bLength = sizeof(struct usb_endpoint_descriptor),
  177. .bDescriptorType = USB_DTYPE_ENDPOINT,
  178. .bEndpointAddress = CDC_TXD_EP,
  179. .bmAttributes = USB_EPTYPE_BULK,
  180. .wMaxPacketSize = CDC_DATA_SZ,
  181. .bInterval = 0x01,
  182. },
  183. #ifdef ENABLE_HID_COMBO
  184. .hid = {
  185. .bLength = sizeof(struct usb_interface_descriptor),
  186. .bDescriptorType = USB_DTYPE_INTERFACE,
  187. .bInterfaceNumber = 2,
  188. .bAlternateSetting = 0,
  189. .bNumEndpoints = 1,
  190. .bInterfaceClass = USB_CLASS_HID,
  191. .bInterfaceSubClass = USB_HID_SUBCLASS_NONBOOT,
  192. .bInterfaceProtocol = USB_HID_PROTO_NONBOOT,
  193. .iInterface = NO_DESCRIPTOR,
  194. },
  195. .hid_desc = {
  196. .bLength = sizeof(struct usb_hid_descriptor),
  197. .bDescriptorType = USB_DTYPE_HID,
  198. .bcdHID = VERSION_BCD(1,0,0),
  199. .bCountryCode = USB_HID_COUNTRY_NONE,
  200. .bNumDescriptors = 1,
  201. .bDescriptorType0 = USB_DTYPE_HID_REPORT,
  202. .wDescriptorLength0 = sizeof(hid_report_desc),
  203. },
  204. .hid_ep = {
  205. .bLength = sizeof(struct usb_endpoint_descriptor),
  206. .bDescriptorType = USB_DTYPE_ENDPOINT,
  207. .bEndpointAddress = HID_RIN_EP,
  208. .bmAttributes = USB_EPTYPE_INTERRUPT,
  209. .wMaxPacketSize = HID_RIN_SZ,
  210. .bInterval = 50,
  211. },
  212. #endif // ENABLE_HID_COMBO
  213. };
  214. static const struct usb_string_descriptor lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
  215. static const struct usb_string_descriptor manuf_desc_en = USB_STRING_DESC("Open source USB stack for STM32");
  216. static const struct usb_string_descriptor prod_desc_en = USB_STRING_DESC("CDC Loopback demo");
  217. static const struct usb_string_descriptor *const dtable[] = {
  218. &lang_desc,
  219. &manuf_desc_en,
  220. &prod_desc_en,
  221. };
  222. usbd_device udev;
  223. uint32_t ubuf[0x20];
  224. uint8_t fifo[0x200];
  225. uint32_t fpos = 0;
  226. static struct usb_cdc_line_coding cdc_line = {
  227. .dwDTERate = 38400,
  228. .bCharFormat = USB_CDC_1_STOP_BITS,
  229. .bParityType = USB_CDC_NO_PARITY,
  230. .bDataBits = 8,
  231. };
  232. static struct {
  233. int8_t x;
  234. int8_t y;
  235. uint8_t buttons;
  236. } __attribute__((packed)) hid_report_data;
  237. static usbd_respond cdc_getdesc (usbd_ctlreq *req, void **address, uint16_t *length) {
  238. const uint8_t dtype = req->wValue >> 8;
  239. const uint8_t dnumber = req->wValue & 0xFF;
  240. const void* desc;
  241. uint16_t len = 0;
  242. switch (dtype) {
  243. case USB_DTYPE_DEVICE:
  244. desc = &device_desc;
  245. break;
  246. case USB_DTYPE_CONFIGURATION:
  247. desc = &config_desc;
  248. len = sizeof(config_desc);
  249. break;
  250. case USB_DTYPE_STRING:
  251. if (dnumber < 3) {
  252. desc = dtable[dnumber];
  253. } else {
  254. return usbd_fail;
  255. }
  256. break;
  257. default:
  258. return usbd_fail;
  259. }
  260. if (len == 0) {
  261. len = ((struct usb_header_descriptor*)desc)->bLength;
  262. }
  263. *address = (void*)desc;
  264. *length = len;
  265. return usbd_ack;
  266. };
  267. static usbd_respond cdc_control(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback) {
  268. if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) == (USB_REQ_INTERFACE | USB_REQ_CLASS)
  269. && req->wIndex == 0 ) {
  270. switch (req->bRequest) {
  271. case USB_CDC_SET_CONTROL_LINE_STATE:
  272. return usbd_ack;
  273. case USB_CDC_SET_LINE_CODING:
  274. memcpy( req->data, &cdc_line, sizeof(cdc_line));
  275. return usbd_ack;
  276. case USB_CDC_GET_LINE_CODING:
  277. dev->status.data_ptr = &cdc_line;
  278. dev->status.data_count = sizeof(cdc_line);
  279. return usbd_ack;
  280. default:
  281. return usbd_fail;
  282. }
  283. }
  284. #ifdef ENABLE_HID_COMBO
  285. if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) == (USB_REQ_INTERFACE | USB_REQ_CLASS)
  286. && req->wIndex == 2 ) {
  287. switch (req->bRequest) {
  288. case USB_HID_SETIDLE:
  289. return usbd_ack;
  290. case USB_HID_GETREPORT:
  291. dev->status.data_ptr = &hid_report_data;
  292. dev->status.data_count = sizeof(hid_report_data);
  293. return usbd_ack;
  294. default:
  295. return usbd_fail;
  296. }
  297. }
  298. if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) == (USB_REQ_INTERFACE | USB_REQ_STANDARD)
  299. && req->wIndex == 2
  300. && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  301. switch (req->wValue >> 8) {
  302. case USB_DTYPE_HID:
  303. dev->status.data_ptr = (uint8_t*)&(config_desc.hid_desc);
  304. dev->status.data_count = sizeof(config_desc.hid_desc);
  305. return usbd_ack;
  306. case USB_DTYPE_HID_REPORT:
  307. dev->status.data_ptr = (uint8_t*)hid_report_desc;
  308. dev->status.data_count = sizeof(hid_report_desc);
  309. return usbd_ack;
  310. default:
  311. return usbd_fail;
  312. }
  313. }
  314. #endif // ENABLE_HID_COMBO
  315. return usbd_fail;
  316. }
  317. static void cdc_rxonly (usbd_device *dev, uint8_t event, uint8_t ep) {
  318. usbd_ep_read(dev, ep, fifo, CDC_DATA_SZ);
  319. }
  320. static void cdc_txonly(usbd_device *dev, uint8_t event, uint8_t ep) {
  321. uint8_t _t = dev->driver->frame_no();
  322. memset(fifo, _t, CDC_DATA_SZ);
  323. usbd_ep_write(dev, ep, fifo, CDC_DATA_SZ);
  324. }
  325. /* HID mouse IN endpoint callback */
  326. static void hid_mouse_move(usbd_device *dev, uint8_t event, uint8_t ep) {
  327. static uint8_t t = 0;
  328. if (t < 0x10) {
  329. hid_report_data.x = 1;
  330. hid_report_data.y = 0;
  331. } else if (t < 0x20) {
  332. hid_report_data.x = 1;
  333. hid_report_data.y = 1;
  334. } else if (t < 0x30) {
  335. hid_report_data.x = 0;
  336. hid_report_data.y = 1;
  337. } else if (t < 0x40) {
  338. hid_report_data.x = -1;
  339. hid_report_data.y = 1;
  340. } else if (t < 0x50) {
  341. hid_report_data.x = -1;
  342. hid_report_data.y = 0;
  343. } else if (t < 0x60) {
  344. hid_report_data.x = -1;
  345. hid_report_data.y = -1;
  346. } else if (t < 0x70) {
  347. hid_report_data.x = 0;
  348. hid_report_data.y = -1;
  349. } else {
  350. hid_report_data.x = 1;
  351. hid_report_data.y = -1;
  352. }
  353. t = (t + 1) & 0x7F;
  354. usbd_ep_write(dev, ep, &hid_report_data, sizeof(hid_report_data));
  355. }
  356. /* CDC loop callback. Both for the Data IN and Data OUT endpoint */
  357. static void cdc_loopback(usbd_device *dev, uint8_t event, uint8_t ep) {
  358. int _t;
  359. switch (event) {
  360. case usbd_evt_eptx:
  361. _t = usbd_ep_write(dev, CDC_TXD_EP, &fifo[0], (fpos < CDC_DATA_SZ) ? fpos : CDC_DATA_SZ);
  362. if (_t > 0) {
  363. memmove(&fifo[0], &fifo[_t], fpos - _t);
  364. fpos -= _t;
  365. }
  366. case usbd_evt_eprx:
  367. if (fpos < (sizeof(fifo) - CDC_DATA_SZ)) {
  368. _t = usbd_ep_read(dev, CDC_RXD_EP, &fifo[fpos], CDC_DATA_SZ);
  369. if (_t > 0) {
  370. fpos += _t;
  371. }
  372. }
  373. default:
  374. break;
  375. }
  376. }
  377. static usbd_respond cdc_setconf (usbd_device *dev, uint8_t cfg) {
  378. switch (cfg) {
  379. case 0:
  380. /* deconfiguring device */
  381. #ifdef ENABLE_HID_COMBO
  382. usbd_ep_deconfig(dev, HID_RIN_EP);
  383. usbd_reg_endpoint(dev, HID_RIN_EP, 0);
  384. #endif // ENABLE_HID_COMBO
  385. usbd_ep_deconfig(dev, CDC_NTF_EP);
  386. usbd_ep_deconfig(dev, CDC_TXD_EP);
  387. usbd_ep_deconfig(dev, CDC_RXD_EP);
  388. usbd_reg_endpoint(dev, CDC_RXD_EP, 0);
  389. usbd_reg_endpoint(dev, CDC_TXD_EP, 0);
  390. return usbd_ack;
  391. case 1:
  392. /* configuring device */
  393. usbd_ep_config(dev, CDC_RXD_EP, USB_EPTYPE_BULK /*| USB_EPTYPE_DBLBUF*/, CDC_DATA_SZ);
  394. usbd_ep_config(dev, CDC_TXD_EP, USB_EPTYPE_BULK /*| USB_EPTYPE_DBLBUF*/, CDC_DATA_SZ);
  395. usbd_ep_config(dev, CDC_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  396. #if defined(CDC_LOOPBACK)
  397. usbd_reg_endpoint(dev, CDC_RXD_EP, cdc_loopback);
  398. usbd_reg_endpoint(dev, CDC_TXD_EP, cdc_loopback);
  399. #else
  400. usbd_reg_endpoint(dev, CDC_RXD_EP, cdc_rxonly);
  401. usbd_reg_endpoint(dev, CDC_TXD_EP, cdc_txonly);
  402. #endif
  403. #ifdef ENABLE_HID_COMBO
  404. usbd_ep_config(dev, HID_RIN_EP, USB_EPTYPE_INTERRUPT, HID_RIN_SZ);
  405. usbd_reg_endpoint(dev, HID_RIN_EP, hid_mouse_move);
  406. usbd_ep_write(dev, HID_RIN_EP, 0, 0);
  407. #endif // ENABLE_HID_COMBO
  408. usbd_ep_write(dev, CDC_TXD_EP, 0, 0);
  409. return usbd_ack;
  410. default:
  411. return usbd_fail;
  412. }
  413. }
  414. static void cdc_init_usbd(void) {
  415. usbd_init(&udev, &usbd_hw, CDC_EP0_SIZE, ubuf, sizeof(ubuf));
  416. usbd_reg_config(&udev, cdc_setconf);
  417. usbd_reg_control(&udev, cdc_control);
  418. usbd_reg_descr(&udev, cdc_getdesc);
  419. }
  420. #if defined(CDC_USE_IRQ)
  421. #if defined(STM32L052xx)
  422. #define USB_HANDLER USB_IRQHandler
  423. #define USB_NVIC_IRQ USB_IRQn
  424. #elif defined(STM32L100xC)
  425. #define USB_HANDLER USB_LP_IRQHandler
  426. #define USB_NVIC_IRQ USB_LP_IRQn
  427. #elif defined(STM32L476xx)
  428. #define USB_HANDLER OTG_FS_IRQHandler
  429. #define USB_NVIC_IRQ OTG_FS_IRQn
  430. #elif defined(STM32F103x6)
  431. #define USB_HANDLER USB_LP_CAN1_RX0_IRQHandler
  432. #define USB_NVIC_IRQ USB_LP_CAN1_RX0_IRQn
  433. #elif defined(STM32F103xE)
  434. #define USB_HANDLER USB_LP_CAN1_RX0_IRQHandler
  435. #define USB_NVIC_IRQ USB_LP_CAN1_RX0_IRQn
  436. #elif defined(STM32F429xx) || defined(STM32F105xC) || defined(STM32F107xC)
  437. #define USB_HANDLER OTG_FS_IRQHandler
  438. #define USB_NVIC_IRQ OTG_FS_IRQn
  439. #else
  440. #error Not supported
  441. #endif
  442. void USB_HANDLER(void) {
  443. usbd_poll(&udev);
  444. }
  445. void main(void) {
  446. cdc_init_usbd();
  447. NVIC_EnableIRQ(USB_NVIC_IRQ);
  448. usbd_enable(&udev, true);
  449. usbd_connect(&udev, true);
  450. while(1) {
  451. __WFI();
  452. }
  453. }
  454. #else
  455. void main(void) {
  456. cdc_init_usbd();
  457. usbd_enable(&udev, true);
  458. usbd_connect(&udev, true);
  459. while(1) {
  460. usbd_poll(&udev);
  461. }
  462. }
  463. #endif