cdc_loop.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "usb.h"
  19. #include "inc/usb_cdc.h"
  20. #define CDC_EP0_SIZE 0x08
  21. #define CDC_RXD_EP 0x01
  22. #define CDC_TXD_EP 0x82
  23. #define CDC_DATA_SZ 0x40
  24. #define CDC_NTF_EP 0x83
  25. #define CDC_NTF_SZ 0x08
  26. #define CDC_LOOPBACK
  27. struct cdc_config {
  28. struct usb_config_descriptor config;
  29. struct usb_interface_descriptor comm;
  30. struct usb_cdc_header_desc cdc_hdr;
  31. struct usb_cdc_call_mgmt_desc cdc_mgmt;
  32. struct usb_cdc_acm_desc cdc_acm;
  33. struct usb_cdc_union_desc cdc_union;
  34. struct usb_endpoint_descriptor comm_ep;
  35. struct usb_interface_descriptor data;
  36. struct usb_endpoint_descriptor data_eprx;
  37. struct usb_endpoint_descriptor data_eptx;
  38. } __attribute__((packed));
  39. static const struct usb_device_descriptor device_desc = {
  40. .bLength = sizeof(struct usb_device_descriptor),
  41. .bDescriptorType = USB_DTYPE_DEVICE,
  42. .bcdUSB = VERSION_BCD(2,0,0),
  43. .bDeviceClass = USB_CLASS_CDC,
  44. .bDeviceSubClass = USB_SUBCLASS_NONE,
  45. .bDeviceProtocol = USB_PROTO_NONE,
  46. .bMaxPacketSize0 = CDC_EP0_SIZE,
  47. .idVendor = 0x0483,
  48. .idProduct = 0x5740,
  49. .bcdDevice = VERSION_BCD(1,0,0),
  50. .iManufacturer = 1,
  51. .iProduct = 2,
  52. .iSerialNumber = INTSERIALNO_DESCRIPTOR,
  53. .bNumConfigurations = 1,
  54. };
  55. static const struct cdc_config config_desc = {
  56. .config = {
  57. .bLength = sizeof(struct usb_config_descriptor),
  58. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  59. .wTotalLength = sizeof(struct cdc_config),
  60. .bNumInterfaces = 2,
  61. .bConfigurationValue = 1,
  62. .iConfiguration = NO_DESCRIPTOR,
  63. .bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
  64. .bMaxPower = USB_CFG_POWER_MA(100),
  65. },
  66. .comm = {
  67. .bLength = sizeof(struct usb_interface_descriptor),
  68. .bDescriptorType = USB_DTYPE_INTERFACE,
  69. .bInterfaceNumber = 0,
  70. .bAlternateSetting = 0,
  71. .bNumEndpoints = 1,
  72. .bInterfaceClass = USB_CLASS_CDC,
  73. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  74. .bInterfaceProtocol = USB_CDC_PROTO_V25TER,
  75. .iInterface = NO_DESCRIPTOR,
  76. },
  77. .cdc_hdr = {
  78. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  79. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  80. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  81. .bcdCDC = VERSION_BCD(1,1,0),
  82. },
  83. .cdc_mgmt = {
  84. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  85. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  86. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  87. .bmCapabilities = 0,
  88. .bDataInterface = 1,
  89. },
  90. .cdc_acm = {
  91. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  92. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  93. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  94. .bmCapabilities = 0,
  95. },
  96. .cdc_union = {
  97. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  98. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  99. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  100. .bMasterInterface0 = 0,
  101. .bSlaveInterface0 = 1,
  102. },
  103. .comm_ep = {
  104. .bLength = sizeof(struct usb_endpoint_descriptor),
  105. .bDescriptorType = USB_DTYPE_ENDPOINT,
  106. .bEndpointAddress = CDC_NTF_EP,
  107. .bmAttributes = USB_EPTYPE_INTERRUPT,
  108. .wMaxPacketSize = CDC_NTF_SZ,
  109. .bInterval = 0xFF,
  110. },
  111. .data = {
  112. .bLength = sizeof(struct usb_interface_descriptor),
  113. .bDescriptorType = USB_DTYPE_INTERFACE,
  114. .bInterfaceNumber = 1,
  115. .bAlternateSetting = 0,
  116. .bNumEndpoints = 2,
  117. .bInterfaceClass = USB_CLASS_CDC_DATA,
  118. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  119. .bInterfaceProtocol = USB_PROTO_NONE,
  120. .iInterface = NO_DESCRIPTOR,
  121. },
  122. .data_eprx = {
  123. .bLength = sizeof(struct usb_endpoint_descriptor),
  124. .bDescriptorType = USB_DTYPE_ENDPOINT,
  125. .bEndpointAddress = CDC_RXD_EP,
  126. .bmAttributes = USB_EPTYPE_BULK,
  127. .wMaxPacketSize = CDC_DATA_SZ,
  128. .bInterval = 0x01,
  129. },
  130. .data_eptx = {
  131. .bLength = sizeof(struct usb_endpoint_descriptor),
  132. .bDescriptorType = USB_DTYPE_ENDPOINT,
  133. .bEndpointAddress = CDC_TXD_EP,
  134. .bmAttributes = USB_EPTYPE_BULK,
  135. .wMaxPacketSize = CDC_DATA_SZ,
  136. .bInterval = 0x01,
  137. },
  138. };
  139. static const struct usb_string_descriptor lang_desc = USB_ARRAY_DESC(USB_LANGID_ENG_US);
  140. static const struct usb_string_descriptor manuf_desc_en = USB_STRING_DESC("Open source USB stack for STM32");
  141. static const struct usb_string_descriptor prod_desc_en = USB_STRING_DESC("CDC Loopback demo");
  142. static const struct usb_string_descriptor *const dtable[] = {
  143. &lang_desc,
  144. &manuf_desc_en,
  145. &prod_desc_en,
  146. };
  147. usbd_device udev;
  148. uint32_t ubuf[0x20];
  149. uint8_t fifo[0x200];
  150. uint32_t fpos = 0;
  151. static struct usb_cdc_line_coding cdc_line = {
  152. .dwDTERate = 38400,
  153. .bCharFormat = USB_CDC_1_STOP_BITS,
  154. .bParityType = USB_CDC_NO_PARITY,
  155. .bDataBits = 8,
  156. };
  157. static usbd_respond cdc_getdesc (usbd_ctlreq *req, void **address, uint16_t *length) {
  158. const uint8_t dtype = req->wValue >> 8;
  159. const uint8_t dnumber = req->wValue & 0xFF;
  160. const void* desc;
  161. uint16_t len = 0;
  162. switch (dtype) {
  163. case USB_DTYPE_DEVICE:
  164. desc = &device_desc;
  165. break;
  166. case USB_DTYPE_CONFIGURATION:
  167. desc = &config_desc;
  168. len = sizeof(config_desc);
  169. break;
  170. case USB_DTYPE_STRING:
  171. if (dnumber < 3) {
  172. desc = dtable[dnumber];
  173. } else {
  174. return usbd_fail;
  175. }
  176. break;
  177. default:
  178. return usbd_fail;
  179. }
  180. if (len == 0) {
  181. len = ((struct usb_header_descriptor*)desc)->bLength;
  182. }
  183. *address = (void*)desc;
  184. *length = len;
  185. return usbd_ack;
  186. };
  187. static usbd_respond cdc_control(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback) {
  188. if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) != (USB_REQ_INTERFACE | USB_REQ_CLASS)) return usbd_fail;
  189. switch (req->bRequest) {
  190. case USB_CDC_SET_CONTROL_LINE_STATE:
  191. return usbd_ack;
  192. case USB_CDC_SET_LINE_CODING:
  193. memmove( req->data, &cdc_line, sizeof(cdc_line));
  194. return usbd_ack;
  195. case USB_CDC_GET_LINE_CODING:
  196. dev->status.data_ptr = &cdc_line;
  197. dev->status.data_count = sizeof(cdc_line);
  198. return usbd_ack;
  199. default:
  200. return usbd_fail;
  201. }
  202. }
  203. static void cdc_rxonly (usbd_device *dev, uint8_t event, uint8_t ep) {
  204. usbd_ep_read(dev, ep, fifo, CDC_DATA_SZ);
  205. }
  206. static void cdc_txonly(usbd_device *dev, uint8_t event, uint8_t ep) {
  207. uint8_t _t = dev->driver->frame_no();
  208. memset(fifo, _t, CDC_DATA_SZ);
  209. usbd_ep_write(dev, ep, fifo, CDC_DATA_SZ);
  210. }
  211. static void cdc_loopback(usbd_device *dev, uint8_t event, uint8_t ep) {
  212. int _t;
  213. switch (event) {
  214. case usbd_evt_eptx:
  215. _t = usbd_ep_write(dev, CDC_TXD_EP, &fifo[0], (fpos < CDC_DATA_SZ) ? fpos : CDC_DATA_SZ);
  216. if (_t > 0) {
  217. memmove(&fifo[0], &fifo[_t], fpos - _t);
  218. fpos -= _t;
  219. }
  220. case usbd_evt_eprx:
  221. if (fpos < (sizeof(fifo) - CDC_DATA_SZ)) {
  222. _t = usbd_ep_read(dev, CDC_RXD_EP, &fifo[fpos], CDC_DATA_SZ);
  223. if (_t > 0) {
  224. fpos += _t;
  225. }
  226. }
  227. default:
  228. break;
  229. }
  230. }
  231. static usbd_respond cdc_setconf (usbd_device *dev, uint8_t cfg) {
  232. switch (cfg) {
  233. case 0:
  234. /* deconfiguring device */
  235. usbd_ep_deconfig(dev, CDC_NTF_EP);
  236. usbd_ep_deconfig(dev, CDC_TXD_EP);
  237. usbd_ep_deconfig(dev, CDC_RXD_EP);
  238. usbd_reg_endpoint(dev, CDC_RXD_EP, 0);
  239. usbd_reg_endpoint(dev, CDC_TXD_EP, 0);
  240. return usbd_ack;
  241. case 1:
  242. /* configuring device */
  243. usbd_ep_config(dev, CDC_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  244. usbd_ep_config(dev, CDC_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  245. usbd_ep_config(dev, CDC_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  246. #if defined(CDC_LOOPBACK)
  247. usbd_reg_endpoint(dev, CDC_RXD_EP, cdc_loopback);
  248. usbd_reg_endpoint(dev, CDC_TXD_EP, cdc_loopback);
  249. #else
  250. usbd_reg_endpoint(dev, CDC_RXD_EP, cdc_rxonly);
  251. usbd_reg_endpoint(dev, CDC_TXD_EP, cdc_txonly);
  252. #endif
  253. usbd_ep_write(dev, CDC_TXD_EP, 0, 0);
  254. return usbd_ack;
  255. default:
  256. return usbd_fail;
  257. }
  258. }
  259. static void cdc_init_usbd(void) {
  260. usbd_init(&udev, &usbd_hw, CDC_EP0_SIZE, ubuf, sizeof(ubuf));
  261. usbd_reg_config(&udev, cdc_setconf);
  262. usbd_reg_control(&udev, cdc_control);
  263. usbd_reg_descr(&udev, cdc_getdesc);
  264. }
  265. void main(void) {
  266. cdc_init_usbd();
  267. usbd_control(&udev, usbd_cmd_enable);
  268. usbd_control(&udev, usbd_cmd_connect);
  269. while(1) {
  270. usbd_poll(&udev);
  271. }
  272. }