cdc_vcom.c 17.9 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * File      : cdc_vcom.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2012, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-10-02     Yi Qiu       first version
 * 2012-12-12     heyuanjie87  change endpoints and class handler
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <rthw.h>
#include "cdc.h"

#ifdef RT_USB_DEVICE_CDC

23
#define CDC_RX_BUFSIZE          2048
24
#define CDC_TX_BUFSIZE          2048
25 26
static rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
static rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
M
Ming, Bai 已提交
27 28 29
static struct rt_ringbuffer rx_ringbuffer;
static struct rt_ringbuffer tx_ringbuffer;
static struct serial_ringbuffer vcom_int_rx;
30 31 32 33

static struct rt_serial_device vcom_serial;

#define CDC_MaxPacketSize 64
34
ALIGN(RT_ALIGN_SIZE)
35
static rt_uint8_t rx_buf[CDC_RX_BUFSIZE];
36
ALIGN(RT_ALIGN_SIZE)
37
static rt_uint8_t tx_buf[CDC_TX_BUFSIZE];
38

39 40
volatile static rt_bool_t vcom_connected = RT_FALSE;
volatile static rt_bool_t vcom_in_sending = RT_FALSE;
M
Ming, Bai 已提交
41 42 43 44 45 46 47 48 49

static struct udevice_descriptor dev_desc =
{
    USB_DESC_LENGTH_DEVICE,     //bLength;
    USB_DESC_TYPE_DEVICE,       //type;
    USB_BCD_VERSION,            //bcdUSB;
    USB_CLASS_CDC,              //bDeviceClass;
    0x00,                       //bDeviceSubClass;
    0x00,                       //bDeviceProtocol;
50
    CDC_MaxPacketSize,          //bMaxPacketSize0;
H
heyuanjie87 已提交
51 52
    _VENDOR_ID,                 //idVendor;
    _PRODUCT_ID,                //idProduct;
M
Ming, Bai 已提交
53 54 55 56
    USB_BCD_DEVICE,             //bcdDevice;
    USB_STRING_MANU_INDEX,      //iManufacturer;
    USB_STRING_PRODUCT_INDEX,   //iProduct;
    USB_STRING_SERIAL_INDEX,    //iSerialNumber;
57
    USB_DYNAMIC,                //bNumConfigurations;
M
Ming, Bai 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
};

/* communcation interface descriptor */
const static struct ucdc_comm_descriptor _comm_desc =
{
#ifdef RT_USB_DEVICE_COMPOSITE
    /* Interface Association Descriptor */
    USB_DESC_LENGTH_IAD,
    USB_DESC_TYPE_IAD,
    USB_DYNAMIC,
    0x02,
    USB_CDC_CLASS_COMM,
    USB_CDC_SUBCLASS_ACM,
    USB_CDC_PROTOCOL_V25TER,
    0x00,
#endif
    /* Interface Descriptor */
    USB_DESC_LENGTH_INTERFACE,
    USB_DESC_TYPE_INTERFACE,
    USB_DYNAMIC,
78
    0x00,
M
Ming, Bai 已提交
79 80 81 82 83
    0x01,
    USB_CDC_CLASS_COMM,
    USB_CDC_SUBCLASS_ACM,
    USB_CDC_PROTOCOL_V25TER,
    0x00,
84 85
    /* Header Functional Descriptor */
    0x05,
M
Ming, Bai 已提交
86 87 88
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_HEADER,
    0x0110,
89 90
    /* Call Management Functional Descriptor */
    0x05,
M
Ming, Bai 已提交
91 92 93 94 95 96 97 98 99
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_CALL_MGMT,
    0x00,
    USB_DYNAMIC,
    /* Abstract Control Management Functional Descriptor */
    0x04,
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_ACM,
    0x02,
100
    /* Union Functional Descriptor */
M
Ming, Bai 已提交
101 102 103 104 105
    0x05,
    USB_CDC_CS_INTERFACE,
    USB_CDC_SCS_UNION,
    USB_DYNAMIC,
    USB_DYNAMIC,
106
    /* Endpoint Descriptor */
M
Ming, Bai 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    USB_DESC_LENGTH_ENDPOINT,
    USB_DESC_TYPE_ENDPOINT,
    USB_DYNAMIC | USB_DIR_IN,
    USB_EP_ATTR_INT,
    0x08,
    0xFF,
};

