cdc_vcom.c 25 KB

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