SDFileSystem with mutex for multiple SPI devices on the same SPI bus
Dependents: CC3000Nucleo401REProject
Fork of SDFileSystem by
SDFileSystem.h@4:f0bd1dfd6f62, 2014-10-16 (annotated)
- Committer:
- vpcola
- Date:
- Thu Oct 16 13:40:12 2014 +0000
- Revision:
- 4:f0bd1dfd6f62
- Parent:
- 3:7b35d1709458
Added mutex for multiple devices on the same SPI bus
Who changed what in which revision?
User | Revision | Line number | New 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" |
vpcola | 4:f0bd1dfd6f62 | 26 | #include "rtos.h" |
emilmont | 1:7153ee70df01 | 27 | #include "FATFileSystem.h" |
emilmont | 1:7153ee70df01 | 28 | #include <stdint.h> |
emilmont | 1:7153ee70df01 | 29 | |
emilmont | 1:7153ee70df01 | 30 | /** Access the filesystem on an SD Card using SPI |
emilmont | 1:7153ee70df01 | 31 | * |
emilmont | 1:7153ee70df01 | 32 | * @code |
emilmont | 1:7153ee70df01 | 33 | * #include "mbed.h" |
emilmont | 1:7153ee70df01 | 34 | * #include "SDFileSystem.h" |
emilmont | 1:7153ee70df01 | 35 | * |
screamer | 3:7b35d1709458 | 36 | * SDFileSystem sd(p5, p6, p7, p12, "sd"); // MOSI, MISO, SCLK, SSEL |
emilmont | 1:7153ee70df01 | 37 | * |
emilmont | 1:7153ee70df01 | 38 | * int main() { |
screamer | 3:7b35d1709458 | 39 | * FILE *fp = fopen("/sd/mbed.txt", "w"); |
emilmont | 1:7153ee70df01 | 40 | * fprintf(fp, "Hello World!\n"); |
emilmont | 1:7153ee70df01 | 41 | * fclose(fp); |
emilmont | 1:7153ee70df01 | 42 | * } |
screamer | 3:7b35d1709458 | 43 | * @endcode |
emilmont | 1:7153ee70df01 | 44 | */ |
emilmont | 1:7153ee70df01 | 45 | class SDFileSystem : public FATFileSystem { |
emilmont | 1:7153ee70df01 | 46 | public: |
emilmont | 1:7153ee70df01 | 47 | |
emilmont | 1:7153ee70df01 | 48 | /** Create the File System for accessing an SD Card using SPI |
emilmont | 1:7153ee70df01 | 49 | * |
emilmont | 1:7153ee70df01 | 50 | * @param mosi SPI mosi pin connected to SD Card |
emilmont | 1:7153ee70df01 | 51 | * @param miso SPI miso pin conencted to SD Card |
emilmont | 1:7153ee70df01 | 52 | * @param sclk SPI sclk pin connected to SD Card |
emilmont | 1:7153ee70df01 | 53 | * @param cs DigitalOut pin used as SD Card chip select |
emilmont | 1:7153ee70df01 | 54 | * @param name The name used to access the virtual filesystem |
emilmont | 1:7153ee70df01 | 55 | */ |
vpcola | 4:f0bd1dfd6f62 | 56 | SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, Mutex & mutex, const char* name); |
screamer | 3:7b35d1709458 | 57 | |
emilmont | 1:7153ee70df01 | 58 | virtual int disk_initialize(); |
emilmont | 1:7153ee70df01 | 59 | virtual int disk_status(); |
emilmont | 1:7153ee70df01 | 60 | virtual int disk_read(uint8_t * buffer, uint64_t block_number); |
emilmont | 1:7153ee70df01 | 61 | virtual int disk_write(const uint8_t * buffer, uint64_t block_number); |
emilmont | 1:7153ee70df01 | 62 | virtual int disk_sync(); |
emilmont | 1:7153ee70df01 | 63 | virtual uint64_t disk_sectors(); |
emilmont | 1:7153ee70df01 | 64 | |
emilmont | 1:7153ee70df01 | 65 | protected: |
emilmont | 1:7153ee70df01 | 66 | |
emilmont | 1:7153ee70df01 | 67 | int _cmd(int cmd, int arg); |
emilmont | 1:7153ee70df01 | 68 | int _cmdx(int cmd, int arg); |
emilmont | 1:7153ee70df01 | 69 | int _cmd8(); |
emilmont | 1:7153ee70df01 | 70 | int _cmd58(); |
emilmont | 1:7153ee70df01 | 71 | int initialise_card(); |
emilmont | 1:7153ee70df01 | 72 | int initialise_card_v1(); |
emilmont | 1:7153ee70df01 | 73 | int initialise_card_v2(); |
emilmont | 1:7153ee70df01 | 74 | |
emilmont | 1:7153ee70df01 | 75 | int _read(uint8_t * buffer, uint32_t length); |
emilmont | 1:7153ee70df01 | 76 | int _write(const uint8_t *buffer, uint32_t length); |
emilmont | 1:7153ee70df01 | 77 | uint64_t _sd_sectors(); |
emilmont | 1:7153ee70df01 | 78 | uint64_t _sectors; |
emilmont | 1:7153ee70df01 | 79 | |
emilmont | 1:7153ee70df01 | 80 | SPI _spi; |
emilmont | 1:7153ee70df01 | 81 | DigitalOut _cs; |
vpcola | 4:f0bd1dfd6f62 | 82 | Mutex & _mutex; |
emilmont | 1:7153ee70df01 | 83 | int cdv; |
emilmont | 1:7153ee70df01 | 84 | }; |
emilmont | 1:7153ee70df01 | 85 | |
emilmont | 1:7153ee70df01 | 86 | #endif |