cdc_vcom.c 26 KB

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