/* data interface descriptor */
const static struct ucdc_data_descriptor _data_desc =
{
    /* interface descriptor */
    USB_DESC_LENGTH_INTERFACE,
    USB_DESC_TYPE_INTERFACE,
    USB_DYNAMIC,
    0x00,
123
    0x02,
M
Ming, Bai 已提交
124
    USB_CDC_CLASS_DATA,
125 126 127
    0x00,
    0x00,
    0x00,
M
Ming, Bai 已提交
128
    /* endpoint, bulk out */
129
    USB_DESC_LENGTH_ENDPOINT,
M
Ming, Bai 已提交
130 131
    USB_DESC_TYPE_ENDPOINT,
    USB_DYNAMIC | USB_DIR_OUT,
132
    USB_EP_ATTR_BULK,
M
Ming, Bai 已提交
133
    USB_CDC_BUFSIZE,
134
    0x00,
M
Ming, Bai 已提交
135 136 137 138
    /* endpoint, bulk in */
    USB_DESC_LENGTH_ENDPOINT,
    USB_DESC_TYPE_ENDPOINT,
    USB_DYNAMIC | USB_DIR_IN,
139
    USB_EP_ATTR_BULK,
M
Ming, Bai 已提交
140 141 142 143
    USB_CDC_BUFSIZE,
    0x00,
};

144 145 146 147 148 149 150 151 152 153
const static char* _ustring[] =
{
    "Language",
    "RT-Thread Team.",
    "RTT Virtual Serial",
    "1.1.0",
    "Configuration",
    "Interface",
};

154 155 156 157 158 159 160 161 162 163 164
static void _vcom_reset_state(void)
{
    int lvl = rt_hw_interrupt_disable();
    tx_ringbuffer.read_mirror  = tx_ringbuffer.read_index = 0;
    tx_ringbuffer.write_mirror = tx_ringbuffer.write_index = 0;
    vcom_connected = RT_FALSE;
    vcom_in_sending = RT_FALSE;
    /*rt_kprintf("reset USB serial\n", cnt);*/
    rt_hw_interrupt_enable(lvl);
}

M
Ming, Bai 已提交
165 166 167 168 169 170 171 172 173 174 175
/**
 * This function will handle cdc bulk in endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
{
    rt_uint32_t level;
176
    rt_uint32_t remain;
M
Ming, Bai 已提交
177 178 179
    cdc_eps_t eps;

    eps = (cdc_eps_t)cls->eps;
180
    level = rt_hw_interrupt_disable();
181 182
    remain = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
    if (remain != 0)
183
    {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        /* although vcom_in_sending is set in SOF handler in the very
         * beginning, we have to guarantee the state is right when start
         * sending. There is at least one extreme case where we have finished the
         * last IN transaction but the vcom_in_sending is RT_FALSE.
         *
         * Ok, what the extreme case is: pour data into vcom in loop. Open
         * terminal on the PC, you will see the data. Then close it. So the
         * data will be sent to the PC in the back. When the buffer of the PC
         * driver is full. It will not send IN packet to the board and you will
         * have no chance to clear vcom_in_sending in this function. The data
         * will fill into the ringbuffer until it is full, and we will reset
         * the state machine and clear vcom_in_sending. When you open the
         * terminal on the PC again. The IN packet will appear on the line and
         * we will, eventually, reach here with vcom_in_sending is clear.
         */
199 200
        vcom_in_sending = RT_TRUE;
        rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, remain);
201
        rt_hw_interrupt_enable(level);
202

203 204
        /* send data to host */
        dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, remain);
M
Ming, Bai 已提交
205

206 207
        return RT_EOK;
    }
M
Ming, Bai 已提交
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    if (size != 0 &&
        (size % CDC_MaxPacketSize) == 0)
    {
        /* don't have data right now. Send a zero-length-packet to
         * terminate the transaction.
         *
         * FIXME: actually, this might not be the right place to send zlp.
         * Only the rt_device_write could know how much data is sending. */
        vcom_in_sending = RT_TRUE;
        rt_hw_interrupt_enable(level);
        dcd_ep_write(device->dcd, eps->ep_in, RT_NULL, 0);
        return RT_EOK;
    }
    else
    {
        vcom_in_sending = RT_FALSE;
        rt_hw_interrupt_enable(level);
        return RT_EOK;
    }
