cdc_vcom.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 % EP_MAXPACKET(data->ep_in)) == 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. /* ensure serial is active */
  270. if(data->serial.parent.open_flag & RT_DEVICE_FLAG_ACTIVATED)
  271. {
  272. /* receive data from USB VCOM */
  273. level = rt_hw_interrupt_disable();
  274. rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
  275. rt_hw_interrupt_enable(level);
  276. /* notify receive data */
  277. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND);
  278. }
  279. data->ep_out->request.buffer = data->ep_out->buffer;
  280. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  281. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  282. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  283. return RT_EOK;
  284. }
  285. /**
  286. * This function will handle cdc interrupt in endpoint request.
  287. *
  288. * @param device the usb device object.
  289. * @param size request size.
  290. *
  291. * @return RT_EOK.
  292. */
  293. static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
  294. {
  295. RT_ASSERT(func != RT_NULL);
  296. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
  297. return RT_EOK;
  298. }
  299. /**
  300. * This function will handle cdc_get_line_coding request.
  301. *
  302. * @param device the usb device object.
  303. * @param setup the setup request.
  304. *
  305. * @return RT_EOK on successful.
  306. */
  307. static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
  308. {
  309. struct ucdc_line_coding data;
  310. rt_uint16_t size;
  311. RT_ASSERT(device != RT_NULL);
  312. RT_ASSERT(setup != RT_NULL);
  313. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));
  314. data.dwDTERate = 115200;
  315. data.bCharFormat = 0;
  316. data.bDataBits = 8;
  317. data.bParityType = 0;
  318. size = setup->wLength > 7 ? 7 : setup->wLength;
  319. rt_usbd_ep0_write(device, (void*)&data, size);
  320. return RT_EOK;
  321. }
  322. static rt_err_t _cdc_set_line_coding_callback(udevice_t device, rt_size_t size)
  323. {
  324. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding_callback\n"));
  325. dcd_ep0_send_status(device->dcd);
  326. return RT_EOK;
  327. }
  328. /**
  329. * This function will handle cdc_set_line_coding request.
  330. *
  331. * @param device the usb device object.
  332. * @param setup the setup request.
  333. *
  334. * @return RT_EOK on successful.
  335. */
  336. static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
  337. {
  338. RT_ASSERT(device != RT_NULL);
  339. RT_ASSERT(setup != RT_NULL);
  340. RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
  341. rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
  342. _cdc_set_line_coding_callback);
  343. return RT_EOK;
  344. }
  345. /**
  346. * This function will handle cdc interface request.
  347. *
  348. * @param device the usb device object.
  349. * @param setup the setup request.
  350. *
  351. * @return RT_EOK on successful.
  352. */
  353. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  354. {
  355. struct vcom *data;
  356. RT_ASSERT(func != RT_NULL);
  357. RT_ASSERT(func->device != RT_NULL);
  358. RT_ASSERT(setup != RT_NULL);
  359. data = (struct vcom*)func->user_data;
  360. switch(setup->bRequest)
  361. {
  362. case CDC_SEND_ENCAPSULATED_COMMAND:
  363. break;
  364. case CDC_GET_ENCAPSULATED_RESPONSE:
  365. break;
  366. case CDC_SET_COMM_FEATURE:
  367. break;
  368. case CDC_GET_COMM_FEATURE:
  369. break;
  370. case CDC_CLEAR_COMM_FEATURE:
  371. break;
  372. case CDC_SET_LINE_CODING:
  373. _cdc_set_line_coding(func->device, setup);
  374. break;
  375. case CDC_GET_LINE_CODING:
  376. _cdc_get_line_coding(func->device, setup);
  377. break;
  378. case CDC_SET_CONTROL_LINE_STATE:
  379. data->connected = (setup->wValue & 0x01) > 0?RT_TRUE:RT_FALSE;
  380. RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom state:%d \n", data->connected));
  381. dcd_ep0_send_status(func->device->dcd);
  382. break;
  383. case CDC_SEND_BREAK:
  384. break;
  385. default:
  386. rt_kprintf("unknown cdc request\n",setup->request_type);
  387. return -RT_ERROR;
  388. }
  389. return RT_EOK;
  390. }
  391. /**
  392. * This function will run cdc function, it will be called on handle set configuration request.
  393. *
  394. * @param func the usb function object.
  395. *
  396. * @return RT_EOK on successful.
  397. */
  398. static rt_err_t _function_enable(ufunction_t func)
  399. {
  400. struct vcom *data;
  401. RT_ASSERT(func != RT_NULL);
  402. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
  403. _vcom_reset_state(func);
  404. data = (struct vcom*)func->user_data;
  405. data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
  406. data->ep_out->request.buffer = data->ep_out->buffer;
  407. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  408. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  409. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  410. return RT_EOK;
  411. }
  412. /**
  413. * This function will stop cdc function, it will be called on handle set configuration request.
  414. *
  415. * @param func the usb function object.
  416. *
  417. * @return RT_EOK on successful.
  418. */
  419. static rt_err_t _function_disable(ufunction_t func)
  420. {
  421. struct vcom *data;
  422. RT_ASSERT(func != RT_NULL);
  423. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
  424. _vcom_reset_state(func);
  425. data = (struct vcom*)func->user_data;
  426. if(data->ep_out->buffer != RT_NULL)
  427. {
  428. rt_free(data->ep_out->buffer);
  429. data->ep_out->buffer = RT_NULL;
  430. }
  431. return RT_EOK;
  432. }
  433. static struct ufunction_ops ops =
  434. {
  435. _function_enable,
  436. _function_disable,
  437. RT_NULL,
  438. };
  439. /**
  440. * This function will configure cdc descriptor.
  441. *
  442. * @param comm the communication interface number.
  443. * @param data the data interface number.
  444. *
  445. * @return RT_EOK on successful.
  446. */
  447. static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm,
  448. rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
  449. {
  450. comm->call_mgmt_desc.data_interface = dintf_nr;
  451. comm->union_desc.master_interface = cintf_nr;
  452. comm->union_desc.slave_interface0 = dintf_nr;
  453. #ifdef RT_USB_DEVICE_COMPOSITE
  454. comm->iad_desc.bFirstInterface = cintf_nr;
  455. #endif
  456. return RT_EOK;
  457. }
  458. /**
  459. * This function will create a cdc function instance.
  460. *
  461. * @param device the usb device object.
  462. *
  463. * @return RT_EOK on successful.
  464. */
  465. ufunction_t rt_usbd_function_cdc_create(udevice_t device)
  466. {
  467. ufunction_t func;
  468. struct vcom* data;
  469. uintf_t intf_comm, intf_data;
  470. ualtsetting_t comm_setting, data_setting;
  471. ucdc_data_desc_t data_desc;
  472. ucdc_comm_desc_t comm_desc;
  473. /* parameter check */
  474. RT_ASSERT(device != RT_NULL);
  475. rt_memset(serno, 0, _SER_NO_LEN + 1);
  476. if(vcom_get_stored_serno(serno, _SER_NO_LEN) != RT_EOK)
  477. {
  478. rt_memset(serno, 0, _SER_NO_LEN + 1);
  479. rt_memcpy(serno, _SER_NO, rt_strlen(_SER_NO));
  480. }
  481. /* set usb device string description */
  482. rt_usbd_device_set_string(device, _ustring);
  483. /* create a cdc function */
  484. func = rt_usbd_function_new(device, &dev_desc, &ops);
  485. rt_usbd_device_set_qualifier(device, &dev_qualifier);
  486. /* allocate memory for cdc vcom data */
  487. data = (struct vcom*)rt_malloc(sizeof(struct vcom));
  488. rt_memset(data, 0, sizeof(struct vcom));
  489. func->user_data = (void*)data;
  490. /* initilize vcom */
  491. rt_usb_vcom_init(func);
  492. /* create a cdc communication interface and a cdc data interface */
  493. intf_comm = rt_usbd_interface_new(device, _interface_handler);
  494. intf_data = rt_usbd_interface_new(device, _interface_handler);
  495. /* create a communication alternate setting and a data alternate setting */
  496. comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_comm_descriptor));
  497. data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
  498. /* config desc in alternate setting */
  499. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  500. (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
  501. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  502. /* configure the cdc interface descriptor */
  503. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);
  504. /* create a command endpoint */
  505. comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
  506. data->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
  507. /* add the command endpoint to the cdc communication interface */
  508. rt_usbd_altsetting_add_endpoint(comm_setting, data->ep_cmd);
  509. /* add the communication alternate setting to the communication interface,
  510. then set default setting of the interface */
  511. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  512. rt_usbd_set_altsetting(intf_comm, 0);
  513. /* add the communication interface to the cdc function */
  514. rt_usbd_function_add_interface(func, intf_comm);
  515. /* create a bulk in and a bulk endpoint */
  516. data_desc = (ucdc_data_desc_t)data_setting->desc;
  517. data->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
  518. data->ep_in = rt_usbd_endpoint_new(&data_desc->ep_in_desc, _ep_in_handler);
  519. /* add the bulk out and bulk in endpoints to the data alternate setting */
  520. rt_usbd_altsetting_add_endpoint(data_setting, data->ep_in);
  521. rt_usbd_altsetting_add_endpoint(data_setting, data->ep_out);
  522. /* add the data alternate setting to the data interface
  523. then set default setting of the interface */
  524. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  525. rt_usbd_set_altsetting(intf_data, 0);
  526. /* add the cdc data interface to cdc function */
  527. rt_usbd_function_add_interface(func, intf_data);
  528. return func;
  529. }
  530. /**
  531. * UART device in RT-Thread
  532. */
  533. static rt_err_t _vcom_configure(struct rt_serial_device *serial,
  534. struct serial_configure *cfg)
  535. {
  536. return RT_EOK;
  537. }
  538. static rt_err_t _vcom_control(struct rt_serial_device *serial,
  539. int cmd, void *arg)
  540. {
  541. switch (cmd)
  542. {
  543. case RT_DEVICE_CTRL_CLR_INT:
  544. /* disable rx irq */
  545. break;
  546. case RT_DEVICE_CTRL_SET_INT:
  547. /* enable rx irq */
  548. break;
  549. }
  550. return RT_EOK;
  551. }
  552. static int _vcom_getc(struct rt_serial_device *serial)
  553. {
  554. int result;
  555. rt_uint8_t ch;
  556. rt_uint32_t level;
  557. struct ufunction *func;
  558. struct vcom *data;
  559. func = (struct ufunction*)serial->parent.user_data;
  560. data = (struct vcom*)func->user_data;
  561. result = -1;
  562. level = rt_hw_interrupt_disable();
  563. if(rt_ringbuffer_getchar(&data->rx_ringbuffer, &ch) != 0)
  564. {
  565. result = ch;
  566. }
  567. rt_hw_interrupt_enable(level);
  568. return result;
  569. }
  570. static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size,int direction)
  571. {
  572. rt_uint32_t level;
  573. struct ufunction *func;
  574. struct vcom *data;
  575. rt_uint32_t baksize = size;
  576. rt_size_t ptr = 0;
  577. int empty = 0;
  578. rt_uint8_t crlf[2] = {'\r', '\n',};
  579. func = (struct ufunction*)serial->parent.user_data;
  580. data = (struct vcom*)func->user_data;
  581. RT_ASSERT(serial != RT_NULL);
  582. RT_ASSERT(buf != RT_NULL);
  583. RT_DEBUG_LOG(RT_DEBUG_USB, ("%s\n",__func__));
  584. if (data->connected)
  585. {
  586. size = 0;
  587. if((serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  588. {
  589. empty = 0;
  590. while(ptr < baksize)
  591. {
  592. while(ptr < baksize && buf[ptr] != '\n')
  593. {
  594. ptr++;
  595. }
  596. if(ptr < baksize)
  597. {
  598. level = rt_hw_interrupt_disable();
  599. size += rt_ringbuffer_put_force(&data->tx_ringbuffer, (const rt_uint8_t *)&buf[size], ptr - size);
  600. rt_hw_interrupt_enable(level);
  601. /* no data was be ignored */
  602. if(size == ptr)
  603. {
  604. level = rt_hw_interrupt_disable();
  605. if(rt_ringbuffer_space_len(&data->tx_ringbuffer) >= 2)
  606. {
  607. rt_ringbuffer_put_force(&data->tx_ringbuffer, crlf, 2);
  608. size++;
  609. }
  610. rt_hw_interrupt_enable(level);
  611. }
  612. else
  613. {
  614. empty = 1;
  615. break;
  616. }
  617. /* ring buffer is full */
  618. if(size == ptr)
  619. {
  620. empty = 1;
  621. break;
  622. }
  623. ptr++;
  624. }
  625. else
  626. {
  627. break;
  628. }
  629. }
  630. }
  631. if(size < baksize && !empty)
  632. {
  633. level = rt_hw_interrupt_disable();
  634. size += rt_ringbuffer_put_force(&data->tx_ringbuffer, (rt_uint8_t *)&buf[size], baksize - size);
  635. rt_hw_interrupt_enable(level);
  636. }
  637. if(size)
  638. {
  639. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  640. }
  641. }
  642. else
  643. {
  644. /* recover dataqueue resources */
  645. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  646. }
  647. return size;
  648. }
  649. static int _vcom_putc(struct rt_serial_device *serial, char c)
  650. {
  651. rt_uint32_t level;
  652. struct ufunction *func;
  653. struct vcom *data;
  654. func = (struct ufunction*)serial->parent.user_data;
  655. data = (struct vcom*)func->user_data;
  656. RT_ASSERT(serial != RT_NULL);
  657. if (data->connected)
  658. {
  659. if(c == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  660. {
  661. level = rt_hw_interrupt_disable();
  662. rt_ringbuffer_putchar_force(&data->tx_ringbuffer, '\r');
  663. rt_hw_interrupt_enable(level);
  664. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  665. }
  666. level = rt_hw_interrupt_disable();
  667. rt_ringbuffer_putchar_force(&data->tx_ringbuffer, c);
  668. rt_hw_interrupt_enable(level);
  669. rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
  670. }
  671. return 1;
  672. }
  673. static const struct rt_uart_ops usb_vcom_ops =
  674. {
  675. _vcom_configure,
  676. _vcom_control,
  677. _vcom_putc,
  678. _vcom_getc,
  679. _vcom_tx
  680. };
  681. /* Vcom Tx Thread */
  682. static void vcom_tx_thread_entry(void* parameter)
  683. {
  684. rt_uint32_t level;
  685. rt_uint32_t res;
  686. struct ufunction *func = (struct ufunction *)parameter;
  687. struct vcom *data = (struct vcom*)func->user_data;
  688. rt_uint8_t ch[64];
  689. while (1)
  690. {
  691. if
  692. (
  693. (rt_event_recv(&data->tx_event, CDC_TX_HAS_DATE,
  694. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  695. RT_WAITING_FOREVER, &res) != RT_EOK) ||
  696. (!(res & CDC_TX_HAS_DATE))
  697. )
  698. {
  699. continue;
  700. }
  701. if(!res & CDC_TX_HAS_DATE)
  702. {
  703. continue;
  704. }
  705. while(rt_ringbuffer_data_len(&data->tx_ringbuffer))
  706. {
  707. level = rt_hw_interrupt_disable();
  708. res = rt_ringbuffer_get(&data->tx_ringbuffer, ch, 64);
  709. rt_hw_interrupt_enable(level);
  710. if(!res)
  711. {
  712. continue;
  713. }
  714. if (!data->connected)
  715. {
  716. if(data->serial.parent.open_flag &
  717. #ifndef VCOM_TX_USE_DMA
  718. RT_DEVICE_FLAG_INT_TX
  719. #else
  720. RT_DEVICE_FLAG_DMA_TX
  721. #endif
  722. )
  723. {
  724. /* drop msg */
  725. #ifndef VCOM_TX_USE_DMA
  726. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
  727. #else
  728. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  729. #endif
  730. }
  731. continue;
  732. }
  733. rt_completion_init(&data->wait);
  734. data->ep_in->request.buffer = ch;
  735. data->ep_in->request.size = res;
  736. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  737. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  738. if (rt_completion_wait(&data->wait, TX_TIMEOUT) != RT_EOK)
  739. {
  740. RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom tx timeout\n"));
  741. }
  742. if(data->serial.parent.open_flag &
  743. #ifndef VCOM_TX_USE_DMA
  744. RT_DEVICE_FLAG_INT_TX
  745. #else
  746. RT_DEVICE_FLAG_DMA_TX
  747. #endif
  748. )
  749. {
  750. #ifndef VCOM_TX_USE_DMA
  751. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
  752. #else
  753. rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
  754. #endif
  755. }
  756. }
  757. }
  758. }
  759. static void rt_usb_vcom_init(struct ufunction *func)
  760. {
  761. rt_err_t result = RT_EOK;
  762. struct serial_configure config;
  763. struct vcom *data = (struct vcom*)func->user_data;
  764. /* initialize ring buffer */
  765. rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE);
  766. rt_ringbuffer_init(&data->tx_ringbuffer, data->tx_rbp, CDC_TX_BUFSIZE);
  767. rt_event_init(&data->tx_event, "vcom", RT_IPC_FLAG_FIFO);
  768. config.baud_rate = BAUD_RATE_115200;
  769. config.data_bits = DATA_BITS_8;
  770. config.stop_bits = STOP_BITS_1;
  771. config.parity = PARITY_NONE;
  772. config.bit_order = BIT_ORDER_LSB;
  773. config.invert = NRZ_NORMAL;
  774. config.bufsz = CDC_RX_BUFSIZE;
  775. data->serial.ops = &usb_vcom_ops;
  776. data->serial.serial_rx = RT_NULL;
  777. data->serial.config = config;
  778. /* register vcom device */
  779. rt_hw_serial_register(&data->serial, VCOM_DEVICE,
  780. #ifndef VCOM_TX_USE_DMA
  781. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  782. #else
  783. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
  784. #endif
  785. func);
  786. /* init usb device thread */
  787. rt_thread_init(&vcom_thread, "vcom",
  788. vcom_tx_thread_entry, func,
  789. vcom_thread_stack, VCOM_TASK_STK_SIZE,
  790. 16, 20);
  791. result = rt_thread_startup(&vcom_thread);
  792. RT_ASSERT(result == RT_EOK);
  793. }
  794. #endif