cdc_vcom.c 26 KB

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