cdc_vcom.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. #define CDC_RX_BUFSIZE 128
  28. #define CDC_MAX_PACKET_SIZE 64
  29. #define VCOM_DEVICE "vcom"
  30. #ifdef RT_VCOM_TASK_STK_SIZE
  31. #define VCOM_TASK_STK_SIZE RT_VCOM_TASK_STK_SIZE
  32. #else /*!RT_VCOM_TASK_STK_SIZE*/
  33. #define VCOM_TASK_STK_SIZE 512
  34. #endif /*RT_VCOM_TASK_STK_SIZE*/
  35. #ifdef RT_VCOM_TX_USE_DMA
  36. #define VCOM_TX_USE_DMA
  37. #endif /*RT_VCOM_TX_USE_DMA*/
  38. #ifdef RT_VCOM_SERNO
  39. #define _SER_NO RT_VCOM_SERNO
  40. #else /*!RT_VCOM_SERNO*/
  41. #define _SER_NO "32021919830108"
  42. #endif /*RT_VCOM_SERNO*/
  43. #ifdef RT_VCOM_SER_LEN
  44. #define _SER_NO_LEN RT_VCOM_SER_LEN
  45. #else /*!RT_VCOM_SER_LEN*/
  46. #define _SER_NO_LEN 14 /*rt_strlen("32021919830108")*/
  47. #endif /*RT_VCOM_SER_LEN*/
  48. ALIGN(RT_ALIGN_SIZE)
  49. static rt_uint8_t vcom_thread_stack[VCOM_TASK_STK_SIZE];
  50. static struct rt_thread vcom_thread;
  51. static struct ucdc_line_coding line_coding;
  52. #define CDC_TX_BUFSIZE 1024
  53. #define CDC_BULKIN_MAXSIZE (CDC_TX_BUFSIZE / 8)
  54. #define CDC_TX_HAS_DATE 0x01
  55. #define CDC_TX_HAS_SPACE 0x02
  56. struct vcom
  57. {
  58. struct rt_serial_device serial;
  59. uep_t ep_out;
  60. uep_t ep_in;
  61. uep_t ep_cmd;
  62. rt_bool_t connected;
  63. rt_bool_t in_sending;
  64. struct rt_completion wait;
  65. rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
  66. struct rt_ringbuffer rx_ringbuffer;
  67. rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
  68. struct rt_ringbuffer tx_ringbuffer;
  69. struct rt_event tx_event;
  70. };
  71. struct vcom_tx_msg
  72. {
  73. struct rt_serial_device * serial;
  74. const char *buf;
  75. rt_size_t size;
  76. };
  77. ALIGN(4)
  78. static struct udevice_descriptor dev_desc =
  79. {
  80. USB_DESC_LENGTH_DEVICE, //bLength;
  81. USB_DESC_TYPE_DEVICE, //type;
  82. USB_BCD_VERSION, //bcdUSB;
  83. USB_CLASS_CDC, //bDeviceClass;
  84. 0x00, //bDeviceSubClass;
  85. 0x00, //bDeviceProtocol;
  86. CDC_MAX_PACKET_SIZE, //bMaxPacketSize0;
  87. _VENDOR_ID, //idVendor;
  88. _PRODUCT_ID, //idProduct;
  89. USB_BCD_DEVICE, //bcdDevice;
  90. USB_STRING_MANU_INDEX, //iManufacturer;
  91. USB_STRING_PRODUCT_INDEX, //iProduct;
  92. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  93. USB_DYNAMIC, //bNumConfigurations;
  94. };
  95. //FS and HS needed
  96. ALIGN(4)
  97. static struct usb_qualifier_descriptor dev_qualifier =
  98. {
  99. sizeof(dev_qualifier), //bLength
  100. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  101. 0x0200, //bcdUSB
  102. USB_CLASS_CDC, //bDeviceClass
  103. 0x00, //bDeviceSubClass
  104. 0x00, //bDeviceProtocol
  105. 64, //bMaxPacketSize0
  106. 0x01, //bNumConfigurations
  107. 0,
  108. };
  109. /* communcation interface descriptor */
  110. ALIGN(4)
  111. const static struct ucdc_comm_descriptor _comm_desc =
  112. {
  113. #ifdef RT_USB_DEVICE_COMPOSITE
  114. /* Interface Association Descriptor */
  115. {
  116. USB_DESC_LENGTH_IAD,
  117. USB_DESC_TYPE_IAD,
  118. USB_DYNAMIC,
  119. 0x02,
  120. USB_CDC_CLASS_COMM,
  121. USB_CDC_SUBCLASS_ACM,
  122. USB_CDC_PROTOCOL_V25TER,
  123. 0x00,
  124. },
  125. #endif
  126. /* Interface Descriptor */
  127. {
  128. USB_DESC_LENGTH_INTERFACE,
  129. USB_DESC_TYPE_INTERFACE,
  130. USB_DYNAMIC,
  131. 0x00,
  132. 0x01,
  133. USB_CDC_CLASS_COMM,
  134. USB_CDC_SUBCLASS_ACM,
  135. USB_CDC_PROTOCOL_V25TER,
  136. 0x00,
  137. },
  138. /* Header Functional Descriptor */
  139. {
  140. 0x05,
  141. USB_CDC_CS_INTERFACE,
  142. USB_CDC_SCS_HEADER,
  143. 0x0110,
  144. },
  145. /* Call Management Functional Descriptor */
  146. {
  147. 0x05,
  148. USB_CDC_CS_INTERFACE,
  149. USB_CDC_SCS_CALL_MGMT,
  150. 0x00,
  151. USB_DYNAMIC,
  152. },
  153. /* Abstract Control Management Functional Descriptor */
  154. {
  155. 0x04,
  156. USB_CDC_CS_INTERFACE,
  157. USB_CDC_SCS_ACM,
  158. 0x02,
  159. },
  160. /* Union Functional Descriptor */
  161. {
  162. 0x05,
  163. USB_CDC_CS_INTERFACE,
  164. USB_CDC_SCS_UNION,
  165. USB_DYNAMIC,
  166. USB_DYNAMIC,
  167. },
  168. /* Endpoint Descriptor */
  169. {
  170. USB_DESC_LENGTH_ENDPOINT,
  171. USB_DESC_TYPE_ENDPOINT,
  172. USB_DYNAMIC | USB_DIR_IN,
  173. USB_EP_ATTR_INT,
  174. 0x08,
  175. 0xFF,
  176. },
  177. };
  178. /* data interface descriptor */
  179. ALIGN(4)
  180. const static struct ucdc_data_descriptor _data_desc =
  181. {
  182. /* interface descriptor */
  183. {
  184. USB_DESC_LENGTH_INTERFACE,
  185. USB_DESC_TYPE_INTERFACE,
  186. USB_DYNAMIC,
  187. 0x00,
  188. 0x02,
  189. USB_CDC_CLASS_DATA,
  190. 0x00,
  191. 0x00,
  192. 0x00,
  193. },
  194. /* endpoint, bulk out */
  195. {
  196. USB_DESC_LENGTH_ENDPOINT,
  197. USB_DESC_TYPE_ENDPOINT,
  198. USB_DYNAMIC | USB_DIR_OUT,
  199. USB_EP_ATTR_BULK,
  200. USB_CDC_BUFSIZE,
  201. 0x00,
  202. },
  203. /* endpoint, bulk in */
  204. {
  205. USB_DESC_LENGTH_ENDPOINT,
  206. USB_DESC_TYPE_ENDPOINT,
  207. USB_DYNAMIC | USB_DIR_IN,
  208. USB_EP_ATTR_BULK,
  209. USB_CDC_BUFSIZE,
  210. 0x00,
  211. },
  212. };
  213. ALIGN(4)
  214. static char serno[_SER_NO_LEN + 1] = {'\0'};
  215. RT_WEAK rt_err_t vcom_get_stored_serno(char *serno, int size);
  216. rt_err_t vcom_get_stored_serno(char *serno, int size)
  217. {
  218. return RT_ERROR;
  219. }
  220. ALIGN(4)
  221. const static char* _ustring[] =
  222. {
  223. "Language",
  224. "RT-Thread Team.",
  225. "RTT Virtual Serial",
  226. serno,
  227. "Configuration",
  228. "Interface",
  229. };
  230. static void rt_usb_vcom_init(struct ufunction *func);
  231. static void _vcom_reset_state(ufunction_t func)
  232. {
  233. struct vcom* data;
  234. int lvl;
  235. RT_ASSERT(func != RT_NULL)
  236. data = (struct vcom*)func->user_data;
  237. lvl = rt_hw_interrupt_disable();
  238. data->connected = RT_FALSE;
  239. data->in_sending = RT_FALSE;
  240. /*rt_kprintf("reset USB serial\n", cnt);*/
  241. rt_hw_interrupt_enable(lvl);
  242. }
  243. /**
  244. * This function will handle cdc bulk in endpoint request.
  245. *
  246. * @param func the usb function object.
  247. * @param size request size.
  248. *
  249. * @return RT_EOK.
  250. */
  251. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  252. {
  253. struct vcom *data;
  254. rt_size_t request_size;
  255. RT_ASSERT(func != RT_NULL);
  256. data = (struct vcom*)func->user_data;
  257. request_size = data->ep_in->request.size;
  258. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler %d\n", request_size));
  259. if ((request_size != 0) && ((request_size % EP_MAXPACKET(data->ep_in)) == 0))
  260. {
  261. /* don't have data right now. Send a zero-length-packet to
  262. * terminate the transaction.
  263. *
  264. * FIXME: actually, this might not be the right place to send zlp.
  265. * Only the rt_device_write could know how much data is sending. */
  266. data->in_sending = RT_TRUE;
  267. data->ep_in->request.buffer = RT_NULL;
  268. data->ep_in->request.size = 0;
  269. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  270. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  271. return RT_EOK;
  272. }
  273. rt_completion_done(&data->wait);
  274. return RT_EOK;
  275. }
  276. /**
  277. * This function will handle cdc bulk out endpoint request.
  278. *
  279. * @param func the usb function object.
  280. * @param size request size.
  281. *
  282. * @return RT_EOK.
  283. */
  284. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  285. {
  286. rt_uint32_t level;
  287. struct vcom *data;
  288. RT_ASSERT(func != RT_NULL);
  289. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
  290. data = (struct vcom*)func->user_data;
  291. /* ensure serial is active */
  292. if((data->serial.parent.flag & RT_DEVICE_FLAG_ACTIVATED)
  293. && (data->serial.parent.open_flag & RT_DEVICE_OFLAG_OPEN))
  294. {
  295. /* receive data from USB VCOM */
  296. level = rt_hw_interrupt_disable();
  297. rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
  298. rt_hw_interrupt_enable(level);
  299. /* notify receive data */
  300. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND);
  301. }
  302. data->ep_out->request.buffer = data->ep_out->buffer;
  303. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  304. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  305. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  306. return RT_EOK;
  307. }
  308. /**
  309. * This function will handle cdc interrupt in endpoint request.
  310. *
  311. * @param device the usb device object.
  312. * @param size request size.
  313. *
  314. * @return RT_EOK.
  315. */
  316. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  317. {
  318. RT_ASSERT(func != RT_NULL);
  319. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
  320. return RT_EOK;
  321. }
  322. /**
  323. * This function will handle cdc_get_line_coding request.
  324. *
  325. * @param device the usb device object.
  326. * @param setup the setup request.
  327. *
  328. * @return RT_EOK on successful.
  329. */
  330. static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
  331. {
  332. struct ucdc_line_coding data;
  333. rt_uint16_t size;
  334. RT_ASSERT(device != RT_NULL);
  335. RT_ASSERT(setup != RT_NULL);
  336. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));
  337. data.dwDTERate = 115200;
  338. data.bCharFormat = 0;
  339. data.bDataBits = 8;
  340. data.bParityType = 0;
  341. size = setup->wLength > 7 ? 7 : setup->wLength;
  342. rt_usbd_ep0_write(device, (void*)&data, size);
  343. return RT_EOK;
  344. }
  345. static rt_err_t _cdc_set_line_coding_callback(udevice_t device, rt_size_t size)
  346. {
  347. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding_callback\n"));
  348. dcd_ep0_send_status(device->dcd);
  349. return RT_EOK;
  350. }
  351. /**
  352. * This function will handle cdc_set_line_coding request.
  353. *
  354. * @param device the usb device object.
  355. * @param setup the setup request.
  356. *
  357. * @return RT_EOK on successful.
  358. */
  359. static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
  360. {
  361. RT_ASSERT(device != RT_NULL);
  362. RT_ASSERT(setup != RT_NULL);
  363. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
  364. rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
  365. _cdc_set_line_coding_callback);
  366. return RT_EOK;
  367. }
  368. /**
  369. * This function will handle cdc interface request.
  370. *
  371. * @param device the usb device object.
  372. * @param setup the setup request.
  373. *
  374. * @return RT_EOK on successful.
  375. */
  376. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  377. {
  378. struct vcom *data;
  379. RT_ASSERT(func != RT_NULL);
  380. RT_ASSERT(func->device != RT_NULL);
  381. RT_ASSERT(setup != RT_NULL);
  382. data = (struct vcom*)func->user_data;
  383. switch(setup->bRequest)
  384. {
  385. case CDC_SEND_ENCAPSULATED_COMMAND:
  386. break;
  387. case CDC_GET_ENCAPSULATED_RESPONSE:
  388. break;
  389. case CDC_SET_COMM_FEATURE:
  390. break;
  391. case CDC_GET_COMM_FEATURE:
  392. break;
  393. case CDC_CLEAR_COMM_FEATURE:
  394. break;
  395. case CDC_SET_LINE_CODING:
  396. _cdc_set_line_coding(func->device, setup);
  397. break;
  398. case CDC_GET_LINE_CODING:
  399. _cdc_get_line_coding(func->device, setup);
  400. break;
  401. case CDC_SET_CONTROL_LINE_STATE:
  402. data->connected = (setup->wValue & 0x01) > 0?RT_TRUE:RT_FALSE;
  403. RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom state:%d \n", data->connected));
  404. dcd_ep0_send_status(func->device->dcd);
  405. break;
  406. case CDC_SEND_BREAK:
  407. break;
  408. default:
  409. rt_kprintf("unknown cdc request\n",setup->request_type);
  410. return -RT_ERROR;
  411. }
  412. return RT_EOK;
  413. }
  414. /**
  415. * This function will run cdc function, it will be called on handle set configuration request.
  416. *
  417. * @param func the usb function object.
  418. *
  419. * @return RT_EOK on successful.
  420. */
  421. static rt_err_t _function_enable(ufunction_t func)
  422. {
  423. struct vcom *data;
  424. RT_ASSERT(func != RT_NULL);
  425. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
  426. _vcom_reset_state(func);
  427. data = (struct vcom*)func->user_data;
  428. data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
  429. data->ep_out->request.buffer = data->ep_out->buffer;
  430. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  431. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  432. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  433. return RT_EOK;
  434. }
  435. /**
  436. * This function will stop cdc function, it will be called on handle set configuration request.
  437. *
  438. * @param func the usb function object.
  439. *
  440. * @return RT_EOK on successful.
  441. */
  442. static rt_err_t _function_disable(ufunction_t func)
  443. {
  444. struct vcom *data;
  445. RT_ASSERT(func != RT_NULL);
  446. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
  447. _vcom_reset_state(func);
  448. data = (struct vcom*)func->user_data;
  449. if(data->ep_out->buffer != RT_NULL)
  450. {
  451. rt_free(data->ep_out->buffer);
  452. data->ep_out->buffer = RT_NULL;
  453. }
  454. return RT_EOK;
  455. }
  456. static struct ufunction_ops ops =
  457. {
  458. _function_enable,
  459. _function_disable,
  460. RT_NULL,
  461. };
  462. /**
  463. * This function will configure cdc descriptor.
  464. *
  465. * @param comm the communication interface number.
  466. * @param data the data interface number.
  467. *
  468. * @return RT_EOK on successful.
  469. */
  470. static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm,
  471. rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
  472. {
  473. comm->call_mgmt_desc.data_interface = dintf_nr;
  474. comm->union_desc.master_interface = cintf_nr;
  475. comm->union_desc.slave_interface0 = dintf_nr;
  476. #ifdef RT_USB_DEVICE_COMPOSITE
  477. comm->iad_desc.bFirstInterface = cintf_nr;
  478. #endif
  479. return RT_EOK;
  480. }
  481. /**
  482. * This function will create a cdc function instance.
  483. *
  484. * @param device the usb device object.
  485. *
  486. * @return RT_EOK on successful.
  487. */
  488. ufunction_t rt_usbd_function_cdc_create(udevice_t device)
  489. {
  490. ufunction_t func;
  491. struct vcom* data;
  492. uintf_t intf_comm, intf_data;
  493. ualtsetting_t comm_setting, data_setting;
  494. ucdc_data_desc_t data_desc;
  495. ucdc_comm_desc_t comm_desc;
  496. /* parameter check */
  497. RT_ASSERT(device != RT_NULL);
  498. rt_memset(serno, 0, _SER_NO_LEN + 1);
  499. if(vcom_get_stored_serno(serno, _SER_NO_LEN) != RT_EOK)
  500. {
  501. rt_memset(serno, 0, _SER_NO_LEN + 1);
  502. rt_memcpy(serno, _SER_NO, rt_strlen(_SER_NO));
  503. }
  504. /* set usb device string description */
  505. rt_usbd_device_set_string(device, _ustring);
  506. /* create a cdc function */
  507. func = rt_usbd_function_new(device, &dev_desc, &ops);
  508. //not support HS
  509. //rt_usbd_device_set_qualifier(device, &dev_qualifier);
  510. /* allocate memory for cdc vcom data */
  511. data = (struct vcom*)rt_malloc(sizeof(struct vcom));
  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