cdc_vcom.c 25.9 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-10-02     Yi Qiu       first version
9 10
 * 2012-12-12     heyuanjie87  change endpoints and function handler 
 * 2013-06-25     heyuanjie87  remove SOF mechinism
还_没_想_好's avatar
还_没_想_好 已提交
11 12
 * 2013-07-20     Yi Qiu       do more test
 * 2016-02-01     Urey         Fix some error
M
Ming, Bai 已提交
13 14
 */

还_没_想_好's avatar
还_没_想_好 已提交
15
#include <rthw.h>
M
Ming, Bai 已提交
16
#include <rtthread.h>
17
#include <rtservice.h>
M
Ming, Bai 已提交
18
#include <rtdevice.h>
还_没_想_好's avatar
还_没_想_好 已提交
19 20
#include <drivers/serial.h>
#include "drivers/usb_device.h"
M
Ming, Bai 已提交
21 22 23 24
#include "cdc.h"

#ifdef RT_USB_DEVICE_CDC

25 26 27 28 29 30
#ifdef RT_VCOM_TX_TIMEOUT
#define VCOM_TX_TIMEOUT      RT_VCOM_TX_TIMEOUT
#else /*!RT_VCOM_TX_TIMEOUT*/
#define VCOM_TX_TIMEOUT      1000
#endif /*RT_VCOM_TX_TIMEOUT*/

31 32 33
#ifdef RT_CDC_RX_BUFSIZE
#define CDC_RX_BUFSIZE          RT_CDC_RX_BUFSIZE
#else
34
#define CDC_RX_BUFSIZE          128
35
#endif
36 37
#define CDC_MAX_PACKET_SIZE     64
#define VCOM_DEVICE             "vcom"
38

39 40 41 42 43
#ifdef RT_VCOM_TASK_STK_SIZE
#define VCOM_TASK_STK_SIZE      RT_VCOM_TASK_STK_SIZE
#else /*!RT_VCOM_TASK_STK_SIZE*/
#define VCOM_TASK_STK_SIZE      512
#endif /*RT_VCOM_TASK_STK_SIZE*/
还_没_想_好's avatar
还_没_想_好 已提交
44

45 46
#ifdef RT_VCOM_TX_USE_DMA
#define VCOM_TX_USE_DMA
47
#endif /*RT_VCOM_TX_USE_DMA*/
48 49 50 51 52 53 54 55

#ifdef RT_VCOM_SERNO
#define _SER_NO RT_VCOM_SERNO
#else /*!RT_VCOM_SERNO*/
#define _SER_NO  "32021919830108"
#endif /*RT_VCOM_SERNO*/

#ifdef RT_VCOM_SER_LEN
56
#define _SER_NO_LEN RT_VCOM_SER_LEN
57
#else /*!RT_VCOM_SER_LEN*/
58
#define _SER_NO_LEN 14 /*rt_strlen("32021919830108")*/
59
#endif /*RT_VCOM_SER_LEN*/
还_没_想_好's avatar
还_没_想_好 已提交
60

61
ALIGN(RT_ALIGN_SIZE)
还_没_想_好's avatar
还_没_想_好 已提交
62
static rt_uint8_t vcom_thread_stack[VCOM_TASK_STK_SIZE];
63 64 65
static struct rt_thread vcom_thread;
static struct ucdc_line_coding line_coding;

66
#define CDC_TX_BUFSIZE    1024
67
#define CDC_BULKIN_MAXSIZE (CDC_TX_BUFSIZE / 8)
68 69

#define CDC_TX_HAS_DATE   0x01
70
#define CDC_TX_HAS_SPACE  0x02
71

72 73
struct vcom
{
74
    struct rt_serial_device serial;
75 76 77 78 79 80
    uep_t ep_out;
    uep_t ep_in;
    uep_t ep_cmd;
    rt_bool_t connected;
    rt_bool_t in_sending;
    struct rt_completion wait;
81
    rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
82 83 84 85
    struct rt_ringbuffer rx_ringbuffer;
    rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
    struct rt_ringbuffer tx_ringbuffer;
    struct rt_event  tx_event;
86
};
87

