cdc_vcom.c 25 KB

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