cdc_vcom.c 26 KB

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