cdc_vcom.c 15 KB

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