88 89 90 91 92 93
struct vcom_tx_msg
{
    struct rt_serial_device * serial;
    const char *buf;
    rt_size_t size;
};
M
Ming, Bai 已提交
94

95
ALIGN(4)
M
Ming, Bai 已提交
96 97 98 99 100 101 102 103
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;
104
    CDC_MAX_PACKET_SIZE,          //bMaxPacketSize0;
H
heyuanjie87 已提交
105 106
    _VENDOR_ID,                 //idVendor;
    _PRODUCT_ID,                //idProduct;
M
Ming, Bai 已提交
107 108 109 110
    USB_BCD_DEVICE,             //bcdDevice;
    USB_STRING_MANU_INDEX,      //iManufacturer;
    USB_STRING_PRODUCT_INDEX,   //iProduct;
    USB_STRING_SERIAL_INDEX,    //iSerialNumber;
111
    USB_DYNAMIC,                //bNumConfigurations;
M
Ming, Bai 已提交
112 113
};

114
//FS and HS needed
115
ALIGN(4)
116 117
static struct usb_qualifier_descriptor dev_qualifier =
{
118 119 120 121 122 123 124 125
    sizeof(dev_qualifier),          //bLength
    USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
    0x0200,                         //bcdUSB
    USB_CLASS_CDC,                  //bDeviceClass
    0x00,                           //bDeviceSubClass
    0x00,                           //bDeviceProtocol
    64,                             //bMaxPacketSize0
    0x01,                           //bNumConfigurations
126 127 128
    0,
};

M
Ming, Bai 已提交
129
/* communcation interface descriptor */
130
ALIGN(4)
M
Ming, Bai 已提交
131 132 133 134
const static struct ucdc_comm_descriptor _comm_desc =
{
#ifdef RT_USB_DEVICE_COMPOSITE
    /* Interface Association Descriptor */
135 136 137 138 139 140 141 142 143 144
    {
        USB_DESC_LENGTH_IAD,
        USB_DESC_TYPE_IAD,
        USB_DYNAMIC,
        0x02,
        USB_CDC_CLASS_COMM,
        USB_CDC_SUBCLASS_ACM,
        USB_CDC_PROTOCOL_V25TER,
        0x00,
    },
M
Ming, Bai 已提交
145 146
#endif
    /* Interface Descriptor */
147 148 149 150 151 152 153 154 155 156 157
    {
        USB_DESC_LENGTH_INTERFACE,
        USB_DESC_TYPE_INTERFACE,
        USB_DYNAMIC,
        0x00,   
        0x01,
        USB_CDC_CLASS_COMM,
        USB_CDC_SUBCLASS_ACM,
        USB_CDC_PROTOCOL_V25TER,
        0x00,
    },
158
    /* Header Functional Descriptor */   
159 160 161 162 163 164
    {
        0x05,                              
        USB_CDC_CS_INTERFACE,
        USB_CDC_SCS_HEADER,
        0x0110,
    },
165
    /* Call Management Functional Descriptor */   
166 167 168 169 170 171 172
    {
        0x05,            
        USB_CDC_CS_INTERFACE,
        USB_CDC_SCS_CALL_MGMT,
        0x00,
        USB_DYNAMIC,
    },
M
Ming, Bai 已提交
173
    /* Abstract Control Management Functional Descriptor */
174 175 176 177 178 179
    {
        0x04,
        USB_CDC_CS_INTERFACE,
        USB_CDC_SCS_ACM,
        0x02,
    },
180
    /* Union Functional Descriptor */   
181 182 183 184 185 186 187
    {
        0x05,
        USB_CDC_CS_INTERFACE,
        USB_CDC_SCS_UNION,
        USB_DYNAMIC,
        USB_DYNAMIC,
    },
188
    /* Endpoint Descriptor */    
189 190 191 192 193 194 195 196
    {
        USB_DESC_LENGTH_ENDPOINT,
        USB_DESC_TYPE_ENDPOINT,
        USB_DYNAMIC | USB_DIR_IN,
        USB_EP_ATTR_INT,
        0x08,
        0xFF,
    },
M
Ming, Bai 已提交
197 198 199
};

