-
Notifications
You must be signed in to change notification settings - Fork 1
/
demoSdFs.c
840 lines (735 loc) · 23.4 KB
/
demoSdFs.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
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
/*
* \file hs_mmcsd_fs.c
*
* \brief FS support for HS MMCSD Sample Application
*
*/
/* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
* ALL RIGHTS RESERVED
*/
#include "ff.h"
#include "cmdline.h"
#include "hs_mmcsd.h"
#include "uartStdio.h"
#include "string.h"
#include "uart_irda_cir.h"
#include "soc_AM335x.h"
#include "demoSdFs.h"
/* Fat devices registered */
#ifndef fatDevice
typedef struct _fatDevice
{
/* Pointer to underlying device/controller */
void *dev;
/* File system pointer */
FATFS *fs;
}fatDevice;
#endif
extern fatDevice fat_devices[2];
/*****************************************************************************
Defines the size of the buffers that hold the path, or temporary data from
the memory card. There are two buffers allocated of this size. The buffer
size must be large enough to hold the longest expected full path name,
including the file name, and a trailing null character.
******************************************************************************/
#define PATH_BUF_SIZE 512
/*****************************************************************************
Defines the size of the buffer that holds the command line.
******************************************************************************/
#define CMD_BUF_SIZE 512
/*****************************************************************************
Current FAT fs state.
******************************************************************************/
static FATFS g_sFatFs;
static DIR g_sDirObject;
static FILINFO g_sFileInfo;
static FIL g_sFileObject;
volatile unsigned int sdCardAccessFlag = FALSE;
static volatile unsigned int sdCardState = FALSE;
/*****************************************************************************
A structure that holds a mapping between an FRESULT numerical code,
and a string representation. FRESULT codes are returned from the FatFs
FAT file system driver.
******************************************************************************/
typedef struct
{
FRESULT fresult;
char *pcResultStr;
}
tFresultString;
/*****************************************************************************
A macro to make it easy to add result codes to the table.
******************************************************************************/
#define FRESULT_ENTRY(f) { (f), (#f) }
/*****************************************************************************
A table that holds a mapping between the numerical FRESULT code and
it's name as a string. This is used for looking up error codes for
printing to the console.
******************************************************************************/
tFresultString g_sFresultStrings[] =
{
FRESULT_ENTRY(FR_OK),
FRESULT_ENTRY(FR_NOT_READY),
FRESULT_ENTRY(FR_NO_FILE),
FRESULT_ENTRY(FR_NO_PATH),
FRESULT_ENTRY(FR_INVALID_NAME),
FRESULT_ENTRY(FR_INVALID_DRIVE),
FRESULT_ENTRY(FR_DENIED),
FRESULT_ENTRY(FR_EXIST),
FRESULT_ENTRY(FR_RW_ERROR),
FRESULT_ENTRY(FR_WRITE_PROTECTED),
FRESULT_ENTRY(FR_NOT_ENABLED),
FRESULT_ENTRY(FR_NO_FILESYSTEM),
FRESULT_ENTRY(FR_INVALID_OBJECT),
FRESULT_ENTRY(FR_MKFS_ABORTED)
};
/*****************************************************************************
A macro that holds the number of result codes.
******************************************************************************/
#define NUM_FRESULT_CODES (sizeof(g_sFresultStrings) / sizeof(tFresultString))
/*****************************************************************************
This buffer holds the full path to the current working directory. Initially
it is root ("/").
******************************************************************************/
static char g_cCwdBuf[PATH_BUF_SIZE] = "/";
/*****************************************************************************
A temporary data buffer used when manipulating file paths, or reading data
from the memory card.
******************************************************************************/
#ifdef __IAR_SYSTEMS_ICC__
#pragma data_alignment=32
static char g_cTmpBuf[PATH_BUF_SIZE];
#elif defined(__TMS470__)
#pragma DATA_ALIGN(g_cTmpBuf, 32);
static char g_cTmpBuf[PATH_BUF_SIZE];
#else
static char g_cTmpBuf[PATH_BUF_SIZE] __attribute__ ((aligned (32)));
#endif
/*****************************************************************************
The buffer that holds the command line.
******************************************************************************/
static char g_cCmdBuf[CMD_BUF_SIZE];
volatile unsigned int g_sPState = 0;
volatile unsigned int g_sCState = 0;
/*******************************************************************************
**
** This function reads a line of text from the UART console.
**
*******************************************************************************/
void
ReadLine(void)
{
unsigned long ulIdx;
unsigned char ucChar;
/*
** Start reading at the beginning of the command buffer and print a prompt.
*/
g_cCmdBuf[0] = '\0';
ulIdx = 0;
sdCardState = TRUE;
/*
** Loop forever. This loop will be explicitly broken out of when the line
** has been fully read.
*/
while(TRUE == sdCardState)
{
/*
** Attempt to open the directory.
*/
f_mount(0, &g_sFatFs);
if(f_opendir(&g_sDirObject, g_cCwdBuf) != FR_OK)
{
UARTPuts("\nFailed to open directory.\n", -1);
g_sCState = 0;
sdCardState = FALSE;
sdCardAccessFlag = FALSE;
return;
}
else
{
g_sCState = 1;
}
if (g_sCState != g_sPState)
{
if (g_sCState == 0)
{
UARTprintf("%s>", "UNKNOWN");
g_sPState = 0;
return;
}
else
{
UARTprintf("%s> %s", g_cCwdBuf, g_cCmdBuf);
g_sPState = 1;
}
}
/*
** Loop while there are characters that have been received from the
** UART.
*/
while(TRUE == UARTCharsAvail(SOC_UART_0_REGS))
{
/*
** Read the next character from the UART.
*/
ucChar = UARTGetc();
/*
** See if this character is a backspace and there is at least one
** character in the input line.
*/
if((ucChar == '\b') && (ulIdx != 0))
{
/*
** Erase the last character from the input line.
*/
UARTprintf("\b \b");
ulIdx--;
g_cCmdBuf[ulIdx] = '\0';
}
/*
** See if this character is a newline.
*/
else if((ucChar == '\r') || (ucChar == '\n'))
{
/*
** Return to the caller.
*/
UARTprintf("\n");
return;
}
/*
** See if this character is an escape or Ctrl-U.
*/
else if((ucChar == 0x1b) || (ucChar == 0x15))
{
sdCardState = FALSE;
return;
}
/*
** See if this is a printable ASCII character.
*/
else if((ucChar >= ' ') && (ucChar <= '~') &&
(ulIdx < (sizeof(g_cCmdBuf) - 1)))
{
/*
** Add this character to the input buffer.
*/
g_cCmdBuf[ulIdx++] = ucChar;
g_cCmdBuf[ulIdx] = '\0';
UARTprintf("%c", ucChar);
}
}
}
}
/*****************************************************************************
This function returns a string representation of an error code that was
returned from a function call to FatFs. It can be used for printing human
readable error messages.
*****************************************************************************/
const char *
StringFromFresult(FRESULT fresult)
{
unsigned int uIdx;
/* Enter a loop to search the error code table for a matching error code. */
for(uIdx = 0; uIdx < NUM_FRESULT_CODES; uIdx++)
{
/*
** If a match is found, then return the string name of the error code.
*/
if(g_sFresultStrings[uIdx].fresult == fresult)
{
return(g_sFresultStrings[uIdx].pcResultStr);
}
}
/*
** At this point no matching code was found, so return a string indicating
** unknown error.
*/
return("UNKNOWN ERROR CODE");
}
/*****************************************************************************
This function implements the "ls" command. It opens the current directory
and enumerates through the contents, and prints a line for each item it
finds. It shows details such as file attributes, time and date, and the
file size, along with the name. It shows a summary of file sizes at the end
along with free space.
*****************************************************************************/
int
Cmd_ls(int argc, char *argv[])
{
unsigned long ulTotalSize;
unsigned long ulFileCount;
unsigned long ulDirCount;
FRESULT fresult;
FATFS *pFatFs;
/*
** Open the current directory for access.
*/
fresult = f_opendir(&g_sDirObject, g_cCwdBuf);
/*
** Check for error and return if there is a problem.
*/
if(fresult != FR_OK)
{
return(fresult);
}
ulTotalSize = 0;
ulFileCount = 0;
ulDirCount = 0;
/*
** Enter loop to enumerate through all directory entries.
*/
while(1)
{
/*
** Read an entry from the directory.
*/
fresult = f_readdir(&g_sDirObject, &g_sFileInfo);
/*
** Check for error and return if there is a problem.
*/
if(fresult != FR_OK)
{
return(fresult);
}
/*
** If the file name is blank, then this is the end of the listing.
*/
if(!g_sFileInfo.fname[0])
{
break;
}
/*
** If the attribute is directory, then increment the directory count.
*/
if(g_sFileInfo.fattrib & AM_DIR)
{
ulDirCount++;
}
/*
** Otherwise, it is a file. Increment the file count, and add in the
** file size to the total.
*/
else
{
ulFileCount++;
ulTotalSize += g_sFileInfo.fsize;
}
/*
** Print the entry information on a single line with formatting to show
** the attributes, date, time, size, and name.
*/
UARTprintf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9u %s\n",
(g_sFileInfo.fattrib & AM_DIR) ? 'D' : '-',
(g_sFileInfo.fattrib & AM_RDO) ? 'R' : '-',
(g_sFileInfo.fattrib & AM_HID) ? 'H' : '-',
(g_sFileInfo.fattrib & AM_SYS) ? 'S' : '-',
(g_sFileInfo.fattrib & AM_ARC) ? 'A' : '-',
(g_sFileInfo.fdate >> 9) + 1980,
(g_sFileInfo.fdate >> 5) & 15,
g_sFileInfo.fdate & 31,
(g_sFileInfo.ftime >> 11),
(g_sFileInfo.ftime >> 5) & 63,
g_sFileInfo.fsize,
g_sFileInfo.fname);
}
/*
** Print summary lines showing the file, dir, and size totals.
*/
UARTprintf("\n%4u File(s),%10u bytes total\n%4u Dir(s)",
ulFileCount, ulTotalSize, ulDirCount);
/*
** Get the free space.
*/
fresult = f_getfree("/", (DWORD *)&ulTotalSize, &pFatFs);
/*
** Check for error and return if there is a problem.
*/
if(fresult != FR_OK)
{
return(fresult);
}
/*
** Display the amount of free space that was calculated.
*/
UARTprintf(", %10uK bytes free\n", ulTotalSize * pFatFs->sects_clust / 2);
/*
** Made it to here, return with no errors.
*/
return(0);
}
/*****************************************************************************
This function implements the "cd" command. It takes an argument that
specifies the directory to make the current working directory. Path
separators must use a forward slash "/". The argument to cd can be one of
the following:
* root ("/")
* a fully specified path ("/my/path/to/mydir")
* a single directory name that is in the current directory ("mydir")
* parent directory ("..")
It does not understand relative paths, so don't try something like this:
("../my/new/path")
Once the new directory is specified, it attempts to open the directory to
make sure it exists. If the new path is opened successfully, then the
current working directory (cwd) is changed to the new path.
*****************************************************************************/
int
Cmd_cd(int argc, char *argv[])
{
unsigned int uIdx;
FRESULT fresult;
/*
** Copy the current working path into a temporary buffer so it can be
** manipulated.
*/
strcpy(g_cTmpBuf, g_cCwdBuf);
/*
** If the first character is /, then this is a fully specified path, and it
** should just be used as-is.
*/
if(argv[1][0] == '/')
{
/*
** Make sure the new path is not bigger than the cwd buffer.
*/
if(strlen(argv[1]) + 1 > sizeof(g_cCwdBuf))
{
UARTprintf("Resulting path name is too long\n");
return(0);
}
/*
** If the new path name (in argv[1]) is not too long, then copy it
** into the temporary buffer so it can be checked.
*/
else
{
strncpy(g_cTmpBuf, argv[1], sizeof(g_cTmpBuf));
}
}
/*
** If the argument is .. then attempt to remove the lowest level on the
** CWD.
*/
else if(!strcmp(argv[1], ".."))
{
/*
** Get the index to the last character in the current path.
*/
uIdx = strlen(g_cTmpBuf) - 1;
/*
** Back up from the end of the path name until a separator (/) is
** found, or until we bump up to the start of the path.
*/
while((g_cTmpBuf[uIdx] != '/') && (uIdx > 1))
{
/*
** Back up one character.
*/
uIdx--;
}
/*
** Now we are either at the lowest level separator in the current path,
** or at the beginning of the string (root). So set the new end of
** string here, effectively removing that last part of the path.
*/
g_cTmpBuf[uIdx] = 0;
}
/*
** Otherwise this is just a normal path name from the current directory,
** and it needs to be appended to the current path.
*/
else
{
/*
** Test to make sure that when the new additional path is added on to
** the current path, there is room in the buffer for the full new path.
** It needs to include a new separator, and a trailing null character.
*/
if(strlen(g_cTmpBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cCwdBuf))
{
UARTprintf("Resulting path name is too long\n");
return(0);
}
/*
** The new path is okay, so add the separator and then append the new
** directory to the path.
*/
else
{
/*
** If not already at the root level, then append a /
*/
if(strcmp(g_cTmpBuf, "/"))
{
strcat(g_cTmpBuf, "/");
}
/*
** Append the new directory to the path.
*/
strcat(g_cTmpBuf, argv[1]);
}
}
/*
** At this point, a candidate new directory path is in chTmpBuf. Try to
** open it to make sure it is valid.
*/
fresult = f_opendir(&g_sDirObject, g_cTmpBuf);
/*
** If it can't be opened, then it is a bad path. Inform user and return.
*/
if(fresult != FR_OK)
{
UARTprintf("cd: %s\n", g_cTmpBuf);
return(fresult);
}
/*
** Otherwise, it is a valid new path, so copy it into the CWD.
*/
else
{
strncpy(g_cCwdBuf, g_cTmpBuf, sizeof(g_cCwdBuf));
}
/*
** Return success.
*/
return(0);
}
/*******************************************************************************
**
** This function implements the "pwd" command. It simply prints the current
** working directory.
**
*******************************************************************************/
int
Cmd_pwd(int argc, char *argv[])
{
/*
** Print the CWD to the console.
*/
UARTprintf("%s\n", g_cCwdBuf);
return(0);
}
/*******************************************************************************
**
** This function implements the "exit" command.
**
*******************************************************************************/
int Cmd_exit(int argc, char *argv[])
{
return 0;
}
/*******************************************************************************
**
** This function implements the "cat" command. It reads the contents of a file
** and prints it to the console. This should only be used on text files. If
** it is used on a binary file, then a bunch of garbage is likely to printed on
** the console.
**
*******************************************************************************/
int
Cmd_cat(int argc, char *argv[])
{
FRESULT fresult;
unsigned short usBytesRead;
/*
** First, check to make sure that the current path (CWD), plus the file
** name, plus a separator and trailing null, will all fit in the temporary
** buffer that will be used to hold the file name. The file name must be
** fully specified, with path, to FatFs.
*/
if(strlen(g_cCwdBuf) + strlen(argv[1]) + 1 + 1 > sizeof(g_cTmpBuf))
{
UARTprintf("Resulting path name is too long\n");
return(0);
}
/*
** Copy the current path to the temporary buffer so it can be manipulated.
*/
strcpy(g_cTmpBuf, g_cCwdBuf);
/*
** If not already at the root level, then append a separator.
*/
if(strcmp("/", g_cCwdBuf))
{
strcat(g_cTmpBuf, "/");
}
/*
** Now finally, append the file name to result in a fully specified file.
*/
strcat(g_cTmpBuf, argv[1]);
/*
** Open the file for reading.
*/
fresult = f_open(&g_sFileObject, g_cTmpBuf, FA_READ);
/*
** If there was some problem opening the file, then return an error.
*/
if(fresult != FR_OK)
{
return(fresult);
}
/*
** Enter a loop to repeatedly read data from the file and display it, until
** the end of the file is reached.
*/
do
{
/*
** Read a block of data from the file. Read as much as can fit in the
** temporary buffer, including a space for the trailing null.
*/
fresult = f_read(&g_sFileObject, g_cTmpBuf, sizeof(g_cTmpBuf) - 1,
&usBytesRead);
/*
** If there was an error reading, then print a newline and return the
** error to the user.
*/
if(fresult != FR_OK)
{
UARTprintf("\n");
return(fresult);
}
/*
** Null terminate the last block that was read to make it a null
** terminated string that can be used with printf.
*/
g_cTmpBuf[usBytesRead] = 0;
/*
** Print the last chunk of the file that was received.
*/
UARTprintf("%s", g_cTmpBuf);
/*
** Continue reading until less than the full number of bytes are read.
** That means the end of the buffer was reached.
*/
}
while(usBytesRead == sizeof(g_cTmpBuf) - 1);
/*
** Return success.
*/
return(0);
}
/*******************************************************************************
**
** This function implements the "help" command. It prints a simple list of the
** available commands with a brief description.
**
*******************************************************************************/
int
Cmd_help(int argc, char *argv[])
{
tCmdLineEntry *pEntry;
/*
** Print some header text.
*/
UARTprintf("\nAvailable commands\n");
UARTprintf("------------------\n");
/*
** Point at the beginning of the command table.
*/
pEntry = &g_sCmdTable[0];
/*
** Enter a loop to read each entry from the command table. The end of the
** table has been reached when the command name is NULL.
*/
while(pEntry->pcCmd)
{
/*
** Print the command name and the brief description.
*/
UARTprintf("%s%s\n", pEntry->pcCmd, pEntry->pcHelp);
/*
** Advance to the next entry in the table.
*/
pEntry++;
}
/*
** Return success.
*/
return(0);
}
void HSMMCSDFsMount(unsigned int driveNum, void *ptr)
{
f_mount(driveNum, &g_sFatFs);
fat_devices[0].dev = ptr;
fat_devices[0].fs = &g_sFatFs;
}
/*******************************************************************************
**
** This is the table that holds the command names, implementing functions, and
** brief description.
**
*******************************************************************************/
tCmdLineEntry g_sCmdTable[] =
{
// { "help", Cmd_help, " : Display list of commands" },
// { "h", Cmd_help, " : alias for help" },
// { "?", Cmd_help, " : alias for help" },
// { "ls", Cmd_ls, " : Display list of files" },
// { "chdir", Cmd_cd, " : Change directory" },
// { "cd", Cmd_cd, " : alias for chdir" },
// { "pwd", Cmd_pwd, " : Show current working directory" },
// { "cat", Cmd_cat, " : Show contents of a text file" },
// { "Ctrl+u", Cmd_exit, ": Exit consloe" },
{ 0, 0, 0 }
};
void HSMMCSDFsProcessCmdLine(void)
{
int iStatus;
static unsigned int cmdEntry = 0;
UARTPuts("\n\r\n\rMMCSD command console entry!\n\r", -1);
UARTPuts("Type 'help' for the list of commands supported \n\r", -1);
UARTPuts("Press ESC key or input Crl+U to exit the console.\n\r", -1);
if(1 == cmdEntry)
{
g_cCmdBuf[0] = '\0';
UARTprintf("%s> %s", g_cCwdBuf, g_cCmdBuf);
}
while(1)
{
//
// Get a line of text from the user.
//
ReadLine();
if(g_cCmdBuf[0] != '\0')
{
//
// Pass the line from the user to the command processor.
// It will be parsed and valid commands executed.
//
iStatus = CmdLineProcess(g_cCmdBuf);
//
// Handle the case of bad command.
//
if(iStatus == CMDLINE_BAD_CMD)
{
UARTprintf("Bad command!\n");
}
//
// Handle the case of too many arguments.
//
else if(iStatus == CMDLINE_TOO_MANY_ARGS)
{
UARTprintf("Too many arguments for command processor!\n");
}
//
// Otherwise the command was executed. Print the error
// code if one was returned.
//
else if(iStatus != 0)
{
UARTprintf("Command returned error code %s\n",
StringFromFresult((FRESULT)iStatus));
}
g_cCmdBuf[0] = '\0';
UARTprintf("%s> %s", g_cCwdBuf, g_cCmdBuf);
}
if(FALSE == sdCardState)
{
UARTPuts("\n\rMMCSD command console exit! \n\r", -1);
sdCardAccessFlag = FALSE;
cmdEntry = 1;
return;
}
}
}