cdc_vcom.c 27 KB

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