/* data interface descriptor */
200
ALIGN(4)
M
Ming, Bai 已提交
201 202 203
const static struct ucdc_data_descriptor _data_desc =
{
    /* interface descriptor */
204 205 206 207 208 209 210 211 212 213 214
    {
        USB_DESC_LENGTH_INTERFACE,
        USB_DESC_TYPE_INTERFACE,
        USB_DYNAMIC,
        0x00,
        0x02,         
        USB_CDC_CLASS_DATA,
        0x00,                             
        0x00,                             
        0x00,              
    },
M
Ming, Bai 已提交
215
    /* endpoint, bulk out */
216 217 218 219 220 221 222 223
    {
        USB_DESC_LENGTH_ENDPOINT,     
        USB_DESC_TYPE_ENDPOINT,
        USB_DYNAMIC | USB_DIR_OUT,
        USB_EP_ATTR_BULK,      
        USB_CDC_BUFSIZE,
        0x00,          
    },
M
Ming, Bai 已提交
224
    /* endpoint, bulk in */
225 226 227 228 229 230 231 232
    {
        USB_DESC_LENGTH_ENDPOINT,
        USB_DESC_TYPE_ENDPOINT,
        USB_DYNAMIC | USB_DIR_IN,
        USB_EP_ATTR_BULK,      
        USB_CDC_BUFSIZE,
        0x00,
    },
M
Ming, Bai 已提交
233
};
234
ALIGN(4)
235 236 237 238
static char serno[_SER_NO_LEN + 1] = {'\0'};
RT_WEAK rt_err_t vcom_get_stored_serno(char *serno, int size);

rt_err_t vcom_get_stored_serno(char *serno, int size)
239 240 241
{
    return RT_ERROR;
}
242
ALIGN(4)
243 244 245 246 247
const static char* _ustring[] =
{
    "Language",
    "RT-Thread Team.",
    "RTT Virtual Serial",
248
    serno,
249 250 251
    "Configuration",
    "Interface",
};
252
static void rt_usb_vcom_init(struct ufunction *func);
253

254
static void _vcom_reset_state(ufunction_t func)
255
{
256 257 258 259 260 261 262 263 264 265
    struct vcom* data;
    int lvl;
    
    RT_ASSERT(func != RT_NULL)

    data = (struct vcom*)func->user_data;
    
    lvl = rt_hw_interrupt_disable();
    data->connected = RT_FALSE;
    data->in_sending = RT_FALSE;
266 267 268 269
    /*rt_kprintf("reset USB serial\n", cnt);*/
    rt_hw_interrupt_enable(lvl);
}

M
Ming, Bai 已提交
270 271 272
/**
 * This function will handle cdc bulk in endpoint request.
 *
273
 * @param func the usb function object.
M
Ming, Bai 已提交
274 275 276 277
 * @param size request size.
 *
 * @return RT_EOK.
 */
278
static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
M
Ming, Bai 已提交
279
{
280
    struct vcom *data;
281
    rt_size_t request_size;
M
Ming, Bai 已提交
282

283
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
284

285
    data = (struct vcom*)func->user_data;
286 287 288
    request_size = data->ep_in->request.size;
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler %d\n", request_size));
    if ((request_size != 0) && ((request_size % EP_MAXPACKET(data->ep_in)) == 0))
289 290 291 292 293 294
    {
        /* 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. */
295 296 297 298 299 300 301
        data->in_sending = RT_TRUE;

        data->ep_in->request.buffer = RT_NULL;
        data->ep_in->request.size = 0;
        data->ep_in->request.req_type = UIO_REQUEST_WRITE;
        rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);

302 303
        return RT_EOK;
    }
304 305 306 307
    
    rt_completion_done(&data->wait);
    
    return RT_EOK;
M
Ming, Bai 已提交
308 309 310 311 312
}

/**
 * This function will handle cdc bulk out endpoint request.
 *
313
 * @param func the usb function object.
M
Ming, Bai 已提交
314 315 316 317
 * @param size request size.
 *
 * @return RT_EOK.
 */
