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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
|
/************************************************************************************
* this module is represents the access layer to USB STACK DRIVER
* used to communication over USB protocol
* the USB protocol implementation is inside the module no need for another includes
* ======== USBCDCD.c ========
************************************************************************************/
/* XDCtools Header files */
#include "include.h"
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/gates/GateMutex.h>
#include <ti/sysbios/hal/Hwi.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Task.h>
#include <stdbool.h>
#include <stdint.h>
/* driverlib Header files */
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_uart.h>
#include <driverlib/rom.h>
#include <driverlib/sysctl.h>
#include <driverlib/usb.h>
#include <inc/hw_memmap.h>
/* usblib Header files */
#include <usblib/usb-ids.h>
#include <usblib/usblib.h>
#include <usblib/usbcdc.h>
#include <usblib/device/usbdevice.h>
#include <usblib/device/usbdcdc.h>
#include <usblib/device/usbddfu-rt.h>
#include <usblib/device/usbdcomp.h>
#include "Drivers/USB_Communication/USBCDCD.h"
#include <utils/ustdlib.h>
#include "Common/Utilities/Utils.h"
#include "Common/SWUpdate/FileSystem.h"
#include "Communication/CommunicationTask.h"
#if defined(TIVAWARE)
typedef uint32_t USBCDCDEventType;
#else
#define eUSBModeForceDevice USB_MODE_FORCE_DEVICE
typedef unsigned long USBCDCDEventType;
#endif
unsigned int gateUSBWaitkey,gateRxSerialkey;
/* Defines */
//****************************************************************************
//
// A buffer into which the composite device can write the combined config
// descriptor.
//
//****************************************************************************
#define DESCRIPTOR_BUFFER_SIZE (COMPOSITE_DDFU_SIZE + COMPOSITE_DCDC_SIZE)
extern Semaphore_Handle updateSem;
extern Semaphore_Handle ReconnectSem;
uint8_t g_pui8DescriptorBuffer[DESCRIPTOR_BUFFER_SIZE];
/* Typedefs */
typedef volatile enum {
USBCDCD_STATE_IDLE = 0,
USBCDCD_STATE_INIT,
USBCDCD_STATE_UNCONFIGURED
} USBCDCD_USBState;
/* Static variables and handles */
static volatile USBCDCD_USBState state;
static unsigned char UsbRxBuffer[COMM_MAX_BUFFER_SIZE];
static unsigned char transmitBuffer[COMM_MAX_BUFFER_SIZE];
int expected_message_size,keep_expected_message_size;
int current_message_size;
static volatile uint32_t g_RxCount;
//static GateMutex_Handle gateTxSerial;
static GateMutex_Handle gateRxSerial;
static GateMutex_Handle gateUSBWait;
//static Semaphore_Handle semTxSerial;
//static Semaphore_Handle semRxSerial;
static Semaphore_Handle semUSBConnected;
extern Semaphore_Handle initConnectionSem;
#define FLAG_STATUS_UPDATE 0
#define FLAG_USB_CONFIGURED 1
#define FLAG_SENDING_BREAK 2
static volatile uint32_t g_ui32Flags;
/* Function prototypes */
/*
static USBCDCDEventType cbRxHandler(void *cbData, USBCDCDEventType event,
USBCDCDEventType _eventMsgData,
void *eventMsgPtr);
static USBCDCDEventType cbSerialHandler(void *cbData, USBCDCDEventType event,
USBCDCDEventType _eventMsgData,
void *eventMsgPtr);
static USBCDCDEventType cbTxHandler(void *cbData, USBCDCDEventType event,
USBCDCDEventType _eventMsgData,
void *eventMsgPtr);
*/
uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData);
uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData);
uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData);
void USBCDCD_hwiHandler(UArg arg0);
void USBCDCD_init(void);
unsigned int USBCDCD_receiveData(unsigned char *_pBuff,
unsigned int _length,
unsigned int _timeout);
bool USBCDCD_waitForConnect(unsigned int _timeout);
uint32_t DFUDetachCallback(void *pvCBData, uint32_t ui32Event,
uint32_t ui32MsgData, void *pvMsgData);
/* The languages supported by this device. */
const unsigned char langDescriptor[] = {
4,
USB_DTYPE_STRING,
USBShort(USB_LANG_EN_US)
};
/* The manufacturer string. */
const unsigned char manufacturerString[] = {
(17 + 1) * 2,
USB_DTYPE_STRING,
'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0,
't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0,
};
/* The product string. */
const unsigned char productString[] = {
2 + (15 * 2),
USB_DTYPE_STRING,
'T', 0, 'w', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'T', 0, 'a', 0,
'n', 0, 'g', 0, 'o', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0,
};
/* The serial number string. */
const unsigned char serialNumberString[] = {
(8 + 1) * 2,
USB_DTYPE_STRING,
'2', 0, '2', 0, '2', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0
};
/* The interface description string. */
const unsigned char controlInterfaceString[] = {
2 + (21 * 2),
USB_DTYPE_STRING,
'J', 0, 'i', 0, 'g', 0, ' ', 0, 'C', 0, '2', 0, 'n', 0, 't', 0,
'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0,
'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0
};
/* The configuration description string. */
const unsigned char configString[] = {
2 + (26 * 2),
USB_DTYPE_STRING,
'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0,
'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0,
'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0,
'o', 0, 'n', 0
};
//*****************************************************************************
//
// The descriptor string table.
//
//*****************************************************************************
const uint8_t * const g_ppui8StringDescriptors[] =
{
langDescriptor,
manufacturerString,
productString,
serialNumberString,
controlInterfaceString,
configString
};
#define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \
sizeof(g_ppui8StringDescriptors[0]))
/* The descriptor string table. */
const unsigned char * const stringDescriptors[] = {
langDescriptor,
manufacturerString,
productString,
serialNumberString,
controlInterfaceString,
configString
};
#define STRINGDESCRIPTORSCOUNT (sizeof(stringDescriptors) / \
sizeof(unsigned char *))
tUSBBuffer txBuffer;
tUSBBuffer rxBuffer;
static tUSBDCDCDevice g_sCDCDevice;
tUSBBuffer rxBuffer =
{
false, /* This is a receive buffer. */
RxHandler, /* pfnCallback */
(void *)&g_sCDCDevice, /* Callback data is our device pointer. */
USBDCDCPacketRead, /* pfnTransfer */
USBDCDCRxPacketAvailable, /* pfnAvailable */
(void *)&g_sCDCDevice, /* pvHandle */
UsbRxBuffer, /* pcBuffer */
COMM_MAX_BUFFER_SIZE, /* ulBufferSize */
{{0, 0, 0, 0}, 0, 0} /* private data workspace */
};
tUSBBuffer txBuffer =
{
true, /* This is a transmit buffer. */
TxHandler, /* pfnCallback */
(void *)&g_sCDCDevice, /* Callback data is our device pointer. */
USBDCDCPacketWrite, /* pfnTransfer */
USBDCDCTxPacketAvailable, /* pfnAvailable */
(void *)&g_sCDCDevice, /* pvHandle */
transmitBuffer, /* pcBuffer */
COMM_MAX_BUFFER_SIZE, /* ulBufferSize */
{{0, 0, 0, 0}, 0, 0} /* private data workspace */
};
static tUSBDCDCDevice g_sCDCDevice =
{
USB_VID_TI_1CBE,
USB_PID_SERIAL,
0,
USB_CONF_ATTR_SELF_PWR,
ControlHandler,
(void *)&g_sCDCDevice,
USBBufferEventCallback,
(void *)&rxBuffer,
USBBufferEventCallback,
(void *)&txBuffer,
stringDescriptors,
STRINGDESCRIPTORSCOUNT
};
//*****************************************************************************
//
// The DFU runtime interface initialization and customization structures
//
//*****************************************************************************
tUSBDDFUDevice g_sDFUDevice =
{
DFUDetachCallback,
(void *)&g_sDFUDevice
};
//****************************************************************************
//
// The number of device class instances that this composite device will
// use.
//
//****************************************************************************
#define NUM_DEVICES 2
//****************************************************************************
//
// The array of devices supported by this composite device.
//
//****************************************************************************
tCompositeEntry g_psCompDevices[NUM_DEVICES];
//****************************************************************************
//
// Additional workspace required by the composite driver to hold a lookup
// table allowing mapping of composite interface and endpoint numbers to
// individual device class instances.
//
//****************************************************************************
uint32_t g_pui32CompWorkspace[NUM_DEVICES];
//****************************************************************************
//
// The instance data for this composite device.
//
//****************************************************************************
tCompositeInstance g_sCompInstance;
//****************************************************************************
//
// Allocate the Device Data for the top level composite device class.
//
//****************************************************************************
tUSBDCompositeDevice g_sCompDevice =
{
//
// Stellaris VID.
//
USB_VID_TI_1CBE,
//
// Stellaris PID for composite SERIAL/DFU device.
//
USB_PID_COMP_SERIAL,
//
// This is in milliamps.
//
250,
//
// Bus powered device.
//
USB_CONF_ATTR_BUS_PWR,
//
// Device event handler function pointer (receives connect, disconnect
// and other device-level notifications).
//
ControlHandler,
//
// The string table.
//
g_ppui8StringDescriptors,
NUM_STRING_DESCRIPTORS,
//
// The Composite device array.
//
NUM_DEVICES,
g_psCompDevices,
};
//static tLineCoding g_sLineCoding = {
// 115200, /* 115200 baud rate. */
// 1, /* 1 Stop Bit. */
// 0, /* No Parity. */
// 8 /* 8 Bits of data. */
//};
bool USB_Reinit = false;
//*****************************************************************************
//
//! Waits for a character from the USB port.
//!
//! This function gets a character from the USB receive buffer.
//! If there are no characters available, this function waits until a
//! character is received before returning.
//!
//! \return Returns the character read from the USB port.
//
//*****************************************************************************
/*char USBGetChar(void)
{
uint8_t ucChar;
int len = 0;
len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER);
if (len !=1)
{
Task_sleep(1);
len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER);
}
//
// Now return the char.
//
return (ucChar);
}
*/
void USBFlush(void)
{
USBBufferFlush(&rxBuffer);
}
//*****************************************************************************
//
// Set the state of the RS232 RTS and DTR signals.
//
//*****************************************************************************
void SetControlLineState(uint16_t ui16State)
{
//
// TODO: If configured with GPIOs controlling the handshake lines,
// set them appropriately depending upon the flags passed in the wValue
// field of the request structure passed.
//
}
//*****************************************************************************
//
// Get the communication parameters in use on the UART.
//
//*****************************************************************************
void GetLineCoding(tLineCoding *psLineCoding)
{
psLineCoding->ui32Rate = 9600;
psLineCoding->ui8Databits = 8;
psLineCoding->ui8Parity = USB_CDC_PARITY_NONE;
psLineCoding->ui8Stop = USB_CDC_STOP_BITS_1;
}
/********************************************************************
* ======== USBCDCD_hwiHandler ========
* This function calls the USB library's device interrupt handler.
********************************************************************/
void USBCDCD_hwiHandler(UArg arg0)
{
USB0DeviceIntHandler();
}
//*****************************************************************************
//
// This is the callback from the USB DFU runtime interface driver.
//
// \param pvCBData is ignored by this function.
// \param ui32Event is one of the valid events for a DFU device.
// \param ui32MsgParam is defined by the event that occurs.
// \param pvMsgData is a pointer to data that is defined by the event that
// occurs.
//
// This function will be called to inform the application when a change occurs
// during operation as a DFU device. Currently, the only event passed to this
// callback is USBD_DFU_EVENT_DETACH which tells the recipient that they should
// pass control to the boot loader at the earliest, non-interrupt context
// point.
//
// \return This function will return 0.
//
//*****************************************************************************
uint32_t
DFUDetachCallback(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgData,
void *pvMsgData)
{
if(ui32Event == USBD_DFU_EVENT_DETACH)
{
//
// Set the flag that the main loop uses to determine when it is time
// to transfer control back to the boot loader. Note that we
// absolutely DO NOT call USBDDFUUpdateBegin() here since we are
// currently in interrupt context and this would cause bad things to
// happen (and the boot loader to not work).
//
//
// Release updateSem
//
UpdateFlag = true;
Semaphore_post(updateSem);
}
return(0);
}
#define MAX_USB_LOG 100
//uint16_t UsbEventId[MAX_USB_LOG+1] = {0};
//uint32_t UsbTime[MAX_USB_LOG+1] = {0};
//uint16_t Usbindex = 0;
//*****************************************************************************
//
// Handles CDC driver notifications related to control and setup of the device.
//
// \param pvCBData is the client-supplied callback pointer for this channel.
// \param ulEvent identifies the event we are being notified about.
// \param ulMsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to perform control-related
// operations on behalf of the USB host. These functions include setting
// and querying the serial communication parameters, setting handshake line
// states and sending break conditions.
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t USBConn = 0, USBDisc = 0;
uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData)
{
/*UsbEventId[Usbindex] = ui32Event;
UsbTime[Usbindex] = msec_millisecondCounter;
if (Usbindex++ >= MAX_USB_LOG)
Usbindex = 0;*/
//
// Which event are we being asked to process?
//
switch(ui32Event)
{
//
// We are connected to a host and communication is now possible.
//
case USB_EVENT_CONNECTED:
{
//
// Now connected and ready for normal operation.
//
HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 1;
SetCommunicationPath(isUSB);
USBConn++;
//
// Flush our buffers.
//
USBBufferFlush(&txBuffer);
USBBufferFlush(&rxBuffer);
//TODO: Notify connection!
//
// Set the command status update flag.
//
HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1;
break;
}
//
// The host has disconnected.
//
case USB_EVENT_DISCONNECTED:
{
//
// No longer connected.
//
HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 0;
//TODO: Notify disconnection!
USBDisc++;
//
// Set the command status update flag.
//
HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1;
break;
}
//
// Return the current serial communication parameters.
//
case USBD_CDC_EVENT_GET_LINE_CODING:
{
GetLineCoding(pvMsgData);
break;
}
//
// Set the current serial communication parameters.
//
case USBD_CDC_EVENT_SET_LINE_CODING:
{
GetLineCoding(pvMsgData);
break;
}
//
// Set the current serial communication parameters.
//
case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE:
{
SetControlLineState((uint16_t)ui32MsgValue);
break;
}
//
// Send a break condition on the serial line.
//
case USBD_CDC_EVENT_SEND_BREAK:
{
break;
}
//
// Clear the break condition on the serial line.
//
case USBD_CDC_EVENT_CLEAR_BREAK:
{
break;
}
//
// Ignore SUSPEND and RESUME for now.
//
case USB_EVENT_SUSPEND:
case USB_EVENT_RESUME:
{
break;
}
//
// We don't expect to receive any other events. Ignore any that show
// up in a release build or hang in a debug build.
//
default:
{
//#ifdef DEBUG
// while(1);
//#else
break;
//#endif
}
}
return(0);
}
uint32_t buffId = 0xFF;
/*#define MAX_USB_LOG 3
uint32_t ULength[MAX_USB_LOG+1] = {0};
uint32_t UDataLength[MAX_USB_LOG] = {0};
byte URxIndex = 0;
*/
uint8_t size[4];
int size_bar = 0;
void handleRx(void)
{
uint32_t ui32Read;
uint8_t ui8Char;
if (expected_message_size == 0)
{
while (size_bar < 4)
{
ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1);
if(ui32Read)
{
size[size_bar++] = ui8Char;
}
//else
// return;
}
if (size_bar == 4)
{
expected_message_size = *(int *)size;
size_bar = 0;
//ULength[URxIndex] = expected_message_size;
if (expected_message_size)
{
buffId = initArray( expected_message_size);
if (buffId == 0xFF)
{
//LOG_ERROR(expected_message_size,"usb message length error");
Report("usb message length error", __FILE__, __LINE__, expected_message_size, RpWarning, (int)buffId, 0);
keep_expected_message_size = expected_message_size;
expected_message_size = 0;
size_bar = 0;
FileChunkUploadError();
}
}
}
}
if (expected_message_size)
{
do
{
ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1);
// Did we get a character?
if(ui32Read)
{
if (insertArray(buffId,ui8Char) == false)
{
expected_message_size = 0;
current_message_size = 0;
LOG_ERROR(buffId,"usb message aborted");
ui32Read = 0;
break;
}
current_message_size++;
//UDataLength[URxIndex] = current_message_size;
}
if (current_message_size >= expected_message_size)
{
g_RxCount += current_message_size;
/*UDataLength[URxIndex] = current_message_size;
if (URxIndex++>= MAX_USB_LOG)
URxIndex = 0;*/
CommunicationTaskMessageReceived(buffId,current_message_size);
expected_message_size = 0;
current_message_size = 0;
size_bar = 0;
break;
}
} while(ui32Read);
}
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the transmit channel (data to
// the USB host).
//
// \param pvCBData is the client-supplied callback pointer for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to notify us of any events
// related to operation of the transmit data channel (the IN channel carrying
// data to the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData)
{
//
// Which event have we been sent?
//
switch(ui32Event)
{
case USB_EVENT_TX_COMPLETE:
{
//
// Since we are using the USBBuffer, we don't need to do anything
// here.
//
break;
}
//
// We don't expect to receive any other events. Ignore any that show
// up in a release build or hang in a debug build.
//
default:
{
#ifdef DEBUG
while(1);
#else
break;
#endif
}
}
return(0);
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the receive channel (data from
// the USB host).
//
// \param pvCBData is the client-supplied callback data value for this channel.
// \param ui32Event identifies the event we are being notified about.
// \param ui32MsgValue is an event-specific value.
// \param pvMsgData is an event-specific pointer.
//
// This function is called by the CDC driver to notify us of any events
// related to operation of the receive data channel (the OUT channel carrying
// data from the USB host).
//
// \return The return value is event-specific.
//
//*****************************************************************************
uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData)
{
//
// Which event are we being sent?
//
switch(ui32Event)
{
//
// A new packet has been received.
//
case USB_EVENT_RX_AVAILABLE:
{
//
// Feed some characters into the UART TX FIFO and enable the
// interrupt so we are told when there is more space.
//
handleRx();
break;
}
//
// We are being asked how much unprocessed data we have still to
// process. We return 0 if the UART is currently idle or 1 if it is
// in the process of transmitting something. The actual number of
// bytes in the UART FIFO is not important here, merely whether or
// not everything previously sent to us has been transmitted.
//
case USB_EVENT_DATA_REMAINING:
{
//
// Get the number of bytes in the buffer and add 1 if some data
// still has to clear the transmitter.
return(0);
}
//
// We are being asked to provide a buffer into which the next packet
// can be read. We do not support this mode of receiving data so let
// the driver know by returning 0. The CDC driver should not be sending
// this message but this is included just for illustration and
// completeness.
//
case USB_EVENT_REQUEST_BUFFER:
{
return(0);
}
//
// We don't expect to receive any other events. Ignore any that show
// up in a release build or hang in a debug build.
//
default:
#ifdef DEBUG
while(1);
#else
break;
#endif
}
return(0);
}
void * USBDComposite = NULL;
/*******************************************************************
* ======== USBCDCD_init ========
*******************************************************************/
void USBCDCD_init(void)
{
Error_Block eb;
Error_init(&eb);
/* Install interrupt handler */
Hwi_Handle hwi;
hwi = Hwi_create(INT_USB0, USBCDCD_hwiHandler, NULL, &eb);
if (hwi == NULL)
{
System_abort("Can't create USB Hwi");
}
/* State specific variables */
state = USBCDCD_STATE_UNCONFIGURED;
/*added lines for drivers version 4.178*/
uint32_t g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |SYSCTL_OSC_MAIN |SYSCTL_USE_PLL |SYSCTL_CFG_VCO_480), 120000000);
USBDCDFeatureSet(0, USBLIB_FEATURE_CPUCLK, &g_ui32SysClock);
//uint32_t ui32PLLRate;
//SysCtlVCOGet(SYSCTL_XTAL_25MHZ, &ui32PLLRate);
//USBDCDFeatureSet(0, USBLIB_FEATURE_USBPLL, &ui32PLLRate);
/* Set the USB stack mode to Device mode with VBUS monitoring */
USBStackModeSet(0, eUSBModeForceDevice, 0);
//should be done here only once for supporting firmware upgrade as composite devise
//if done several times in different places doesn't work in the upgrade.
USBBufferInit(&txBuffer);
USBBufferInit(&rxBuffer);
if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0])))
{
System_abort("Error initializing the serial device");
}
if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1])))
{
System_abort("Error initializing the DFU device");
}
//
// Pass the USB library our device information, initialize the USB
// controller and connect the device to the bus.
//
g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200;
USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer);
if (!USBDComposite)
{
System_abort("Error initializing the composite device");
}
/*
//if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0])))
USBDComposite = USBDCDCInit(0, &g_sCDCDevice);
if (!USBDComposite)
{
System_abort("Error initializing the serial device");
}
*/
}
//-----------------------------------------------------------
void USBCDCD_Reinit(void)
{
USB_Reinit = true;
USBDCompositeTerm(USBDComposite);
/* State specific variables */
state = USBCDCD_STATE_UNCONFIGURED;
if (gateRxSerialkey)
GateMutex_leave(gateRxSerial, gateRxSerialkey);
if (gateUSBWaitkey)
GateMutex_leave(gateUSBWait, gateUSBWaitkey);
/* Set the USB stack mode to Device mode with VBUS monitoring */
//USBStackModeSet(0, eUSBModeForceDevice, 0);
HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 0;
//TODO: Notify disconnection!
USBDisc++;
//
// Set the command status update flag.
//
HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1;
USBBufferInit(&txBuffer);
USBBufferInit(&rxBuffer);
USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer);
if (!USBDComposite)
{
System_abort("Error initializing the composite device");
}
USB_Reinit = false;
}
//-----------------------------------------------------------
/**************************************************************
* ======== USBCDCD_receiveData ========
************************************************************* */
unsigned int USBCDCD_receiveData(unsigned char *_pBuff,
unsigned int _length,
unsigned int _timeout)
{
unsigned int retValue = 0;
switch (state)
{
case USBCDCD_STATE_UNCONFIGURED:
{
USBCDCD_waitForConnect(_timeout);
break;
}
case USBCDCD_STATE_INIT:
{
/* Acquire lock */
gateRxSerialkey = GateMutex_enter(gateRxSerial);
state = USBCDCD_STATE_IDLE;
handleRx();
//retValue = rxData(_pBuff, _length, _timeout);
/* Release lock */
GateMutex_leave(gateRxSerial, gateRxSerialkey);
break;
}
case USBCDCD_STATE_IDLE:
{
/* Acquire lock */
gateRxSerialkey = GateMutex_enter(gateRxSerial);
handleRx();
//retValue = rxData(_pBuff, _length, _timeout);
/* Release lock */
GateMutex_leave(gateRxSerial, gateRxSerialkey);
break;
}
default:
break;
}
return (retValue);
}
/*******************************************************
* ======== USBCDCD_sendData ========
*******************************************************/
unsigned int USBCDCD_sendData(const unsigned char *_pBuff,
unsigned int _length,
unsigned int _timeout)
{
//int len = 0;
uint8_t size[4];
size[3] = (_length>>24) & 0xFF;
size[2] = (_length>>16) & 0xFF;
size[1] = (_length>>8) & 0xFF;
size[0] = _length & 0xFF;
if (USB_Reinit ==true)
return OK;
/*len = */USBBufferWrite((tUSBBuffer *)&txBuffer, size, 4);
/*if (len == 0)
{
USBRingBufFlush(&(txBuffer.sPrivateData.sRingBuf));
len = USBBufferWrite((tUSBBuffer *)&txBuffer, size, 4);
}*/
return USBBufferWrite((tUSBBuffer *)&txBuffer, (uint8_t*)_pBuff, _length);
}
/************************************************
* ======== USBCDCD_waitForConnect ========
*************************************************/
bool USBCDCD_waitForConnect(unsigned int _timeout)
{
bool ret = true;
// Need exclusive access to prevent a race condition
gateUSBWaitkey = GateMutex_enter(gateUSBWait);
if (state == USBCDCD_STATE_UNCONFIGURED)
{
if (!Semaphore_pend(semUSBConnected, _timeout))
{
ret = false;
}
}
GateMutex_leave(gateUSBWait, gateUSBWaitkey);
return (ret);
}
|