cdc_vcom.c 26 KB

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