318
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
M
Ming, Bai 已提交
319 320
{
    rt_uint32_t level;
321
    struct vcom *data;
M
Ming, Bai 已提交
322

323
    RT_ASSERT(func != RT_NULL);
324

325
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
326

327
    data = (struct vcom*)func->user_data;
328
    /* ensure serial is active */
C
charlown 已提交
329 330
    if((data->serial.parent.flag & RT_DEVICE_FLAG_ACTIVATED)
        && (data->serial.parent.open_flag & RT_DEVICE_OFLAG_OPEN))
331 332 333
    {
        /* receive data from USB VCOM */
        level = rt_hw_interrupt_disable();
334

335 336
        rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
        rt_hw_interrupt_enable(level);
M
Ming, Bai 已提交
337

338 339 340
        /* notify receive data */
        rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND);
    }
M
Ming, Bai 已提交
341

342 343
    data->ep_out->request.buffer = data->ep_out->buffer;
    data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
344
    data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
345
    rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
M
Ming, Bai 已提交
346 347 348 349 350 351 352 353 354 355 356 357

    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.
 */
358
static rt_err_t _ep_cmd_handler(ufunction_t func, rt_size_t size)
M
Ming, Bai 已提交
359
{
360
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

    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;
379

M
Ming, Bai 已提交
380 381
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);
382

383 384
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));

M
Ming, Bai 已提交
385 386 387 388
    data.dwDTERate = 115200;
    data.bCharFormat = 0;
    data.bDataBits = 8;
    data.bParityType = 0;
还_没_想_好's avatar
还_没_想_好 已提交
389
    size = setup->wLength > 7 ? 7 : setup->wLength;
390

391 392 393 394 395 396 397 398
    rt_usbd_ep0_write(device, (void*)&data, size);

    return RT_EOK;
}

static rt_err_t _cdc_set_line_coding_callback(udevice_t device, rt_size_t size)
{
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding_callback\n"));
399

400 401
    dcd_ep0_send_status(device->dcd);
    
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 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)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

418
    RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
419

420 421
    rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
        _cdc_set_line_coding_callback);
422

M
Ming, Bai 已提交
423 424 425 426 427 428 429 430 431 432 433
    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.
 */
