Alek Boving / SDFileSystem8
Committer:
alekboving
Date:
Thu Nov 12 15:20:15 2020 +0000
Revision:
0:83e8ad3d9a65
initial commit

Who changed what in which revision?

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