HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fatfs_sup.c
Go to the documentation of this file.
1 
30 #include "user_config.h"
31 #include "fatfs.h"
32 
33 #ifdef AVR
34 #include <stdlib.h>
35 #endif
36 
37 #include "mathio.h"
38 
39 #include "time.h"
40 
42 FATFS Fatfs[FF_VOLUMES]; /* File system object for each logical drive */
43 
44 #if FF_MULTI_PARTITION != 0
45 const PARTITION Drives[] =
47 {
48  {
49  0,0
50  }
51  ,
52  {
53  0,1
54  }
55 };
56 #endif
57 
58 #if FATFS_DEBUG > 0
59 static const char *err_msg[] =
63 {
64  "OK",
65  "DISK_ERR",
66  "INT_ERR",
67  "NOT_READY",
68  "NO_FILE",
69  "NO_PATH",
70  "INVALID_NAME",
71  "DENIED",
72  "EXIST",
73  "INVALID_OBJECT",
74  "WRITE_PROTECTED",
75  "INVALID_DRIVE",
76  "NOT_ENABLED",
77  "NO_FILE_SYSTEM",
78  "MKFS_ABORTED",
79  "TIMEOUT",
80  "LOCKED",
81  "NOT_ENOUGH_CORE",
82  "TOO_MANY_OPEN_FILES",
83  "INVALID_PARAMETER",
84  NULL
85 };
86 #endif
87 
103 
109 MEMSPACE
110 uint32_t tm_to_fat(tm_t *t)
111 {
112  uint32_t fat;
113 /* Pack date and time into a uint32_t variable */
114  fat = ((uint32_t)(t->tm_year - 80) << 25)
115  | (((uint32_t)t->tm_mon+1) << 21)
116  | (((uint32_t)t->tm_mday) << 16)
117  | ((uint32_t)t->tm_hour << 11)
118  | ((uint32_t)t->tm_min << 5)
119  | ((uint32_t)t->tm_sec >> 1);
120  return(fat);
121 }
122 
123 
128 MEMSPACE
130 {
131  time_t t;
132 /* Get GMT time */
133  time(&t);
134  return( tm_to_fat(localtime(&t)));
135 }
136 
137 
143 
144 MEMSPACE
145 void put_rc (int rc)
146 {
147 #if FATFS_DEBUG > 0
148  char *ptr;
149  if(rc > 19)
150  ptr = "INVALID ERROR MESSAGE";
151  else
152  ptr = (char *) err_msg[(int)rc];
153  printf("rc=%u FR_%s\n", rc, ptr);
154 #else
155  printf("rc=%u\n", rc);
156 #endif
157 }
158 
159 
162 
165 
178 static
179 MEMSPACE
180 int fatfs_scan_files (char *path) /* Pointer to the working buffer with start path */
181 {
182  DIR dirs;
183  FRESULT fr;
184  int i;
185  FILINFO info;
186  fr = f_opendir(&dirs, path);
187  if (fr == FR_OK)
188  {
189  while (((fr = f_readdir(&dirs, &info)) == FR_OK) && info.fname[0])
190  {
191  if (info.fattrib & AM_DIR)
192  {
193  AccDirs++;
194  i = strlen(path);
195  path[i] = '/'; strcpy(path+i+1, info.fname);
196  fr = fatfs_scan_files(path);
197  path[i] = 0;
198  if (fr != FR_OK) break;
199  }
200  else
201  {
202 // xprintf(PSTR("%s/%s\n"), path, info.fname);
203  AccFiles++;
204  AccSize += info.fsize;
205  }
206 #ifdef ESP8266
207  optimistic_yield(1000);
208  wdt_reset();
209 #endif
210  }
211  }
212 
213  return fr;
214 }
215 
216 
217 
221 MEMSPACE
222 char *fatfs_fstype(int type)
223 {
224  char *ptr;
225  switch(type)
226  {
227  case FS_FAT12:
228  ptr = "FAT12";
229  break;
230  case FS_FAT16:
231  ptr = "FAT16";
232  break;
233  case FS_FAT32:
234  ptr = "FAT32";
235  break;
236  case FS_EXFAT:
237  ptr = "EXFAT";
238  break;
239  default:
240  ptr = "UNKNOWN";
241  break;
242  }
243  return(ptr);
244 }
245 
246 
258 MEMSPACE
259 int fatfs_status(char *name)
260 {
261  long p2;
262  int res;
263  FATFS *fs;
264  char label[24+2];
265  DWORD vsn; // volume serial number
266  char buff[MAX_NAME_LEN+1];
267 
268  memset(buff,0,sizeof(buff)-1);
269  while(*name == ' ' || *name == '\t')
270  ++name;
271 
272  printf("MMC SLOW clock=%8ld\n", (long)MMC_SLOW);
273  printf("MMC FAST clock=%8ld\n", (long)MMC_FAST);
274 
275  if(!name || !*name)
276  {
277  strcpy(buff,".");
278  }
279  else
280  {
281  strcpy(buff,name);
282  }
283  printf("fatfs status:%s\n",buff);
284  res = f_getfree(buff, (DWORD*)&p2, &fs);
285  if (res)
286  {
287  printf("fatfs_status f_getfree failed\n");
288  return(0);
289  }
290  printf("FAT type = %s\n", fatfs_fstype(fs->fs_type));
291  printf("Bytes/Cluster = %lu\n", (DWORD)fs->csize * 512);
292  printf("Number of FATs = %u\n", fs->n_fats);
293  printf("Root DIR entries = %u\n", fs->n_rootdir);
294  printf("Sectors/FAT = %lu\n", fs->fsize);
295  printf("Number of clusters = %lu\n", fs->n_fatent - 2);
296  printf("FAT start (lba) = %lu\n", fs->fatbase);
297  printf("DIR start (lba,clustor) = %lu\n", fs->dirbase);
298  printf("Data start (lba) = %lu\n", fs->database);
299 
300 #if FF_USE_LABEL
301  res = f_getlabel(buff, label, (DWORD*)&vsn);
302  if (res)
303  {
304  printf("fatfs_status f_getlabel failed\n");
305  return(0);
306  }
307  printf("Volume name = %s\n", label[0] ? label : "<blank>");
308  printf("Volume S/N = %04X-%04X\n", (WORD)((DWORD)vsn >> 16), (WORD)(vsn & 0xFFFF));
309 #endif
310 
311  AccSize = AccFiles = AccDirs = 0;
312  res = fatfs_scan_files(buff);
313  if (res)
314  {
315  printf("fatfs_status fatfs_scan_files failed\n");
316  return(0);
317  }
318  printf("%u files, %lu bytes.\n%u folders.\n"
319  "%lu KB total disk space.\n%lu KB available.\n",
321  (fs->n_fatent - 2) * fs->csize / 2, p2 * fs->csize / 2
322  );
323  return(1);
324 }
325 
326 
338 
339 MEMSPACE
341 {
342  char attrs[6];
343  if(info->fname[0] == 0)
344  {
345  printf("fatfs_filinfo_list: empty\n");
346  return;
347  }
348  attrs[0] = (info->fattrib & AM_DIR) ? 'D' : '-';
349  attrs[1] = (info->fattrib & AM_RDO) ? 'R' : '-';
350  attrs[2] = (info->fattrib & AM_HID) ? 'H' : '-';
351  attrs[3] = (info->fattrib & AM_SYS) ? 'S' : '-';
352  attrs[4] = (info->fattrib & AM_ARC) ? 'A' : '-';
353  attrs[5] = 0;
354  printf("%s %u/%02u/%02u %02u:%02u %9lu %s",
355  attrs,
356  (info->fdate >> 9) + 1980, (info->fdate >> 5) & 15, info->fdate & 31,
357  (info->ftime >> 11), (info->ftime >> 5) & 63,
358  info->fsize, info->fname);
359  printf("\n");
360 }
FATFS
Definition: ff.h:132
FILINFO::ftime
WORD ftime
Definition: ff.h:250
fatfs.h
AccDirs
WORD AccDirs
Definition: fatfs_sup.c:164
MAX_NAME_LEN
#define MAX_NAME_LEN
Definition: posix.h:133
printf
MEMSPACE int printf(const char *format,...)
tm::tm_min
int tm_min
Definition: time.h:43
FATFS::n_fats
BYTE n_fats
Definition: ff.h:135
AM_HID
#define AM_HID
Definition: ff.h:416
MEMSPACE
#define MEMSPACE
Definition: user_config.h:17
get_fattime
MEMSPACE DWORD get_fattime(void)
Read time and convert to FAT32 time.
Definition: fatfs_sup.c:129
FILINFO::fdate
WORD fdate
Definition: ff.h:249
tm::tm_sec
int tm_sec
Definition: time.h:42
FATFS::fatbase
LBA_t fatbase
Definition: ff.h:168
FS_EXFAT
#define FS_EXFAT
Definition: ff.h:412
FATFS::fs_type
BYTE fs_type
Definition: ff.h:133
tm::tm_mon
int tm_mon
Definition: time.h:46
MMC_SLOW
#define MMC_SLOW
Definition: user_config.h:26
tm::tm_mday
int tm_mday
Definition: time.h:45
FATFS::database
LBA_t database
Definition: ff.h:170
FS_FAT16
#define FS_FAT16
Definition: ff.h:410
tm::tm_hour
int tm_hour
Definition: time.h:44
FATFS::fsize
DWORD fsize
Definition: ff.h:166
fatfs_fstype
MEMSPACE char * fatfs_fstype(int type)
return a string with the file system type
Definition: fatfs_sup.c:222
FF_VOLUMES
#define FF_VOLUMES
Definition: ffconf.h:169
fatfs_filinfo_list
MEMSPACE void fatfs_filinfo_list(FILINFO *info)
Display FILINFO structure in a readable format.
Definition: fatfs_sup.c:340
FATFS::n_rootdir
WORD n_rootdir
Definition: ff.h:139
strcpy
MEMSPACE WEAK_ATR char * strcpy(char *dest, const char *src)
copy a string
Definition: stringsup.c:160
NULL
#define NULL
Definition: user_config.h:85
put_rc
MEMSPACE void put_rc(int rc)
display FatFs return code as ascii string
Definition: fatfs_sup.c:145
strlen
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:144
FS_FAT32
#define FS_FAT32
Definition: ff.h:411
time.h
Common Linux/POSIX time functions.
f_opendir
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition: ff.c:4571
fatfs_status
MEMSPACE int fatfs_status(char *name)
Compute space used, number of directories and files contained used by a drive.
Definition: fatfs_sup.c:259
FILINFO::fattrib
BYTE fattrib
Definition: ff.h:251
FATFS::dirbase
LBA_t dirbase
Definition: ff.h:169
time
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
AM_SYS
#define AM_SYS
Definition: ff.h:417
MMC_FAST
#define MMC_FAST
Definition: user_config.h:30
FRESULT
FRESULT
Definition: ff.h:276
DIR
Definition: ff.h:228
AM_DIR
#define AM_DIR
Definition: ff.h:418
AccSize
DWORD AccSize
Total file space used.
Definition: fatfs_sup.c:161
FR_OK
@ FR_OK
Definition: ff.h:277
FS_FAT12
#define FS_FAT12
Definition: ff.h:409
localtime
MEMSPACE tm_t * localtime(time_t *tp)
Convert POSIX epoch time_t *tp into POSIX tm_t *result.
Definition: time.c:522
FILINFO::fname
TCHAR fname[FF_LFN_BUF+1]
Definition: ff.h:254
tm
POSIX struct tm.
Definition: time.h:40
tm::tm_year
int tm_year
Definition: time.h:47
FILINFO::fsize
FSIZE_t fsize
Definition: ff.h:248
time_t
uint32_t time_t
type of EPOCH result.
Definition: time.h:34
Fatfs
FATFS Fatfs[FF_VOLUMES]
FatFs Drive Volumes.
Definition: fatfs_sup.c:42
FATFS::n_fatent
DWORD n_fatent
Definition: ff.h:165
WORD
unsigned short WORD
Definition: ff.h:55
AccFiles
WORD AccFiles
Total number or Files and Directories.
Definition: fatfs_sup.c:164
f_getlabel
FRESULT f_getlabel(const TCHAR *path, TCHAR *label, DWORD *vsn)
Definition: ff.c:5312
mathio.h
Math IO functions, and verious conversion code with floating point support.
FILINFO
Definition: ff.h:247
FATFS::csize
WORD csize
Definition: ff.h:140
DWORD
unsigned long DWORD
Definition: ff.h:56
tm_to_fat
MEMSPACE uint32_t tm_to_fat(tm_t *t)
FAT time structer reference.
Definition: fatfs_sup.c:110
AM_RDO
#define AM_RDO
Definition: ff.h:415
AM_ARC
#define AM_ARC
Definition: ff.h:419
f_readdir
FRESULT f_readdir(DIR *dp, FILINFO *fno)
Definition: ff.c:4667
f_getfree
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
Definition: ff.c:4790
fatfs_scan_files
static MEMSPACE int fatfs_scan_files(char *path)
Compute space used, number of directories and files contained under a specified directory.
Definition: fatfs_sup.c:180