cdc_vcom.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-10-02 Yi Qiu first version
  9. * 2012-12-12 heyuanjie87 change endpoints and function handler
  10. * 2013-06-25 heyuanjie87 remove SOF mechinism
  11. * 2013-07-20 Yi Qiu do more test
  12. * 2016-02-01 Urey Fix some error
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <rtservice.h>
  17. #include <rtdevice.h>
  18. #include <drivers/serial.h>
  19. #include "drivers/usb_device.h"
  20. #include "cdc.h"
  21. #ifdef RT_USB_DEVICE_CDC
  22. #define TX_TIMEOUT 1000
  23. #define CDC_RX_BUFSIZE 128
  24. #define CDC_MAX_PACKET_SIZE 64
  25. #define VCOM_DEVICE "vcom"
  26. #ifdef RT_VCOM_TASK_STK_SIZE
  27. #define VCOM_TASK_STK_SIZE RT_VCOM_TASK_STK_SIZE
  28. #else /*!RT_VCOM_TASK_STK_SIZE*/
  29. #define VCOM_TASK_STK_SIZE 512
  30. #endif /*RT_VCOM_TASK_STK_SIZE*/
  31. #ifdef RT_VCOM_TX_USE_DMA
  32. #define VCOM_TX_USE_DMA
  33. #endif /*RT_VCOM_TX_USE_DMA*/
  34. #ifdef RT_VCOM_SERNO
  35. #define _SER_NO RT_VCOM_SERNO
  36. #else /*!RT_VCOM_SERNO*/
  37. #define _SER_NO "32021919830108"
  38. #endif /*RT_VCOM_SERNO*/
  39. #ifdef RT_VCOM_SER_LEN
  40. #define _SER_NO_LEN RT_VCOM_SER_LEN
  41. #else /*!RT_VCOM_SER_LEN*/
  42. #define _SER_NO_LEN 14 /*rt_strlen("32021919830108")*/
  43. #endif /*RT_VCOM_SER_LEN*/
  44. ALIGN(RT_ALIGN_SIZE)
  45. static rt_uint8_t vcom_thread_stack[VCOM_TASK_STK_SIZE];
  46. static struct rt_thread vcom_thread;
  47. static struct ucdc_line_coding line_coding;
  48. #define CDC_TX_BUFSIZE 1024
  49. #define CDC_BULKIN_MAXSIZE (CDC_TX_BUFSIZE / 8)
  50. #define CDC_TX_HAS_DATE 0x01
  51. struct vcom
  52. {
  53. struct rt_serial_device serial;
  54. uep_t ep_out;
  55. uep_t ep_in;
  56. uep_t ep_cmd;
  57. rt_bool_t connected;
  58. rt_bool_t in_sending;
  59. struct rt_completion wait;
  60. rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
  61. struct rt_ringbuffer rx_ringbuffer;
  62. rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
  63. struct rt_ringbuffer tx_ringbuffer;
  64. struct rt_event tx_event;
  65. };
  66. struct vcom_tx_msg
  67. {
  68. struct rt_serial_device * serial;
  69. const char *buf;
  70. rt_size_t size;
  71. };
  72. ALIGN(4)
  73. static struct udevice_descriptor dev_desc =
  74. {
  75. USB_DESC_LENGTH_DEVICE, //bLength;
  76. USB_DESC_TYPE_DEVICE, //type;
  77. USB_BCD_VERSION, //bcdUSB;
  78. USB_CLASS_CDC, //bDeviceClass;
  79. 0x00, //bDeviceSubClass;
  80. 0x00, //bDeviceProtocol;
  81. CDC_MAX_PACKET_SIZE, //bMaxPacketSize0;
  82. _VENDOR_ID, //idVendor;
  83. _PRODUCT_ID, //idProduct;
  84. USB_BCD_DEVICE, //bcdDevice;
  85. USB_STRING_MANU_INDEX, //iManufacturer;
  86. USB_STRING_PRODUCT_INDEX, //iProduct;
  87. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  88. USB_DYNAMIC, //bNumConfigurations;
  89. };
  90. //FS and HS needed
  91. ALIGN(4)
  92. static struct usb_qualifier_descriptor dev_qualifier =
  93. {
  94. sizeof(dev_qualifier), //bLength
  95. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  96. 0x0200, //bcdUSB
  97. USB_CLASS_CDC, //bDeviceClass
  98. 0x00, //bDeviceSubClass
  99. 0x00, //bDeviceProtocol
  100. 64, //bMaxPacketSize0
  101. 0x01, //bNumConfigurations
  102. 0,
  103. };
  104. /* communcation interface descriptor */
  105. ALIGN(4)
  106. const static struct ucdc_comm_descriptor _comm_desc =
  107. {
  108. #ifdef RT_USB_DEVICE_COMPOSITE
  109. /* Interface Association Descriptor */
  110. {
  111. USB_DESC_LENGTH_IAD,
  112. USB_DESC_TYPE_IAD,
  113. USB_DYNAMIC,
  114. 0x02,
  115. USB_CDC_CLASS_COMM,
  116. USB_CDC_SUBCLASS_ACM,
  117. USB_CDC_PROTOCOL_V25TER,
  118. 0x00,
  119. },
  120. #endif
  121. /* Interface Descriptor */
  122. {
  123. USB_DESC_LENGTH_INTERFACE,
  124. USB_DESC_TYPE_INTERFACE,
  125. USB_DYNAMIC,
  126. 0x00,
  127. 0x01,
  128. USB_CDC_CLASS_COMM,
  129. USB_CDC_SUBCLASS_ACM,
  130. USB_CDC_PROTOCOL_V25TER,
  131. 0x00,
  132. },
  133. /* Header Functional Descriptor */
  134. {
  135. 0x05,
  136. USB_CDC_CS_INTERFACE,
  137. USB_CDC_SCS_HEADER,
  138. 0x0110,
  139. },
  140. /* Call Management Functional Descriptor */
  141. {
  142. 0x05,
  143. USB_CDC_CS_INTERFACE,
  144. USB_CDC_SCS_CALL_MGMT,
  145. 0x00,
  146. USB_DYNAMIC,
  147. },
  148. /* Abstract Control Management Functional Descriptor */
  149. {
  150. 0x04,
  151. USB_CDC_CS_INTERFACE,
  152. USB_CDC_SCS_ACM,
  153. 0x02,
  154. },
  155. /* Union Functional Descriptor */
  156. {
  157. 0x05,
  158. USB_CDC_CS_INTERFACE,
  159. USB_CDC_SCS_UNION,
  160. USB_DYNAMIC,
  161. USB_DYNAMIC,
  162. },
  163. /* Endpoint Descriptor */
  164. {
  165. USB_DESC_LENGTH_ENDPOINT,
  166. USB_DESC_TYPE_ENDPOINT,
  167. USB_DYNAMIC | USB_DIR_IN,
  168. USB_EP_ATTR_INT,
  169. 0x08,
  170. 0xFF,
  171. },
  172. };
  173. /* data interface descriptor */
  174. ALIGN(4)
  175. const static struct ucdc_data_descriptor _data_desc =
  176. {
  177. /* interface descriptor */
  178. {
  179. USB_DESC_LENGTH_INTERFACE,
  180. USB_DESC_TYPE_INTERFACE,
  181. USB_DYNAMIC,
  182. 0x00,
  183. 0x02,
  184. USB_CDC_CLASS_DATA,
  185. 0x00,
  186. 0x00,
  187. 0x00,
  188. },
  189. /* endpoint, bulk out */
  190. {
  191. USB_DESC_LENGTH_ENDPOINT,
  192. USB_DESC_TYPE_ENDPOINT,
  193. USB_DYNAMIC | USB_DIR_OUT,
  194. USB_EP_ATTR_BULK,
  195. USB_CDC_BUFSIZE,
  196. 0x00,
  197. },
  198. /* endpoint, bulk in */
  199. {
  200. USB_DESC_LENGTH_ENDPOINT,
  201. USB_DESC_TYPE_ENDPOINT,
  202. USB_DYNAMIC | USB_DIR_IN,
  203. USB_EP_ATTR_BULK,
  204. USB_CDC_BUFSIZE,
  205. 0x00,
  206. },
  207. };
  208. ALIGN(4)
  209. static char serno[_SER_NO_LEN + 1] = {'\0'};
  210. RT_WEAK rt_err_t vcom_get_stored_serno(char *serno, int size);
  211. rt_err_t vcom_get_stored_serno(char *serno, int size)
  212. {
  213. return RT_ERROR;
  214. }
  215. ALIGN(4)
  216. const static char* _ustring[] =
  217. {
  218. "Language",
  219. "RT-Thread Team.",
  220. "RTT Virtual Serial",
  221. serno,
  222. "Configuration",
  223. "Interface",
  224. };
  225. static void rt_usb_vcom_init(struct ufunction *func);
  226. static void _vcom_reset_state(ufunction_t func)
  227. {
  228. struct vcom* data;
  229. int lvl;
  230. RT_ASSERT(func != RT_NULL)
  231. data = (struct vcom*)func->user_data;
  232. lvl = rt_hw_interrupt_disable();
  233. data->connected = RT_FALSE;
  234. data->in_sending = RT_FALSE;
  235. /*rt_kprintf("reset USB serial\n", cnt);*/
  236. rt_hw_interrupt_enable(lvl);
  237. }
  238. /**
  239. * This function will handle cdc bulk in endpoint request.
  240. *
  241. * @param func the usb function object.
  242. * @param size request size.
  243. *
  244. * @return RT_EOK.
  245. */
  246. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  247. {
  248. struct vcom *data;
  249. rt_size_t request_size;
  250. RT_ASSERT(func != RT_NULL);
  251. data = (struct vcom*)func->user_data;
  252. request_size = data->ep_in->request.size;
  253. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler %d\n", request_size));
  254. if ((request_size != 0) && ((request_size % EP_MAXPACKET(data->ep_in)) == 0))
  255. {
  256. /* don't have data right now. Send a zero-length-packet to
  257. * terminate the transaction.
  258. *
  259. * FIXME: actually, this might not be the right place to send zlp.
  260. * Only the rt_device_write could know how much data is sending. */
  261. data->in_sending = RT_TRUE;
  262. data->ep_in->request.buffer = RT_NULL;
  263. data->ep_in->request.size = 0;
  264. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  265. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  266. return RT_EOK;
  267. }
  268. rt_completion_done(&data->wait);
  269. return RT_EOK;
  270. }
  271. /**
  272. * This function will handle cdc bulk out endpoint request.
  273. *
  274. * @param func the usb function object.
  275. * @param size request size.
  276. *
  277. * @return RT_EOK.
  278. */
  279. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  280. {
  281. rt_uint32_t level;
  282. struct vcom *data;
  283. RT_ASSERT(func != RT_NULL);
  284. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
  285. data = (struct vcom*)func->user_data;
  286. /* ensure serial is active */
  287. if((data->serial.parent.flag & RT_DEVICE_FLAG_ACTIVATED)
  288. && (data->serial.parent.open_flag & RT_DEVICE_OFLAG_OPEN))
  289. {
  290. /* receive data from USB VCOM */
  291. level = rt_hw_interrupt_disable();
  292. rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
  293. rt_hw_interrupt_enable(level);
  294. /* notify receive data */
  295. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND);
  296. }
  297. data->ep_out->request.buffer = data->ep_out->buffer;
  298. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  299. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  300. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  301. return RT_EOK;
  302. }
  303. /**
  304. * This function will handle cdc interrupt in endpoint request.
  305. *
  306. * @param device the usb device object.
  307. * @param size request size.
  308. *
  309. * @return RT_EOK.
  310. */
  311. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  312. {
  313. RT_ASSERT(func != RT_NULL);
  314. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
  315. return RT_EOK;
  316. }
  317. /**
  318. * This function will handle cdc_get_line_coding request.
  319. *
  320. * @param device the usb device object.
  321. * @param setup the setup request.
  322. *
  323. * @return RT_EOK on successful.
  324. */
  325. static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
  326. {
  327. struct ucdc_line_coding data;
  328. rt_uint16_t size;
  329. RT_ASSERT(device != RT_NULL);
  330. RT_ASSERT(setup != RT_NULL);
  331. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));
  332. data.dwDTERate = 115200;
  333. data.bCharFormat = 0;
  334. data.bDataBits = 8;
  335. data.bParityType = 0;
  336. size = setup->wLength > 7 ? 7 : setup->wLength;
  337. rt_usbd_ep0_write(device, (void*)&data, size);
  338. return RT_EOK;
  339. }
  340. static rt_err_t _cdc_set_line_coding_callback(udevice_t device, rt_size_t size)
  341. {
  342. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding_callback\n"));
  343. dcd_ep0_send_status(device->dcd);
  344. return RT_EOK;
  345. }
  346. /**
  347. * This function will handle cdc_set_line_coding request.
  348. *
  349. * @param device the usb device object.
  350. * @param setup the setup request.
  351. *
  352. * @return RT_EOK on successful.
  353. */
  354. static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
  355. {
  356. RT_ASSERT(device != RT_NULL);
  357. RT_ASSERT(setup != RT_NULL);
  358. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
  359. rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
  360. _cdc_set_line_coding_callback);
  361. return RT_EOK;
  362. }
  363. /**
  364. * This function will handle cdc interface request.
  365. *
  366. * @param device the usb device object.
  367. * @param setup the setup request.
  368. *
  369. * @return RT_EOK on successful.
  370. */
  371. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  372. {
  373. struct vcom *data;
  374. RT_ASSERT(func != RT_NULL);
  375. RT_ASSERT(func->device != RT_NULL);
  376. RT_ASSERT(setup != RT_NULL);
  377. data = (struct vcom*)func->user_data;
  378. switch(setup->bRequest)
  379. {
  380. case CDC_SEND_ENCAPSULATED_COMMAND:
  381. break;
  382. case CDC_GET_ENCAPSULATED_RESPONSE:
  383. break;
  384. case CDC_SET_COMM_FEATURE:
  385. break;
  386. case CDC_GET_COMM_FEATURE:
  387. break;
  388. case CDC_CLEAR_COMM_FEATURE:
  389. break;
  390. case CDC_SET_LINE_CODING:
  391. _cdc_set_line_coding(func->device, setup);
  392. break;
  393. case CDC_GET_LINE_CODING:
  394. _cdc_get_line_coding(func->device, setup);
  395. break;
  396. case CDC_SET_CONTROL_LINE_STATE:
  397. data->connected = (setup->wValue & 0x01) > 0?RT_TRUE:RT_FALSE;
  398. RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom state:%d \n", data->connected));
  399. dcd_ep0_send_status(func->device->dcd);
  400. break;
  401. case CDC_SEND_BREAK:
  402. break;
  403. default:
  404. rt_kprintf("unknown cdc request\n",setup->request_type);
  405. return -RT_ERROR;
  406. }
  407. return RT_EOK;
  408. }
  409. /**
  410. * This function will run cdc function, it will be called on handle set configuration request.
  411. *
  412. * @param func the usb function object.
  413. *
  414. * @return RT_EOK on successful.
  415. */
  416. static rt_err_t _function_enable(ufunction_t func)
  417. {
  418. struct vcom *data;
  419. RT_ASSERT(func != RT_NULL);
  420. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
  421. _vcom_reset_state(func);
  422. data = (struct vcom*)func->user_data;
  423. data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
  424. data->ep_out->request.buffer = data->ep_out->buffer;
  425. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  426. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  427. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  428. return RT_EOK;
  429. }
  430. /**
  431. * This function will stop cdc function, it will be called on handle set configuration request.
  432. *
  433. * @param func the usb function object.
  434. *
  435. * @return RT_EOK on successful.
  436. */
  437. static rt_err_t _function_disable(ufunction_t func)
  438. {
  439. struct vcom *data;
  440. RT_ASSERT(func != RT_NULL);
  441. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
  442. _vcom_reset_state(func);
  443. data = (struct vcom*)func->user_data;
  444. if(data->ep_out->buffer != RT_NULL)
  445. {
  446. rt_free(data->ep_out->buffer);
  447. data->ep_out->buffer = RT_NULL;
  448. }
  449. return RT_EOK;
  450. }
  451. static struct ufunction_ops ops =
  452. {
  453. _function_enable,
  454. _function_disable,
  455. RT_NULL,
  456. };
  457. /**
  458. * This function will configure cdc descriptor.
  459. *
  460. * @param comm the communication interface number.
  461. * @param data the data interface number.
  462. *
  463. * @return RT_EOK on successful.
  464. */
  465. static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm,
  466. rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
  467. {
  468. comm->call_mgmt_desc.data_interface = dintf_nr;
  469. comm->union_desc.master_interface = cintf_nr;
  470. comm->union_desc.slave_interface0 = dintf_nr;
  471. #ifdef RT_USB_DEVICE_COMPOSITE
  472. comm->iad_desc.bFirstInterface = cintf_nr;
  473. #endif
  474. return RT_EOK;
  475. }
  476. /**
  477. * This function will create a cdc function instance.
  478. *
  479. * @param device the usb device object.
  480. *
  481. * @return RT_EOK on successful.
  482. */
  483. ufunction_t rt_usbd_function_cdc_create(udevice_t device)
  484. {
  485. ufunction_t func;
  486. struct vcom* data;
  487. uintf_t intf_comm, intf_data;
  488. ualtsetting_t comm_setting, data_setting;
  489. ucdc_data_desc_t data_desc;
  490. ucdc_comm_desc_t comm_desc;
  491. /* parameter check */
  492. RT_ASSERT(device != RT_NULL);
  493. rt_memset(serno, 0, _SER_NO_LEN + 1);
  494. if(vcom_get_stored_serno(serno, _SER_NO_LEN) != RT_EOK)
  495. {
  496. rt_memset(serno, 0, _SER_NO_LEN + 1);
  497. rt_memcpy(serno, _SER_NO, rt_strlen(_SER_NO));
  498. }
  499. /* set usb device string description */
  500. rt_usbd_device_set_string(device, _ustring);
  501. /* create a cdc function */
  502. func = rt_usbd_function_new(device, &dev_desc, &ops);
  503. //not support HS
  504. //rt_usbd_device_set_qualifier(device, &dev_qualifier);
  505. /* allocate memory for cdc vcom data */
  506. data = (struct vcom*)rt_malloc(sizeof(struct vcom));
  507. rt_memset(data, 0, sizeof(struct vcom));
  508. func->user_data = (void*)data;
  509. /* initilize vcom */
  510. rt_usb_vcom_init(func);
  511. /* create a cdc communication interface and a cdc data interface */
  512. intf_comm = rt_usbd_interface_new(device, _interface_handler);
  513. intf_data = rt_usbd_interface_new(device, _interface_handler);
  514. /* create a communication alternate setting and a data alternate setting */
  515. comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_comm_descriptor));
  516. data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
  517. /* config desc in alternate setting */
  518. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  519. (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
  520. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  521. /* configure the cdc interface descriptor */
  522. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);
  523. /* create a command endpoint */
  524. comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
  525. data->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
  526. /* add the command endpoint to the cdc communication interface */
  527. rt_usbd_altsetting_add_endpoint(comm_setting, data->ep_cmd);
  528. /* add the communication alternate setting to the communication interface,
  529. then set default setting of the interface */
  530. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  531. rt_usbd_set_altsetting(intf_comm, 0);
  532. /* add the communication interface to the cdc function */
  533. rt_usbd_function_add_interface(func, intf_comm);
  534. /* create a bulk in and a bulk endpoint */
  535. data_desc = (ucdc_data_desc_t)data_setting->desc;
  536. data->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
  537. data->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
  538. /* add the bulk out and bulk in endpoints to the data alternate setting */
  539. rt_usbd_altsetting_add_endpoint(data_setting, data->ep_in);
  540. rt_usbd_altsetting_add_endpoint(data_setting, data->ep_out);
  541. /* add the data alternate setting to the data interface
  542. then set default setting of the interface */
  543. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  544. rt_usbd_set_altsetting(intf_data, 0);
  545. /* add the cdc data interface to cdc function */
  546. rt_usbd_function_add_interface(func, intf_data);
  547. return func;
  548. }
  549. /**
  550. * UART device in RT-Thread
  551. */
  552. static rt_err_t _vcom_configure(struct rt_serial_device *serial,
  553. struct serial_configure *cfg)
  554. {
  555. return RT_EOK;
  556. }
  557. static rt_err_t _vcom_control(struct rt_serial_device *serial,
  558. int cmd, void *arg)
  559. {
  560. switch (cmd)
  561. {
  562. case RT_DEVICE_CTRL_CLR_INT:
  563. /* disable rx irq */
  564. break;
  565. case RT_DEVICE_CTRL_SET_INT:
  566. /* enable rx irq */
  567. break;
  568. }
  569. return RT_EOK;
  570. }
  571. static int _vcom_getc(struct rt_serial_device *serial)
  572. {
  573. int result;
  574. rt_uint8_t ch;
  575. rt_uint32_t level;
  576. struct ufunction *func;
  577. struct vcom *data;
  578. func = (struct ufunction*)serial->parent.user_data;
  579. data = (struct vcom*)func->user_data;
  580. result = -1;
  581. level = rt_hw_interrupt_disable();
  582. if(rt_ringbuffer_getchar(&data->rx_ringbuffer, &ch) != 0)
  583. {
  584. result = ch;
  585. }
  586. rt_hw_interrupt_enable(level);
  587. return result;
  588. }
  589. static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size,int direction)
  590. {
  591. rt_uint32_t level;
  592. struct ufunction *func;
  593. struct vcom *data;
  594. rt_uint32_t baksize;
  595. rt_size_t ptr = 0;
  596. int empty = 0;
  597. rt_uint8_t crlf[2] = {'\r', '\n',};
  598. func = (struct ufunction*)serial->parent.user_data;
  599. data = (struct vcom*)func->user_data;
  600. size = (size >= CDC_BULKIN_MAXSIZE) ? CDC_BULKIN_MAXSIZE : size;
  601. baksize = size;
  602. RT_ASSERT(serial != RT_NULL);
  603. RT_ASSERT(buf != RT_NULL);
  604. RT_DEBUG_LOG(RT_DEBUG_USB, ("%s\n",__func__));
  605. if (data->connected)
  606. {
  607. size = 0;
  608. if((serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  609. {
  610. empty = 0;
  611. while(ptr < baksize)
  612. {
  613. while(ptr < baksize && buf[ptr] != '\n')
  614. {
  615. ptr++;
  616. }
  617. if(ptr < baksize)
  618. {
  619. level = rt_hw_interrupt_disable();
  620. size += rt_ringbuffer_put_force(&data->tx_ringbuffer, (const rt_uint8_t *)&buf[size], ptr - size);
  621. rt_hw_interrupt_enable(level);
  622. /* no data was be ignored */
  623. if(size == ptr)
  624. {
  625. level = rt_hw_interrupt_disable();
  626. if(rt_ringbuffer_space_len(&data->tx_ringbuffer) >= 2)
  627. {
  628. rt_ringbuffer_put_force(&data->tx_ringbuffer, crlf, 2);
  629. size++;
  630. }
  631. rt_hw_interrupt_enable(level);
  632. }
  633. else
  634. {
  635. empty = 1;
  636. break;
  637. }
  638. /* ring buffer is full */
  639. if(size == ptr)
  640. {
  641. empty = 1;
  642. break;
  643. }
  644. ptr++;
  645. }
  646. else
  647. {
  648. break;
  649. }
  650. }
  651. }
  652. if(size < baksize && !empty)
  653. {
  654. level = rt_hw_interrupt_disable();
  655. size += rt_ringbuffer_put_force(&data->tx_ringbuffer, (rt_uint8_t *)&buf[size], baksize - size);
  656. rt_hw_interrupt_enable(level);
  657. }
  658. if(size)
  659. {
  660. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  661. }
  662. }
  663. else
  664. {
  665. /* recover dataqueue resources */
  666. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  667. }
  668. return size;
  669. }
  670. static int _vcom_putc(struct rt_serial_device *serial, char c)
  671. {
  672. rt_uint32_t level;
  673. struct ufunction *func;
  674. struct vcom *data;
  675. func = (struct ufunction*)serial->parent.user_data;
  676. data = (struct vcom*)func->user_data;
  677. RT_ASSERT(serial != RT_NULL);
  678. if (data->connected)
  679. {
  680. if(c == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  681. {
  682. level = rt_hw_interrupt_disable();
  683. rt_ringbuffer_putchar_force(&data->tx_ringbuffer, '\r');
  684. rt_hw_interrupt_enable(level);
  685. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  686. }
  687. level = rt_hw_interrupt_disable();
  688. rt_ringbuffer_putchar_force(&data->tx_ringbuffer, c);
  689. rt_hw_interrupt_enable(level);
  690. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  691. }
  692. return 1;
  693. }
  694. static const struct rt_uart_ops usb_vcom_ops =
  695. {
  696. _vcom_configure,
  697. _vcom_control,
  698. _vcom_putc,
  699. _vcom_getc,
  700. _vcom_tx
  701. };
  702. /* Vcom Tx Thread */
  703. static void vcom_tx_thread_entry(void* parameter)
  704. {
  705. rt_uint32_t level;
  706. rt_uint32_t res;
  707. struct ufunction *func = (struct ufunction *)parameter;
  708. struct vcom *data = (struct vcom*)func->user_data;
  709. rt_uint8_t ch[CDC_BULKIN_MAXSIZE];
  710. while (1)
  711. {
  712. if
  713. (
  714. (rt_event_recv(&data->tx_event, CDC_TX_HAS_DATE,
  715. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  716. RT_WAITING_FOREVER, &res) != RT_EOK) ||
  717. (!(res & CDC_TX_HAS_DATE))
  718. )
  719. {
  720. continue;
  721. }
  722. if(!(res & CDC_TX_HAS_DATE))
  723. {
  724. continue;
  725. }
  726. while(rt_ringbuffer_data_len(&data->tx_ringbuffer))
  727. {
  728. level = rt_hw_interrupt_disable();
  729. res = rt_ringbuffer_get(&data->tx_ringbuffer, ch, CDC_BULKIN_MAXSIZE);
  730. rt_hw_interrupt_enable(level);
  731. if(!res)
  732. {
  733. continue;
  734. }
  735. if (!data->connected)
  736. {
  737. if(data->serial.parent.open_flag &
  738. #ifndef VCOM_TX_USE_DMA
  739. RT_DEVICE_FLAG_INT_TX
  740. #else
  741. RT_DEVICE_FLAG_DMA_TX
  742. #endif
  743. )
  744. {
  745. /* drop msg */
  746. #ifndef VCOM_TX_USE_DMA
  747. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
  748. #else
  749. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  750. #endif
  751. }
  752. continue;
  753. }
  754. rt_completion_init(&data->wait);
  755. data->ep_in->request.buffer = ch;
  756. data->ep_in->request.size = res;
  757. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  758. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  759. if (rt_completion_wait(&data->wait, TX_TIMEOUT) != RT_EOK)
  760. {
  761. RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom tx timeout\n"));
  762. }
  763. if(data->serial.parent.open_flag &
  764. #ifndef VCOM_TX_USE_DMA
  765. RT_DEVICE_FLAG_INT_TX
  766. #else
  767. RT_DEVICE_FLAG_DMA_TX
  768. #endif
  769. )
  770. {
  771. #ifndef VCOM_TX_USE_DMA
  772. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
  773. #else
  774. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  775. #endif
  776. }
  777. }
  778. }
  779. }
  780. static void rt_usb_vcom_init(struct ufunction *func)
  781. {
  782. rt_err_t result = RT_EOK;
  783. struct serial_configure config;
  784. struct vcom *data = (struct vcom*)func->user_data;
  785. /* initialize ring buffer */
  786. rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE);
  787. rt_ringbuffer_init(&data->tx_ringbuffer, data->tx_rbp, CDC_TX_BUFSIZE);
  788. rt_event_init(&data->tx_event, "vcom", RT_IPC_FLAG_FIFO);
  789. config.baud_rate = BAUD_RATE_115200;
  790. config.data_bits = DATA_BITS_8;
  791. config.stop_bits = STOP_BITS_1;
  792. config.parity = PARITY_NONE;
  793. config.bit_order = BIT_ORDER_LSB;
  794. config.invert = NRZ_NORMAL;
  795. config.bufsz = CDC_RX_BUFSIZE;
  796. data->serial.ops = &usb_vcom_ops;
  797. data->serial.serial_rx = RT_NULL;
  798. data->serial.config = config;
  799. /* register vcom device */
  800. rt_hw_serial_register(&data->serial, VCOM_DEVICE,
  801. #ifndef VCOM_TX_USE_DMA
  802. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  803. #else
  804. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
  805. #endif
  806. func);
  807. /* init usb device thread */
  808. rt_thread_init(&vcom_thread, "vcom",
  809. vcom_tx_thread_entry, func,
  810. vcom_thread_stack, VCOM_TASK_STK_SIZE,
  811. 16, 20);
  812. result = rt_thread_startup(&vcom_thread);
  813. RT_ASSERT(result == RT_EOK);
  814. }
  815. struct udclass vcom_class =
  816. {
  817. .rt_usbd_function_create = rt_usbd_function_cdc_create
  818. };
  819. int rt_usbd_vcom_class_register(void)
  820. {
  821. rt_usbd_class_register(&vcom_class);
  822. return 0;
  823. }
  824. INIT_PREV_EXPORT(rt_usbd_vcom_class_register);
  825. #endif