Updated get_fattime to use rtc and provide a date/time. This has been an annoying missing feature.

Dependents:   IoTGateway_Basic y_XBeeTest_5_read CameraC1098_picture LifeCam ... more

Committer:
SomeRandomBloke
Date:
Mon Apr 02 22:06:35 2012 +0000
Revision:
1:5baba5d5b728
Parent:
0:93acdd9f65f4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:93acdd9f65f4 1 /* mbed Microcontroller Library - FATFileSystem
SomeRandomBloke 0:93acdd9f65f4 2 * Copyright (c) 2008, sford
SomeRandomBloke 0:93acdd9f65f4 3 */
SomeRandomBloke 0:93acdd9f65f4 4
SomeRandomBloke 0:93acdd9f65f4 5 #include "FATFileSystem.h"
SomeRandomBloke 0:93acdd9f65f4 6
SomeRandomBloke 0:93acdd9f65f4 7 #include "mbed.h"
SomeRandomBloke 0:93acdd9f65f4 8
SomeRandomBloke 0:93acdd9f65f4 9 #include "FileSystemLike.h"
SomeRandomBloke 0:93acdd9f65f4 10 #include "FATFileHandle.h"
SomeRandomBloke 0:93acdd9f65f4 11 #include "FATDirHandle.h"
SomeRandomBloke 0:93acdd9f65f4 12 #include "ff.h"
SomeRandomBloke 0:93acdd9f65f4 13 //#include "Debug.h"
SomeRandomBloke 0:93acdd9f65f4 14 #include <stdio.h>
SomeRandomBloke 0:93acdd9f65f4 15 #include <stdlib.h>
SomeRandomBloke 0:93acdd9f65f4 16 #include <time.h>
SomeRandomBloke 0:93acdd9f65f4 17
SomeRandomBloke 0:93acdd9f65f4 18 /*
SomeRandomBloke 0:93acdd9f65f4 19 Currnet time is returned with packed into a DWORD value. The bit field is as follows:
SomeRandomBloke 0:93acdd9f65f4 20 bit31:25
SomeRandomBloke 0:93acdd9f65f4 21 Year from 1980 (0..127)
SomeRandomBloke 0:93acdd9f65f4 22 bit24:21
SomeRandomBloke 0:93acdd9f65f4 23 Month (1..12)
SomeRandomBloke 0:93acdd9f65f4 24 bit20:16
SomeRandomBloke 0:93acdd9f65f4 25 Day in month(1..31)
SomeRandomBloke 0:93acdd9f65f4 26 bit15:11
SomeRandomBloke 0:93acdd9f65f4 27 Hour (0..23)
SomeRandomBloke 0:93acdd9f65f4 28 bit10:5
SomeRandomBloke 0:93acdd9f65f4 29 Minute (0..59)
SomeRandomBloke 0:93acdd9f65f4 30 bit4:0
SomeRandomBloke 0:93acdd9f65f4 31 Second / 2 (0..29)
SomeRandomBloke 0:93acdd9f65f4 32
SomeRandomBloke 0:93acdd9f65f4 33
SomeRandomBloke 0:93acdd9f65f4 34 int tm_sec;
SomeRandomBloke 0:93acdd9f65f4 35 int tm_min;
SomeRandomBloke 0:93acdd9f65f4 36 int tm_hour;
SomeRandomBloke 0:93acdd9f65f4 37 int tm_mday;
SomeRandomBloke 0:93acdd9f65f4 38 int tm_mon;
SomeRandomBloke 0:93acdd9f65f4 39 int tm_year;
SomeRandomBloke 0:93acdd9f65f4 40 int tm_wday;
SomeRandomBloke 0:93acdd9f65f4 41 int tm_yday;
SomeRandomBloke 0:93acdd9f65f4 42 int tm_isdst;
SomeRandomBloke 0:93acdd9f65f4 43
SomeRandomBloke 0:93acdd9f65f4 44 */
SomeRandomBloke 0:93acdd9f65f4 45
SomeRandomBloke 0:93acdd9f65f4 46 DWORD get_fattime (void) {
SomeRandomBloke 0:93acdd9f65f4 47 time_t rawtime;
SomeRandomBloke 0:93acdd9f65f4 48 struct tm *ptm;
SomeRandomBloke 0:93acdd9f65f4 49 time ( &rawtime );
SomeRandomBloke 0:93acdd9f65f4 50 ptm = localtime ( &rawtime );
SomeRandomBloke 0:93acdd9f65f4 51 FFSDEBUG("DTM: %d/%d/%d %d:%d:%d\n",ptm->tm_year,ptm->tm_mon,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
SomeRandomBloke 0:93acdd9f65f4 52 DWORD fattime = (DWORD)(ptm->tm_year - 80) << 25
SomeRandomBloke 0:93acdd9f65f4 53 | (DWORD)(ptm->tm_mon + 1) << 21
SomeRandomBloke 0:93acdd9f65f4 54 | (DWORD)(ptm->tm_mday) << 16
SomeRandomBloke 0:93acdd9f65f4 55 | (DWORD)(ptm->tm_hour) << 11
SomeRandomBloke 0:93acdd9f65f4 56 | (DWORD)(ptm->tm_min) << 5
SomeRandomBloke 0:93acdd9f65f4 57 | (DWORD)(ptm->tm_sec/2);
SomeRandomBloke 0:93acdd9f65f4 58
SomeRandomBloke 0:93acdd9f65f4 59 FFSDEBUG("Converted: %x\n",fattime);
SomeRandomBloke 0:93acdd9f65f4 60 return fattime;
SomeRandomBloke 0:93acdd9f65f4 61 }
SomeRandomBloke 0:93acdd9f65f4 62
SomeRandomBloke 0:93acdd9f65f4 63 namespace mbed {
SomeRandomBloke 0:93acdd9f65f4 64
SomeRandomBloke 0:93acdd9f65f4 65 #if FFSDEBUG_ENABLED
SomeRandomBloke 0:93acdd9f65f4 66 static const char *FR_ERRORS[] = {
SomeRandomBloke 0:93acdd9f65f4 67 "FR_OK = 0",
SomeRandomBloke 0:93acdd9f65f4 68 "FR_NOT_READY",
SomeRandomBloke 0:93acdd9f65f4 69 "FR_NO_FILE",
SomeRandomBloke 0:93acdd9f65f4 70 "FR_NO_PATH",
SomeRandomBloke 0:93acdd9f65f4 71 "FR_INVALID_NAME",
SomeRandomBloke 0:93acdd9f65f4 72 "FR_INVALID_DRIVE",
SomeRandomBloke 0:93acdd9f65f4 73 "FR_DENIED",
SomeRandomBloke 0:93acdd9f65f4 74 "FR_EXIST",
SomeRandomBloke 0:93acdd9f65f4 75 "FR_RW_ERROR",
SomeRandomBloke 0:93acdd9f65f4 76 "FR_WRITE_PROTECTED",
SomeRandomBloke 0:93acdd9f65f4 77 "FR_NOT_ENABLED",
SomeRandomBloke 0:93acdd9f65f4 78 "FR_NO_FILESYSTEM",
SomeRandomBloke 0:93acdd9f65f4 79 "FR_INVALID_OBJECT",
SomeRandomBloke 0:93acdd9f65f4 80 "FR_MKFS_ABORTED"
SomeRandomBloke 0:93acdd9f65f4 81 };
SomeRandomBloke 0:93acdd9f65f4 82 #endif
SomeRandomBloke 0:93acdd9f65f4 83
SomeRandomBloke 0:93acdd9f65f4 84 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
SomeRandomBloke 0:93acdd9f65f4 85
SomeRandomBloke 0:93acdd9f65f4 86 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
SomeRandomBloke 0:93acdd9f65f4 87 FFSDEBUG("FATFileSystem(%s)\n", n);
SomeRandomBloke 0:93acdd9f65f4 88 for(int i=0; i<_VOLUMES; i++) {
SomeRandomBloke 0:93acdd9f65f4 89 if(_ffs[i] == 0) {
SomeRandomBloke 0:93acdd9f65f4 90 _ffs[i] = this;
SomeRandomBloke 0:93acdd9f65f4 91 _fsid = i;
SomeRandomBloke 0:93acdd9f65f4 92 FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
SomeRandomBloke 0:93acdd9f65f4 93 f_mount(i, &_fs);
SomeRandomBloke 0:93acdd9f65f4 94 return;
SomeRandomBloke 0:93acdd9f65f4 95 }
SomeRandomBloke 0:93acdd9f65f4 96 }
SomeRandomBloke 0:93acdd9f65f4 97 error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n);
SomeRandomBloke 0:93acdd9f65f4 98 }
SomeRandomBloke 0:93acdd9f65f4 99
SomeRandomBloke 0:93acdd9f65f4 100 FATFileSystem::~FATFileSystem() {
SomeRandomBloke 0:93acdd9f65f4 101 for(int i=0; i<_VOLUMES; i++) {
SomeRandomBloke 0:93acdd9f65f4 102 if(_ffs[i] == this) {
SomeRandomBloke 0:93acdd9f65f4 103 _ffs[i] = 0;
SomeRandomBloke 0:93acdd9f65f4 104 f_mount(i, NULL);
SomeRandomBloke 0:93acdd9f65f4 105 }
SomeRandomBloke 0:93acdd9f65f4 106 }
SomeRandomBloke 0:93acdd9f65f4 107 }
SomeRandomBloke 0:93acdd9f65f4 108
SomeRandomBloke 0:93acdd9f65f4 109 FileHandle *FATFileSystem::open(const char* name, int flags) {
SomeRandomBloke 0:93acdd9f65f4 110 FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
SomeRandomBloke 0:93acdd9f65f4 111 char n[64];
SomeRandomBloke 0:93acdd9f65f4 112 sprintf(n, "%d:/%s", _fsid, name);
SomeRandomBloke 0:93acdd9f65f4 113
SomeRandomBloke 0:93acdd9f65f4 114 /* POSIX flags -> FatFS open mode */
SomeRandomBloke 0:93acdd9f65f4 115 BYTE openmode;
SomeRandomBloke 0:93acdd9f65f4 116 if(flags & O_RDWR) {
SomeRandomBloke 0:93acdd9f65f4 117 openmode = FA_READ|FA_WRITE;
SomeRandomBloke 0:93acdd9f65f4 118 } else if(flags & O_WRONLY) {
SomeRandomBloke 0:93acdd9f65f4 119 openmode = FA_WRITE;
SomeRandomBloke 0:93acdd9f65f4 120 } else {
SomeRandomBloke 0:93acdd9f65f4 121 openmode = FA_READ;
SomeRandomBloke 0:93acdd9f65f4 122 }
SomeRandomBloke 0:93acdd9f65f4 123 if(flags & O_CREAT) {
SomeRandomBloke 0:93acdd9f65f4 124 if(flags & O_TRUNC) {
SomeRandomBloke 0:93acdd9f65f4 125 openmode |= FA_CREATE_ALWAYS;
SomeRandomBloke 0:93acdd9f65f4 126 } else {
SomeRandomBloke 0:93acdd9f65f4 127 openmode |= FA_OPEN_ALWAYS;
SomeRandomBloke 0:93acdd9f65f4 128 }
SomeRandomBloke 0:93acdd9f65f4 129 }
SomeRandomBloke 0:93acdd9f65f4 130
SomeRandomBloke 0:93acdd9f65f4 131 FIL fh;
SomeRandomBloke 0:93acdd9f65f4 132 FRESULT res = f_open(&fh, n, openmode);
SomeRandomBloke 0:93acdd9f65f4 133 if(res) {
SomeRandomBloke 0:93acdd9f65f4 134 FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:93acdd9f65f4 135 return NULL;
SomeRandomBloke 0:93acdd9f65f4 136 }
SomeRandomBloke 0:93acdd9f65f4 137 if(flags & O_APPEND) {
SomeRandomBloke 0:93acdd9f65f4 138 f_lseek(&fh, fh.fsize);
SomeRandomBloke 0:93acdd9f65f4 139 }
SomeRandomBloke 0:93acdd9f65f4 140 return new FATFileHandle(fh);
SomeRandomBloke 0:93acdd9f65f4 141 }
SomeRandomBloke 0:93acdd9f65f4 142
SomeRandomBloke 0:93acdd9f65f4 143 int FATFileSystem::remove(const char *filename) {
SomeRandomBloke 0:93acdd9f65f4 144 FRESULT res = f_unlink(filename);
SomeRandomBloke 0:93acdd9f65f4 145 if(res) {
SomeRandomBloke 0:93acdd9f65f4 146 FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:93acdd9f65f4 147 return -1;
SomeRandomBloke 0:93acdd9f65f4 148 }
SomeRandomBloke 0:93acdd9f65f4 149 return 0;
SomeRandomBloke 0:93acdd9f65f4 150 }
SomeRandomBloke 0:93acdd9f65f4 151
SomeRandomBloke 0:93acdd9f65f4 152 int FATFileSystem::format() {
SomeRandomBloke 0:93acdd9f65f4 153 FFSDEBUG("format()\n");
SomeRandomBloke 0:93acdd9f65f4 154 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
SomeRandomBloke 0:93acdd9f65f4 155 if(res) {
SomeRandomBloke 0:93acdd9f65f4 156 FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:93acdd9f65f4 157 return -1;
SomeRandomBloke 0:93acdd9f65f4 158 }
SomeRandomBloke 0:93acdd9f65f4 159 return 0;
SomeRandomBloke 0:93acdd9f65f4 160 }
SomeRandomBloke 0:93acdd9f65f4 161
SomeRandomBloke 0:93acdd9f65f4 162 DirHandle *FATFileSystem::opendir(const char *name) {
SomeRandomBloke 0:93acdd9f65f4 163 FATFS_DIR dir;
SomeRandomBloke 0:93acdd9f65f4 164 FRESULT res = f_opendir(&dir, name);
SomeRandomBloke 0:93acdd9f65f4 165 if(res != 0) {
SomeRandomBloke 0:93acdd9f65f4 166 return NULL;
SomeRandomBloke 0:93acdd9f65f4 167 }
SomeRandomBloke 0:93acdd9f65f4 168 return new FATDirHandle(dir);
SomeRandomBloke 0:93acdd9f65f4 169 }
SomeRandomBloke 0:93acdd9f65f4 170
SomeRandomBloke 0:93acdd9f65f4 171 int FATFileSystem::mkdir(const char *name, mode_t mode) {
SomeRandomBloke 0:93acdd9f65f4 172 FRESULT res = f_mkdir(name);
SomeRandomBloke 0:93acdd9f65f4 173 return res == 0 ? 0 : -1;
SomeRandomBloke 0:93acdd9f65f4 174 }
SomeRandomBloke 0:93acdd9f65f4 175
SomeRandomBloke 0:93acdd9f65f4 176 } // namespace mbed