cdc_vcom.c
26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-10-02 Yi Qiu first version
* 2012-12-12 heyuanjie87 change endpoints and function handler
* 2013-06-25 heyuanjie87 remove SOF mechinism
* 2013-07-20 Yi Qiu do more test
* 2016-02-01 Urey Fix some error
* 2021-10-14 mazhiyuan Fix some error
*/
#include <rthw.h>
#include <rtdevice.h>
#include "drivers/usb_device.h"
#include "cdc.h"
#ifdef RT_USB_DEVICE_CDC
#define VCOM_INTF_STR_INDEX 5
#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*/
#ifdef RT_CDC_RX_BUFSIZE
#define CDC_RX_BUFSIZE RT_CDC_RX_BUFSIZE
#else
#define CDC_RX_BUFSIZE 128
#endif
#define CDC_MAX_PACKET_SIZE 64
#define VCOM_DEVICE "vcom"
#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*/
#ifdef RT_VCOM_TX_USE_DMA
#define VCOM_TX_USE_DMA
#endif /*RT_VCOM_TX_USE_DMA*/
#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
#define _SER_NO_LEN RT_VCOM_SER_LEN
#else /*!RT_VCOM_SER_LEN*/
#define _SER_NO_LEN 14 /*rt_strlen("32021919830108")*/
#endif /*RT_VCOM_SER_LEN*/
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t vcom_thread_stack[VCOM_TASK_STK_SIZE];
static struct rt_thread vcom_thread;
static struct ucdc_line_coding line_coding;
#define CDC_TX_BUFSIZE 1024
#define CDC_BULKIN_MAXSIZE (CDC_TX_BUFSIZE / 8)
#define CDC_TX_HAS_DATE 0x01
#define CDC_TX_HAS_SPACE 0x02
struct vcom
{
struct rt_serial_device serial;
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;
rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
struct rt_ringbuffer rx_ringbuffer;
rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
struct rt_ringbuffer tx_ringbuffer;
struct rt_event tx_event;
};
struct vcom_tx_msg
{
struct rt_serial_device * serial;
const char *buf;
rt_size_t size;
};
ALIGN(4)
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;
CDC_MAX_PACKET_SIZE, //bMaxPacketSize0;
_VENDOR_ID, //idVendor;
_PRODUCT_ID, //idProduct;
USB_BCD_DEVICE, //bcdDevice;
USB_STRING_MANU_INDEX, //iManufacturer;
USB_STRING_PRODUCT_INDEX, //iProduct;
USB_STRING_SERIAL_INDEX, //iSerialNumber;
USB_DYNAMIC, //bNumConfigurations;
};
//FS and HS needed
ALIGN(4)
static struct usb_qualifier_descriptor dev_qualifier =
{
sizeof(dev_qualifier), //bLength
USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
0x0200, //bcdUSB
USB_CLASS_CDC, //bDeviceClass
0x00, //bDeviceSubClass
0x00, //bDeviceProtocol
64, //bMaxPacketSize0
0x01, //bNumConfigurations
0,
};
/* communcation interface descriptor */
ALIGN(4)
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,
0x00,
0x01,
USB_CDC_CLASS_COMM,
USB_CDC_SUBCLASS_ACM,
USB_CDC_PROTOCOL_V25TER,
#ifdef RT_USB_DEVICE_COMPOSITE
VCOM_INTF_STR_INDEX,
#else
0,
#endif
},
/* Header Functional Descriptor */
{
0x05,
USB_CDC_CS_INTERFACE,
USB_CDC_SCS_HEADER,
0x0110,
},
/* Call Management Functional Descriptor */
{
0x05,
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,
},
/* Union Functional Descriptor */
{
0x05,
USB_CDC_CS_INTERFACE,
USB_CDC_SCS_UNION,
USB_DYNAMIC,
USB_DYNAMIC,
},
/* Endpoint Descriptor */
{
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_IN,
USB_EP_ATTR_INT,
0x08,
0xFF,
},
};
/* data interface descriptor */
ALIGN(4)
const static struct ucdc_data_descriptor _data_desc =
{
/* interface descriptor */
{
USB_DESC_LENGTH_INTERFACE,
USB_DESC_TYPE_INTERFACE,
USB_DYNAMIC,
0x00,
0x02,
USB_CDC_CLASS_DATA,
0x00,
0x00,
0x00,
},
/* endpoint, bulk out */
{
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_OUT,
USB_EP_ATTR_BULK,
USB_CDC_BUFSIZE,
0x00,
},
/* endpoint, bulk in */
{
USB_DESC_LENGTH_ENDPOINT,
USB_DESC_TYPE_ENDPOINT,
USB_DYNAMIC | USB_DIR_IN,
USB_EP_ATTR_BULK,
USB_CDC_BUFSIZE,
0x00,
},
};
ALIGN(4)
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)
{
return RT_ERROR;
}
ALIGN(4)
const static char* _ustring[] =
{
"Language",
"RT-Thread Team.",
"RTT Virtual Serial",
serno,
"Configuration",
"Interface",
};
static void rt_usb_vcom_init(struct ufunction *func);
static void _vcom_reset_state(ufunction_t func)
{
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;
/*rt_kprintf("reset USB serial\n", cnt);*/
rt_hw_interrupt_enable(lvl);
}
/**
* This function will handle cdc bulk in endpoint request.
*
* @param func the usb function object.
* @param size request size.
*
* @return RT_EOK.
*/
static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
{
struct vcom *data;
rt_size_t request_size;
RT_ASSERT(func != RT_NULL);
data = (struct vcom*)func->user_data;
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))
{
/* 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. */
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);
return RT_EOK;
}
rt_completion_done(&data->wait);
return RT_EOK;
}
/**
* This function will handle cdc bulk out endpoint request.
*
* @param func the usb function object.
* @param size request size.
*
* @return RT_EOK.
*/
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
{
rt_uint32_t level;
struct vcom *data;
RT_ASSERT(func != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
data = (struct vcom*)func->user_data;
/* ensure serial is active */
if((data->serial.parent.flag & RT_DEVICE_FLAG_ACTIVATED)
&& (data->serial.parent.open_flag & RT_DEVICE_OFLAG_OPEN))
{
/* receive data from USB VCOM */
level = rt_hw_interrupt_disable();
rt_ringbuffer_put(&data->rx_ringbuffer, data->ep_out->buffer, size);
rt_hw_interrupt_enable(level);
/* notify receive data */
rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_RX_IND);
}
data->ep_out->request.buffer = data->ep_out->buffer;
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
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(ufunction_t func, rt_size_t size)
{
RT_ASSERT(func != 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;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_get_line_coding\n"));
data.dwDTERate = 115200;
data.bCharFormat = 0;
data.bDataBits = 8;
data.bParityType = 0;
size = setup->wLength > 7 ? 7 : setup->wLength;
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"));
dcd_ep0_send_status(device->dcd);
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);
RT_DEBUG_LOG(RT_DEBUG_USB, ("_cdc_set_line_coding\n"));
rt_usbd_ep0_read(device, (void*)&line_coding, sizeof(struct ucdc_line_coding),
_cdc_set_line_coding_callback);
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(ufunction_t func, ureq_t setup)
{
struct vcom *data;
RT_ASSERT(func != RT_NULL);
RT_ASSERT(func->device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
data = (struct vcom*)func->user_data;
switch(setup->bRequest)
{
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(func->device, setup);
break;
case CDC_GET_LINE_CODING:
_cdc_get_line_coding(func->device, setup);
break;
case CDC_SET_CONTROL_LINE_STATE:
data->connected = (setup->wValue & 0x01) > 0?RT_TRUE:RT_FALSE;
RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom state:%d \n", data->connected));
dcd_ep0_send_status(func->device->dcd);
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 function, it will be called on handle set configuration request.
*
* @param func the usb function object.
*
* @return RT_EOK on successful.
*/
static rt_err_t _function_enable(ufunction_t func)
{
struct vcom *data;
RT_ASSERT(func != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function enable\n"));
_vcom_reset_state(func);
data = (struct vcom*)func->user_data;
data->ep_out->buffer = rt_malloc(CDC_RX_BUFSIZE);
RT_ASSERT(data->ep_out->buffer != RT_NULL);
data->ep_out->request.buffer = data->ep_out->buffer;
data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
return RT_EOK;
}
/**
* This function will stop cdc function, it will be called on handle set configuration request.
*
* @param func the usb function object.
*
* @return RT_EOK on successful.
*/
static rt_err_t _function_disable(ufunction_t func)
{
struct vcom *data;
RT_ASSERT(func != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc function disable\n"));
_vcom_reset_state(func);
data = (struct vcom*)func->user_data;
if(data->ep_out->buffer != RT_NULL)
{
rt_free(data->ep_out->buffer);
data->ep_out->buffer = RT_NULL;
}
return RT_EOK;
}
static struct ufunction_ops ops =
{
_function_enable,
_function_disable,
RT_NULL,
};
/**
* 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;
#ifdef RT_USB_DEVICE_COMPOSITE
comm->iad_desc.bFirstInterface = cintf_nr;
#endif
return RT_EOK;
}
/**
* This function will create a cdc function instance.
*
* @param device the usb device object.
*
* @return RT_EOK on successful.
*/
ufunction_t rt_usbd_function_cdc_create(udevice_t device)
{
ufunction_t func;
struct vcom* data;
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);
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));
}
#ifdef RT_USB_DEVICE_COMPOSITE
rt_usbd_device_set_interface_string(device, VCOM_INTF_STR_INDEX, _ustring[2]);
#else
/* set usb device string description */
rt_usbd_device_set_string(device, _ustring);
#endif
/* create a cdc function */
func = rt_usbd_function_new(device, &dev_desc, &ops);
/* support HS */
rt_usbd_device_set_qualifier(device, &dev_qualifier);
/* allocate memory for cdc vcom data */
data = (struct vcom*)rt_malloc(sizeof(struct vcom));
RT_ASSERT(data != RT_NULL);
rt_memset(data, 0, sizeof(struct vcom));
func->user_data = (void*)data;
/* initilize vcom */
rt_usb_vcom_init(func);
/* create a cdc communication interface and a cdc data interface */
intf_comm = rt_usbd_interface_new(device, _interface_handler);
intf_data = rt_usbd_interface_new(device, _interface_handler);
/* create a communication alternate setting and a data alternate setting */
comm_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_comm_descriptor));
data_setting = rt_usbd_altsetting_new(sizeof(struct ucdc_data_descriptor));
/* config desc in alternate setting */
rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
(rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
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;
data->ep_cmd = rt_usbd_endpoint_new(&comm_desc->ep_desc, _ep_cmd_handler);
/* add the command endpoint to the cdc communication interface */
rt_usbd_altsetting_add_endpoint(comm_setting, data->ep_cmd);
/* 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 function */
rt_usbd_function_add_interface(func, intf_comm);
/* 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;
}
/**
* UART device in RT-Thread
*/
static rt_err_t _vcom_configure(struct rt_serial_device *serial,
struct serial_configure *cfg)
{
return RT_EOK;
}
static rt_err_t _vcom_control(struct rt_serial_device *serial,
int cmd, void *arg)
{
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_getc(struct rt_serial_device *serial)
{
int result;
rt_uint8_t ch;
rt_uint32_t level;
struct ufunction *func;
struct vcom *data;
func = (struct ufunction*)serial->parent.user_data;
data = (struct vcom*)func->user_data;
result = -1;
level = rt_hw_interrupt_disable();
if(rt_ringbuffer_getchar(&data->rx_ringbuffer, &ch) != 0)
{
result = ch;
}
rt_hw_interrupt_enable(level);
return result;
}
static rt_size_t _vcom_rb_block_put(struct vcom *data, const rt_uint8_t *buf, rt_size_t size)
{
rt_uint32_t level;
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);
}
}
return size;
}
static rt_size_t _vcom_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
struct ufunction *func;
struct vcom *data;
rt_uint32_t send_size = 0;
rt_size_t ptr = 0;
rt_uint8_t crlf[2] = {'\r', '\n',};
func = (struct ufunction*)serial->parent.user_data;
data = (struct vcom*)func->user_data;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(buf != RT_NULL);
RT_DEBUG_LOG(RT_DEBUG_USB, ("%s\n",__func__));
if (data->connected)
{
if((serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
{
while(send_size < size)
{
while(ptr < size && buf[ptr] != '\n')
{
ptr++;
}
if(ptr < size)
{
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++;
ptr++;
}
else if (ptr == size)
{
send_size += _vcom_rb_block_put(data, (const rt_uint8_t *)&buf[send_size], ptr - send_size);
}
else
{
break;
}
}
}
else
{
while (send_size < size)
{
send_size += _vcom_rb_block_put(data, (rt_uint8_t *)&buf[send_size], size - send_size);
}
}
}
else
{
/* recover dataqueue resources */
rt_hw_serial_isr(&data->serial,RT_SERIAL_EVENT_TX_DMADONE);
}
return size;
}
static int _vcom_putc(struct rt_serial_device *serial, char c)
{
rt_uint32_t level;
struct ufunction *func;
struct vcom *data;
func = (struct ufunction*)serial->parent.user_data;
data = (struct vcom*)func->user_data;
RT_ASSERT(serial != RT_NULL);
if (data->connected)
{
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);
}
return 1;
}
static const struct rt_uart_ops usb_vcom_ops =
{
_vcom_configure,
_vcom_control,
_vcom_putc,
_vcom_getc,
_vcom_tx
};
/* Vcom Tx Thread */
static void vcom_tx_thread_entry(void* parameter)
{
rt_uint32_t level;
rt_uint32_t res;
struct ufunction *func = (struct ufunction *)parameter;
struct vcom *data = (struct vcom*)func->user_data;
rt_uint8_t ch[CDC_BULKIN_MAXSIZE];
while (1)
{
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))
)
{
continue;
}
if(!(res & CDC_TX_HAS_DATE))
{
continue;
}
while(rt_ringbuffer_data_len(&data->tx_ringbuffer))
{
level = rt_hw_interrupt_disable();
res = rt_ringbuffer_get(&data->tx_ringbuffer, ch, CDC_BULKIN_MAXSIZE);
rt_hw_interrupt_enable(level);
if(!res)
{
continue;
}
if (!data->connected)
{
if(data->serial.parent.open_flag &
#ifndef VCOM_TX_USE_DMA
RT_DEVICE_FLAG_INT_TX
#else
RT_DEVICE_FLAG_DMA_TX
#endif
)
{
/* drop msg */
#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
}
continue;
}
rt_completion_init(&data->wait);
data->ep_in->request.buffer = ch;
data->ep_in->request.size = res;
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
if (rt_completion_wait(&data->wait, VCOM_TX_TIMEOUT) != RT_EOK)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom tx timeout\n"));
}
if(data->serial.parent.open_flag &
#ifndef VCOM_TX_USE_DMA
RT_DEVICE_FLAG_INT_TX
#else
RT_DEVICE_FLAG_DMA_TX
#endif
)
{
#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
rt_event_send(&data->tx_event, CDC_TX_HAS_SPACE);
}
}
}
}
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;
/* initialize ring buffer */
rt_ringbuffer_init(&data->rx_ringbuffer, data->rx_rbp, CDC_RX_BUFSIZE);
rt_ringbuffer_init(&data->tx_ringbuffer, data->tx_rbp, CDC_TX_BUFSIZE);
rt_event_init(&data->tx_event, "vcom", RT_IPC_FLAG_FIFO);
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;
#if defined(RT_USING_SERIAL_V1)
config.bufsz = CDC_RX_BUFSIZE;
#elif defined(RT_USING_SERIAL_V2)
config.rx_bufsz = CDC_RX_BUFSIZE;
config.tx_bufsz = CDC_TX_BUFSIZE;
#endif
data->serial.ops = &usb_vcom_ops;
data->serial.serial_rx = RT_NULL;
data->serial.config = config;
/* register vcom device */
rt_hw_serial_register(&data->serial, VCOM_DEVICE,
#ifndef VCOM_TX_USE_DMA
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
#else
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,
#endif
func);
/* init usb device thread */
rt_thread_init(&vcom_thread, "vcom",
vcom_tx_thread_entry, func,
vcom_thread_stack, VCOM_TASK_STK_SIZE,
16, 20);
result = rt_thread_startup(&vcom_thread);
RT_ASSERT(result == RT_EOK);
}
struct udclass vcom_class =
{
.rt_usbd_function_create = rt_usbd_function_cdc_create
};
int rt_usbd_vcom_class_register(void)
{
rt_usbd_class_register(&vcom_class);
return 0;
}
INIT_PREV_EXPORT(rt_usbd_vcom_class_register);
#endif