See v1.0.0 changelog
[hackover2013-badge-firmware.git] / project / commands / cmd_sd_dir.c
1 /**************************************************************************/
2 /*!
3 @file cmd_sd_dir.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Code to execute for cmd_sd_dir in the 'core/cmd'
7 command-line interpretter.
8
9 @section LICENSE
10
11 Software License Agreement (BSD License)
12
13 Copyright (c) 2010, microBuilder SARL
14 All rights reserved.
15
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.
26
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.
37 */
38 /**************************************************************************/
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "projectconfig.h"
43 #include "core/cmd/cmd.h"
44 #include "project/commands.h" // Generic helper functions
45
46 #ifdef CFG_SDCARD
47 #include "core/timer32/timer32.h"
48 #include "core/ssp/ssp.h"
49 #include "drivers/fatfs/diskio.h"
50 #include "drivers/fatfs/ff.h"
51
52 static FILINFO Finfo;
53 static FATFS Fatfs[1];
54
55 /**************************************************************************/
56 /*!
57 sd 'dir' command handler
58
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.
62 */
63 /**************************************************************************/
64 void cmd_sd_dir(uint8_t argc, char **argv)
65 {
66 char* path;
67
68 // Display root folder by default
69 path = argc > 0 ? argv[0] : "/";
70
71 // Initialise SD Card
72 DSTATUS stat;
73 stat = disk_initialize(0);
74 if (stat & STA_NOINIT)
75 {
76 printf("SD init failed%s", CFG_PRINTF_NEWLINE);
77 }
78 if (stat & STA_NODISK)
79 {
80 printf("No SD card%s", CFG_PRINTF_NEWLINE);
81 }
82 if (stat == 0)
83 {
84 BYTE res;
85 DIR dir;
86
87 // Try to mount drive
88 res = f_mount(0, &Fatfs[0]);
89 if (res != FR_OK)
90 {
91 printf("Failed to mount partition%s" , CFG_PRINTF_NEWLINE);
92 }
93 if (res == FR_OK)
94 {
95 res = f_opendir(&dir, path);
96 if (res)
97 {
98 printf("Failed to open '%s' %s", path, CFG_PRINTF_NEWLINE);
99 return;
100 }
101
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);
106
107 // Read directory contents
108 int folderBytes = 0;
109 for(;;)
110 {
111 res = f_readdir(&dir, &Finfo);
112 if ((res != FR_OK) || !Finfo.fname[0]) break;
113 #if _USE_LFN == 0
114 if (Finfo.fattrib & AM_DIR)
115 printf(" <DIR> %-25s %s", (char *)&Finfo.fname[0], CFG_PRINTF_NEWLINE);
116 else
117 printf(" %-25s %12d Bytes %s", (char *)&Finfo.fname[0], (int)(Finfo.fsize), CFG_PRINTF_NEWLINE);
118 folderBytes += Finfo.fsize;
119 #else
120 // ToDo
121 #endif
122 }
123
124 // Display folder size
125 printf("%s", CFG_PRINTF_NEWLINE);
126 printf(" %-25s %12d KB %s", "Folder Size: ", (int)(folderBytes / 1024), CFG_PRINTF_NEWLINE);
127
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];
131 DWORD clust;
132
133 // Get free clusters
134 res = f_getfree("0:", &clust, &fs);
135
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);
139 #endif
140 }
141 }
142 }
143
144 #endif
This page took 0.056478 seconds and 5 git commands to generate.