Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
ffxx68
Date:
Tue Jul 19 13:49:28 2022 +0000
Revision:
2:02f003d025a7
Parent:
1:3488faedd0d1
SD Card handling added

Who changed what in which revision?

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