sd file system

Dependencies:   FATFileSystem

Dependents:   final_mp3_player

Committer:
lfink6
Date:
Fri Dec 10 17:42:49 2021 +0000
Revision:
7:8f2e6fd46974
Parent:
4:3f40cbfe801c
rev 2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:7153ee70df01 1 /* mbed Microcontroller Library
emilmont 1:7153ee70df01 2 * Copyright (c) 2006-2012 ARM Limited
emilmont 1:7153ee70df01 3 *
emilmont 1:7153ee70df01 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
emilmont 1:7153ee70df01 5 * of this software and associated documentation files (the "Software"), to deal
emilmont 1:7153ee70df01 6 * in the Software without restriction, including without limitation the rights
emilmont 1:7153ee70df01 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emilmont 1:7153ee70df01 8 * copies of the Software, and to permit persons to whom the Software is
emilmont 1:7153ee70df01 9 * furnished to do so, subject to the following conditions:
emilmont 1:7153ee70df01 10 *
emilmont 1:7153ee70df01 11 * The above copyright notice and this permission notice shall be included in
emilmont 1:7153ee70df01 12 * all copies or substantial portions of the Software.
emilmont 1:7153ee70df01 13 *
emilmont 1:7153ee70df01 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emilmont 1:7153ee70df01 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emilmont 1:7153ee70df01 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emilmont 1:7153ee70df01 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emilmont 1:7153ee70df01 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 1:7153ee70df01 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
emilmont 1:7153ee70df01 20 * SOFTWARE.
emilmont 1:7153ee70df01 21 */
emilmont 1:7153ee70df01 22 #ifndef MBED_SDFILESYSTEM_H
emilmont 1:7153ee70df01 23 #define MBED_SDFILESYSTEM_H
emilmont 1:7153ee70df01 24
emilmont 1:7153ee70df01 25 #include "mbed.h"
emilmont 1:7153ee70df01 26 #include "FATFileSystem.h"
emilmont 1:7153ee70df01 27 #include <stdint.h>
emilmont 1:7153ee70df01 28
emilmont 1:7153ee70df01 29 /** Access the filesystem on an SD Card using SPI
emilmont 1:7153ee70df01 30 *
emilmont 1:7153ee70df01 31 * @code
emilmont 1:7153ee70df01 32 * #include "mbed.h"
emilmont 1:7153ee70df01 33 * #include "SDFileSystem.h"
emilmont 1:7153ee70df01 34 *
maclobdell 4:3f40cbfe801c 35 * SDFileSystem sd(p5, p6, p7, p12, "sd"); // mosi, miso, sclk, cs
maclobdell 4:3f40cbfe801c 36 *
emilmont 1:7153ee70df01 37 * int main() {
maclobdell 4:3f40cbfe801c 38 * FILE *fp = fopen("/sd/myfile.txt", "w");
emilmont 1:7153ee70df01 39 * fprintf(fp, "Hello World!\n");
emilmont 1:7153ee70df01 40 * fclose(fp);
emilmont 1:7153ee70df01 41 * }
emilmont 1:7153ee70df01 42 */
emilmont 1:7153ee70df01 43 class SDFileSystem : public FATFileSystem {
emilmont 1:7153ee70df01 44 public:
emilmont 1:7153ee70df01 45
emilmont 1:7153ee70df01 46 /** Create the File System for accessing an SD Card using SPI
emilmont 1:7153ee70df01 47 *
emilmont 1:7153ee70df01 48 * @param mosi SPI mosi pin connected to SD Card
emilmont 1:7153ee70df01 49 * @param miso SPI miso pin conencted to SD Card
emilmont 1:7153ee70df01 50 * @param sclk SPI sclk pin connected to SD Card
emilmont 1:7153ee70df01 51 * @param cs DigitalOut pin used as SD Card chip select
emilmont 1:7153ee70df01 52 * @param name The name used to access the virtual filesystem
emilmont 1:7153ee70df01 53 */
emilmont 1:7153ee70df01 54 SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
emilmont 1:7153ee70df01 55 virtual int disk_initialize();
emilmont 1:7153ee70df01 56 virtual int disk_status();
maclobdell 4:3f40cbfe801c 57 virtual int disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count);
maclobdell 4:3f40cbfe801c 58 virtual int disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count);
emilmont 1:7153ee70df01 59 virtual int disk_sync();
maclobdell 4:3f40cbfe801c 60 virtual uint32_t disk_sectors();
emilmont 1:7153ee70df01 61
emilmont 1:7153ee70df01 62 protected:
emilmont 1:7153ee70df01 63
emilmont 1:7153ee70df01 64 int _cmd(int cmd, int arg);
emilmont 1:7153ee70df01 65 int _cmdx(int cmd, int arg);
emilmont 1:7153ee70df01 66 int _cmd8();
emilmont 1:7153ee70df01 67 int _cmd58();
emilmont 1:7153ee70df01 68 int initialise_card();
emilmont 1:7153ee70df01 69 int initialise_card_v1();
emilmont 1:7153ee70df01 70 int initialise_card_v2();
maclobdell 4:3f40cbfe801c 71
emilmont 1:7153ee70df01 72 int _read(uint8_t * buffer, uint32_t length);
emilmont 1:7153ee70df01 73 int _write(const uint8_t *buffer, uint32_t length);
maclobdell 4:3f40cbfe801c 74 uint32_t _sd_sectors();
maclobdell 4:3f40cbfe801c 75 uint32_t _sectors;
maclobdell 4:3f40cbfe801c 76
maclobdell 4:3f40cbfe801c 77 void set_init_sck(uint32_t sck) { _init_sck = sck; }
maclobdell 4:3f40cbfe801c 78 // Note: The highest SPI clock rate is 20 MHz for MMC and 25 MHz for SD
maclobdell 4:3f40cbfe801c 79 void set_transfer_sck(uint32_t sck) { _transfer_sck = sck; }
maclobdell 4:3f40cbfe801c 80 uint32_t _init_sck;
maclobdell 4:3f40cbfe801c 81 uint32_t _transfer_sck;
maclobdell 4:3f40cbfe801c 82
emilmont 1:7153ee70df01 83 SPI _spi;
emilmont 1:7153ee70df01 84 DigitalOut _cs;
emilmont 1:7153ee70df01 85 int cdv;
maclobdell 4:3f40cbfe801c 86 int _is_initialized;
emilmont 1:7153ee70df01 87 };
emilmont 1:7153ee70df01 88
maclobdell 4:3f40cbfe801c 89 #endif