microSDカードからWaveファイルを再生するサンプルです。

Dependencies:   mbed FATFileSystem

Committer:
jksoft
Date:
Mon May 12 14:45:42 2014 +0000
Revision:
0:e9f196d85a46
First edition

Who changed what in which revision?

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