this library has few changes from the original library, for effects of this work.

Dependents:   OBC3_1_h

Committer:
FannyCalle
Date:
Mon May 21 15:10:37 2018 +0000
Revision:
0:8214896432e0
el sd hay que revisar;

Who changed what in which revision?

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