M
Ming, Bai 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}

/**
 * This function will handle cdc bulk out endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
{
    rt_uint32_t level;
    cdc_eps_t eps;

    RT_ASSERT(device != RT_NULL);
244

M
Ming, Bai 已提交
245 246 247
    eps = (cdc_eps_t)cls->eps;
    /* receive data from USB VCOM */
    level = rt_hw_interrupt_disable();
248

M
Ming, Bai 已提交
249 250 251 252 253 254
    rt_ringbuffer_put(&rx_ringbuffer, eps->ep_out->buffer, size);
    rt_hw_interrupt_enable(level);

    /* notify receive data */
    rt_hw_serial_isr(&vcom_serial);

255 256
    dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
                eps->ep_out->ep_desc->wMaxPacketSize);
M
Ming, Bai 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    return RT_EOK;
}

/**
 * This function will handle cdc interrupt in endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_cmd_handler(udevice_t device, uclass_t cls, rt_size_t size)
{
    RT_ASSERT(device != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_cmd_handler\n"));

    return RT_EOK;
}

/**
 * This function will handle cdc_get_line_coding request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
{
    struct ucdc_line_coding data;
    rt_uint16_t size;
290

M
Ming, Bai 已提交
291 292
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
293

M
Ming, Bai 已提交
294 295 296 297 298
    data.dwDTERate = 115200;
    data.bCharFormat = 0;
    data.bDataBits = 8;
    data.bParityType = 0;
    size = setup->length > 7 ? 7 : setup->length;
299

M
Ming, Bai 已提交
300
    dcd_ep_write(device->dcd, 0, (void*)&data, size);
301

M
Ming, Bai 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314
    return RT_EOK;
}

/**
 * This function will handle cdc_set_line_coding request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _cdc_set_line_coding(udevice_t device, ureq_t setup)
{
315
    struct ucdc_line_coding data;
M
Ming, Bai 已提交
316 317 318 319 320 321
    rt_err_t ret;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    rt_completion_init(&device->dcd->completion);
322

M
Ming, Bai 已提交
323 324 325 326 327 328 329
    dcd_ep_read(device->dcd, 0, (void*)&data, setup->length);

    ret = rt_completion_wait(&device->dcd->completion, 100);
    if(ret != RT_EOK)
    {
        rt_kprintf("_cdc_set_line_coding timeout\n");
    }
330

M
Ming, Bai 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    return RT_EOK;
}

/**
 * This function will handle cdc interface request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
346

M
Ming, Bai 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360
    switch(setup->request)
    {
    case CDC_SEND_ENCAPSULATED_COMMAND:
        break;
    case CDC_GET_ENCAPSULATED_RESPONSE:
        break;
    case CDC_SET_COMM_FEATURE:
        break;
    case CDC_GET_COMM_FEATURE:
        break;
    case CDC_CLEAR_COMM_FEATURE:
        break;
    case CDC_SET_LINE_CODING:
        _cdc_set_line_coding(device, setup);
361
        vcom_connected = RT_TRUE;
M
Ming, Bai 已提交
362 363
        break;
    case CDC_GET_LINE_CODING:
364
        _cdc_get_line_coding(device, setup);
M
Ming, Bai 已提交
365 366
        break;
    case CDC_SET_CONTROL_LINE_STATE:
367
        dcd_send_status(device->dcd);
M
Ming, Bai 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        break;
    case CDC_SEND_BREAK:
        break;
    default:
        rt_kprintf("unknown cdc request\n",setup->request_type);
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
 * This function will run cdc class, it will be called on handle set configuration request.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _class_run(udevice_t device, uclass_t cls)
{
    cdc_eps_t eps;
    RT_ASSERT(device != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class run\n"));
    eps = (cdc_eps_t)cls->eps;

394 395
    eps->ep_in->buffer = tx_buf;
    eps->ep_out->buffer = rx_buf;
M
Ming, Bai 已提交
396

397 398
    _vcom_reset_state();

399 400 401
    dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
                eps->ep_out->ep_desc->wMaxPacketSize);

M
Ming, Bai 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    return RT_EOK;
}

/**
 * This function will stop cdc class, it will be called on handle set configuration request.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _class_stop(udevice_t device, uclass_t cls)
{
    RT_ASSERT(device != RT_NULL);

    RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class stop\n"));

418 419
    _vcom_reset_state();

M
Ming, Bai 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    return RT_EOK;
}

/**
 * This function will handle system sof event.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
{
    rt_uint32_t level;
    rt_size_t size;
    cdc_eps_t eps;

436 437
    if (vcom_connected != RT_TRUE)
        return -RT_ERROR;
438

439
    if (vcom_in_sending)
440
    {
441
        return RT_EOK;
442
    }
443

M
Ming, Bai 已提交
444 445
    eps = (cdc_eps_t)cls->eps;

446
    size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
447
    if (size == 0)
448
        return -RT_EFULL;
M
Ming, Bai 已提交
449

450 451 452
    level = rt_hw_interrupt_disable();
    rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, size);
    rt_hw_interrupt_enable(level);
453

454
    /* send data to host */
