P2-2 Harris Barton

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
hbarton7
Date:
Wed Nov 25 01:17:39 2020 +0000
Revision:
3:e2fb359d6545
Parent:
1:10330bce85cb
P2-2 Harris Barton;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DCchico 1:10330bce85cb 1 /* mbed Microcontroller Library
DCchico 1:10330bce85cb 2 * Copyright (c) 2006-2012 ARM Limited
DCchico 1:10330bce85cb 3 *
DCchico 1:10330bce85cb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
DCchico 1:10330bce85cb 5 * of this software and associated documentation files (the "Software"), to deal
DCchico 1:10330bce85cb 6 * in the Software without restriction, including without limitation the rights
DCchico 1:10330bce85cb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DCchico 1:10330bce85cb 8 * copies of the Software, and to permit persons to whom the Software is
DCchico 1:10330bce85cb 9 * furnished to do so, subject to the following conditions:
DCchico 1:10330bce85cb 10 *
DCchico 1:10330bce85cb 11 * The above copyright notice and this permission notice shall be included in
DCchico 1:10330bce85cb 12 * all copies or substantial portions of the Software.
DCchico 1:10330bce85cb 13 *
DCchico 1:10330bce85cb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DCchico 1:10330bce85cb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DCchico 1:10330bce85cb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DCchico 1:10330bce85cb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DCchico 1:10330bce85cb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DCchico 1:10330bce85cb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
DCchico 1:10330bce85cb 20 * SOFTWARE.
DCchico 1:10330bce85cb 21 */
DCchico 1:10330bce85cb 22 #include "mbed.h"
DCchico 1:10330bce85cb 23
DCchico 1:10330bce85cb 24 #include "ffconf.h"
DCchico 1:10330bce85cb 25 #include "mbed_debug.h"
DCchico 1:10330bce85cb 26
DCchico 1:10330bce85cb 27 #include "FATFileSystem.h"
DCchico 1:10330bce85cb 28 #include "FATFileHandle.h"
DCchico 1:10330bce85cb 29 #include "FATDirHandle.h"
DCchico 1:10330bce85cb 30
DCchico 1:10330bce85cb 31 DWORD get_fattime(void) {
DCchico 1:10330bce85cb 32 time_t rawtime;
DCchico 1:10330bce85cb 33 time(&rawtime);
DCchico 1:10330bce85cb 34 struct tm *ptm = localtime(&rawtime);
DCchico 1:10330bce85cb 35 return (DWORD)(ptm->tm_year - 80) << 25
DCchico 1:10330bce85cb 36 | (DWORD)(ptm->tm_mon + 1 ) << 21
DCchico 1:10330bce85cb 37 | (DWORD)(ptm->tm_mday ) << 16
DCchico 1:10330bce85cb 38 | (DWORD)(ptm->tm_hour ) << 11
DCchico 1:10330bce85cb 39 | (DWORD)(ptm->tm_min ) << 5
DCchico 1:10330bce85cb 40 | (DWORD)(ptm->tm_sec/2 );
DCchico 1:10330bce85cb 41 }
DCchico 1:10330bce85cb 42
DCchico 1:10330bce85cb 43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
DCchico 1:10330bce85cb 44
DCchico 1:10330bce85cb 45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
DCchico 1:10330bce85cb 46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
DCchico 1:10330bce85cb 47 for(int i=0; i<_VOLUMES; i++) {
DCchico 1:10330bce85cb 48 if(_ffs[i] == 0) {
DCchico 1:10330bce85cb 49 _ffs[i] = this;
DCchico 1:10330bce85cb 50 _fsid = i;
DCchico 1:10330bce85cb 51 debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
DCchico 1:10330bce85cb 52 f_mount(i, &_fs);
DCchico 1:10330bce85cb 53 return;
DCchico 1:10330bce85cb 54 }
DCchico 1:10330bce85cb 55 }
DCchico 1:10330bce85cb 56 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
DCchico 1:10330bce85cb 57 }
DCchico 1:10330bce85cb 58
DCchico 1:10330bce85cb 59 FATFileSystem::~FATFileSystem() {
DCchico 1:10330bce85cb 60 for (int i=0; i<_VOLUMES; i++) {
DCchico 1:10330bce85cb 61 if (_ffs[i] == this) {
DCchico 1:10330bce85cb 62 _ffs[i] = 0;
DCchico 1:10330bce85cb 63 f_mount(i, NULL);
DCchico 1:10330bce85cb 64 }
DCchico 1:10330bce85cb 65 }
DCchico 1:10330bce85cb 66 }
DCchico 1:10330bce85cb 67
DCchico 1:10330bce85cb 68 FileHandle *FATFileSystem::open(const char* name, int flags) {
DCchico 1:10330bce85cb 69 debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
DCchico 1:10330bce85cb 70 char n[64];
DCchico 1:10330bce85cb 71 sprintf(n, "%d:/%s", _fsid, name);
DCchico 1:10330bce85cb 72
DCchico 1:10330bce85cb 73 /* POSIX flags -> FatFS open mode */
DCchico 1:10330bce85cb 74 BYTE openmode;
DCchico 1:10330bce85cb 75 if (flags & O_RDWR) {
DCchico 1:10330bce85cb 76 openmode = FA_READ|FA_WRITE;
DCchico 1:10330bce85cb 77 } else if(flags & O_WRONLY) {
DCchico 1:10330bce85cb 78 openmode = FA_WRITE;
DCchico 1:10330bce85cb 79 } else {
DCchico 1:10330bce85cb 80 openmode = FA_READ;
DCchico 1:10330bce85cb 81 }
DCchico 1:10330bce85cb 82 if(flags & O_CREAT) {
DCchico 1:10330bce85cb 83 if(flags & O_TRUNC) {
DCchico 1:10330bce85cb 84 openmode |= FA_CREATE_ALWAYS;
DCchico 1:10330bce85cb 85 } else {
DCchico 1:10330bce85cb 86 openmode |= FA_OPEN_ALWAYS;
DCchico 1:10330bce85cb 87 }
DCchico 1:10330bce85cb 88 }
DCchico 1:10330bce85cb 89
DCchico 1:10330bce85cb 90 FIL fh;
DCchico 1:10330bce85cb 91 FRESULT res = f_open(&fh, n, openmode);
DCchico 1:10330bce85cb 92 if (res) {
DCchico 1:10330bce85cb 93 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
DCchico 1:10330bce85cb 94 return NULL;
DCchico 1:10330bce85cb 95 }
DCchico 1:10330bce85cb 96 if (flags & O_APPEND) {
DCchico 1:10330bce85cb 97 f_lseek(&fh, fh.fsize);
DCchico 1:10330bce85cb 98 }
DCchico 1:10330bce85cb 99 return new FATFileHandle(fh);
DCchico 1:10330bce85cb 100 }
DCchico 1:10330bce85cb 101
DCchico 1:10330bce85cb 102 int FATFileSystem::remove(const char *filename) {
DCchico 1:10330bce85cb 103 FRESULT res = f_unlink(filename);
DCchico 1:10330bce85cb 104 if (res) {
DCchico 1:10330bce85cb 105 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
DCchico 1:10330bce85cb 106 return -1;
DCchico 1:10330bce85cb 107 }
DCchico 1:10330bce85cb 108 return 0;
DCchico 1:10330bce85cb 109 }
DCchico 1:10330bce85cb 110
DCchico 1:10330bce85cb 111 int FATFileSystem::format() {
DCchico 1:10330bce85cb 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
DCchico 1:10330bce85cb 113 if (res) {
DCchico 1:10330bce85cb 114 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
DCchico 1:10330bce85cb 115 return -1;
DCchico 1:10330bce85cb 116 }
DCchico 1:10330bce85cb 117 return 0;
DCchico 1:10330bce85cb 118 }
DCchico 1:10330bce85cb 119
DCchico 1:10330bce85cb 120 DirHandle *FATFileSystem::opendir(const char *name) {
DCchico 1:10330bce85cb 121 FATFS_DIR dir;
DCchico 1:10330bce85cb 122 FRESULT res = f_opendir(&dir, name);
DCchico 1:10330bce85cb 123 if (res != 0) {
DCchico 1:10330bce85cb 124 return NULL;
DCchico 1:10330bce85cb 125 }
DCchico 1:10330bce85cb 126 return new FATDirHandle(dir);
DCchico 1:10330bce85cb 127 }
DCchico 1:10330bce85cb 128
DCchico 1:10330bce85cb 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
DCchico 1:10330bce85cb 130 FRESULT res = f_mkdir(name);
DCchico 1:10330bce85cb 131 return res == 0 ? 0 : -1;
DCchico 1:10330bce85cb 132 }