rtos sd encoder systems

Dependencies:   mbed

Committer:
eembed
Date:
Fri Aug 30 11:55:47 2019 +0000
Revision:
3:a921792d9913
Parent:
0:5459cdde6298
commit before edit; ~JKD;

Who changed what in which revision?

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