455
    vcom_in_sending = RT_TRUE;
456
    dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, size);
M
Ming, Bai 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

    return RT_EOK;
}

static struct uclass_ops ops =
{
    _class_run,
    _class_stop,
    _class_sof_handler,
};

/**
 * This function will configure cdc descriptor.
 *
 * @param comm the communication interface number.
 * @param data the data interface number.
 *
 * @return RT_EOK on successful.
 */
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)
{
    comm->call_mgmt_desc.data_interface = dintf_nr;
    comm->union_desc.master_interface = cintf_nr;
    comm->union_desc.slave_interface0 = dintf_nr;
481
#ifdef RT_USB_DEVICE_COMPOSITE
M
Ming, Bai 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    comm->iad_desc.bFirstInterface = cintf_nr;
#endif

    return RT_EOK;
}

/**
 * This function will create a cdc class instance.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
uclass_t rt_usbd_class_cdc_create(udevice_t device)
{
    uclass_t cdc;
    cdc_eps_t eps;
    uintf_t intf_comm, intf_data;
    ualtsetting_t comm_setting, data_setting;
    ucdc_data_desc_t data_desc;
    ucdc_comm_desc_t comm_desc;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
506 507 508

    /* set usb device string description */
    rt_usbd_device_set_string(device, _ustring);
M
Ming, Bai 已提交
509 510 511 512 513 514 515 516 517 518 519
    /* create a cdc class */
    cdc = rt_usbd_class_create(device, &dev_desc, &ops);
    /* create a cdc class endpoints collection */
    eps = rt_malloc(sizeof(struct cdc_eps));
    cdc->eps = (void*)eps;

    /* create a cdc communication interface and a cdc data interface */
    intf_comm = rt_usbd_interface_create(device, _interface_handler);
    intf_data = rt_usbd_interface_create(device, _interface_handler);

    /* create a communication alternate setting and a data alternate setting */
520 521 522 523
    comm_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_comm_descriptor));
    data_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_data_descriptor));

    /* config desc in alternate setting */
M
Ming, Bai 已提交
524
    rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
525
                                         (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
M
Ming, Bai 已提交
526 527 528 529 530 531 532 533 534 535 536 537
    rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
    /* configure the cdc interface descriptor */
    _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);

    /* create a bulk in and a bulk endpoint */
    data_desc = (ucdc_data_desc_t)data_setting->desc;
    eps->ep_out = rt_usbd_endpoint_create(&data_desc->ep_out_desc, _ep_out_handler);
    eps->ep_in = rt_usbd_endpoint_create(&data_desc->ep_in_desc, _ep_in_handler);

    /* add the bulk out and bulk in endpoints to the data alternate setting */
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
538

M
Ming, Bai 已提交
539 540 541 542 543 544
    /* add the data alternate setting to the data interface
            then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_data, data_setting);
    rt_usbd_set_altsetting(intf_data, 0);

    /* add the cdc data interface to cdc class */
