Sauvegarde sur carte microSD

Dependents:   microSD_Card

Committer:
adrevong
Date:
Tue Jan 28 11:11:57 2020 +0000
Revision:
0:c9f6f3f0cd78

        

Who changed what in which revision?

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