434
static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
M
Ming, Bai 已提交
435
{
436 437 438 439
    struct vcom *data;

    RT_ASSERT(func != RT_NULL);
    RT_ASSERT(func->device != RT_NULL);
M
Ming, Bai 已提交
440
    RT_ASSERT(setup != RT_NULL);
441

442 443
    data = (struct vcom*)func->user_data;
    
还_没_想_好's avatar
还_没_想_好 已提交
444
    switch(setup->bRequest)
M
Ming, Bai 已提交
445 446 447 448 449 450 451 452 453 454 455 456
    {
    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:
457
        _cdc_set_line_coding(func->device, setup);
M
Ming, Bai 已提交
458 459
        break;
    case CDC_GET_LINE_CODING:
460
        _cdc_get_line_coding(func->device, setup);
M
Ming, Bai 已提交
461 462
        break;
    case CDC_SET_CONTROL_LINE_STATE:
463 464
        data->connected = (setup->wValue & 0x01) > 0?RT_TRUE:RT_FALSE;
        RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom state:%d \n", data->connected));
465
        dcd_ep0_send_status(func->device->dcd);
M
Ming, Bai 已提交
466 467 468 469 470 471 472 473 474 475 476 477
        break;
    case CDC_SEND_BREAK:
        break;
    default:
        rt_kprintf("unknown cdc request\n",setup->request_type);
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
478
 * This function will run cdc function, it will be called on handle set configuration request.
M
Ming, Bai 已提交
479
 *
480
 * @param func the usb function object.
M
Ming, Bai 已提交
481 482 483
 *
 * @return RT_EOK on successful.
 */
484
static rt_err_t _function_enable(ufunction_t func)
M
Ming, Bai 已提交
485
{
486
    struct vcom *data;
M
Ming, Bai 已提交
487

488
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
489

490
    RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
491

492 493 494 495
    _vcom_reset_state(func);
    
    data = (struct vcom*)func->user_data;
    data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
496

497 498 499
    data->ep_out->request.buffer = data->ep_out->buffer;
    data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
    
500
    data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
501 502
    rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
    
M
Ming, Bai 已提交
503 504 505 506
    return RT_EOK;
}

/**
507
 * This function will stop cdc function, it will be called on handle set configuration request.
M
Ming, Bai 已提交
508
 *
509
 * @param func the usb function object.
M
Ming, Bai 已提交
510 511 512
 *
 * @return RT_EOK on successful.
 */
513
static rt_err_t _function_disable(ufunction_t func)
M
Ming, Bai 已提交
514
{
515
    struct vcom *data;
M
Ming, Bai 已提交
516

517
    RT_ASSERT(func != RT_NULL);
M
Ming, Bai 已提交
518

519
    RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
M
Ming, Bai 已提交
520

521
    _vcom_reset_state(func);
522

523 524
    data = (struct vcom*)func->user_data;
    if(data->ep_out->buffer != RT_NULL)
525
    {
526 527
        rt_free(data->ep_out->buffer);
        data->ep_out->buffer = RT_NULL;        
528
    }
529

M
Ming, Bai 已提交
530 531 532
    return RT_EOK;
}

533
static struct ufunction_ops ops =
M
Ming, Bai 已提交
534
{
535 536 537
    _function_enable,
    _function_disable,
    RT_NULL,
M
Ming, Bai 已提交
538 539 540 541 542 543 544 545 546 547
};

/**
 * This function will configure cdc descriptor.
 *
 * @param comm the communication interface number.
 * @param data the data interface number.
 *
 * @return RT_EOK on successful.
 */
548 549
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)
M
Ming, Bai 已提交
550 551 552 553
{
    comm->call_mgmt_desc.data_interface = dintf_nr;
    comm->union_desc.master_interface = cintf_nr;
    comm->union_desc.slave_interface0 = dintf_nr;
554
#ifdef RT_USB_DEVICE_COMPOSITE
M
Ming, Bai 已提交
555 556 557 558 559 560 561
    comm->iad_desc.bFirstInterface = cintf_nr;
#endif

    return RT_EOK;
}

/**
562
 * This function will create a cdc function instance.
M
Ming, Bai 已提交
563 564 565 566 567
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
568
ufunction_t rt_usbd_function_cdc_create(udevice_t device)
M
Ming, Bai 已提交
569
{
570 571
    ufunction_t func;
    struct vcom* data;
M
Ming, Bai 已提交
572 573 574 575 576 577 578
    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);
579

580 581 582 583 584 585
    rt_memset(serno, 0, _SER_NO_LEN + 1);
    if(vcom_get_stored_serno(serno, _SER_NO_LEN) != RT_EOK)
    {
        rt_memset(serno, 0, _SER_NO_LEN + 1);
        rt_memcpy(serno, _SER_NO, rt_strlen(_SER_NO));
    }
586 587
    /* set usb device string description */
    rt_usbd_device_set_string(device, _ustring);
588 589 590
    
    /* create a cdc function */
    func = rt_usbd_function_new(device, &dev_desc, &ops);
591 592
    //not support HS
    //rt_usbd_device_set_qualifier(device, &dev_qualifier);
593 594 595 596 597 598 599 600
    
    /* allocate memory for cdc vcom data */
    data = (struct vcom*)rt_malloc(sizeof(struct vcom));
    rt_memset(data, 0, sizeof(struct vcom));
    func->user_data = (void*)data;
    
    /* initilize vcom */
    rt_usb_vcom_init(func);
M
Ming, Bai 已提交
601 602

    /* create a cdc communication interface and a cdc data interface */
603 604
    intf_comm = rt_usbd_interface_new(device, _interface_handler);
    intf_data = rt_usbd_interface_new(device, _interface_handler);
M
Ming, Bai 已提交
605 606

    /* create a communication alternate setting and a data alternate setting */
607 608
    comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_comm_descriptor));
    data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
609 610

    /* config desc in alternate setting */
M
Ming, Bai 已提交
611
    rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
