first 2016/02 SDFileSystemDMA inherited from Official SDFileSystem.
Dependents: SDFileSystemDMA-test DmdFullRGB_0_1
Fork of SDFileSystemDMA by
SDFileSystemDMA is enhanced SDFileSystem library for STM32 micros by using DMA functionality.
Max read transfer rate reaches over 2MByte/sec at 24MHz SPI clock if enough read buffer size is set.
Even though minimum read buffer size (512Byte) is set, read transfer rate will reach over 1MByte/sec at 24MHz SPI Clock.
( but depends on the ability of each SD card)
Test program is here.
https://developer.mbed.org/users/mimi3/code/SDFileSystemDMA-test/
Supported SPI port is shown below table.
(v): Verified. It works well.
(w): Probably it will work well. (not tested)
(c): Only compiled. (not tested)
(f): Over flash.
(r): Only read mode. (when _FS_READONLY==1)
(u) Under construction
(z): Dose not work.
Caution
If your board has SRAM less than or equal to 8KB, the buffer size must be set to 512 Bytes.
Supported Boards:
Cortex-M0
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
NUCLEO-F030R8 | 8KB | (v) | ||
DISCO-F051R8 | 8KB | (w) | ||
4KB | (f) | |||
NUCLEO-F042K6 | 6KB | (r) | ||
NUCLEO-F070RB | 16KB | (w) | ||
NUCLEO-F072RB | 16KB | (w) | ||
NUCLEO-F091RC | 32KB | (c) |
Cortex-L0
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-L053C8 | 8KB | (c) | ||
NUCLEO-L053R8 | 8KB | (c) | ||
NUCLEO-L073RZ | 20KB | (c) |
Cortex-M3
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-F100RB | 8KB | (v) | (v) | - |
BLUEPILL-F103CB | 20KB | (w) | (w) | - |
NUCLEO-F103RB | 20KB | (v) | (v) | - |
NUCLEO-L152RE | 80KB | (v) | (w) | - |
MOTE-L152RC | 32KB | (w) | (w) | - |
Cortex-M4
F3
Board | SRAM | SPI1 | SPI2 | SPI3 |
---|---|---|---|---|
DISCO-F303VC | 40KB | - | (v) | (v) |
NUCLEO-F303RE | 64KB | (w) | (w) | (w) |
NUCLEO-F302R8 | 16KB | - | - | (c) |
NUCLEO-F303K8 | 12KB | (c) | - | - |
DISCO-F334C8 | 12KB | (c) | - | - |
NUCLEO-F334R8 | 12KB | (c) | - | - |
F4
Board | SPI1 | SPI2 | SPI3 |
---|---|---|---|
ELMO-F411RE | (w) | - | (w) |
MTS-MDOT-F411RE | (u) | - | (u) |
MTS-DRAGONFLY-F411RE | (w) | - | (w) |
NUCLEO-F411RE | (v) | - | (v) |
NUCLEO-F401RE | (w) | - | (w) |
MTS-MDOT-F405RG | (u) | - | (u) |
NUCLEO-F410RB | (c) | - | (c) |
NUCLEO-F446RE | (c) | - | (c) |
NUCLEO-F429ZI | (c) | - | (c) |
B96B-F446VE | (c) | - | (c) |
NUCLEO-F446ZE | (c) | - | (c) |
DISCO-F429ZI | (u) | - | (u) |
DISCO-F469NI | (c) | - | (c) |
Information
This library is set to use "short file name" in SDFileSystemDMA/FATFileSystem/ChaN/ffconf.h . ( _USE_LFN=0)
You can change this option to _USE_LFN=1 .
FATFileSystem/FATFileSystem.cpp@31:cfad42bb789a, 2016-08-27 (annotated)
- Committer:
- dinau
- Date:
- Sat Aug 27 14:03:13 2016 +0900
- Revision:
- 31:cfad42bb789a
- Parent:
- 10:b48d3ace55db
- Child:
- 34:b065d97cb70f
Fixed: Compile error at mbed os 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimi3 | 10:b48d3ace55db | 1 | /* mbed Microcontroller Library |
mimi3 | 10:b48d3ace55db | 2 | * Copyright (c) 2006-2012 ARM Limited |
mimi3 | 10:b48d3ace55db | 3 | * |
mimi3 | 10:b48d3ace55db | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mimi3 | 10:b48d3ace55db | 5 | * of this software and associated documentation files (the "Software"), to deal |
mimi3 | 10:b48d3ace55db | 6 | * in the Software without restriction, including without limitation the rights |
mimi3 | 10:b48d3ace55db | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mimi3 | 10:b48d3ace55db | 8 | * copies of the Software, and to permit persons to whom the Software is |
mimi3 | 10:b48d3ace55db | 9 | * furnished to do so, subject to the following conditions: |
mimi3 | 10:b48d3ace55db | 10 | * |
mimi3 | 10:b48d3ace55db | 11 | * The above copyright notice and this permission notice shall be included in |
mimi3 | 10:b48d3ace55db | 12 | * all copies or substantial portions of the Software. |
mimi3 | 10:b48d3ace55db | 13 | * |
mimi3 | 10:b48d3ace55db | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mimi3 | 10:b48d3ace55db | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mimi3 | 10:b48d3ace55db | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mimi3 | 10:b48d3ace55db | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mimi3 | 10:b48d3ace55db | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mimi3 | 10:b48d3ace55db | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mimi3 | 10:b48d3ace55db | 20 | * SOFTWARE. |
mimi3 | 10:b48d3ace55db | 21 | */ |
mimi3 | 10:b48d3ace55db | 22 | #include "mbed.h" |
mimi3 | 10:b48d3ace55db | 23 | |
mimi3 | 10:b48d3ace55db | 24 | #include "ffconf.h" |
mimi3 | 10:b48d3ace55db | 25 | #include "mbed_debug.h" |
mimi3 | 10:b48d3ace55db | 26 | |
mimi3 | 10:b48d3ace55db | 27 | #include "FATFileSystem.h" |
mimi3 | 10:b48d3ace55db | 28 | #include "FATFileHandle.h" |
mimi3 | 10:b48d3ace55db | 29 | #include "FATDirHandle.h" |
mimi3 | 10:b48d3ace55db | 30 | |
mimi3 | 10:b48d3ace55db | 31 | DWORD get_fattime(void) { |
mimi3 | 10:b48d3ace55db | 32 | time_t rawtime; |
mimi3 | 10:b48d3ace55db | 33 | time(&rawtime); |
mimi3 | 10:b48d3ace55db | 34 | struct tm *ptm = localtime(&rawtime); |
mimi3 | 10:b48d3ace55db | 35 | return (DWORD)(ptm->tm_year - 80) << 25 |
mimi3 | 10:b48d3ace55db | 36 | | (DWORD)(ptm->tm_mon + 1 ) << 21 |
mimi3 | 10:b48d3ace55db | 37 | | (DWORD)(ptm->tm_mday ) << 16 |
mimi3 | 10:b48d3ace55db | 38 | | (DWORD)(ptm->tm_hour ) << 11 |
mimi3 | 10:b48d3ace55db | 39 | | (DWORD)(ptm->tm_min ) << 5 |
mimi3 | 10:b48d3ace55db | 40 | | (DWORD)(ptm->tm_sec/2 ); |
mimi3 | 10:b48d3ace55db | 41 | } |
mimi3 | 10:b48d3ace55db | 42 | |
mimi3 | 10:b48d3ace55db | 43 | FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0}; |
mimi3 | 10:b48d3ace55db | 44 | |
mimi3 | 10:b48d3ace55db | 45 | FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) { |
mimi3 | 10:b48d3ace55db | 46 | debug_if(FFS_DBG, "FATFileSystem(%s)\n", n); |
mimi3 | 10:b48d3ace55db | 47 | for(int i=0; i<_VOLUMES; i++) { |
mimi3 | 10:b48d3ace55db | 48 | if(_ffs[i] == 0) { |
mimi3 | 10:b48d3ace55db | 49 | _ffs[i] = this; |
mimi3 | 10:b48d3ace55db | 50 | _fsid[0] = '0' + i; |
mimi3 | 10:b48d3ace55db | 51 | _fsid[1] = '\0'; |
dinau | 31:cfad42bb789a | 52 | debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%s]\n", getName(), _fsid); |
mimi3 | 10:b48d3ace55db | 53 | f_mount(&_fs, _fsid, 0); |
mimi3 | 10:b48d3ace55db | 54 | return; |
mimi3 | 10:b48d3ace55db | 55 | } |
mimi3 | 10:b48d3ace55db | 56 | } |
mimi3 | 10:b48d3ace55db | 57 | error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n); |
mimi3 | 10:b48d3ace55db | 58 | } |
mimi3 | 10:b48d3ace55db | 59 | |
mimi3 | 10:b48d3ace55db | 60 | FATFileSystem::~FATFileSystem() { |
mimi3 | 10:b48d3ace55db | 61 | for (int i=0; i<_VOLUMES; i++) { |
mimi3 | 10:b48d3ace55db | 62 | if (_ffs[i] == this) { |
mimi3 | 10:b48d3ace55db | 63 | _ffs[i] = 0; |
mimi3 | 10:b48d3ace55db | 64 | f_mount(NULL, _fsid, 0); |
mimi3 | 10:b48d3ace55db | 65 | } |
mimi3 | 10:b48d3ace55db | 66 | } |
mimi3 | 10:b48d3ace55db | 67 | } |
mimi3 | 10:b48d3ace55db | 68 | |
mimi3 | 10:b48d3ace55db | 69 | FileHandle *FATFileSystem::open(const char* name, int flags) { |
dinau | 31:cfad42bb789a | 70 | debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", name, getName(), _fsid); |
mimi3 | 10:b48d3ace55db | 71 | char n[64]; |
mimi3 | 10:b48d3ace55db | 72 | sprintf(n, "%s:/%s", _fsid, name); |
mimi3 | 10:b48d3ace55db | 73 | |
mimi3 | 10:b48d3ace55db | 74 | /* POSIX flags -> FatFS open mode */ |
mimi3 | 10:b48d3ace55db | 75 | BYTE openmode; |
mimi3 | 10:b48d3ace55db | 76 | if (flags & O_RDWR) { |
mimi3 | 10:b48d3ace55db | 77 | openmode = FA_READ|FA_WRITE; |
mimi3 | 10:b48d3ace55db | 78 | } else if(flags & O_WRONLY) { |
mimi3 | 10:b48d3ace55db | 79 | openmode = FA_WRITE; |
mimi3 | 10:b48d3ace55db | 80 | } else { |
mimi3 | 10:b48d3ace55db | 81 | openmode = FA_READ; |
mimi3 | 10:b48d3ace55db | 82 | } |
mimi3 | 10:b48d3ace55db | 83 | if(flags & O_CREAT) { |
mimi3 | 10:b48d3ace55db | 84 | if(flags & O_TRUNC) { |
mimi3 | 10:b48d3ace55db | 85 | openmode |= FA_CREATE_ALWAYS; |
mimi3 | 10:b48d3ace55db | 86 | } else { |
mimi3 | 10:b48d3ace55db | 87 | openmode |= FA_OPEN_ALWAYS; |
mimi3 | 10:b48d3ace55db | 88 | } |
mimi3 | 10:b48d3ace55db | 89 | } |
mimi3 | 10:b48d3ace55db | 90 | |
mimi3 | 10:b48d3ace55db | 91 | FIL fh; |
mimi3 | 10:b48d3ace55db | 92 | FRESULT res = f_open(&fh, n, openmode); |
mimi3 | 10:b48d3ace55db | 93 | if (res) { |
mimi3 | 10:b48d3ace55db | 94 | debug_if(FFS_DBG, "f_open('w') failed: %d\n", res); |
mimi3 | 10:b48d3ace55db | 95 | return NULL; |
mimi3 | 10:b48d3ace55db | 96 | } |
mimi3 | 10:b48d3ace55db | 97 | if (flags & O_APPEND) { |
mimi3 | 10:b48d3ace55db | 98 | f_lseek(&fh, fh.fsize); |
mimi3 | 10:b48d3ace55db | 99 | } |
mimi3 | 10:b48d3ace55db | 100 | return new FATFileHandle(fh); |
mimi3 | 10:b48d3ace55db | 101 | } |
mimi3 | 10:b48d3ace55db | 102 | |
mimi3 | 10:b48d3ace55db | 103 | int FATFileSystem::remove(const char *filename) { |
mimi3 | 10:b48d3ace55db | 104 | FRESULT res = f_unlink(filename); |
mimi3 | 10:b48d3ace55db | 105 | if (res) { |
mimi3 | 10:b48d3ace55db | 106 | debug_if(FFS_DBG, "f_unlink() failed: %d\n", res); |
mimi3 | 10:b48d3ace55db | 107 | return -1; |
mimi3 | 10:b48d3ace55db | 108 | } |
mimi3 | 10:b48d3ace55db | 109 | return 0; |
mimi3 | 10:b48d3ace55db | 110 | } |
mimi3 | 10:b48d3ace55db | 111 | |
mimi3 | 10:b48d3ace55db | 112 | int FATFileSystem::rename(const char *oldname, const char *newname) { |
mimi3 | 10:b48d3ace55db | 113 | FRESULT res = f_rename(oldname, newname); |
mimi3 | 10:b48d3ace55db | 114 | if (res) { |
mimi3 | 10:b48d3ace55db | 115 | debug_if(FFS_DBG, "f_rename() failed: %d\n", res); |
mimi3 | 10:b48d3ace55db | 116 | return -1; |
mimi3 | 10:b48d3ace55db | 117 | } |
mimi3 | 10:b48d3ace55db | 118 | return 0; |
mimi3 | 10:b48d3ace55db | 119 | } |
mimi3 | 10:b48d3ace55db | 120 | |
mimi3 | 10:b48d3ace55db | 121 | int FATFileSystem::format() { |
mimi3 | 10:b48d3ace55db | 122 | FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster) |
mimi3 | 10:b48d3ace55db | 123 | if (res) { |
mimi3 | 10:b48d3ace55db | 124 | debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res); |
mimi3 | 10:b48d3ace55db | 125 | return -1; |
mimi3 | 10:b48d3ace55db | 126 | } |
mimi3 | 10:b48d3ace55db | 127 | return 0; |
mimi3 | 10:b48d3ace55db | 128 | } |
mimi3 | 10:b48d3ace55db | 129 | |
mimi3 | 10:b48d3ace55db | 130 | DirHandle *FATFileSystem::opendir(const char *name) { |
mimi3 | 10:b48d3ace55db | 131 | FATFS_DIR dir; |
mimi3 | 10:b48d3ace55db | 132 | FRESULT res = f_opendir(&dir, name); |
mimi3 | 10:b48d3ace55db | 133 | if (res != 0) { |
mimi3 | 10:b48d3ace55db | 134 | return NULL; |
mimi3 | 10:b48d3ace55db | 135 | } |
mimi3 | 10:b48d3ace55db | 136 | return new FATDirHandle(dir); |
mimi3 | 10:b48d3ace55db | 137 | } |
mimi3 | 10:b48d3ace55db | 138 | |
mimi3 | 10:b48d3ace55db | 139 | int FATFileSystem::mkdir(const char *name, mode_t mode) { |
mimi3 | 10:b48d3ace55db | 140 | FRESULT res = f_mkdir(name); |
mimi3 | 10:b48d3ace55db | 141 | return res == 0 ? 0 : -1; |
mimi3 | 10:b48d3ace55db | 142 | } |
mimi3 | 10:b48d3ace55db | 143 | |
mimi3 | 10:b48d3ace55db | 144 | int FATFileSystem::mount() { |
mimi3 | 10:b48d3ace55db | 145 | FRESULT res = f_mount(&_fs, _fsid, 1); |
mimi3 | 10:b48d3ace55db | 146 | return res == 0 ? 0 : -1; |
mimi3 | 10:b48d3ace55db | 147 | } |
mimi3 | 10:b48d3ace55db | 148 | |
mimi3 | 10:b48d3ace55db | 149 | int FATFileSystem::unmount() { |
mimi3 | 10:b48d3ace55db | 150 | if (disk_sync()) |
mimi3 | 10:b48d3ace55db | 151 | return -1; |
mimi3 | 10:b48d3ace55db | 152 | FRESULT res = f_mount(NULL, _fsid, 0); |
mimi3 | 10:b48d3ace55db | 153 | return res == 0 ? 0 : -1; |
mimi3 | 10:b48d3ace55db | 154 | } |