cdc_vcom.c 27 KB

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