1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Code to execute for cmd_sd_dir in the 'core/cmd'
7 command-line interpretter.
11 Software License Agreement (BSD License)
13 Copyright (c) 2010, microBuilder SARL
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. Neither the name of the copyright holders nor the
24 names of its contributors may be used to endorse or promote products
25 derived from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
28 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /**************************************************************************/
42 #include "projectconfig.h"
43 #include "core/cmd/cmd.h"
44 #include "project/commands.h" // Generic helper functions
47 #include "core/timer32/timer32.h"
48 #include "core/ssp/ssp.h"
49 #include "drivers/fatfs/diskio.h"
50 #include "drivers/fatfs/ff.h"
53 static FATFS Fatfs
[1];
55 /**************************************************************************/
57 sd 'dir' command handler
59 This demonstrates how to initialise the SD card, read the contents
60 of a directory (including checking if an entry is a folder or a file),
61 and checking the amount of freespace on available on the disk.
63 /**************************************************************************/
64 void cmd_sd_dir(uint8_t argc
, char **argv
)
68 // Display root folder by default
69 path
= argc
> 0 ? argv
[0] : "/";
73 stat
= disk_initialize(0);
74 if (stat
& STA_NOINIT
)
76 printf("SD init failed%s", CFG_PRINTF_NEWLINE
);
78 if (stat
& STA_NODISK
)
80 printf("No SD card%s", CFG_PRINTF_NEWLINE
);
88 res
= f_mount(0, &Fatfs
[0]);
91 printf("Failed to mount partition%s" , CFG_PRINTF_NEWLINE
);
95 res
= f_opendir(&dir
, path
);
98 printf("Failed to open '%s' %s", path
, CFG_PRINTF_NEWLINE
);
102 // Display directory name
103 printf("%s Contents of %s %s%s", CFG_PRINTF_NEWLINE
, path
, CFG_PRINTF_NEWLINE
, CFG_PRINTF_NEWLINE
);
104 printf(" %-25s %12s %s", "Filename", "Size", CFG_PRINTF_NEWLINE
);
105 printf(" %-25s %12s %s", "--------", "----", CFG_PRINTF_NEWLINE
);
107 // Read directory contents
111 res
= f_readdir(&dir
, &Finfo
);
112 if ((res
!= FR_OK
) || !Finfo
.fname
[0]) break;
114 if (Finfo
.fattrib
& AM_DIR
)
115 printf(" <DIR> %-25s %s", (char *)&Finfo
.fname
[0], CFG_PRINTF_NEWLINE
);
117 printf(" %-25s %12d Bytes %s", (char *)&Finfo
.fname
[0], (int)(Finfo
.fsize
), CFG_PRINTF_NEWLINE
);
118 folderBytes
+= Finfo
.fsize
;
124 // Display folder size
125 printf("%s", CFG_PRINTF_NEWLINE
);
126 printf(" %-25s %12d KB %s", "Folder Size: ", (int)(folderBytes
/ 1024), CFG_PRINTF_NEWLINE
);
128 // Get free disk space (only available if FATFS was compiled with _FS_MINIMIZE set to 0)
129 #if _FS_MINIMIZE == 0 && _FS_READONLY == 0
130 FATFS
*fs
= &Fatfs
[0];
134 res
= f_getfree("0:", &clust
, &fs
);
136 // Display total and free space
137 printf(" %-25s %12d KB %s", "Disk Size: ", (int)((DWORD
)(fs
->max_clust
- 2) * fs
->csize
/ 2), CFG_PRINTF_NEWLINE
);
138 printf(" %-25s %12d KB %s", "Space Available: ", (int)(clust
* fs
->csize
/ 2), CFG_PRINTF_NEWLINE
);
This page took 0.048 seconds and 5 git commands to generate.