cdc_vcom.c 14 KB

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