
Fork of my MQTTGateway
sd-driver/features/filesystem/sd/SDBlockDevice.h@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | /* mbed Microcontroller Library |
vpcola | 0:f1d3878b8dd9 | 2 | * Copyright (c) 2006-2012 ARM Limited |
vpcola | 0:f1d3878b8dd9 | 3 | * |
vpcola | 0:f1d3878b8dd9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
vpcola | 0:f1d3878b8dd9 | 5 | * of this software and associated documentation files (the "Software"), to deal |
vpcola | 0:f1d3878b8dd9 | 6 | * in the Software without restriction, including without limitation the rights |
vpcola | 0:f1d3878b8dd9 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
vpcola | 0:f1d3878b8dd9 | 8 | * copies of the Software, and to permit persons to whom the Software is |
vpcola | 0:f1d3878b8dd9 | 9 | * furnished to do so, subject to the following conditions: |
vpcola | 0:f1d3878b8dd9 | 10 | * |
vpcola | 0:f1d3878b8dd9 | 11 | * The above copyright notice and this permission notice shall be included in |
vpcola | 0:f1d3878b8dd9 | 12 | * all copies or substantial portions of the Software. |
vpcola | 0:f1d3878b8dd9 | 13 | * |
vpcola | 0:f1d3878b8dd9 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
vpcola | 0:f1d3878b8dd9 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
vpcola | 0:f1d3878b8dd9 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
vpcola | 0:f1d3878b8dd9 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
vpcola | 0:f1d3878b8dd9 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
vpcola | 0:f1d3878b8dd9 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
vpcola | 0:f1d3878b8dd9 | 20 | * SOFTWARE. |
vpcola | 0:f1d3878b8dd9 | 21 | */ |
vpcola | 0:f1d3878b8dd9 | 22 | #ifndef MBED_SD_BLOCK_DEVICE_H |
vpcola | 0:f1d3878b8dd9 | 23 | #define MBED_SD_BLOCK_DEVICE_H |
vpcola | 0:f1d3878b8dd9 | 24 | |
vpcola | 0:f1d3878b8dd9 | 25 | /* If the target has no SPI support then SDCard is not supported */ |
vpcola | 0:f1d3878b8dd9 | 26 | #ifdef DEVICE_SPI |
vpcola | 0:f1d3878b8dd9 | 27 | |
vpcola | 0:f1d3878b8dd9 | 28 | #include "BlockDevice.h" |
vpcola | 0:f1d3878b8dd9 | 29 | #include "mbed.h" |
vpcola | 0:f1d3878b8dd9 | 30 | |
vpcola | 0:f1d3878b8dd9 | 31 | |
vpcola | 0:f1d3878b8dd9 | 32 | /** Access an SD Card using SPI |
vpcola | 0:f1d3878b8dd9 | 33 | * |
vpcola | 0:f1d3878b8dd9 | 34 | * @code |
vpcola | 0:f1d3878b8dd9 | 35 | * #include "mbed.h" |
vpcola | 0:f1d3878b8dd9 | 36 | * #include "SDBlockDevice.h" |
vpcola | 0:f1d3878b8dd9 | 37 | * |
vpcola | 0:f1d3878b8dd9 | 38 | * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs |
vpcola | 0:f1d3878b8dd9 | 39 | * uint8_t block[512] = "Hello World!\n"; |
vpcola | 0:f1d3878b8dd9 | 40 | * |
vpcola | 0:f1d3878b8dd9 | 41 | * int main() { |
vpcola | 0:f1d3878b8dd9 | 42 | * sd.init(); |
vpcola | 0:f1d3878b8dd9 | 43 | * sd.write(block, 0, 512); |
vpcola | 0:f1d3878b8dd9 | 44 | * sd.read(block, 0, 512); |
vpcola | 0:f1d3878b8dd9 | 45 | * printf("%s", block); |
vpcola | 0:f1d3878b8dd9 | 46 | * sd.deinit(); |
vpcola | 0:f1d3878b8dd9 | 47 | * } |
vpcola | 0:f1d3878b8dd9 | 48 | */ |
vpcola | 0:f1d3878b8dd9 | 49 | class SDBlockDevice : public BlockDevice { |
vpcola | 0:f1d3878b8dd9 | 50 | public: |
vpcola | 0:f1d3878b8dd9 | 51 | /** Lifetime of an SD card |
vpcola | 0:f1d3878b8dd9 | 52 | */ |
vpcola | 0:f1d3878b8dd9 | 53 | SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs); |
vpcola | 0:f1d3878b8dd9 | 54 | virtual ~SDBlockDevice(); |
vpcola | 0:f1d3878b8dd9 | 55 | |
vpcola | 0:f1d3878b8dd9 | 56 | /** Initialize a block device |
vpcola | 0:f1d3878b8dd9 | 57 | * |
vpcola | 0:f1d3878b8dd9 | 58 | * @return 0 on success or a negative error code on failure |
vpcola | 0:f1d3878b8dd9 | 59 | */ |
vpcola | 0:f1d3878b8dd9 | 60 | virtual int init(); |
vpcola | 0:f1d3878b8dd9 | 61 | |
vpcola | 0:f1d3878b8dd9 | 62 | /** Deinitialize a block device |
vpcola | 0:f1d3878b8dd9 | 63 | * |
vpcola | 0:f1d3878b8dd9 | 64 | * @return 0 on success or a negative error code on failure |
vpcola | 0:f1d3878b8dd9 | 65 | */ |
vpcola | 0:f1d3878b8dd9 | 66 | virtual int deinit(); |
vpcola | 0:f1d3878b8dd9 | 67 | |
vpcola | 0:f1d3878b8dd9 | 68 | /** Read blocks from a block device |
vpcola | 0:f1d3878b8dd9 | 69 | * |
vpcola | 0:f1d3878b8dd9 | 70 | * @param buffer Buffer to write blocks to |
vpcola | 0:f1d3878b8dd9 | 71 | * @param addr Address of block to begin reading from |
vpcola | 0:f1d3878b8dd9 | 72 | * @param size Size to read in bytes, must be a multiple of read block size |
vpcola | 0:f1d3878b8dd9 | 73 | * @return 0 on success, negative error code on failure |
vpcola | 0:f1d3878b8dd9 | 74 | */ |
vpcola | 0:f1d3878b8dd9 | 75 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
vpcola | 0:f1d3878b8dd9 | 76 | |
vpcola | 0:f1d3878b8dd9 | 77 | /** Program blocks to a block device |
vpcola | 0:f1d3878b8dd9 | 78 | * |
vpcola | 0:f1d3878b8dd9 | 79 | * The blocks must have been erased prior to being programmed |
vpcola | 0:f1d3878b8dd9 | 80 | * |
vpcola | 0:f1d3878b8dd9 | 81 | * @param buffer Buffer of data to write to blocks |
vpcola | 0:f1d3878b8dd9 | 82 | * @param addr Address of block to begin writing to |
vpcola | 0:f1d3878b8dd9 | 83 | * @param size Size to write in bytes, must be a multiple of program block size |
vpcola | 0:f1d3878b8dd9 | 84 | * @return 0 on success, negative error code on failure |
vpcola | 0:f1d3878b8dd9 | 85 | */ |
vpcola | 0:f1d3878b8dd9 | 86 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
vpcola | 0:f1d3878b8dd9 | 87 | |
vpcola | 0:f1d3878b8dd9 | 88 | /** Erase blocks on a block device |
vpcola | 0:f1d3878b8dd9 | 89 | * |
vpcola | 0:f1d3878b8dd9 | 90 | * The state of an erased block is undefined until it has been programmed |
vpcola | 0:f1d3878b8dd9 | 91 | * |
vpcola | 0:f1d3878b8dd9 | 92 | * @param addr Address of block to begin erasing |
vpcola | 0:f1d3878b8dd9 | 93 | * @param size Size to erase in bytes, must be a multiple of erase block size |
vpcola | 0:f1d3878b8dd9 | 94 | * @return 0 on success, negative error code on failure |
vpcola | 0:f1d3878b8dd9 | 95 | */ |
vpcola | 0:f1d3878b8dd9 | 96 | virtual int erase(bd_addr_t addr, bd_size_t size); |
vpcola | 0:f1d3878b8dd9 | 97 | |
vpcola | 0:f1d3878b8dd9 | 98 | /** Get the size of a readable block |
vpcola | 0:f1d3878b8dd9 | 99 | * |
vpcola | 0:f1d3878b8dd9 | 100 | * @return Size of a readable block in bytes |
vpcola | 0:f1d3878b8dd9 | 101 | */ |
vpcola | 0:f1d3878b8dd9 | 102 | virtual bd_size_t get_read_size() const; |
vpcola | 0:f1d3878b8dd9 | 103 | |
vpcola | 0:f1d3878b8dd9 | 104 | /** Get the size of a programable block |
vpcola | 0:f1d3878b8dd9 | 105 | * |
vpcola | 0:f1d3878b8dd9 | 106 | * @return Size of a programable block in bytes |
vpcola | 0:f1d3878b8dd9 | 107 | * @note Must be a multiple of the read size |
vpcola | 0:f1d3878b8dd9 | 108 | */ |
vpcola | 0:f1d3878b8dd9 | 109 | virtual bd_size_t get_program_size() const; |
vpcola | 0:f1d3878b8dd9 | 110 | |
vpcola | 0:f1d3878b8dd9 | 111 | /** Get the size of a eraseable block |
vpcola | 0:f1d3878b8dd9 | 112 | * |
vpcola | 0:f1d3878b8dd9 | 113 | * @return Size of a eraseable block in bytes |
vpcola | 0:f1d3878b8dd9 | 114 | * @note Must be a multiple of the program size |
vpcola | 0:f1d3878b8dd9 | 115 | */ |
vpcola | 0:f1d3878b8dd9 | 116 | virtual bd_size_t get_erase_size() const; |
vpcola | 0:f1d3878b8dd9 | 117 | |
vpcola | 0:f1d3878b8dd9 | 118 | /** Get the total size of the underlying device |
vpcola | 0:f1d3878b8dd9 | 119 | * |
vpcola | 0:f1d3878b8dd9 | 120 | * @return Size of the underlying device in bytes |
vpcola | 0:f1d3878b8dd9 | 121 | */ |
vpcola | 0:f1d3878b8dd9 | 122 | virtual bd_size_t size() const; |
vpcola | 0:f1d3878b8dd9 | 123 | |
vpcola | 0:f1d3878b8dd9 | 124 | /** Enable or disable debugging |
vpcola | 0:f1d3878b8dd9 | 125 | * |
vpcola | 0:f1d3878b8dd9 | 126 | * @param State of debugging |
vpcola | 0:f1d3878b8dd9 | 127 | */ |
vpcola | 0:f1d3878b8dd9 | 128 | virtual void debug(bool dbg); |
vpcola | 0:f1d3878b8dd9 | 129 | |
vpcola | 0:f1d3878b8dd9 | 130 | private: |
vpcola | 0:f1d3878b8dd9 | 131 | int _cmd(int cmd, int arg); |
vpcola | 0:f1d3878b8dd9 | 132 | int _cmdx(int cmd, int arg); |
vpcola | 0:f1d3878b8dd9 | 133 | int _cmd8(); |
vpcola | 0:f1d3878b8dd9 | 134 | int _cmd58(); |
vpcola | 0:f1d3878b8dd9 | 135 | int _initialise_card(); |
vpcola | 0:f1d3878b8dd9 | 136 | int _initialise_card_v1(); |
vpcola | 0:f1d3878b8dd9 | 137 | int _initialise_card_v2(); |
vpcola | 0:f1d3878b8dd9 | 138 | |
vpcola | 0:f1d3878b8dd9 | 139 | int _read(uint8_t * buffer, uint32_t length); |
vpcola | 0:f1d3878b8dd9 | 140 | int _write(const uint8_t *buffer, uint32_t length); |
vpcola | 0:f1d3878b8dd9 | 141 | uint32_t _sd_sectors(); |
vpcola | 0:f1d3878b8dd9 | 142 | uint32_t _sectors; |
vpcola | 0:f1d3878b8dd9 | 143 | |
vpcola | 0:f1d3878b8dd9 | 144 | uint32_t _init_sck; |
vpcola | 0:f1d3878b8dd9 | 145 | uint32_t _transfer_sck; |
vpcola | 0:f1d3878b8dd9 | 146 | |
vpcola | 0:f1d3878b8dd9 | 147 | SPI _spi; |
vpcola | 0:f1d3878b8dd9 | 148 | DigitalOut _cs; |
vpcola | 0:f1d3878b8dd9 | 149 | unsigned _block_size; |
vpcola | 0:f1d3878b8dd9 | 150 | bool _is_initialized; |
vpcola | 0:f1d3878b8dd9 | 151 | bool _dbg; |
vpcola | 0:f1d3878b8dd9 | 152 | Mutex _lock; |
vpcola | 0:f1d3878b8dd9 | 153 | }; |
vpcola | 0:f1d3878b8dd9 | 154 | |
vpcola | 0:f1d3878b8dd9 | 155 | |
vpcola | 0:f1d3878b8dd9 | 156 | #endif /* DEVICE_SPI */ |
vpcola | 0:f1d3878b8dd9 | 157 | |
vpcola | 0:f1d3878b8dd9 | 158 | #endif /* MBED_SD_BLOCK_DEVICE_H */ |