545
    rt_usbd_class_add_interface(cdc, intf_data);
M
Ming, Bai 已提交
546 547 548 549 550 551 552

    /* create a command endpoint */
    comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
    eps->ep_cmd = rt_usbd_endpoint_create(&comm_desc->ep_desc, _ep_cmd_handler);

    /* add the command endpoint to the cdc communication interface */
    rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
553

M
Ming, Bai 已提交
554 555 556 557 558 559
    /* add the communication alternate setting to the communication interface,
       then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
    rt_usbd_set_altsetting(intf_comm, 0);

    /* add the communication interface to the cdc class */
560
    rt_usbd_class_add_interface(cdc, intf_comm);
M
Ming, Bai 已提交
561 562 563 564 565 566 567

    return cdc;
}

/**
* UART device in RT-Thread
*/
568 569
static rt_err_t _vcom_configure(struct rt_serial_device *serial,
                                struct serial_configure *cfg)
M
Ming, Bai 已提交
570 571 572 573
{
    return RT_EOK;
}

574 575
static rt_err_t _vcom_control(struct rt_serial_device *serial,
                              int cmd, void *arg)
M
Ming, Bai 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
{
    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        break;
    }

    return RT_EOK;
}

static int _vcom_putc(struct rt_serial_device *serial, char c)
{
    rt_uint32_t level;
593
    int cnt;
594 595

    if (vcom_connected != RT_TRUE)
596
    {
597
        return 0;
598
    }
599

600 601 602 603 604 605
    /* if the buffer is full, there is a chance that the host would pull some
     * data out soon. But we cannot rely on that and if we wait to long, just
     * return. */
    for (cnt = 500;
         RT_RINGBUFFER_EMPTY(&tx_ringbuffer) == 0 && cnt;
         cnt--)
606
    {
607
        /*rt_kprintf("wait for %d\n", cnt);*/
608 609 610
        if (vcom_connected != RT_TRUE)
            return 0;
    }
611
    if (cnt == 0)
612 613 614 615 616
    {
        /* OK, we believe that the connection is lost. So don't send any more
         * data and act as the USB cable is not plugged in. Reset the VCOM
         * state machine */
        _vcom_reset_state();
617
        return 0;
618
    }
619

M
Ming, Bai 已提交
620 621 622 623 624
    level = rt_hw_interrupt_disable();
    if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer))
    {
        rt_ringbuffer_putchar(&tx_ringbuffer, c);
    }
625 626
    rt_hw_interrupt_enable(level);

M
Ming, Bai 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    return 1;
}

static int _vcom_getc(struct rt_serial_device *serial)
{
    int result;
    rt_uint8_t ch;
    rt_uint32_t level;

    result = -1;

    level = rt_hw_interrupt_disable();
    if (RT_RINGBUFFER_SIZE(&rx_ringbuffer))
    {
        rt_ringbuffer_getchar(&rx_ringbuffer, &ch);
        result = ch;
    }
    rt_hw_interrupt_enable(level);

    return result;
}

static const struct rt_uart_ops usb_vcom_ops =
{
    _vcom_configure,
    _vcom_control,
    _vcom_putc,
    _vcom_getc,
};

void rt_usb_vcom_init(void)
{
    struct serial_configure config;

    /* initialize ring buffer */
662 663
    rt_ringbuffer_init(&rx_ringbuffer, rx_rbp, CDC_RX_BUFSIZE);
    rt_ringbuffer_init(&tx_ringbuffer, tx_rbp, CDC_TX_BUFSIZE);
M
Ming, Bai 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677

    config.baud_rate = BAUD_RATE_115200;
    config.bit_order = BIT_ORDER_LSB;
    config.data_bits = DATA_BITS_8;
    config.parity = PARITY_NONE;
    config.stop_bits = STOP_BITS_1;
    config.invert = NRZ_NORMAL;

    vcom_serial.ops = &usb_vcom_ops;
    vcom_serial.int_rx = &vcom_int_rx;
    vcom_serial.config = config;

    /* register vcom device */
    rt_hw_serial_register(&vcom_serial, "vcom",
678
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
679
                          RT_NULL);
M
Ming, Bai 已提交
680 681 682
}

#endif