cdc_loop.c 17 KB

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