612
                                         (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
M
Ming, Bai 已提交
613 614 615 616 617 618
    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 command endpoint */
    comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
619
    data->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
M
Ming, Bai 已提交
620 621

    /* add the command endpoint to the cdc communication interface */
622
    rt_usbd_altsetting_add_endpoint(comm_setting, data->ep_cmd);
623

M
Ming, Bai 已提交
624 625 626 627 628
    /* 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);

629 630
    /* add the communication interface to the cdc function */
    rt_usbd_function_add_interface(func, intf_comm);
M
Ming, Bai 已提交
631

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    /* create a bulk in and a bulk endpoint */
    data_desc = (ucdc_data_desc_t)data_setting->desc;
    data->ep_out = rt_usbd_endpoint_new(&data_desc->ep_out_desc, _ep_out_handler);
    data->ep_in = rt_usbd_endpoint_new(&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, data->ep_in);
    rt_usbd_altsetting_add_endpoint(data_setting, data->ep_out);

    /* 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 function */
    rt_usbd_function_add_interface(func, intf_data);
    
    return func;
M
Ming, Bai 已提交
650 651 652 653 654
}

/**
* UART device in RT-Thread
*/
655 656
static rt_err_t _vcom_configure(struct rt_serial_device *serial,
                                struct serial_configure *cfg)
M
Ming, Bai 已提交
657 658 659 660
{
    return RT_EOK;
}

661 662
static rt_err_t _vcom_control(struct rt_serial_device *serial,
                              int cmd, void *arg)
M
Ming, Bai 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676
{
    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;
}

677
static int _vcom_getc(struct rt_serial_device *serial)
M
Ming, Bai 已提交
678
{
679 680
    int result;
    rt_uint8_t ch;
M
Ming, Bai 已提交
681
    rt_uint32_t level;
682 683 684 685 686
    struct ufunction *func;
    struct vcom *data;
    
    func = (struct ufunction*)serial->parent.user_data;
    data = (struct vcom*)func->user_data;
687

688
    result = -1;
689

M
Ming, Bai 已提交
690
    level = rt_hw_interrupt_disable();
691 692

    if(rt_ringbuffer_getchar(&data->rx_ringbuffer, &ch) != 0)
M
Ming, Bai 已提交
693
    {
694
        result = ch;
M
Ming, Bai 已提交
695
    }
696

697 698
    rt_hw_interrupt_enable(level);

699
    return result;
M
Ming, Bai 已提交
700
}
701 702

static rt_size_t _vcom_rb_block_put(struct vcom *data, const rt_uint8_t *buf, rt_size_t size)
M
Ming, Bai 已提交
703
{
704
    rt_uint32_t level;
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
    rt_size_t   put_len = 0;
    rt_size_t   w_ptr = 0;
    rt_uint32_t res;
    rt_size_t   remain_size = size;

    while (remain_size)
    {
        level = rt_hw_interrupt_disable();
        put_len = rt_ringbuffer_put(&data->tx_ringbuffer, (const rt_uint8_t *)&buf[w_ptr], remain_size);
        rt_hw_interrupt_enable(level);
        w_ptr += put_len;
        remain_size -= put_len;
        if (put_len == 0)
        {
            rt_event_recv(&data->tx_event, CDC_TX_HAS_SPACE,
                    RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                    VCOM_TX_TIMEOUT, &res);
        }
        else
        {
            rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
        }
    }
728

729 730 731 732 733
    return size;
}

