cdc_vcom.c 15 KB

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