1
0

cdc_loop.c 17 KB

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