cdc_vcom.c 26 KB

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