Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
SDFileSystem/FATFileSystem/Interface/FATFileSystem.cpp@0:a6a169de725f, 2013-05-27 (annotated)
- Committer:
- shimniok
- Date:
- Mon May 27 13:26:03 2013 +0000
- Revision:
- 0:a6a169de725f
Working version with priorities set and update time display
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| shimniok | 0:a6a169de725f | 1 | /* mbed Microcontroller Library - FATFileSystem |
| shimniok | 0:a6a169de725f | 2 | Copyright (c) 2008, sford */ |
| shimniok | 0:a6a169de725f | 3 | |
| shimniok | 0:a6a169de725f | 4 | //Modified by Thomas Hamilton, Copyright 2010 |
| shimniok | 0:a6a169de725f | 5 | |
| shimniok | 0:a6a169de725f | 6 | #include "FATFileSystem.h" |
| shimniok | 0:a6a169de725f | 7 | |
| shimniok | 0:a6a169de725f | 8 | DWORD get_fattime(void) |
| shimniok | 0:a6a169de725f | 9 | { |
| shimniok | 0:a6a169de725f | 10 | return 35719201; |
| shimniok | 0:a6a169de725f | 11 | } |
| shimniok | 0:a6a169de725f | 12 | |
| shimniok | 0:a6a169de725f | 13 | FATFileSystem* FATFileSystem::DriveArray[_DRIVES] = {0}; |
| shimniok | 0:a6a169de725f | 14 | |
| shimniok | 0:a6a169de725f | 15 | FATFileSystem::FATFileSystem(const char* SystemName) : FileSystemLike(SystemName) |
| shimniok | 0:a6a169de725f | 16 | { |
| shimniok | 0:a6a169de725f | 17 | for (unsigned char i = 0; i < _DRIVES; i++) |
| shimniok | 0:a6a169de725f | 18 | { |
| shimniok | 0:a6a169de725f | 19 | if(!DriveArray[i]) |
| shimniok | 0:a6a169de725f | 20 | { |
| shimniok | 0:a6a169de725f | 21 | DriveArray[i] = this; |
| shimniok | 0:a6a169de725f | 22 | Drive = i; |
| shimniok | 0:a6a169de725f | 23 | f_mount((BYTE)i, &FileSystemObject); |
| shimniok | 0:a6a169de725f | 24 | return; |
| shimniok | 0:a6a169de725f | 25 | } |
| shimniok | 0:a6a169de725f | 26 | } |
| shimniok | 0:a6a169de725f | 27 | } |
| shimniok | 0:a6a169de725f | 28 | |
| shimniok | 0:a6a169de725f | 29 | FATFileSystem::~FATFileSystem() |
| shimniok | 0:a6a169de725f | 30 | { |
| shimniok | 0:a6a169de725f | 31 | for (unsigned char i = 0; i < _DRIVES; i++) |
| shimniok | 0:a6a169de725f | 32 | { |
| shimniok | 0:a6a169de725f | 33 | if (DriveArray[i] == this) |
| shimniok | 0:a6a169de725f | 34 | { |
| shimniok | 0:a6a169de725f | 35 | DriveArray[i] = NULL; |
| shimniok | 0:a6a169de725f | 36 | f_mount((BYTE)i, NULL); |
| shimniok | 0:a6a169de725f | 37 | } |
| shimniok | 0:a6a169de725f | 38 | } |
| shimniok | 0:a6a169de725f | 39 | delete this; |
| shimniok | 0:a6a169de725f | 40 | } |
| shimniok | 0:a6a169de725f | 41 | |
| shimniok | 0:a6a169de725f | 42 | FileHandle* FATFileSystem::open(const char* filename, int flags) |
| shimniok | 0:a6a169de725f | 43 | { |
| shimniok | 0:a6a169de725f | 44 | FAT_FIL FileObject; |
| shimniok | 0:a6a169de725f | 45 | char FileName[64]; |
| shimniok | 0:a6a169de725f | 46 | BYTE ModeFlags = 0; |
| shimniok | 0:a6a169de725f | 47 | |
| shimniok | 0:a6a169de725f | 48 | sprintf(FileName, "%d:/%s", Drive, filename); |
| shimniok | 0:a6a169de725f | 49 | switch (flags & 3) |
| shimniok | 0:a6a169de725f | 50 | { |
| shimniok | 0:a6a169de725f | 51 | case O_RDONLY: ModeFlags = FA_READ; break; |
| shimniok | 0:a6a169de725f | 52 | case O_WRONLY: ModeFlags = FA_WRITE; break; |
| shimniok | 0:a6a169de725f | 53 | case O_RDWR: ModeFlags = FA_READ | FA_WRITE; break; |
| shimniok | 0:a6a169de725f | 54 | } |
| shimniok | 0:a6a169de725f | 55 | if(flags & O_CREAT) |
| shimniok | 0:a6a169de725f | 56 | { |
| shimniok | 0:a6a169de725f | 57 | if(flags & O_TRUNC) |
| shimniok | 0:a6a169de725f | 58 | { |
| shimniok | 0:a6a169de725f | 59 | ModeFlags |= FA_CREATE_ALWAYS; |
| shimniok | 0:a6a169de725f | 60 | } |
| shimniok | 0:a6a169de725f | 61 | else |
| shimniok | 0:a6a169de725f | 62 | { |
| shimniok | 0:a6a169de725f | 63 | ModeFlags |= FA_OPEN_ALWAYS; |
| shimniok | 0:a6a169de725f | 64 | } |
| shimniok | 0:a6a169de725f | 65 | } |
| shimniok | 0:a6a169de725f | 66 | else |
| shimniok | 0:a6a169de725f | 67 | { |
| shimniok | 0:a6a169de725f | 68 | ModeFlags |= FA_OPEN_EXISTING; |
| shimniok | 0:a6a169de725f | 69 | } |
| shimniok | 0:a6a169de725f | 70 | if (f_open(&FileObject, (const TCHAR*)FileName, ModeFlags)) |
| shimniok | 0:a6a169de725f | 71 | { |
| shimniok | 0:a6a169de725f | 72 | return NULL; |
| shimniok | 0:a6a169de725f | 73 | } |
| shimniok | 0:a6a169de725f | 74 | else |
| shimniok | 0:a6a169de725f | 75 | { |
| shimniok | 0:a6a169de725f | 76 | if (flags & O_APPEND) |
| shimniok | 0:a6a169de725f | 77 | { |
| shimniok | 0:a6a169de725f | 78 | f_lseek(&FileObject, (DWORD)FileObject.fsize); |
| shimniok | 0:a6a169de725f | 79 | } |
| shimniok | 0:a6a169de725f | 80 | return new FATFileHandle(FileObject); |
| shimniok | 0:a6a169de725f | 81 | } |
| shimniok | 0:a6a169de725f | 82 | } |
| shimniok | 0:a6a169de725f | 83 | |
| shimniok | 0:a6a169de725f | 84 | int FATFileSystem::remove(const char* filename) |
| shimniok | 0:a6a169de725f | 85 | { |
| shimniok | 0:a6a169de725f | 86 | char FileName[64]; |
| shimniok | 0:a6a169de725f | 87 | |
| shimniok | 0:a6a169de725f | 88 | sprintf(FileName, "%d:/%s", Drive, filename); |
| shimniok | 0:a6a169de725f | 89 | if (f_unlink((const TCHAR*)FileName)) |
| shimniok | 0:a6a169de725f | 90 | { |
| shimniok | 0:a6a169de725f | 91 | return -1; |
| shimniok | 0:a6a169de725f | 92 | } |
| shimniok | 0:a6a169de725f | 93 | else |
| shimniok | 0:a6a169de725f | 94 | { |
| shimniok | 0:a6a169de725f | 95 | return 0; |
| shimniok | 0:a6a169de725f | 96 | } |
| shimniok | 0:a6a169de725f | 97 | } |
| shimniok | 0:a6a169de725f | 98 | |
| shimniok | 0:a6a169de725f | 99 | int FATFileSystem::rename(const char* oldname, const char* newname) |
| shimniok | 0:a6a169de725f | 100 | { |
| shimniok | 0:a6a169de725f | 101 | char OldName[64]; |
| shimniok | 0:a6a169de725f | 102 | |
| shimniok | 0:a6a169de725f | 103 | sprintf(OldName, "%d:/%s", Drive, oldname); |
| shimniok | 0:a6a169de725f | 104 | if (f_rename((const TCHAR*)OldName, (const TCHAR*)newname)) |
| shimniok | 0:a6a169de725f | 105 | { |
| shimniok | 0:a6a169de725f | 106 | return -1; |
| shimniok | 0:a6a169de725f | 107 | } |
| shimniok | 0:a6a169de725f | 108 | else |
| shimniok | 0:a6a169de725f | 109 | { |
| shimniok | 0:a6a169de725f | 110 | return 0; |
| shimniok | 0:a6a169de725f | 111 | } |
| shimniok | 0:a6a169de725f | 112 | } |
| shimniok | 0:a6a169de725f | 113 | |
| shimniok | 0:a6a169de725f | 114 | DirHandle* FATFileSystem::opendir(const char* name) |
| shimniok | 0:a6a169de725f | 115 | { |
| shimniok | 0:a6a169de725f | 116 | FAT_DIR DirectoryObject; |
| shimniok | 0:a6a169de725f | 117 | char DirectoryName[64]; |
| shimniok | 0:a6a169de725f | 118 | |
| shimniok | 0:a6a169de725f | 119 | sprintf(DirectoryName, "%d:%s", Drive, name); |
| shimniok | 0:a6a169de725f | 120 | if (f_opendir(&DirectoryObject, (const TCHAR*)DirectoryName)) |
| shimniok | 0:a6a169de725f | 121 | { |
| shimniok | 0:a6a169de725f | 122 | return NULL; |
| shimniok | 0:a6a169de725f | 123 | } |
| shimniok | 0:a6a169de725f | 124 | else |
| shimniok | 0:a6a169de725f | 125 | { |
| shimniok | 0:a6a169de725f | 126 | return new FATDirHandle(DirectoryObject); |
| shimniok | 0:a6a169de725f | 127 | } |
| shimniok | 0:a6a169de725f | 128 | } |
| shimniok | 0:a6a169de725f | 129 | |
| shimniok | 0:a6a169de725f | 130 | int FATFileSystem::mkdir(const char* name, mode_t mode) |
| shimniok | 0:a6a169de725f | 131 | { |
| shimniok | 0:a6a169de725f | 132 | char DirectoryName[64]; |
| shimniok | 0:a6a169de725f | 133 | |
| shimniok | 0:a6a169de725f | 134 | sprintf(DirectoryName, "%d:%s", Drive, name); |
| shimniok | 0:a6a169de725f | 135 | if (f_mkdir((const TCHAR*)DirectoryName)) |
| shimniok | 0:a6a169de725f | 136 | { |
| shimniok | 0:a6a169de725f | 137 | return -1; |
| shimniok | 0:a6a169de725f | 138 | } |
| shimniok | 0:a6a169de725f | 139 | else |
| shimniok | 0:a6a169de725f | 140 | { |
| shimniok | 0:a6a169de725f | 141 | return 0; |
| shimniok | 0:a6a169de725f | 142 | } |
| shimniok | 0:a6a169de725f | 143 | } |
| shimniok | 0:a6a169de725f | 144 | |
| shimniok | 0:a6a169de725f | 145 | int FATFileSystem::format(unsigned int allocationunit) |
| shimniok | 0:a6a169de725f | 146 | { |
| shimniok | 0:a6a169de725f | 147 | if (f_mkfs(Drive, 0, allocationunit)) |
| shimniok | 0:a6a169de725f | 148 | { |
| shimniok | 0:a6a169de725f | 149 | return -1; |
| shimniok | 0:a6a169de725f | 150 | } |
| shimniok | 0:a6a169de725f | 151 | else |
| shimniok | 0:a6a169de725f | 152 | { |
| shimniok | 0:a6a169de725f | 153 | return 0; |
| shimniok | 0:a6a169de725f | 154 | } |
| shimniok | 0:a6a169de725f | 155 | } |