cdc_vcom.c 26 KB

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