cdc_vcom.c 15 KB

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