/
usb_handlers.c
482 lines (442 loc) · 14.1 KB
/
usb_handlers.c
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
/*
* usb_handlers.c
*
* Created on: Mar 1, 2014
* Author: Dan
*/
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_uart.h"
#include "inc/hw_sysctl.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "driverlib/usb.h"
#include "driverlib/rom.h"
#include "usblib/usblib.h"
#include "usblib/usbcdc.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdcdc.h"
#include "utils/ustdlib.h"
#include "utils/uartstdio.h"
#include "usb_structs.h"
#include "usb_handlers.h"
#include "main.h"
#include "String.h"
#include "proto_defs.h"
#include "stdio.h"
#include "stdlib.h"
//*****************************************************************************
//
// 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.
//
//*****************************************************************************
//*****************************************************************************
//
// Global variables
//
//*****************************************************************************
unsigned char g_ucReadMode = WAITING;
unsigned long g_ulCharIndex = 0;
unsigned long g_ulParamIndex = 0;
unsigned long g_ulReceiveMode = REC_MODE_NORMAL;
unsigned char *g_strFuncName;
unsigned long g_ulParams[N_PARAMS];
unsigned char g_strAssembler[MAX_STR_LEN];
void (*g_ptrStreamFcn)(unsigned char); //Pointer to stream function
void USB_resetPacketAssembler(void){
g_ucReadMode = WAITING;
g_ulCharIndex = 0;
g_ulParamIndex = 0;
g_ulReceiveMode = REC_MODE_NORMAL;
#ifdef __DEBUG__
UARTprintf("\nTx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer));
UARTprintf("\nRx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sRxBuffer));
UARTprintf("\nCLEARING USB BUFFERS...");
#endif
USBBufferFlush((tUSBBuffer *)&g_sTxBuffer);
USBBufferFlush((tUSBBuffer *)&g_sRxBuffer);
#ifdef __DEBUG__
UARTprintf("\nTx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer));
UARTprintf("\nRx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sRxBuffer));
#endif
}
void GetLineCoding(tLineCoding *psLineCoding)
{
psLineCoding->ulRate = 9600;
psLineCoding->ucDatabits = 8;
psLineCoding->ucParity = USB_CDC_PARITY_NONE;
psLineCoding->ucStop = USB_CDC_STOP_BITS_1;
}
unsigned long
USB_ControlHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgValue, void *pvMsgData)
{
//
// Which event are we being asked to process?
//
switch(ulEvent)
{
//
// We are connected to a host and communication is now possible.
//
case USB_EVENT_CONNECTED:
g_bUSBConfigured = true;
// Flush our buffers.
USBBufferFlush(&g_sTxBuffer);
USBBufferFlush(&g_sRxBuffer);
#ifdef __DEBUG__
UARTprintf("\nUSB Connected");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
break;
//
// The host has disconnected.
//
case USB_EVENT_DISCONNECTED:
g_bUSBConfigured = false;
#ifdef __DEBUG__
UARTprintf("\nUSB Disconnected");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
break;
//
// Return the current serial communication parameters.
//
case USBD_CDC_EVENT_GET_LINE_CODING:
GetLineCoding(pvMsgData);
break;
case USBD_CDC_EVENT_SET_LINE_CODING:
break;
case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE:
break;
case USBD_CDC_EVENT_SEND_BREAK:
break;
case USBD_CDC_EVENT_CLEAR_BREAK:
break;
case USB_EVENT_SUSPEND:
break;
case USB_EVENT_RESUME:
break;
default:
#ifdef DEBUG
#else
break;
#endif
}
return(0);
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the transmit channel (data to
// the USB host).
//
// \param ulCBData 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 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.
//
//*****************************************************************************
unsigned long
USB_TxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue,
void *pvMsgData)
{
//
// Which event have we been sent?
//
switch(ulEvent)
{
case USB_EVENT_TX_COMPLETE:
//
// Increment Packet Counter
g_ulUSBTxCount ++;
#ifdef __DEBUG__
UARTprintf("\nUSB Tx Complete");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
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:
break;
}
return(0);
}
//*****************************************************************************
//
// Handles CDC driver notifications related to the receive channel (data from
// the USB host).
//
// \param ulCBData is the client-supplied callback data value 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 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.
//
//*****************************************************************************
unsigned long
USB_RxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue,
void *pvMsgData)
{
unsigned long ulCount;
unsigned char ucCharArr[USB_RX_LENGTH];
unsigned char ucBytesRead;
unsigned char nextByte;
//
// Which event are we being sent?
//
switch(ulEvent)
{
//
// A new packet has been received.
//
case USB_EVENT_RX_AVAILABLE:
{
//Increment Packet Counter
g_ulUSBRxCount++;
if(g_ulReceiveMode == REC_MODE_STREAM){
//Buffer reading handled in stream interrupt
#ifdef __DEBUG__
UARTprintf("\nUSB Stream Mode Rx Event");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
}
else{
//allocate packet array
ucBytesRead = USBBufferRead((tUSBBuffer *)&g_sRxBuffer, ucCharArr, USB_RX_LENGTH-1);
ucCharArr[ucBytesRead] = NULL; //Null terminate string
#ifdef __DEBUG__
UARTprintf("\nUSB Buffer Dump (length = %d):\n%s\n",ucBytesRead, ucCharArr);
#endif
for(ulCount=0;ulCount<ucBytesRead;ulCount++){
nextByte = ucCharArr[ulCount];
#ifdef __DEBUG__
#ifdef __DEBUG_USB_EVERYCHAR__
UARTprintf("\nUSB nextByte = %c, ",nextByte);
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
#endif
//Ignore non-printing and whitespace characters
if(nextByte <= ' '){
#ifdef __DEBUG__
UARTprintf("\nUSB Ignored Character = %c, ",nextByte);
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
continue;
}
if(nextByte == '#'){
if(g_ucReadMode == COMMENT){
g_ucReadMode = WAITING;
g_ulCharIndex = 0;
g_ulParamIndex = 0;
#ifdef __DEBUG__
UARTprintf("\ng_ucReadMode = FUNCTION");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
continue;
}
else{
g_ucReadMode = COMMENT;
#ifdef __DEBUG__
UARTprintf("\ng_ucReadMode = COMMENT");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
continue;
}
}
switch(g_ucReadMode){
case WAITING:
if(nextByte < 'A' || nextByte > 'Z'){
continue;
}
else{
g_ucReadMode = FUNCTION;
}
//DON'T PUT A BREAK HERE!
case FUNCTION:
if(nextByte == '('){ //end of Function section
g_strAssembler[g_ulCharIndex] = NULL; //Terminate string
g_strFuncName = malloc(strlen((char *)g_strAssembler) + 1); //allocate memory for function name
strcpy((char *)g_strFuncName,(char *)g_strAssembler); //Copy function name
g_ulCharIndex = 0;
g_ucReadMode = PARAMETER;
#ifdef __DEBUG__
#ifdef __DEBUG_USB_EVERYCHAR__
UARTprintf("\ng_ucReadMode = PARAMETER");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
#endif
continue;
}
else{
g_strAssembler[g_ulCharIndex] = nextByte;
g_ulCharIndex ++;
if(g_ulCharIndex > MAX_FUNC_LENGTH){ //Error Checking
g_ucReadMode = WAITING;
g_ulCharIndex = 0;
g_ulParamIndex = 0;
#ifdef __DEBUG__
UARTprintf("\nFUNCTION LENGTH EXCEEDED, g_ucReadMode = WAITING");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
continue;
}
}
break;
case PARAMETER:
if(nextByte == ')'){ //end of input
g_strAssembler[g_ulCharIndex] = NULL;
g_ulParams[g_ulParamIndex] = str2ul(g_strAssembler);
if(g_ulParamIndex > 0){
g_ulParamIndex ++;
}
parseUSB(g_strFuncName,g_ulParams,g_ulParamIndex);
g_ucReadMode = WAITING;
g_ulCharIndex = 0;
g_ulParamIndex = 0;
free(g_strFuncName);
#ifdef __DEBUG__
#ifdef __DEBUG_USB_EVERYCHAR__
UARTprintf("\ng_ucReadMode = WAITING");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
#endif
continue;
}
else{
if(nextByte == ' ' || nextByte == ','){ //next parameter
g_strAssembler[g_ulCharIndex] = NULL;
g_ulParams[g_ulParamIndex] = str2ul(g_strAssembler);
g_ulParamIndex ++;
g_ulCharIndex = 0;
continue;
}
else{
g_strAssembler[g_ulCharIndex] = nextByte;
g_ulCharIndex ++;
if(g_ulCharIndex > MAX_PARAM_LENGTH){ //Error Checking
g_ucReadMode = WAITING;
g_ulCharIndex = 0;
g_ulParamIndex = 0;
#ifdef __DEBUG__
#ifdef __DEBUG_USB_EVERYCHAR__
UARTprintf("\nPARAMEtER LENGTH EXCEEDED, g_ucReadMode = WAITING");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
#endif
continue;
}
}
}
break;
default:
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:
{
//
// For now just return 0
//
#ifdef __DEBUG__
UARTprintf("\nUSB_EVENT_DATA_REMAINING");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
ulCount = 0;
return(ulCount);
}
//
// 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:
{
#ifdef __DEBUG__
UARTprintf("\nUSB_EVENT_REQUEST_BUFFER");
#ifdef __DEBUG_TRACEBACK__
UARTprintf(" File: %s Line: %d", __FILE__,__LINE__);
#endif
#endif
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:
return(0);
}
}
void USBSetReceiveMode(unsigned long mode){
g_ulReceiveMode = mode;
}