SD Card Interface class. Log raw data bytes to memory addresses of your choice, or format the card and use the FAT file system to write files.
FATFileSystem/Interface/FATFileSystem.cpp@4:9a5878d316d5, 2010-08-27 (annotated)
- Committer:
- Blaze513
- Date:
- Fri Aug 27 00:59:28 2010 +0000
- Revision:
- 4:9a5878d316d5
- Parent:
- 3:210eb67b260c
- Child:
- 6:ddf09d859ed7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Blaze513 | 2:716abfa4a4d0 | 1 | /* mbed Microcontroller Library - FATFileSystem |
Blaze513 | 2:716abfa4a4d0 | 2 | Copyright (c) 2008, sford */ |
Blaze513 | 2:716abfa4a4d0 | 3 | |
Blaze513 | 3:210eb67b260c | 4 | //Modified by Thomas Hamilton, Copyright 2010 |
Blaze513 | 3:210eb67b260c | 5 | |
Blaze513 | 2:716abfa4a4d0 | 6 | #include "FATFileSystem.h" |
Blaze513 | 2:716abfa4a4d0 | 7 | |
Blaze513 | 2:716abfa4a4d0 | 8 | DWORD get_fattime(void) |
Blaze513 | 2:716abfa4a4d0 | 9 | { |
Blaze513 | 2:716abfa4a4d0 | 10 | return 35719201; |
Blaze513 | 2:716abfa4a4d0 | 11 | } |
Blaze513 | 2:716abfa4a4d0 | 12 | |
Blaze513 | 2:716abfa4a4d0 | 13 | FATFileSystem* FATFileSystem::DriveArray[_DRIVES] = {0}; |
Blaze513 | 2:716abfa4a4d0 | 14 | |
Blaze513 | 2:716abfa4a4d0 | 15 | FATFileSystem::FATFileSystem(const char* SystemName) : FileSystemLike(SystemName) |
Blaze513 | 2:716abfa4a4d0 | 16 | { |
Blaze513 | 2:716abfa4a4d0 | 17 | for (unsigned char i = 0; i < _DRIVES; i++) |
Blaze513 | 2:716abfa4a4d0 | 18 | { |
Blaze513 | 2:716abfa4a4d0 | 19 | if(!DriveArray[i]) |
Blaze513 | 2:716abfa4a4d0 | 20 | { |
Blaze513 | 2:716abfa4a4d0 | 21 | DriveArray[i] = this; |
Blaze513 | 2:716abfa4a4d0 | 22 | Drive = i; |
Blaze513 | 2:716abfa4a4d0 | 23 | f_mount((BYTE)i, &FileSystemObject); |
Blaze513 | 2:716abfa4a4d0 | 24 | return; |
Blaze513 | 2:716abfa4a4d0 | 25 | } |
Blaze513 | 2:716abfa4a4d0 | 26 | } |
Blaze513 | 2:716abfa4a4d0 | 27 | } |
Blaze513 | 4:9a5878d316d5 | 28 | |
Blaze513 | 2:716abfa4a4d0 | 29 | FATFileSystem::~FATFileSystem() |
Blaze513 | 2:716abfa4a4d0 | 30 | { |
Blaze513 | 2:716abfa4a4d0 | 31 | for (unsigned char i = 0; i < _DRIVES; i++) |
Blaze513 | 2:716abfa4a4d0 | 32 | { |
Blaze513 | 2:716abfa4a4d0 | 33 | if (DriveArray[i] == this) |
Blaze513 | 2:716abfa4a4d0 | 34 | { |
Blaze513 | 2:716abfa4a4d0 | 35 | DriveArray[i] = NULL; |
Blaze513 | 2:716abfa4a4d0 | 36 | f_mount((BYTE)i, NULL); |
Blaze513 | 2:716abfa4a4d0 | 37 | } |
Blaze513 | 2:716abfa4a4d0 | 38 | } |
Blaze513 | 3:210eb67b260c | 39 | delete this; |
Blaze513 | 2:716abfa4a4d0 | 40 | } |
Blaze513 | 4:9a5878d316d5 | 41 | |
Blaze513 | 2:716abfa4a4d0 | 42 | FileHandle* FATFileSystem::open(const char* filename, int flags) |
Blaze513 | 2:716abfa4a4d0 | 43 | { |
Blaze513 | 2:716abfa4a4d0 | 44 | FAT_FIL FileObject; |
Blaze513 | 2:716abfa4a4d0 | 45 | char FileName[64]; |
Blaze513 | 2:716abfa4a4d0 | 46 | BYTE ModeFlags = 0; |
Blaze513 | 4:9a5878d316d5 | 47 | |
Blaze513 | 2:716abfa4a4d0 | 48 | sprintf(FileName, "%d:/%s", Drive, filename); |
Blaze513 | 2:716abfa4a4d0 | 49 | switch (flags & 3) |
Blaze513 | 2:716abfa4a4d0 | 50 | { |
Blaze513 | 2:716abfa4a4d0 | 51 | case O_RDONLY: ModeFlags = FA_READ; break; |
Blaze513 | 2:716abfa4a4d0 | 52 | case O_WRONLY: ModeFlags = FA_WRITE; break; |
Blaze513 | 2:716abfa4a4d0 | 53 | case O_RDWR: ModeFlags = FA_READ | FA_WRITE; break; |
Blaze513 | 2:716abfa4a4d0 | 54 | } |
Blaze513 | 2:716abfa4a4d0 | 55 | if(flags & O_CREAT) |
Blaze513 | 2:716abfa4a4d0 | 56 | { |
Blaze513 | 2:716abfa4a4d0 | 57 | if(flags & O_TRUNC) |
Blaze513 | 2:716abfa4a4d0 | 58 | { |
Blaze513 | 2:716abfa4a4d0 | 59 | ModeFlags |= FA_CREATE_ALWAYS; |
Blaze513 | 2:716abfa4a4d0 | 60 | } |
Blaze513 | 2:716abfa4a4d0 | 61 | else |
Blaze513 | 2:716abfa4a4d0 | 62 | { |
Blaze513 | 2:716abfa4a4d0 | 63 | ModeFlags |= FA_OPEN_ALWAYS; |
Blaze513 | 2:716abfa4a4d0 | 64 | } |
Blaze513 | 2:716abfa4a4d0 | 65 | } |
Blaze513 | 2:716abfa4a4d0 | 66 | else |
Blaze513 | 2:716abfa4a4d0 | 67 | { |
Blaze513 | 2:716abfa4a4d0 | 68 | ModeFlags |= FA_OPEN_EXISTING; |
Blaze513 | 2:716abfa4a4d0 | 69 | } |
Blaze513 | 2:716abfa4a4d0 | 70 | if (f_open(&FileObject, (const TCHAR*)FileName, ModeFlags)) |
Blaze513 | 2:716abfa4a4d0 | 71 | { |
Blaze513 | 2:716abfa4a4d0 | 72 | return NULL; |
Blaze513 | 2:716abfa4a4d0 | 73 | } |
Blaze513 | 2:716abfa4a4d0 | 74 | else |
Blaze513 | 2:716abfa4a4d0 | 75 | { |
Blaze513 | 2:716abfa4a4d0 | 76 | if (flags & O_APPEND) |
Blaze513 | 2:716abfa4a4d0 | 77 | { |
Blaze513 | 2:716abfa4a4d0 | 78 | f_lseek(&FileObject, (DWORD)FileObject.fsize); |
Blaze513 | 2:716abfa4a4d0 | 79 | } |
Blaze513 | 2:716abfa4a4d0 | 80 | return new FATFileHandle(FileObject); |
Blaze513 | 2:716abfa4a4d0 | 81 | } |
Blaze513 | 2:716abfa4a4d0 | 82 | } |
Blaze513 | 4:9a5878d316d5 | 83 | |
Blaze513 | 2:716abfa4a4d0 | 84 | int FATFileSystem::remove(const char* filename) |
Blaze513 | 2:716abfa4a4d0 | 85 | { |
Blaze513 | 2:716abfa4a4d0 | 86 | char FileName[64]; |
Blaze513 | 4:9a5878d316d5 | 87 | |
Blaze513 | 2:716abfa4a4d0 | 88 | sprintf(FileName, "%d:/%s", Drive, filename); |
Blaze513 | 2:716abfa4a4d0 | 89 | if (f_unlink((const TCHAR*)FileName)) |
Blaze513 | 2:716abfa4a4d0 | 90 | { |
Blaze513 | 2:716abfa4a4d0 | 91 | return -1; |
Blaze513 | 2:716abfa4a4d0 | 92 | } |
Blaze513 | 2:716abfa4a4d0 | 93 | else |
Blaze513 | 2:716abfa4a4d0 | 94 | { |
Blaze513 | 2:716abfa4a4d0 | 95 | return 0; |
Blaze513 | 2:716abfa4a4d0 | 96 | } |
Blaze513 | 2:716abfa4a4d0 | 97 | } |
Blaze513 | 2:716abfa4a4d0 | 98 | |
Blaze513 | 2:716abfa4a4d0 | 99 | int FATFileSystem::rename(const char* oldname, const char* newname) |
Blaze513 | 2:716abfa4a4d0 | 100 | { |
Blaze513 | 4:9a5878d316d5 | 101 | /*char OldName[64]; |
Blaze513 | 4:9a5878d316d5 | 102 | |
Blaze513 | 2:716abfa4a4d0 | 103 | sprintf(OldName, "%d:/%s", Drive, oldname); |
Blaze513 | 2:716abfa4a4d0 | 104 | if (f_rename((const TCHAR*)OldName, (const TCHAR*)newname)) |
Blaze513 | 2:716abfa4a4d0 | 105 | { |
Blaze513 | 2:716abfa4a4d0 | 106 | return -1; |
Blaze513 | 2:716abfa4a4d0 | 107 | } |
Blaze513 | 2:716abfa4a4d0 | 108 | else |
Blaze513 | 2:716abfa4a4d0 | 109 | { |
Blaze513 | 2:716abfa4a4d0 | 110 | return 0; |
Blaze513 | 4:9a5878d316d5 | 111 | }*/ |
Blaze513 | 4:9a5878d316d5 | 112 | return 0; |
Blaze513 | 2:716abfa4a4d0 | 113 | } |
Blaze513 | 2:716abfa4a4d0 | 114 | |
Blaze513 | 2:716abfa4a4d0 | 115 | DirHandle* FATFileSystem::opendir(const char* name) |
Blaze513 | 2:716abfa4a4d0 | 116 | { |
Blaze513 | 2:716abfa4a4d0 | 117 | FAT_DIR DirectoryObject; |
Blaze513 | 2:716abfa4a4d0 | 118 | char DirectoryName[64]; |
Blaze513 | 4:9a5878d316d5 | 119 | |
Blaze513 | 2:716abfa4a4d0 | 120 | sprintf(DirectoryName, "%d:%s", Drive, name); |
Blaze513 | 2:716abfa4a4d0 | 121 | if (f_opendir(&DirectoryObject, (const TCHAR*)DirectoryName)) |
Blaze513 | 2:716abfa4a4d0 | 122 | { |
Blaze513 | 2:716abfa4a4d0 | 123 | return NULL; |
Blaze513 | 2:716abfa4a4d0 | 124 | } |
Blaze513 | 2:716abfa4a4d0 | 125 | else |
Blaze513 | 2:716abfa4a4d0 | 126 | { |
Blaze513 | 2:716abfa4a4d0 | 127 | return new FATDirHandle(DirectoryObject); |
Blaze513 | 2:716abfa4a4d0 | 128 | } |
Blaze513 | 2:716abfa4a4d0 | 129 | } |
Blaze513 | 2:716abfa4a4d0 | 130 | |
Blaze513 | 2:716abfa4a4d0 | 131 | int FATFileSystem::mkdir(const char* name, mode_t mode) |
Blaze513 | 2:716abfa4a4d0 | 132 | { |
Blaze513 | 2:716abfa4a4d0 | 133 | char DirectoryName[64]; |
Blaze513 | 4:9a5878d316d5 | 134 | |
Blaze513 | 2:716abfa4a4d0 | 135 | sprintf(DirectoryName, "%d:%s", Drive, name); |
Blaze513 | 2:716abfa4a4d0 | 136 | if (f_mkdir((const TCHAR*)DirectoryName)) |
Blaze513 | 2:716abfa4a4d0 | 137 | { |
Blaze513 | 2:716abfa4a4d0 | 138 | return -1; |
Blaze513 | 2:716abfa4a4d0 | 139 | } |
Blaze513 | 2:716abfa4a4d0 | 140 | else |
Blaze513 | 2:716abfa4a4d0 | 141 | { |
Blaze513 | 2:716abfa4a4d0 | 142 | return 0; |
Blaze513 | 2:716abfa4a4d0 | 143 | } |
Blaze513 | 1:94c648931f84 | 144 | } |