Georgia Tech ECE2035 Project P2-2 shell code.

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

q

Committer:
rashidi1saeed
Date:
Mon Oct 28 05:26:35 2019 +0000
Revision:
3:e4690ad5a4d2
Parent:
0:35660d7952f7
added more comments

Who changed what in which revision?

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