cdc_vcom.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-02 Yi Qiu first version
  13. * 2012-12-12 heyuanjie87 change endpoints and class handler
  14. */
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <rthw.h>
  18. #include "cdc.h"
  19. #ifdef RT_USB_DEVICE_CDC
  20. #define CDC_RX_BUFSIZE 2048
  21. #define CDC_TX_BUFSIZE 2048
  22. static rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
  23. static rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
  24. static struct rt_ringbuffer rx_ringbuffer;
  25. static struct rt_ringbuffer tx_ringbuffer;
  26. static struct serial_ringbuffer vcom_int_rx;
  27. static struct rt_serial_device vcom_serial;
  28. #define CDC_MaxPacketSize 64
  29. ALIGN(RT_ALIGN_SIZE)
  30. static rt_uint8_t rx_buf[CDC_RX_BUFSIZE];
  31. ALIGN(RT_ALIGN_SIZE)
  32. static rt_uint8_t tx_buf[CDC_TX_BUFSIZE];
  33. volatile static rt_bool_t vcom_connected = RT_FALSE;
  34. volatile static rt_bool_t vcom_in_sending = RT_FALSE;
  35. static struct udevice_descriptor dev_desc =
  36. {
  37. USB_DESC_LENGTH_DEVICE, //bLength;
  38. USB_DESC_TYPE_DEVICE, //type;
  39. USB_BCD_VERSION, //bcdUSB;
  40. USB_CLASS_CDC, //bDeviceClass;
  41. 0x00, //bDeviceSubClass;
  42. 0x00, //bDeviceProtocol;
  43. CDC_MaxPacketSize, //bMaxPacketSize0;
  44. _VENDOR_ID, //idVendor;
  45. _PRODUCT_ID, //idProduct;
  46. USB_BCD_DEVICE, //bcdDevice;
  47. USB_STRING_MANU_INDEX, //iManufacturer;
  48. USB_STRING_PRODUCT_INDEX, //iProduct;
  49. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  50. USB_DYNAMIC, //bNumConfigurations;
  51. };
  52. /* communcation interface descriptor */
  53. const static struct ucdc_comm_descriptor _comm_desc =
  54. {
  55. #ifdef RT_USB_DEVICE_COMPOSITE
  56. /* Interface Association Descriptor */
  57. USB_DESC_LENGTH_IAD,
  58. USB_DESC_TYPE_IAD,
  59. USB_DYNAMIC,
  60. 0x02,
  61. USB_CDC_CLASS_COMM,
  62. USB_CDC_SUBCLASS_ACM,
  63. USB_CDC_PROTOCOL_V25TER,
  64. 0x00,
  65. #endif
  66. /* Interface Descriptor */
  67. USB_DESC_LENGTH_INTERFACE,
  68. USB_DESC_TYPE_INTERFACE,
  69. USB_DYNAMIC,
  70. 0x00,
  71. 0x01,
  72. USB_CDC_CLASS_COMM,
  73. USB_CDC_SUBCLASS_ACM,
  74. USB_CDC_PROTOCOL_V25TER,
  75. 0x00,
  76. /* Header Functional Descriptor */
  77. 0x05,
  78. USB_CDC_CS_INTERFACE,
  79. USB_CDC_SCS_HEADER,
  80. 0x0110,
  81. /* Call Management Functional Descriptor */
  82. 0x05,
  83. USB_CDC_CS_INTERFACE,
  84. USB_CDC_SCS_CALL_MGMT,
  85. 0x00,
  86. USB_DYNAMIC,
  87. /* Abstract Control Management Functional Descriptor */
  88. 0x04,
  89. USB_CDC_CS_INTERFACE,
  90. USB_CDC_SCS_ACM,
  91. 0x02,
  92. /* Union Functional Descriptor */
  93. 0x05,
  94. USB_CDC_CS_INTERFACE,
  95. USB_CDC_SCS_UNION,
  96. USB_DYNAMIC,
  97. USB_DYNAMIC,
  98. /* Endpoint Descriptor */
  99. USB_DESC_LENGTH_ENDPOINT,
  100. USB_DESC_TYPE_ENDPOINT,
  101. USB_DYNAMIC | USB_DIR_IN,
  102. USB_EP_ATTR_INT,
  103. 0x08,
  104. 0xFF,
  105. };
  106. /* data interface descriptor */
  107. const static struct ucdc_data_descriptor _data_desc =
  108. {
  109. /* interface descriptor */
  110. USB_DESC_LENGTH_INTERFACE,
  111. USB_DESC_TYPE_INTERFACE,
  112. USB_DYNAMIC,
  113. 0x00,
  114. 0x02,
  115. USB_CDC_CLASS_DATA,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. /* endpoint, bulk out */
  120. USB_DESC_LENGTH_ENDPOINT,
  121. USB_DESC_TYPE_ENDPOINT,
  122. USB_DYNAMIC | USB_DIR_OUT,
  123. USB_EP_ATTR_BULK,
  124. USB_CDC_BUFSIZE,
  125. 0x00,
  126. /* endpoint, bulk in */
  127. USB_DESC_LENGTH_ENDPOINT,
  128. USB_DESC_TYPE_ENDPOINT,
  129. USB_DYNAMIC | USB_DIR_IN,
  130. USB_EP_ATTR_BULK,
  131. USB_CDC_BUFSIZE,
  132. 0x00,
  133. };
  134. const static char* _ustring[] =
  135. {
  136. "Language",
  137. "RT-Thread Team.",
  138. "RTT Virtual Serial",
  139. "1.1.0",
  140. "Configuration",
  141. "Interface",
  142. };
  143. static void _vcom_reset_state(void)
  144. {
  145. int lvl = rt_hw_interrupt_disable();
  146. tx_ringbuffer.read_mirror = tx_ringbuffer.read_index = 0;
  147. tx_ringbuffer.write_mirror = tx_ringbuffer.write_index = 0;
  148. vcom_connected = RT_FALSE;
  149. vcom_in_sending = RT_FALSE;
  150. /*rt_kprintf("reset USB serial\n", cnt);*/
  151. rt_hw_interrupt_enable(lvl);
  152. }
  153. /**
  154. * This function will handle cdc bulk in endpoint request.
  155. *
  156. * @param device the usb device object.
  157. * @param size request size.
  158. *
  159. * @return RT_EOK.
  160. */
  161. static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
  162. {
  163. rt_uint32_t level;
  164. rt_uint32_t remain;
  165. cdc_eps_t eps;
  166. eps = (cdc_eps_t)cls->eps;
  167. level = rt_hw_interrupt_disable();
  168. remain = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
  169. if (remain != 0)
  170. {
  171. /* although vcom_in_sending is set in SOF handler in the very
  172. * beginning, we have to guarantee the state is right when start
  173. * sending. There is at least one extreme case where we have finished the
  174. * last IN transaction but the vcom_in_sending is RT_FALSE.
  175. *
  176. * Ok, what the extreme case is: pour data into vcom in loop. Open
  177. * terminal on the PC, you will see the data. Then close it. So the
  178. * data will be sent to the PC in the back. When the buffer of the PC
  179. * driver is full. It will not send IN packet to the board and you will
  180. * have no chance to clear vcom_in_sending in this function. The data
  181. * will fill into the ringbuffer until it is full, and we will reset
  182. * the state machine and clear vcom_in_sending. When you open the
  183. * terminal on the PC again. The IN packet will appear on the line and
  184. * we will, eventually, reach here with vcom_in_sending is clear.
  185. */
  186. vcom_in_sending = RT_TRUE;
  187. rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, remain);
  188. rt_hw_interrupt_enable(level);
  189. /* send data to host */
  190. dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, remain);
  191. return RT_EOK;
  192. }
  193. if (size != 0 &&
  194. (size % CDC_MaxPacketSize) == 0)
  195. {
  196. /* don't have data right now. Send a zero-length-packet to
  197. * terminate the transaction.
  198. *
  199. * FIXME: actually, this might not be the right place to send zlp.
  200. * Only the rt_device_write could know how much data is sending. */
  201. vcom_in_sending = RT_TRUE;
  202. rt_hw_interrupt_enable(level);
  203. dcd_ep_write(device->dcd, eps->ep_in, RT_NULL, 0);
  204. return RT_EOK;
  205. }
  206. else
  207. {
  208. vcom_in_sending = RT_FALSE;
  209. rt_hw_interrupt_enable(level);
  210. return RT_EOK;
  211. }
  212. }
  213. /**
  214. * This function will handle cdc bulk out endpoint request.
  215. *
  216. * @param device the usb device object.
  217. * @param size request size.
  218. *
  219. * @return RT_EOK.
  220. */
  221. static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
  222. {
  223. rt_uint32_t level;
  224. cdc_eps_t eps;
  225. RT_ASSERT(device != RT_NULL);
  226. eps = (cdc_eps_t)cls->eps;
  227. /* receive data from USB VCOM */
  228. level = rt_hw_interrupt_disable();
  229. rt_ringbuffer_put(&rx_ringbuffer, eps->ep_out->buffer, size);
  230. rt_hw_interrupt_enable(level);
  231. /* notify receive data */
  232. rt_hw_serial_isr(&vcom_serial);
  233. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
  234. eps->ep_out->ep_desc->wMaxPacketSize);
  235. return RT_EOK;
  236. }
  237. /**
  238. * This function will handle cdc interrupt in endpoint request.
  239. *
  240. * @param device the usb device object.
  241. * @param size request size.
  242. *
  243. * @return RT_EOK.
  244. */
  245. static rt_err_t _ep_cmd_handler(udevice_t device, uclass_t cls, rt_size_t size)
  246. {
  247. RT_ASSERT(device != RT_NULL);
  248. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));
  249. return RT_EOK;
  250. }
  251. /**
  252. * This function will handle cdc_get_line_coding request.
  253. *
  254. * @param device the usb device object.
  255. * @param setup the setup request.
  256. *
  257. * @return RT_EOK on successful.
  258. */
  259. static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
  260. {
  261. struct ucdc_line_coding data;
  262. rt_uint16_t size;
  263. RT_ASSERT(device != RT_NULL);
  264. RT_ASSERT(setup != RT_NULL);
  265. data.dwDTERate = 115200;
  266. data.bCharFormat = 0;
  267. data.bDataBits = 8;
  268. data.bParityType = 0;
  269. size = setup->length > 7 ? 7 : setup->length;
  270. dcd_ep_write(device->dcd, 0, (void*)&data, size);
  271. return RT_EOK;
  272. }
  273. /**
  274. * This function will handle cdc_set_line_coding request.
  275. *
  276. * @param device the usb device object.
  277. * @param setup the setup request.
  278. *
  279. * @return RT_EOK on successful.
  280. */
  281. static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
  282. {
  283. struct ucdc_line_coding data;
  284. rt_err_t ret;
  285. RT_ASSERT(device != RT_NULL);
  286. RT_ASSERT(setup != RT_NULL);
  287. rt_completion_init(&device->dcd->completion);
  288. dcd_ep_read(device->dcd, 0, (void*)&data, setup->length);
  289. ret = rt_completion_wait(&device->dcd->completion, 100);
  290. if(ret != RT_EOK)
  291. {
  292. rt_kprintf("_cdc_set_line_coding timeout\n");
  293. }
  294. return RT_EOK;
  295. }
  296. /**
  297. * This function will handle cdc interface request.
  298. *
  299. * @param device the usb device object.
  300. * @param setup the setup request.
  301. *
  302. * @return RT_EOK on successful.
  303. */
  304. static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
  305. {
  306. RT_ASSERT(device != RT_NULL);
  307. RT_ASSERT(setup != RT_NULL);
  308. switch(setup->request)
  309. {
  310. case CDC_SEND_ENCAPSULATED_COMMAND:
  311. break;
  312. case CDC_GET_ENCAPSULATED_RESPONSE:
  313. break;
  314. case CDC_SET_COMM_FEATURE:
  315. break;
  316. case CDC_GET_COMM_FEATURE:
  317. break;
  318. case CDC_CLEAR_COMM_FEATURE:
  319. break;
  320. case CDC_SET_LINE_CODING:
  321. _cdc_set_line_coding(device, setup);
  322. vcom_connected = RT_TRUE;
  323. break;
  324. case CDC_GET_LINE_CODING:
  325. _cdc_get_line_coding(device, setup);
  326. break;
  327. case CDC_SET_CONTROL_LINE_STATE:
  328. dcd_send_status(device->dcd);
  329. break;
  330. case CDC_SEND_BREAK:
  331. break;
  332. default:
  333. rt_kprintf("unknown cdc request\n",setup->request_type);
  334. return -RT_ERROR;
  335. }
  336. return RT_EOK;
  337. }
  338. /**
  339. * This function will run cdc class, it will be called on handle set configuration request.
  340. *
  341. * @param device the usb device object.
  342. *
  343. * @return RT_EOK on successful.
  344. */
  345. static rt_err_t _class_run(udevice_t device, uclass_t cls)
  346. {
  347. cdc_eps_t eps;
  348. RT_ASSERT(device != RT_NULL);
  349. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class run\n"));
  350. eps = (cdc_eps_t)cls->eps;
  351. eps->ep_in->buffer = tx_buf;
  352. eps->ep_out->buffer = rx_buf;
  353. _vcom_reset_state();
  354. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
  355. eps->ep_out->ep_desc->wMaxPacketSize);
  356. return RT_EOK;
  357. }
  358. /**
  359. * This function will stop cdc class, it will be called on handle set configuration request.
  360. *
  361. * @param device the usb device object.
  362. *
  363. * @return RT_EOK on successful.
  364. */
  365. static rt_err_t _class_stop(udevice_t device, uclass_t cls)
  366. {
  367. RT_ASSERT(device != RT_NULL);
  368. RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class stop\n"));
  369. _vcom_reset_state();
  370. return RT_EOK;
  371. }
  372. /**
  373. * This function will handle system sof event.
  374. *
  375. * @param device the usb device object.
  376. *
  377. * @return RT_EOK on successful.
  378. */
  379. static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
  380. {
  381. rt_uint32_t level;
  382. rt_size_t size;
  383. cdc_eps_t eps;
  384. if (vcom_connected != RT_TRUE)
  385. return -RT_ERROR;
  386. if (vcom_in_sending)
  387. {
  388. return RT_EOK;
  389. }
  390. eps = (cdc_eps_t)cls->eps;
  391. size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
  392. if (size == 0)
  393. return -RT_EFULL;
  394. level = rt_hw_interrupt_disable();
  395. rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, size);
  396. rt_hw_interrupt_enable(level);
  397. /* send data to host */
  398. vcom_in_sending = RT_TRUE;
  399. dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, size);
  400. return RT_EOK;
  401. }
  402. static struct uclass_ops ops =
  403. {
  404. _class_run,
  405. _class_stop,
  406. _class_sof_handler,
  407. };
  408. /**
  409. * This function will configure cdc descriptor.
  410. *
  411. * @param comm the communication interface number.
  412. * @param data the data interface number.
  413. *
  414. * @return RT_EOK on successful.
  415. */
  416. static rt_err_t _cdc_descriptor_config(ucdc_comm_desc_t comm, rt_uint8_t cintf_nr, ucdc_data_desc_t data, rt_uint8_t dintf_nr)
  417. {
  418. comm->call_mgmt_desc.data_interface = dintf_nr;
  419. comm->union_desc.master_interface = cintf_nr;
  420. comm->union_desc.slave_interface0 = dintf_nr;
  421. #ifdef RT_USB_DEVICE_COMPOSITE
  422. comm->iad_desc.bFirstInterface = cintf_nr;
  423. #endif
  424. return RT_EOK;
  425. }
  426. /**
  427. * This function will create a cdc class instance.
  428. *
  429. * @param device the usb device object.
  430. *
  431. * @return RT_EOK on successful.
  432. */
  433. uclass_t rt_usbd_class_cdc_create(udevice_t device)
  434. {
  435. uclass_t cdc;
  436. cdc_eps_t eps;
  437. uintf_t intf_comm, intf_data;
  438. ualtsetting_t comm_setting, data_setting;
  439. ucdc_data_desc_t data_desc;
  440. ucdc_comm_desc_t comm_desc;
  441. /* parameter check */
  442. RT_ASSERT(device != RT_NULL);
  443. /* set usb device string description */
  444. rt_usbd_device_set_string(device, _ustring);
  445. /* create a cdc class */
  446. cdc = rt_usbd_class_create(device, &dev_desc, &ops);
  447. /* create a cdc class endpoints collection */
  448. eps = rt_malloc(sizeof(struct cdc_eps));
  449. cdc->eps = (void*)eps;
  450. /* create a cdc communication interface and a cdc data interface */
  451. intf_comm = rt_usbd_interface_create(device, _interface_handler);
  452. intf_data = rt_usbd_interface_create(device, _interface_handler);
  453. /* create a communication alternate setting and a data alternate setting */
  454. comm_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_comm_descriptor));
  455. data_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_data_descriptor));
  456. /* config desc in alternate setting */
  457. rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
  458. (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
  459. rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
  460. /* configure the cdc interface descriptor */
  461. _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);
  462. /* create a bulk in and a bulk endpoint */
  463. data_desc = (ucdc_data_desc_t)data_setting->desc;
  464. eps->ep_out = rt_usbd_endpoint_create(&data_desc->ep_out_desc, _ep_out_handler);
  465. eps->ep_in = rt_usbd_endpoint_create(&data_desc->ep_in_desc, _ep_in_handler);
  466. /* add the bulk out and bulk in endpoints to the data alternate setting */
  467. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
  468. rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
  469. /* add the data alternate setting to the data interface
  470. then set default setting of the interface */
  471. rt_usbd_interface_add_altsetting(intf_data, data_setting);
  472. rt_usbd_set_altsetting(intf_data, 0);
  473. /* add the cdc data interface to cdc class */
  474. rt_usbd_class_add_interface(cdc, intf_data);
  475. /* create a command endpoint */
  476. comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
  477. eps->ep_cmd = rt_usbd_endpoint_create(&comm_desc->ep_desc, _ep_cmd_handler);
  478. /* add the command endpoint to the cdc communication interface */
  479. rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
  480. /* add the communication alternate setting to the communication interface,
  481. then set default setting of the interface */
  482. rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
  483. rt_usbd_set_altsetting(intf_comm, 0);
  484. /* add the communication interface to the cdc class */
  485. rt_usbd_class_add_interface(cdc, intf_comm);
  486. return cdc;
  487. }
  488. /**
  489. * UART device in RT-Thread
  490. */
  491. static rt_err_t _vcom_configure(struct rt_serial_device *serial,
  492. struct serial_configure *cfg)
  493. {
  494. return RT_EOK;
  495. }
  496. static rt_err_t _vcom_control(struct rt_serial_device *serial,
  497. int cmd, void *arg)
  498. {
  499. switch (cmd)
  500. {
  501. case RT_DEVICE_CTRL_CLR_INT:
  502. /* disable rx irq */
  503. break;
  504. case RT_DEVICE_CTRL_SET_INT:
  505. /* enable rx irq */
  506. break;
  507. }
  508. return RT_EOK;
  509. }
  510. static int _vcom_putc(struct rt_serial_device *serial, char c)
  511. {
  512. rt_uint32_t level;
  513. int cnt;
  514. if (vcom_connected != RT_TRUE)
  515. {
  516. return 0;
  517. }
  518. /* if the buffer is full, there is a chance that the host would pull some
  519. * data out soon. But we cannot rely on that and if we wait to long, just
  520. * return. */
  521. for (cnt = 500;
  522. RT_RINGBUFFER_EMPTY(&tx_ringbuffer) == 0 && cnt;
  523. cnt--)
  524. {
  525. /*rt_kprintf("wait for %d\n", cnt);*/
  526. if (vcom_connected != RT_TRUE)
  527. return 0;
  528. }
  529. if (cnt == 0)
  530. {
  531. /* OK, we believe that the connection is lost. So don't send any more
  532. * data and act as the USB cable is not plugged in. Reset the VCOM
  533. * state machine */
  534. _vcom_reset_state();
  535. return 0;
  536. }
  537. level = rt_hw_interrupt_disable();
  538. if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer))
  539. {
  540. rt_ringbuffer_putchar(&tx_ringbuffer, c);
  541. }
  542. rt_hw_interrupt_enable(level);
  543. return 1;
  544. }
  545. static int _vcom_getc(struct rt_serial_device *serial)
  546. {
  547. int result;
  548. rt_uint8_t ch;
  549. rt_uint32_t level;
  550. result = -1;
  551. level = rt_hw_interrupt_disable();
  552. if (RT_RINGBUFFER_SIZE(&rx_ringbuffer))
  553. {
  554. rt_ringbuffer_getchar(&rx_ringbuffer, &ch);
  555. result = ch;
  556. }
  557. rt_hw_interrupt_enable(level);
  558. return result;
  559. }
  560. static const struct rt_uart_ops usb_vcom_ops =
  561. {
  562. _vcom_configure,
  563. _vcom_control,
  564. _vcom_putc,
  565. _vcom_getc,
  566. };
  567. void rt_usb_vcom_init(void)
  568. {
  569. struct serial_configure config;
  570. /* initialize ring buffer */
  571. rt_ringbuffer_init(&rx_ringbuffer, rx_rbp, CDC_RX_BUFSIZE);
  572. rt_ringbuffer_init(&tx_ringbuffer, tx_rbp, CDC_TX_BUFSIZE);
  573. config.baud_rate = BAUD_RATE_115200;
  574. config.bit_order = BIT_ORDER_LSB;
  575. config.data_bits = DATA_BITS_8;
  576. config.parity = PARITY_NONE;
  577. config.stop_bits = STOP_BITS_1;
  578. config.invert = NRZ_NORMAL;
  579. vcom_serial.ops = &usb_vcom_ops;
  580. vcom_serial.int_rx = &vcom_int_rx;
  581. vcom_serial.config = config;
  582. /* register vcom device */
  583. rt_hw_serial_register(&vcom_serial, "vcom",
  584. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  585. RT_NULL);
  586. }
  587. #endif