static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size,int direction)
{
734 735
    struct ufunction *func;
    struct vcom *data;
736
    rt_uint32_t send_size = 0;
737 738
    rt_size_t ptr = 0;
    rt_uint8_t crlf[2] = {'\r', '\n',};
M
Ming, Bai 已提交
739

740 741 742
    func = (struct ufunction*)serial->parent.user_data;
    data = (struct vcom*)func->user_data;

743 744
    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(buf != RT_NULL);
M
Ming, Bai 已提交
745

746
    RT_DEBUG_LOG(RT_DEBUG_USB, ("%s\n",__func__));
还_没_想_好's avatar
还_没_想_好 已提交
747

748
    if (data->connected)
M
Ming, Bai 已提交
749
    {
750 751
        if((serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
        {
752
            while(send_size < size)
753
            {
754
                while(ptr < size && buf[ptr] != '\n')
755 756 757
                {
                    ptr++;
                }
758
                if(ptr < size)
759
                {
760 761 762
                    send_size += _vcom_rb_block_put(data, (const rt_uint8_t *)&buf[send_size], ptr - send_size);
                    _vcom_rb_block_put(data, crlf, 2);
                    send_size++;
763 764
                    ptr++;
                }
765 766 767 768
                else if (ptr == size)
                {
                    send_size += _vcom_rb_block_put(data, (const rt_uint8_t *)&buf[send_size], ptr - send_size);
                }
769 770 771 772 773 774
                else
                {
                    break;
                }
            }
        }
775
        else
776
        {
777 778 779 780
            while (send_size < size)
            {
                send_size += _vcom_rb_block_put(data, (rt_uint8_t *)&buf[send_size], size - send_size);
            }
781 782
        }
    }
783 784 785 786 787 788
    else
    {
        /* recover dataqueue resources */
        rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
    }

789
    return size;
M
Ming, Bai 已提交
790
}
还_没_想_好's avatar
还_没_想_好 已提交
791 792
static int _vcom_putc(struct rt_serial_device *serial, char c)
{
793 794 795 796 797 798
    rt_uint32_t level;
    struct ufunction *func;
    struct vcom *data;

    func = (struct ufunction*)serial->parent.user_data;
    data = (struct vcom*)func->user_data;
还_没_想_好's avatar
还_没_想_好 已提交
799 800 801

    RT_ASSERT(serial != RT_NULL);

802
    if (data->connected)
还_没_想_好's avatar
还_没_想_好 已提交
803
    {
804 805 806 807 808 809 810 811 812 813 814
        if(c == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
        {
            level = rt_hw_interrupt_disable();
            rt_ringbuffer_putchar_force(&data->tx_ringbuffer, '\r');
            rt_hw_interrupt_enable(level);
            rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
        }
        level = rt_hw_interrupt_disable();
        rt_ringbuffer_putchar_force(&data->tx_ringbuffer, c);
        rt_hw_interrupt_enable(level);
        rt_event_send(&data->tx_event, CDC_TX_HAS_DATE);
还_没_想_好's avatar
还_没_想_好 已提交
815 816 817 818
    }

    return 1;
}
M
Ming, Bai 已提交
819 820 821 822 823

static const struct rt_uart_ops usb_vcom_ops =
{
    _vcom_configure,
    _vcom_control,
还_没_想_好's avatar
还_没_想_好 已提交
824
    _vcom_putc,
M
Ming, Bai 已提交
825
    _vcom_getc,
还_没_想_好's avatar
还_没_想_好 已提交
826
    _vcom_tx
M
Ming, Bai 已提交
827 828
};

829 830
/* Vcom Tx Thread */
static void vcom_tx_thread_entry(void* parameter)
M
Ming, Bai 已提交
831
{
832 833 834 835
    rt_uint32_t level;
    rt_uint32_t res;
    struct ufunction *func = (struct ufunction *)parameter;
    struct vcom *data = (struct vcom*)func->user_data;
836
    rt_uint8_t ch[CDC_BULKIN_MAXSIZE];
837

838 839
    while (1)
    {
840 841 842 843 844 845 846
        if
        (
            (rt_event_recv(&data->tx_event, CDC_TX_HAS_DATE,
                    RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                    RT_WAITING_FOREVER, &res) != RT_EOK) ||
                                 (!(res & CDC_TX_HAS_DATE))
        )
847
        {
848 849
            continue;
        }
850
        if(!(res & CDC_TX_HAS_DATE))
851 852 853 854 855 856
        {
            continue;
        }
        while(rt_ringbuffer_data_len(&data->tx_ringbuffer))
        {
            level = rt_hw_interrupt_disable();
857
            res = rt_ringbuffer_get(&data->tx_ringbuffer, ch, CDC_BULKIN_MAXSIZE);
858
            rt_hw_interrupt_enable(level);
还_没_想_好's avatar
还_没_想_好 已提交
859

860 861 862 863
            if(!res)
            {
                continue;
            }
864 865
            if (!data->connected)
            {
866 867 868 869 870 871 872 873
                if(data->serial.parent.open_flag &
#ifndef VCOM_TX_USE_DMA
                         RT_DEVICE_FLAG_INT_TX
#else
                         RT_DEVICE_FLAG_DMA_TX
#endif
                )
                {
还_没_想_好's avatar
还_没_想_好 已提交
874 875
                /* drop msg */
#ifndef VCOM_TX_USE_DMA
876
                    rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
还_没_想_好's avatar
还_没_想_好 已提交
877
#else
878
                    rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
还_没_想_好's avatar
还_没_想_好 已提交
879
#endif
880
                }
881 882 883
                continue;
            }
            rt_completion_init(&data->wait);
884 885 886
            data->ep_in->request.buffer     = ch;
            data->ep_in->request.size       = res;

还_没_想_好's avatar
还_没_想_好 已提交
887
            data->ep_in->request.req_type   = UIO_REQUEST_WRITE;
888 889

            rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
890

891
            if (rt_completion_wait(&data->wait, VCOM_TX_TIMEOUT) != RT_EOK)
892
            {
893
                RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom tx timeout\n"));
还_没_想_好's avatar
还_没_想_好 已提交
894
            }
895
            if(data->serial.parent.open_flag &
还_没_想_好's avatar
还_没_想_好 已提交
896
#ifndef VCOM_TX_USE_DMA
897
                         RT_DEVICE_FLAG_INT_TX
还_没_想_好's avatar
还_没_想_好 已提交
898
#else
899
                         RT_DEVICE_FLAG_DMA_TX
还_没_想_好's avatar
还_没_想_好 已提交
900
#endif
901 902 903 904 905 906 907
            )
            {
#ifndef VCOM_TX_USE_DMA
                rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DONE);
#else
                rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
#endif
908
                rt_event_send(&data->tx_event, CDC_TX_HAS_SPACE);
909
            }
910
        }
911

912 913
    }
}
M
Ming, Bai 已提交
914

915 916 917 918 919 920
static void rt_usb_vcom_init(struct ufunction *func)
{
    rt_err_t result = RT_EOK;
    struct serial_configure config;
    struct vcom *data = (struct vcom*)func->user_data;
    
M
Ming, Bai 已提交
921
    /* initialize ring buffer */
922
    rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE);
923 924
    rt_ringbuffer_init(&data->tx_ringbuffer, data->tx_rbp, CDC_TX_BUFSIZE);

925
    rt_event_init(&data->tx_event, "vcom", RT_IPC_FLAG_FIFO);
M
Ming, Bai 已提交
926

还_没_想_好's avatar
还_没_想_好 已提交
927 928 929 930 931 932 933
    config.baud_rate    = BAUD_RATE_115200;
    config.data_bits    = DATA_BITS_8;
    config.stop_bits    = STOP_BITS_1;
    config.parity       = PARITY_NONE;
    config.bit_order    = BIT_ORDER_LSB;
    config.invert       = NRZ_NORMAL;
    config.bufsz        = CDC_RX_BUFSIZE;
M
Ming, Bai 已提交
934

还_没_想_好's avatar
还_没_想_好 已提交
935 936 937
    data->serial.ops        = &usb_vcom_ops;
    data->serial.serial_rx  = RT_NULL;
    data->serial.config     = config;
M
Ming, Bai 已提交
938 939

    /* register vcom device */
940
    rt_hw_serial_register(&data->serial, VCOM_DEVICE,
还_没_想_好's avatar
还_没_想_好 已提交
941
#ifndef VCOM_TX_USE_DMA
942
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
还_没_想_好's avatar
还_没_想_好 已提交
943 944 945
#else
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
#endif
946 947 948
                          func);

    /* init usb device thread */
还_没_想_好's avatar
还_没_想_好 已提交
949
    rt_thread_init(&vcom_thread, "vcom",
950
                   vcom_tx_thread_entry, func,
还_没_想_好's avatar
还_没_想_好 已提交
951
                   vcom_thread_stack, VCOM_TASK_STK_SIZE,
952
                   16, 20);
953 954
    result = rt_thread_startup(&vcom_thread);
    RT_ASSERT(result == RT_EOK);       
M
Ming, Bai 已提交
955
}
956 957 958 959
struct udclass vcom_class = 
{
    .rt_usbd_function_create = rt_usbd_function_cdc_create
};
M
Ming, Bai 已提交
960

961 962 963 964 965 966
int rt_usbd_vcom_class_register(void)
{
    rt_usbd_class_register(&vcom_class);
    return 0;
}
INIT_PREV_EXPORT(rt_usbd_vcom_class_register);
967

968
#endif