cdc_vcom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * File : cdc_vcom.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-02 Yi Qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <rthw.h>
  17. #include "cdc.h"
  18. static uclass_t cdc;
  19. static uep_t ep_in, ep_out, ep_cmd;
  20. #define CDC_RX_BUFSIZE 64
  21. #define CDC_TX_BUFSIZE 2048
  22. static rt_uint8_t rx_pool[CDC_RX_BUFSIZE];
  23. static rt_uint8_t tx_pool[CDC_TX_BUFSIZE];
  24. static struct rt_ringbuffer rx_ringbuffer;
  25. static struct rt_ringbuffer tx_ringbuffer;
  26. static struct rt_serial_device vcom_serial;
  27. static struct serial_ringbuffer vcom_int_rx;
  28. static rt_bool_t vcom_connected = RT_FALSE;
  29. static struct udevice_descriptor dev_desc =
  30. {
  31. USB_DESC_LENGTH_DEVICE, //bLength;
  32. USB_DESC_TYPE_DEVICE, //type;
  33. USB_BCD_VERSION, //bcdUSB;
  34. USB_CLASS_CDC, //bDeviceClass;
  35. 0x00, //bDeviceSubClass;
  36. 0x00, //bDeviceProtocol;
  37. 0x40, //bMaxPacketSize0;
  38. USB_VENDOR_ID, //idVendor;
  39. USB_CDC_PRODUCT_ID, //idProduct;
  40. USB_BCD_DEVICE, //bcdDevice;
  41. USB_STRING_MANU_INDEX, //iManufacturer;
  42. USB_STRING_PRODUCT_INDEX, //iProduct;
  43. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  44. USB_DYNAMIC, //bNumConfigurations;
  45. };
  46. /* communcation interface descriptor */
  47. static struct ucdc_comm_descriptor comm_desc =
  48. {
  49. /* Interface Descriptor */
  50. USB_DESC_LENGTH_INTERFACE,
  51. USB_DESC_TYPE_INTERFACE,
  52. USB_DYNAMIC,
  53. 0x00,
  54. 0x01,
  55. USB_CDC_CLASS_COMM,
  56. USB_CDC_SUBCLASS_ACM,
  57. USB_CDC_PROTOCOL_V25TER,
  58. 0x00,
  59. /* Header Functional Descriptor */
  60. 0x05,
  61. USB_CDC_CS_INTERFACE,
  62. USB_CDC_SCS_HEADER,
  63. 0x0110,
  64. /* Call Management Functional Descriptor */
  65. 0x05,
  66. USB_CDC_CS_INTERFACE,
  67. USB_CDC_SCS_CALL_MGMT,
  68. 0x00,
  69. USB_DYNAMIC,
  70. /* Abstract Control Management Functional Descriptor */
  71. 0x04,
  72. USB_CDC_CS_INTERFACE,
  73. USB_CDC_SCS_ACM,
  74. 0x02,
  75. /* Union Functional Descriptor */
  76. 0x05,
  77. USB_CDC_CS_INTERFACE,
  78. USB_CDC_SCS_UNION,
  79. USB_DYNAMIC,
  80. USB_DYNAMIC,
  81. /* Endpoint Descriptor */
  82. USB_DESC_LENGTH_ENDPOINT,
  83. USB_DESC_TYPE_ENDPOINT,
  84. USB_DYNAMIC | USB_DIR_IN,
  85. USB_EP_ATTR_INT,
  86. 0x08,
  87. 0xFF,
  88. };
  89. /* data interface descriptor */
  90. static struct ucdc_data_descriptor data_desc =
  91. {
  92. /* interface descriptor */
  93. USB_DESC_LENGTH_INTERFACE,
  94. USB_DESC_TYPE_INTERFACE,
  95. USB_DYNAMIC,
  96. 0x00,
  97. 0x02,
  98. USB_CDC_CLASS_DATA,
  99. 0x00,
  100. 0x00,
  101. 0x00,
  102. /* endpoint, bulk out */
  103. USB_DESC_LENGTH_ENDPOINT,
  104. USB_DESC_TYPE_ENDPOINT,
  105. USB_DYNAMIC | USB_DIR_OUT,
  106. USB_EP_ATTR_BULK,
  107. USB_CDC_BUFSIZE,
  108. 0x00,
  109. /* endpoint, bulk in */
  110. USB_DESC_LENGTH_ENDPOINT,
  111. USB_DESC_TYPE_ENDPOINT,
  112. USB_DYNAMIC | USB_DIR_IN,
  113. USB_EP_ATTR_BULK,
  114. USB_CDC_BUFSIZE,
  115. 0x00,
  116. };
  117. /**
  118. * This function will handle cdc bulk in endpoint request.
  119. *
  120. * @param device the usb device object.
  121. * @param size request size.
  122. *
  123. * @return RT_EOK.
  124. */
  125. static rt_err_t _ep_in_handler(udevice_t device, rt_size_t size)
  126. {
  127. rt_uint32_t level;
  128. rt_size_t length;
  129. rt_size_t mps = ep_in->ep_desc->wMaxPacketSize;
  130. size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
  131. if(size == 0) return RT_EOK;
  132. length = size > mps ? mps : size;
  133. level = rt_hw_interrupt_disable();
  134. rt_ringbuffer_get(&tx_ringbuffer, ep_in->buffer, length);
  135. rt_hw_interrupt_enable(level);
  136. /* send data to host */
  137. dcd_ep_write(device->dcd, ep_in, ep_in->buffer, length);
  138. return RT_EOK;
  139. }
  140. /**
  141. * This function will handle cdc bulk out endpoint request.
  142. *
  143. * @param device the usb device object.
  144. * @param size request size.
  145. *
  146. * @return RT_EOK.
  147. */
  148. static rt_err_t _ep_out_handler(udevice_t device, rt_size_t size)
  149. {
  150. rt_uint32_t level;
  151. RT_ASSERT(device != RT_NULL);
  152. /* receive data from USB VCOM */
  153. level = rt_hw_interrupt_disable();
  154. rt_ringbuffer_put(&rx_ringbuffer, ep_out->buffer, size);
  155. rt_hw_interrupt_enable(level);
  156. /* notify receive data */
  157. rt_hw_serial_isr(&vcom_serial);
  158. dcd_ep_read(device->dcd, ep_out, ep_out->buffer,
  159. ep_out->ep_desc->wMaxPacketSize);
  160. return RT_EOK;
  161. }
  162. /**
  163. * This function will handle cdc interrupt in endpoint request.
  164. *
  165. * @param device the usb device object.
  166. * @param size request size.
  167. *
  168. * @return RT_EOK.
  169. */
  170. static rt_err_t _ep_cmd_handler(udevice_t device, rt_size_t size)
  171. {
  172. RT_ASSERT(device != RT_NULL);
  173. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
  174. return RT_EOK;
  175. }
  176. /**
  177. * This function will handle cdc_get_line_coding request.
  178. *
  179. * @param device the usb device object.
  180. * @param setup the setup request.
  181. *
  182. * @return RT_EOK on successful.
  183. */
  184. static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
  185. {
  186. struct ucdc_line_coding data;
  187. rt_uint16_t size;
  188. RT_ASSERT(device != RT_NULL);
  189. RT_ASSERT(setup != RT_NULL);
  190. data.dwDTERate = 115200;
  191. data.bCharFormat = 0;
  192. data.bDataBits = 8;
  193. data.bParityType = 0;
  194. size = setup->length > 7 ? 7 : setup->length;
  195. dcd_ep_write(device->dcd, 0, (void*)&data, size);
  196. return RT_EOK;
  197. }
  198. /**
  199. * This function will handle cdc_set_line_coding request.
  200. *
  201. * @param device the usb device object.
  202. * @param setup the setup request.
  203. *
  204. * @return RT_EOK on successful.
  205. */
  206. static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
  207. {
  208. struct ucdc_line_coding data;
  209. rt_err_t ret;
  210. RT_ASSERT(device != RT_NULL);
  211. RT_ASSERT(setup != RT_NULL);
  212. rt_completion_init(&device->dcd->completion);
  213. dcd_ep_read(device->dcd, 0, (void*)&data, setup->length);
  214. ret = rt_completion_wait(&device->dcd->completion, 100);
  215. if(ret != RT_EOK)
  216. {
  217. rt_kprintf("_cdc_set_line_coding timeout\n");
  218. }
  219. return RT_EOK;
  220. }
  221. /**
  222. * This function will handle cdc interface request.
  223. *
  224. * @param device the usb device object.
  225. * @param setup the setup request.
  226. *
  227. * @return RT_EOK on successful.
  228. */
  229. static rt_err_t _interface_handler(udevice_t device, ureq_t setup)
  230. {
  231. RT_ASSERT(device != RT_NULL);
  232. RT_ASSERT(setup != RT_NULL);
  233. switch(setup->request)
  234. {
  235. case CDC_SEND_ENCAPSULATED_COMMAND:
  236. break;
  237. case CDC_GET_ENCAPSULATED_RESPONSE:
  238. break;
  239. case CDC_SET_COMM_FEATURE:
  240. break;
  241. case CDC_GET_COMM_FEATURE:
  242. break;
  243. case CDC_CLEAR_COMM_FEATURE:
  244. break;
  245. case CDC_SET_LINE_CODING:
  246. _cdc_set_line_coding(device, setup);
  247. vcom_connected = RT_TRUE;
  248. break;
  249. case CDC_GET_LINE_CODING:
  250. _cdc_get_line_coding(device, setup);
  251. break;
  252. case CDC_SET_CONTROL_LINE_STATE:
  253. rt_device_control((rt_device_t)device->dcd, CONTROL_SEND_STATUS, RT_NULL);
  254. break;
  255. case CDC_SEND_BREAK:
  256. break;
  257. default:
  258. rt_kprintf("unknown cdc request\n",setup->request_type);
  259. return -RT_ERROR;
  260. }
  261. return RT_EOK;
  262. }
  263. /**
  264. * This function will run cdc class, it will be called on handle set configuration request.
  265. *
  266. * @param device the usb device object.
  267. *
  268. * @return RT_EOK on successful.
  269. */
  270. static rt_err_t _class_run(udevice_t device)
  271. {
  272. RT_ASSERT(device != RT_NULL);
  273. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class run\n"));
  274. dcd_ep_read(device->dcd, ep_out, ep_out->buffer,
  275. ep_out->ep_desc->wMaxPacketSize);
  276. return RT_EOK;
  277. }
  278. /**
  279. * This function will stop cdc class, it will be called on handle set configuration request.
  280. *
  281. * @param device the usb device object.
  282. *
  283. * @return RT_EOK on successful.
  284. */
  285. static rt_err_t _class_stop(udevice_t device)
  286. {
  287. RT_ASSERT(device != RT_NULL);
  288. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class stop\n"));
  289. return RT_EOK;
  290. }
  291. /**
  292. * This function will handle system sof event.
  293. *
  294. * @param device the usb device object.
  295. *
  296. * @return RT_EOK on successful.
  297. */
  298. static rt_err_t _class_sof_handler(udevice_t device)
  299. {
  300. rt_uint32_t level;
  301. rt_size_t size;
  302. static rt_uint32_t frame_count = 0;
  303. if(vcom_connected != RT_TRUE) return -RT_ERROR;
  304. if (frame_count ++ == 5)
  305. {
  306. rt_size_t mps = ep_in->ep_desc->wMaxPacketSize;
  307. /* reset the frame counter */
  308. frame_count = 0;
  309. size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
  310. if(size == 0) return -RT_EFULL;
  311. size = size > mps ? mps : size;
  312. level = rt_hw_interrupt_disable();
  313. rt_ringbuffer_get(&tx_ringbuffer, ep_in->buffer, size);
  314. rt_hw_interrupt_enable(level);
  315. /* send data to host */
  316. dcd_ep_write(device->dcd, ep_in, ep_in->buffer, size);
  317. }
  318. return RT_EOK;
  319. }
  320. static struct uclass_ops ops =
  321. {
  322. _class_run,
  323. _class_stop,
  324. _class_sof_handler,
  325. };
  326. /**
  327. * This function will configure cdc descriptor.
  328. *
  329. * @param comm the communication interface number.
  330. * @param data the data interface number.
  331. *
  332. * @return RT_EOK on successful.
  333. */
  334. static rt_err_t _cdc_descriptor_config(rt_uint8_t comm, rt_uint8_t data)
  335. {
  336. comm_desc.call_mgmt_desc.data_interface = data;
  337. comm_desc.union_desc.master_interface = comm;
  338. comm_desc.union_desc.slave_interface0 = data;
  339. return RT_EOK;
  340. }
  341. /**
  342. * This function will create a cdc class instance.
  343. *
  344. * @param device the usb device object.
  345. *
  346. * @return RT_EOK on successful.
  347. */
  348. uclass_t rt_usbd_class_cdc_create(udevice_t device)
  349. {
  350. uintf_t intf_comm, intf_data;
  351. ualtsetting_t comm_setting, data_setting;
  352. /* parameter check */
  353. RT_ASSERT(device != RT_NULL);
  354. /* create a cdc class */
  355. cdc = rt_usbd_class_create(device, &dev_desc, &ops);
  356. /* create a cdc communication interface and a cdc data interface */
  357. intf_comm = rt_usbd_interface_create(device, _interface_handler);
  358. intf_data = rt_usbd_interface_create(device, _interface_handler);
  359. /* create a communication alternate setting and a data alternate setting */
  360. comm_setting = rt_usbd_altsetting_create(&comm_desc.intf_desc,
  361. sizeof(struct ucdc_comm_descriptor));
  362. data_setting = rt_usbd_altsetting_create(&data_desc.intf_desc,
  363. sizeof(struct ucdc_data_descriptor));
  364. /* configure the cdc interface descriptor */
  365. _cdc_descriptor_config(intf_comm->intf_num, intf_data->intf_num);
  366. /* create a bulk in and a bulk endpoint */
  367. ep_out = rt_usbd_endpoint_create(&data_desc.ep_out_desc, _ep_out_handler);
  368. ep_in = rt_usbd_endpoint_create(&data_desc.ep_in_desc, _ep_in_handler);
  369. /* add the bulk out and bulk in endpoints to the data alternate setting */
  370. rt_usbd_altsetting_add_endpoint(data_setting, ep_in);
  371. rt_usbd_altsetting_add_endpoint(data_setting, ep_out);
  372. /* add the data alternate setting to the data interface
  373. then set default setting of the interface */
  374. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  375. rt_usbd_set_altsetting(intf_data, 0);
  376. /* add the cdc data interface to cdc class */
  377. rt_usbd_class_add_interface(cdc, intf_data);
  378. /* create a command endpoint */
  379. ep_cmd = rt_usbd_endpoint_create(&comm_desc.ep_desc, _ep_cmd_handler);
  380. /* add the command endpoint to the cdc communication interface */
  381. rt_usbd_altsetting_add_endpoint(comm_setting, ep_cmd);
  382. /* add the communication alternate setting to the communication interface,
  383. then set default setting of the interface */
  384. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  385. rt_usbd_set_altsetting(intf_comm, 0);
  386. /* add the communication interface to the cdc class */
  387. rt_usbd_class_add_interface(cdc, intf_comm);
  388. return cdc;
  389. }
  390. /**
  391. * UART device in RT-Thread
  392. */
  393. static rt_err_t _vcom_configure(struct rt_serial_device *serial,
  394. struct serial_configure *cfg)
  395. {
  396. return RT_EOK;
  397. }
  398. static rt_err_t _vcom_control(struct rt_serial_device *serial,
  399. int cmd, void *arg)
  400. {
  401. switch (cmd)
  402. {
  403. case RT_DEVICE_CTRL_CLR_INT:
  404. /* disable rx irq */
  405. break;
  406. case RT_DEVICE_CTRL_SET_INT:
  407. /* enable rx irq */
  408. break;
  409. }
  410. return RT_EOK;
  411. }
  412. static int _vcom_putc(struct rt_serial_device *serial, char c)
  413. {
  414. rt_uint32_t level;
  415. if (vcom_connected != RT_TRUE) return 0;
  416. level = rt_hw_interrupt_disable();
  417. if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer))
  418. {
  419. rt_ringbuffer_putchar(&tx_ringbuffer, c);
  420. }
  421. rt_hw_interrupt_enable(level);
  422. return 1;
  423. }
  424. static int _vcom_getc(struct rt_serial_device *serial)
  425. {
  426. int result;
  427. rt_uint8_t ch;
  428. rt_uint32_t level;
  429. result = -1;
  430. level = rt_hw_interrupt_disable();
  431. if (RT_RINGBUFFER_SIZE(&rx_ringbuffer))
  432. {
  433. rt_ringbuffer_getchar(&rx_ringbuffer, &ch);
  434. result = ch;
  435. }
  436. rt_hw_interrupt_enable(level);
  437. return result;
  438. }
  439. static const struct rt_uart_ops usb_vcom_ops =
  440. {
  441. _vcom_configure,
  442. _vcom_control,
  443. _vcom_putc,
  444. _vcom_getc,
  445. };
  446. void rt_usb_vcom_init(void)
  447. {
  448. struct serial_configure config;
  449. /* initialize ring buffer */
  450. rt_ringbuffer_init(&rx_ringbuffer, rx_pool, CDC_RX_BUFSIZE);
  451. rt_ringbuffer_init(&tx_ringbuffer, tx_pool, CDC_TX_BUFSIZE);
  452. config.baud_rate = BAUD_RATE_115200;
  453. config.bit_order = BIT_ORDER_LSB;
  454. config.data_bits = DATA_BITS_8;
  455. config.parity = PARITY_NONE;
  456. config.stop_bits = STOP_BITS_1;
  457. config.invert = NRZ_NORMAL;
  458. vcom_serial.ops = &usb_vcom_ops;
  459. vcom_serial.int_rx = &vcom_int_rx;
  460. vcom_serial.config = config;
  461. /* register vcom device */
  462. rt_hw_serial_register(&vcom_serial, "vcom",
  463. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  464. RT_NULL);
  465. }