cdc_vcom.c 21 KB

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