cdc_vcom.c 26 KB

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