cdc_vcom.c 26 KB

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