Repository navigation
Expand file tree
/
Copy pathUSBStickModule.c
More file actions
619 lines (454 loc) · 16 KB
/
Copy pathUSBStickModule.c
File metadata and controls
619 lines (454 loc) · 16 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
#include "USBStickModule.h"
//only for ccs!!
#pragma DATA_ALIGN(g_psDMAControlTable, 1024)
tDMAControlTable g_psDMAControlTable[6];
// USBSticks HCD pool and buffer
uint8_t g_pui8HCDPool[USBSTICK_HCD_MEMORY_SIZE];
// The instance data for the MSC driver.
tUSBHMSCInstance *g_psMSCInstance = 0;
// Declare the USB Events driver interface.
DECLARE_EVENT_DRIVER(g_sUSBEventDriver, 0, 0, USBHCDEvents);
// The global that holds all of the host drivers in use in the application.
// In this case, only the MSC class is loaded.
static tUSBHostClassDriver const * const g_ppHostClassDrivers[] = {
&g_sUSBHostMSCClassDriver, &g_sUSBEventDriver };
// This global holds the number of class drivers in the g_ppHostClassDrivers
// list.
static const uint32_t g_ui32NumHostClassDrivers = sizeof(g_ppHostClassDrivers)
/ sizeof(tUSBHostClassDriver *);
//*****************************************************************************
//
// This is the generic callback from host stack.
//
// pvData is actually a pointer to a tEventInfo structure.
//
// This function will be called to inform the application when a USB event has
// occurred that is outside those related to the mass storage device. At this
// point this is used to detect unsupported devices being inserted and removed.
// It is also used to inform the application when a power fault has occurred.
// This function is required when the g_USBGenericEventDriver is included in
// the host controller driver array that is passed in to the
// USBHCDRegisterDrivers() function.
//
//
//*****************************************************************************
void USBHCDEvents(void *pvData) {
tEventInfo *psEventInfo;
//
// Cast this pointer to its actual type.
//
psEventInfo = (tEventInfo *) pvData;
//
// Process each kind of event
//
switch (psEventInfo->ui32Event) {
//
// An unknown device has been connected.
//
case USB_EVENT_UNKNOWN_CONNECTED: {
//
// An unknown device was detected.
//
_deviceState = eSTATE_UNKNOWN_DEVICE;
break;
}
//
// The unknown device has been been unplugged.
//
case USB_EVENT_DISCONNECTED: {
//
// Unknown device has been removed.
//
_deviceState = eSTATE_NO_DEVICE;
break;
}
//
// A bus power fault was detected.
//
case USB_EVENT_POWER_FAULT: {
//
// No power means no device is present.
//
_deviceState = eSTATE_POWER_FAULT;
break;
}
default: {
break;
}
}
}
char _USBStickBuffer[USBSTICK_BUFFER_SIZE];
void USBStick_Construct(ClassUSBStick * usbStickModule, uint32_t refreshRate) {
usbStickModule->_USBEnable = false;
usbStickModule->_fileOpened = false;
usbStickModule->_cycleCnt = 0;
USBStick_SetRefreshRate(usbStickModule, refreshRate);
USBStick_Config(usbStickModule);
usbStickModule->_varsNum = 0;
}
void USBStick_Config(ClassUSBStick * usbStickModule) {
// Enable USB
SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
SysCtlUSBPLLEnable();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1); //id and vbus
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5); //dp and dm
IntPrioritySet(USB_DMA_INT_CH6, USBSTICK_INT_PRIORITY);
//
// Initialize the USB stack mode and pass in a mode callback.
//
USBStackModeSet(0, eUSBModeHost, 0);
//
// Initialize the stack to be used with USB stick.
//
StickInit(usbStickModule);
USBHCDInit(0, g_pui8HCDPool, USBSTICK_HCD_MEMORY_SIZE);
//TODO IMPLEMENT REMAINING METHODS
// ComHandler_Construct(&usbStickModule->_USBStickHandler,
// (void*) usbStickModule, USBStick_PrintStreamFlush,
// USBStick_IsEnable, USBStick_GetRefreshRate, USBStick_Start,
// USBStick_Stop,USBStick_PrintSignalStreamFlush);
///////////////////
}
void StickInit(ClassUSBStick * usbStickModule) {
//
// Enable the uDMA controller and set up the control table base.
// The uDMA controller is used by the USB library.
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
MAP_uDMAEnable();
MAP_uDMAControlBaseSet(g_psDMAControlTable);
//
// Initially wait for device connection.
//
_deviceState = eSTATE_NO_DEVICE;
//
// Register the host class drivers.
//
USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);
//
// Open an instance of the mass storage class driver.
//
g_psMSCInstance = USBHMSCDriveOpen(0, MSCCallback);
//
// Initialize the power configuration. This sets the power enable signal
// to be active high and does not enable the power fault.
//
USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
//
// Run initial pass through USB host stack.
//
USBHCDMain();
//
// Initialize the file system.
//
FileInit();
}
bool USBStick_OpenFile(ClassUSBStick * usbStickModule, char * fileName8Char) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened == false) {
FRESULT iFResult;
static char pcFilename[32];
uint32_t ui32Len;
if (_deviceState == eSTATE_DEVICE_READY) {
// If a file name is specified then open that file
if (fileName8Char && fileName8Char[0]) {
// Copy the filename into local storage and cap at 8 characters
// length.
memcpy(pcFilename, fileName8Char, 8);
pcFilename[8] = 0;
// Find the actual length of the string (8 chars or less) so we
// know where to add the extension.
ui32Len = strlen(pcFilename);
// Add the extension to the file name.
usnprintf(&pcFilename[ui32Len], 5, ".CSV");
}
// Otherwise no file name was specified so create a new one.
else {
if (CreateFileName(pcFilename, sizeof(pcFilename))) {
// There was a problem creating a file name so return an error
return (0);
}
}
// Open the file by name that was determined above. If the file exists
// it will be opened, and if not it will be created.
iFResult = f_open(&g_sFileObject, pcFilename, (FA_OPEN_ALWAYS |
FA_WRITE));
if (iFResult != FR_OK) {
return (0);
}
// Since it is possible that the file already existed when opened,
// seek to the end of the file so new data will be appended. If this
// is a new file then this will just be the beginning of the file.
iFResult = f_lseek(&g_sFileObject, g_sFileObject.fsize);
if (iFResult != FR_OK) {
return (0);
}
usbStickModule->_fileOpened = true;
return (1);
}
else {
// Device not ready so return NULL.
return (0);
}
} else {
return (0);
}
}
static int32_t CreateFileName(char *pcFilename, uint32_t ui32Len) {
FRESULT iFResult;
uint32_t ui32FileNum = 0;
// Enter loop to search for available file name
do {
// Prepare a numerical based file name and attempt to open it
usnprintf(pcFilename, ui32Len, USBSTICK_DEFAULT_FILENAME, ui32FileNum);
iFResult = f_open(&g_sFileObject, pcFilename, FA_OPEN_EXISTING);
// If file does not exist, then we have found a useable file name
if (iFResult == FR_NO_FILE) {
// Return to caller, indicating that a file name has been found.
return (0);
}
// Otherwise, advance to the next number in the file name sequence.
ui32FileNum++;
} while (ui32FileNum < 1000);
// If we reach this point, it means that no useable file name was found
// after attempting 10000 file names.
return (1);
}
void USBStick_CloseFile(ClassUSBStick * usbStickModule) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
StickFlush(usbStickModule);
f_close(&g_sFileObject);
usbStickModule->_fileOpened = false;
}
}
/*
* label 16 char max
*/
void USBStick_AddVar2Stream(ClassUSBStick * usbStickModule, float * varP,
char * label) {
if (usbStickModule->_varsNum <= 0) {
// Alloc memory for the first labels's pointer
usbStickModule->_labelsArray = malloc(1 * sizeof(char*));
// Alloc memory for the first var's pointer
usbStickModule->_varsArray = malloc(1 * sizeof(float*));
} else {
// reAlloc memory for labels's pointer
usbStickModule->_labelsArray = realloc(usbStickModule->_labelsArray,
(usbStickModule->_varsNum + 1) * sizeof(char*));
// Alloc memory for var's pointer
usbStickModule->_varsArray = realloc(usbStickModule->_varsArray,
(usbStickModule->_varsNum + 1) * sizeof(float*));
}
// Alloc memory for the first label's string
usbStickModule->_labelsArray[usbStickModule->_varsNum] = malloc(
(strlen(label) + 1) * sizeof(char));
// Store label
strcpy(usbStickModule->_labelsArray[usbStickModule->_varsNum], label);
// Store var's pointer
usbStickModule->_varsArray[usbStickModule->_varsNum] = varP;
usbStickModule->_varsNum++;
}
void USBStick_ClearStream(ClassUSBStick * usbStickModule) {
int i;
// Free label Array
for (i = 0; i < usbStickModule->_varsNum; ++i) {
free(usbStickModule->_labelsArray[i]);
}
free(usbStickModule->_labelsArray);
// Free vars Array
free(usbStickModule->_varsArray);
usbStickModule->_varsNum = 0;
}
void USBStick_PrintMenu(ClassUSBStick * usbStickModule, bool flushBuffer) { // IMPLEMENT OTHER FUNTIONS LIKE IT
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
// UARTprintf("\n _________ _____ __________ ___________ .__ _____ _______ _______ ____ \n");
// UARTprintf(" \_ ___ \ / _ \\______ \ \__ ___/___ | | ____ / \ _____ ____ \ _ \ \ _ \/_ |\n");
// UARTprintf(" / \ \/ / /_\ \| | _/ | |_/ __ \| | _/ __ \ / \ / \\__ \ / \ / /_\ \ / /_\ \| |\n");
// UARTprintf(" \ \____/ | \ | \ | |\ ___/| |_\ ___// Y \/ __ \| | \ \ \_/ \ \ \_/ \ |\n");
// UARTprintf(" \______ /\____|__ /______ / |____| \___ >____/\___ >____|__ (____ /___| / \_____ / /\ \_____ /___|\n");
// UARTprintf(" \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \n\n");
UARTprintf("Connection opened \n");
if (flushBuffer) {
StickFlush(usbStickModule);
}
}
}
void USBStick_PrintHeader(ClassUSBStick * usbStickModule, bool flushBuffer) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
int i, strLength;
for (i = 0; i < usbStickModule->_varsNum; ++i) {
if (i != usbStickModule->_varsNum - 1) {
strLength = snprintf(_USBStickBuffer, USBSTICK_BUFFER_SIZE,
"%s,", usbStickModule->_labelsArray[i]);
} else {
strLength = snprintf(_USBStickBuffer, USBSTICK_BUFFER_SIZE,
"%s\n", usbStickModule->_labelsArray[i]);
}
WriteInFile(usbStickModule, _USBStickBuffer, strLength);
}
if (flushBuffer) {
StickFlush(usbStickModule);
}
}
}
void USBStick_PrintStream(ClassUSBStick * usbStickModule, bool flushBuffer) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
int i, strLength;
for (i = 0; i < usbStickModule->_varsNum; ++i) {
if (i != usbStickModule->_varsNum - 1) {
strLength = snprintf(_USBStickBuffer, USBSTICK_BUFFER_SIZE,
"%5.3f,", usbStickModule->_varsArray[i][0]);
} else {
strLength = snprintf(_USBStickBuffer, USBSTICK_BUFFER_SIZE,
"%5.3f\n", usbStickModule->_varsArray[i][0]);
}
WriteInFile(usbStickModule, _USBStickBuffer, strLength);
}
if (flushBuffer) {
StickFlush(usbStickModule);
}
}
}
void USBStick_PrintString(ClassUSBStick * usbStickModule, char * stringP,
bool flushBuffer) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
int strLength = snprintf(_USBStickBuffer, USBSTICK_BUFFER_SIZE, "%s",
stringP);
WriteInFile(usbStickModule, _USBStickBuffer, strLength);
if (flushBuffer) {
StickFlush(usbStickModule);
}
}
}
void USBStick_SetRefreshRate(ClassUSBStick * usbStickModule,
uint32_t refreshRate) {
usbStickModule->_refreshRate = refreshRate;
}
uint32_t USBStick_GetRefreshRate(void * usbStickModule) {
ClassUSBStick * usbStick = (ClassUSBStick *) usbStickModule;
return usbStick->_refreshRate;
}
void USBStick_Run(ClassUSBStick * usbStickModule) {
if (usbStickModule->_USBEnable) {
// Call the USB stack to keep it running.
USBHCDMain();
// Take action based on the application state.
switch (_deviceState) {
// A device has enumerated.
case eSTATE_DEVICE_ENUM: {
// Check to see if the device is ready. If not then stay
// in this state and we will check it again on the next pass.
if (USBHMSCDriveReady(g_psMSCInstance) != 0) {
// Wait about 500ms before attempting to check if the
// device is ready again.
MAP_SysCtlDelay(MAP_SysCtlClockGet() / 3);
break;
}
// If there were no errors reported, we are ready for
// MSC operation.
_deviceState = eSTATE_DEVICE_READY;
// Set the Device Present flag.
usbStickModule->_fileOpened = false;
usbStickModule->_devicePresent = true;
break;
}
// If there is no device then just wait for one.
case eSTATE_NO_DEVICE: {
if (usbStickModule->_devicePresent) {
usbStickModule->_devicePresent = false;
}
if (usbStickModule->_fileOpened) {
usbStickModule->_fileOpened = false;
}
}
break;
// An unknown device was connected.
case eSTATE_UNKNOWN_DEVICE: {
// If this is a new device then change the status.
if (usbStickModule->_devicePresent == false) {
// Unknown device is present
}
// Set the Device Present flag even though the unknown device
// is not useful to us.
usbStickModule->_devicePresent = true;
usbStickModule->_fileOpened = false;
break;
}
//
// The device is ready and in use.
//
case eSTATE_DEVICE_READY: {
break;
}
// Something has caused a power fault.
case eSTATE_POWER_FAULT: {
break;
}
// Unexpected USB state. Set back to default.
default: {
_deviceState = eSTATE_NO_DEVICE;
// Set the Device Present flag.
usbStickModule->_fileOpened = false;
usbStickModule->_devicePresent = false;
break;
}
}
}
}
void USBStick_Enable(ClassUSBStick * usbStickModule) {
usbStickModule->_USBEnable = true;
}
void USBStick_Disable(ClassUSBStick * usbStickModule) {
usbStickModule->_USBEnable = false;
}
bool USBStick_IsEnable(void * usbStickModule) {
ClassUSBStick * usbStick = (ClassUSBStick *) usbStickModule;
return usbStick->_USBEnable;
}
void StickFlush(ClassUSBStick * usbStickModule) {
// if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
// f_sync(&g_sFileObject);
// }
}
bool WriteInFile(ClassUSBStick * usbStickModule, char * dataBuffer,
uint32_t dataLength) {
if (usbStickModule->_USBEnable && usbStickModule->_fileOpened) {
FRESULT iFResult;
uint32_t ui32BytesWritten;
// Now write the entire buffer to the USB stick file
iFResult = f_write(&g_sFileObject, dataBuffer, dataLength,
(UINT *) &ui32BytesWritten);
// Check for errors
if ((iFResult != FR_OK) || (ui32BytesWritten != dataLength)) {
// Some error occurred
usbStickModule->_fileOpened = false;
return (0);
} else {
// No errors occurred, return success
return (1);
}
} else {
return (0);
}
}
// Handler Functions
void USBStick_PrintStreamFlush(void * usbStickModule) {
ClassUSBStick * usbStick = (ClassUSBStick *) usbStickModule;
USBStick_PrintStream(usbStick, true);
}
void USBStick_PrintSignalStreamFlush(void * usbStickModule) {
}//TODO: implement
void USBStick_Start(void * usbStickModule) {
ClassUSBStick * usbStick = (ClassUSBStick *) usbStickModule;
if (usbStick->_fileOpened) {
USBStick_CloseFile(usbStick);
}
USBStick_OpenFile(usbStick, 0);
USBStick_PrintHeader(usbStick, true);
}
void USBStick_Stop(void * usbStickModule) {
ClassUSBStick * usbStick = (ClassUSBStick *) usbStickModule;
f_sync(&g_sFileObject);
USBStick_CloseFile(usbStick);
}