cdc_vcom.c 16 KB

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