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.
Fork of Wio_3G-example-sd-driver by
sd-driver/SDBlockDevice.h@0:8eedb2495d52, 2018-08-09 (annotated)
- Committer:
- MACRUM
- Date:
- Thu Aug 09 01:42:53 2018 +0000
- Revision:
- 0:8eedb2495d52
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:8eedb2495d52 | 1 | /* mbed Microcontroller Library |
MACRUM | 0:8eedb2495d52 | 2 | * Copyright (c) 2006-2013 ARM Limited |
MACRUM | 0:8eedb2495d52 | 3 | * |
MACRUM | 0:8eedb2495d52 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 0:8eedb2495d52 | 5 | * you may not use this file except in compliance with the License. |
MACRUM | 0:8eedb2495d52 | 6 | * You may obtain a copy of the License at |
MACRUM | 0:8eedb2495d52 | 7 | * |
MACRUM | 0:8eedb2495d52 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:8eedb2495d52 | 9 | * |
MACRUM | 0:8eedb2495d52 | 10 | * Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:8eedb2495d52 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 0:8eedb2495d52 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:8eedb2495d52 | 13 | * See the License for the specific language governing permissions and |
MACRUM | 0:8eedb2495d52 | 14 | * limitations under the License. |
MACRUM | 0:8eedb2495d52 | 15 | */ |
MACRUM | 0:8eedb2495d52 | 16 | |
MACRUM | 0:8eedb2495d52 | 17 | #ifndef MBED_SD_BLOCK_DEVICE_H |
MACRUM | 0:8eedb2495d52 | 18 | #define MBED_SD_BLOCK_DEVICE_H |
MACRUM | 0:8eedb2495d52 | 19 | |
MACRUM | 0:8eedb2495d52 | 20 | /* If the target has no SPI support then SDCard is not supported */ |
MACRUM | 0:8eedb2495d52 | 21 | #ifdef DEVICE_SPI |
MACRUM | 0:8eedb2495d52 | 22 | |
MACRUM | 0:8eedb2495d52 | 23 | #include "BlockDevice.h" |
MACRUM | 0:8eedb2495d52 | 24 | #include "mbed.h" |
MACRUM | 0:8eedb2495d52 | 25 | #include "platform/PlatformMutex.h" |
MACRUM | 0:8eedb2495d52 | 26 | |
MACRUM | 0:8eedb2495d52 | 27 | /** Access an SD Card using SPI |
MACRUM | 0:8eedb2495d52 | 28 | * |
MACRUM | 0:8eedb2495d52 | 29 | * @code |
MACRUM | 0:8eedb2495d52 | 30 | * #include "mbed.h" |
MACRUM | 0:8eedb2495d52 | 31 | * #include "SDBlockDevice.h" |
MACRUM | 0:8eedb2495d52 | 32 | * |
MACRUM | 0:8eedb2495d52 | 33 | * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs |
MACRUM | 0:8eedb2495d52 | 34 | * uint8_t block[512] = "Hello World!\n"; |
MACRUM | 0:8eedb2495d52 | 35 | * |
MACRUM | 0:8eedb2495d52 | 36 | * int main() { |
MACRUM | 0:8eedb2495d52 | 37 | * sd.init(); |
MACRUM | 0:8eedb2495d52 | 38 | * sd.write(block, 0, 512); |
MACRUM | 0:8eedb2495d52 | 39 | * sd.read(block, 0, 512); |
MACRUM | 0:8eedb2495d52 | 40 | * printf("%s", block); |
MACRUM | 0:8eedb2495d52 | 41 | * sd.deinit(); |
MACRUM | 0:8eedb2495d52 | 42 | * } |
MACRUM | 0:8eedb2495d52 | 43 | */ |
MACRUM | 0:8eedb2495d52 | 44 | class SDBlockDevice : public BlockDevice { |
MACRUM | 0:8eedb2495d52 | 45 | public: |
MACRUM | 0:8eedb2495d52 | 46 | /** Lifetime of an SD card |
MACRUM | 0:8eedb2495d52 | 47 | */ |
MACRUM | 0:8eedb2495d52 | 48 | SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz=1000000, bool crc_on=0); |
MACRUM | 0:8eedb2495d52 | 49 | virtual ~SDBlockDevice(); |
MACRUM | 0:8eedb2495d52 | 50 | |
MACRUM | 0:8eedb2495d52 | 51 | /** Initialize a block device |
MACRUM | 0:8eedb2495d52 | 52 | * |
MACRUM | 0:8eedb2495d52 | 53 | * @return 0 on success or a negative error code on failure |
MACRUM | 0:8eedb2495d52 | 54 | */ |
MACRUM | 0:8eedb2495d52 | 55 | virtual int init(); |
MACRUM | 0:8eedb2495d52 | 56 | |
MACRUM | 0:8eedb2495d52 | 57 | /** Deinitialize a block device |
MACRUM | 0:8eedb2495d52 | 58 | * |
MACRUM | 0:8eedb2495d52 | 59 | * @return 0 on success or a negative error code on failure |
MACRUM | 0:8eedb2495d52 | 60 | */ |
MACRUM | 0:8eedb2495d52 | 61 | virtual int deinit(); |
MACRUM | 0:8eedb2495d52 | 62 | |
MACRUM | 0:8eedb2495d52 | 63 | /** Read blocks from a block device |
MACRUM | 0:8eedb2495d52 | 64 | * |
MACRUM | 0:8eedb2495d52 | 65 | * @param buffer Buffer to write blocks to |
MACRUM | 0:8eedb2495d52 | 66 | * @param addr Address of block to begin reading from |
MACRUM | 0:8eedb2495d52 | 67 | * @param size Size to read in bytes, must be a multiple of read block size |
MACRUM | 0:8eedb2495d52 | 68 | * @return 0 on success, negative error code on failure |
MACRUM | 0:8eedb2495d52 | 69 | */ |
MACRUM | 0:8eedb2495d52 | 70 | virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
MACRUM | 0:8eedb2495d52 | 71 | |
MACRUM | 0:8eedb2495d52 | 72 | /** Program blocks to a block device |
MACRUM | 0:8eedb2495d52 | 73 | * |
MACRUM | 0:8eedb2495d52 | 74 | * The blocks must have been erased prior to being programmed |
MACRUM | 0:8eedb2495d52 | 75 | * |
MACRUM | 0:8eedb2495d52 | 76 | * @param buffer Buffer of data to write to blocks |
MACRUM | 0:8eedb2495d52 | 77 | * @param addr Address of block to begin writing to |
MACRUM | 0:8eedb2495d52 | 78 | * @param size Size to write in bytes, must be a multiple of program block size |
MACRUM | 0:8eedb2495d52 | 79 | * @return 0 on success, negative error code on failure |
MACRUM | 0:8eedb2495d52 | 80 | */ |
MACRUM | 0:8eedb2495d52 | 81 | virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
MACRUM | 0:8eedb2495d52 | 82 | |
MACRUM | 0:8eedb2495d52 | 83 | /** Mark blocks as no longer in use |
MACRUM | 0:8eedb2495d52 | 84 | * |
MACRUM | 0:8eedb2495d52 | 85 | * This function provides a hint to the underlying block device that a region of blocks |
MACRUM | 0:8eedb2495d52 | 86 | * is no longer in use and may be erased without side effects. Erase must still be called |
MACRUM | 0:8eedb2495d52 | 87 | * before programming, but trimming allows flash-translation-layers to schedule erases when |
MACRUM | 0:8eedb2495d52 | 88 | * the device is not busy. |
MACRUM | 0:8eedb2495d52 | 89 | * |
MACRUM | 0:8eedb2495d52 | 90 | * @param addr Address of block to mark as unused |
MACRUM | 0:8eedb2495d52 | 91 | * @param size Size to mark as unused in bytes, must be a multiple of erase block size |
MACRUM | 0:8eedb2495d52 | 92 | * @return 0 on success, negative error code on failure |
MACRUM | 0:8eedb2495d52 | 93 | */ |
MACRUM | 0:8eedb2495d52 | 94 | virtual int trim(bd_addr_t addr, bd_size_t size); |
MACRUM | 0:8eedb2495d52 | 95 | |
MACRUM | 0:8eedb2495d52 | 96 | /** Get the size of a readable block |
MACRUM | 0:8eedb2495d52 | 97 | * |
MACRUM | 0:8eedb2495d52 | 98 | * @return Size of a readable block in bytes |
MACRUM | 0:8eedb2495d52 | 99 | */ |
MACRUM | 0:8eedb2495d52 | 100 | virtual bd_size_t get_read_size() const; |
MACRUM | 0:8eedb2495d52 | 101 | |
MACRUM | 0:8eedb2495d52 | 102 | /** Get the size of a programable block |
MACRUM | 0:8eedb2495d52 | 103 | * |
MACRUM | 0:8eedb2495d52 | 104 | * @return Size of a programable block in bytes |
MACRUM | 0:8eedb2495d52 | 105 | * @note Must be a multiple of the read size |
MACRUM | 0:8eedb2495d52 | 106 | */ |
MACRUM | 0:8eedb2495d52 | 107 | virtual bd_size_t get_program_size() const; |
MACRUM | 0:8eedb2495d52 | 108 | |
MACRUM | 0:8eedb2495d52 | 109 | /** Get the total size of the underlying device |
MACRUM | 0:8eedb2495d52 | 110 | * |
MACRUM | 0:8eedb2495d52 | 111 | * @return Size of the underlying device in bytes |
MACRUM | 0:8eedb2495d52 | 112 | */ |
MACRUM | 0:8eedb2495d52 | 113 | virtual bd_size_t size() const; |
MACRUM | 0:8eedb2495d52 | 114 | |
MACRUM | 0:8eedb2495d52 | 115 | /** Enable or disable debugging |
MACRUM | 0:8eedb2495d52 | 116 | * |
MACRUM | 0:8eedb2495d52 | 117 | * @param State of debugging |
MACRUM | 0:8eedb2495d52 | 118 | */ |
MACRUM | 0:8eedb2495d52 | 119 | virtual void debug(bool dbg); |
MACRUM | 0:8eedb2495d52 | 120 | |
MACRUM | 0:8eedb2495d52 | 121 | /** Set the transfer frequency |
MACRUM | 0:8eedb2495d52 | 122 | * |
MACRUM | 0:8eedb2495d52 | 123 | * @param Transfer frequency |
MACRUM | 0:8eedb2495d52 | 124 | * @note Max frequency supported is 25MHZ |
MACRUM | 0:8eedb2495d52 | 125 | */ |
MACRUM | 0:8eedb2495d52 | 126 | virtual int frequency(uint64_t freq); |
MACRUM | 0:8eedb2495d52 | 127 | |
MACRUM | 0:8eedb2495d52 | 128 | |
MACRUM | 0:8eedb2495d52 | 129 | private: |
MACRUM | 0:8eedb2495d52 | 130 | /* Commands : Listed below are commands supported |
MACRUM | 0:8eedb2495d52 | 131 | * in SPI mode for SD card : Only Mandatory ones |
MACRUM | 0:8eedb2495d52 | 132 | */ |
MACRUM | 0:8eedb2495d52 | 133 | enum cmdSupported { |
MACRUM | 0:8eedb2495d52 | 134 | CMD_NOT_SUPPORTED = -1, /**< Command not supported error */ |
MACRUM | 0:8eedb2495d52 | 135 | CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */ |
MACRUM | 0:8eedb2495d52 | 136 | CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */ |
MACRUM | 0:8eedb2495d52 | 137 | CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */ |
MACRUM | 0:8eedb2495d52 | 138 | CMD8_SEND_IF_COND = 8, /**< Supply voltage info */ |
MACRUM | 0:8eedb2495d52 | 139 | CMD9_SEND_CSD = 9, /**< Provides Card Specific data */ |
MACRUM | 0:8eedb2495d52 | 140 | CMD10_SEND_CID = 10, /**< Provides Card Identification */ |
MACRUM | 0:8eedb2495d52 | 141 | CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */ |
MACRUM | 0:8eedb2495d52 | 142 | CMD13_SEND_STATUS = 13, /**< Card responds with status */ |
MACRUM | 0:8eedb2495d52 | 143 | CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */ |
MACRUM | 0:8eedb2495d52 | 144 | CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */ |
MACRUM | 0:8eedb2495d52 | 145 | CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted |
MACRUM | 0:8eedb2495d52 | 146 | by a STOP_TRANSMISSION command */ |
MACRUM | 0:8eedb2495d52 | 147 | CMD24_WRITE_BLOCK = 24, /**< Write single block of data */ |
MACRUM | 0:8eedb2495d52 | 148 | CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until |
MACRUM | 0:8eedb2495d52 | 149 | 'Stop Tran' token is sent */ |
MACRUM | 0:8eedb2495d52 | 150 | CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */ |
MACRUM | 0:8eedb2495d52 | 151 | CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write |
MACRUM | 0:8eedb2495d52 | 152 | block to be erased. */ |
MACRUM | 0:8eedb2495d52 | 153 | CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write |
MACRUM | 0:8eedb2495d52 | 154 | block of the continuous range to be erased.*/ |
MACRUM | 0:8eedb2495d52 | 155 | CMD38_ERASE = 38, /**< Erases all previously selected write blocks */ |
MACRUM | 0:8eedb2495d52 | 156 | CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */ |
MACRUM | 0:8eedb2495d52 | 157 | CMD56_GEN_CMD = 56, /**< General Purpose Command */ |
MACRUM | 0:8eedb2495d52 | 158 | CMD58_READ_OCR = 58, /**< Read OCR register of card */ |
MACRUM | 0:8eedb2495d52 | 159 | CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/ |
MACRUM | 0:8eedb2495d52 | 160 | // App Commands |
MACRUM | 0:8eedb2495d52 | 161 | ACMD6_SET_BUS_WIDTH = 6, |
MACRUM | 0:8eedb2495d52 | 162 | ACMD13_SD_STATUS = 13, |
MACRUM | 0:8eedb2495d52 | 163 | ACMD22_SEND_NUM_WR_BLOCKS = 22, |
MACRUM | 0:8eedb2495d52 | 164 | ACMD23_SET_WR_BLK_ERASE_COUNT = 23, |
MACRUM | 0:8eedb2495d52 | 165 | ACMD41_SD_SEND_OP_COND = 41, |
MACRUM | 0:8eedb2495d52 | 166 | ACMD42_SET_CLR_CARD_DETECT = 42, |
MACRUM | 0:8eedb2495d52 | 167 | ACMD51_SEND_SCR = 51, |
MACRUM | 0:8eedb2495d52 | 168 | }; |
MACRUM | 0:8eedb2495d52 | 169 | |
MACRUM | 0:8eedb2495d52 | 170 | uint8_t _card_type; |
MACRUM | 0:8eedb2495d52 | 171 | int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd=0, uint32_t *resp=NULL); |
MACRUM | 0:8eedb2495d52 | 172 | int _cmd8(); |
MACRUM | 0:8eedb2495d52 | 173 | |
MACRUM | 0:8eedb2495d52 | 174 | /* Move the SDCard into the SPI Mode idle state |
MACRUM | 0:8eedb2495d52 | 175 | * |
MACRUM | 0:8eedb2495d52 | 176 | * The card is transitioned from SDCard mode to SPI mode by sending the |
MACRUM | 0:8eedb2495d52 | 177 | * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the |
MACRUM | 0:8eedb2495d52 | 178 | * "SPI Startup" section of the comments at the head of the |
MACRUM | 0:8eedb2495d52 | 179 | * implementation file for further details and specification references. |
MACRUM | 0:8eedb2495d52 | 180 | * |
MACRUM | 0:8eedb2495d52 | 181 | * @return Response form the card. R1_IDLE_STATE (0x1), the successful |
MACRUM | 0:8eedb2495d52 | 182 | * response from CMD0. R1_XXX_XXX for more response |
MACRUM | 0:8eedb2495d52 | 183 | */ |
MACRUM | 0:8eedb2495d52 | 184 | uint32_t _go_idle_state(); |
MACRUM | 0:8eedb2495d52 | 185 | int _initialise_card(); |
MACRUM | 0:8eedb2495d52 | 186 | |
MACRUM | 0:8eedb2495d52 | 187 | bd_size_t _sectors; |
MACRUM | 0:8eedb2495d52 | 188 | bd_size_t _sd_sectors(); |
MACRUM | 0:8eedb2495d52 | 189 | |
MACRUM | 0:8eedb2495d52 | 190 | bool _is_valid_trim(bd_addr_t addr, bd_size_t size); |
MACRUM | 0:8eedb2495d52 | 191 | |
MACRUM | 0:8eedb2495d52 | 192 | /* SPI functions */ |
MACRUM | 0:8eedb2495d52 | 193 | Timer _spi_timer; /**< Timer Class object used for busy wait */ |
MACRUM | 0:8eedb2495d52 | 194 | uint32_t _init_sck; /**< Intial SPI frequency */ |
MACRUM | 0:8eedb2495d52 | 195 | uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */ |
MACRUM | 0:8eedb2495d52 | 196 | SPI _spi; /**< SPI Class object */ |
MACRUM | 0:8eedb2495d52 | 197 | |
MACRUM | 0:8eedb2495d52 | 198 | /* SPI initialization function */ |
MACRUM | 0:8eedb2495d52 | 199 | void _spi_init(); |
MACRUM | 0:8eedb2495d52 | 200 | uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg); |
MACRUM | 0:8eedb2495d52 | 201 | void _spi_wait(uint8_t count); |
MACRUM | 0:8eedb2495d52 | 202 | |
MACRUM | 0:8eedb2495d52 | 203 | bool _wait_token(uint8_t token); /**< Wait for token */ |
MACRUM | 0:8eedb2495d52 | 204 | bool _wait_ready(uint16_t ms=300); /**< 300ms default wait for card to be ready */ |
MACRUM | 0:8eedb2495d52 | 205 | int _read(uint8_t * buffer, uint32_t length); |
MACRUM | 0:8eedb2495d52 | 206 | int _read_bytes(uint8_t * buffer, uint32_t length); |
MACRUM | 0:8eedb2495d52 | 207 | uint8_t _write(const uint8_t *buffer,uint8_t token, uint32_t length); |
MACRUM | 0:8eedb2495d52 | 208 | int _freq(void); |
MACRUM | 0:8eedb2495d52 | 209 | |
MACRUM | 0:8eedb2495d52 | 210 | /* Chip Select and SPI mode select */ |
MACRUM | 0:8eedb2495d52 | 211 | DigitalOut _cs; |
MACRUM | 0:8eedb2495d52 | 212 | void _select(); |
MACRUM | 0:8eedb2495d52 | 213 | void _deselect(); |
MACRUM | 0:8eedb2495d52 | 214 | |
MACRUM | 0:8eedb2495d52 | 215 | virtual void lock() { |
MACRUM | 0:8eedb2495d52 | 216 | _mutex.lock(); |
MACRUM | 0:8eedb2495d52 | 217 | } |
MACRUM | 0:8eedb2495d52 | 218 | |
MACRUM | 0:8eedb2495d52 | 219 | virtual void unlock() { |
MACRUM | 0:8eedb2495d52 | 220 | _mutex.unlock(); |
MACRUM | 0:8eedb2495d52 | 221 | } |
MACRUM | 0:8eedb2495d52 | 222 | |
MACRUM | 0:8eedb2495d52 | 223 | PlatformMutex _mutex; |
MACRUM | 0:8eedb2495d52 | 224 | bd_size_t _block_size; |
MACRUM | 0:8eedb2495d52 | 225 | bd_size_t _erase_size; |
MACRUM | 0:8eedb2495d52 | 226 | bool _is_initialized; |
MACRUM | 0:8eedb2495d52 | 227 | bool _dbg; |
MACRUM | 0:8eedb2495d52 | 228 | bool _crc_on; |
MACRUM | 0:8eedb2495d52 | 229 | |
MACRUM | 0:8eedb2495d52 | 230 | MbedCRC<POLY_7BIT_SD, 7> _crc7; |
MACRUM | 0:8eedb2495d52 | 231 | MbedCRC<POLY_16BIT_CCITT, 16> _crc16; |
MACRUM | 0:8eedb2495d52 | 232 | }; |
MACRUM | 0:8eedb2495d52 | 233 | |
MACRUM | 0:8eedb2495d52 | 234 | #endif /* DEVICE_SPI */ |
MACRUM | 0:8eedb2495d52 | 235 | |
MACRUM | 0:8eedb2495d52 | 236 | #endif /* MBED_SD_BLOCK_DEVICE_H */ |