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