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