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