Updated

Dependents:   PROJECTTEST

Committer:
Swabey89
Date:
Sat Jan 05 15:02:34 2019 +0000
Revision:
0:19febeae2548
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 0:19febeae2548 1 /* mbed Microcontroller Library
Swabey89 0:19febeae2548 2 * Copyright (c) 2006-2012 ARM Limited
Swabey89 0:19febeae2548 3 *
Swabey89 0:19febeae2548 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Swabey89 0:19febeae2548 5 * of this software and associated documentation files (the "Software"), to deal
Swabey89 0:19febeae2548 6 * in the Software without restriction, including without limitation the rights
Swabey89 0:19febeae2548 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Swabey89 0:19febeae2548 8 * copies of the Software, and to permit persons to whom the Software is
Swabey89 0:19febeae2548 9 * furnished to do so, subject to the following conditions:
Swabey89 0:19febeae2548 10 *
Swabey89 0:19febeae2548 11 * The above copyright notice and this permission notice shall be included in
Swabey89 0:19febeae2548 12 * all copies or substantial portions of the Software.
Swabey89 0:19febeae2548 13 *
Swabey89 0:19febeae2548 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Swabey89 0:19febeae2548 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Swabey89 0:19febeae2548 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Swabey89 0:19febeae2548 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Swabey89 0:19febeae2548 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Swabey89 0:19febeae2548 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Swabey89 0:19febeae2548 20 * SOFTWARE.
Swabey89 0:19febeae2548 21 */
Swabey89 0:19febeae2548 22 #ifndef MBED_SD_BLOCK_DEVICE_H
Swabey89 0:19febeae2548 23 #define MBED_SD_BLOCK_DEVICE_H
Swabey89 0:19febeae2548 24
Swabey89 0:19febeae2548 25 /* If the target has no SPI support then SDCard is not supported */
Swabey89 0:19febeae2548 26 #ifdef DEVICE_SPI
Swabey89 0:19febeae2548 27
Swabey89 0:19febeae2548 28 #include "BlockDevice.h"
Swabey89 0:19febeae2548 29 #include "mbed.h"
Swabey89 0:19febeae2548 30
Swabey89 0:19febeae2548 31
Swabey89 0:19febeae2548 32 /** Access an SD Card using SPI
Swabey89 0:19febeae2548 33 *
Swabey89 0:19febeae2548 34 * @code
Swabey89 0:19febeae2548 35 * #include "mbed.h"
Swabey89 0:19febeae2548 36 * #include "SDBlockDevice.h"
Swabey89 0:19febeae2548 37 *
Swabey89 0:19febeae2548 38 * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs
Swabey89 0:19febeae2548 39 * uint8_t block[512] = "Hello World!\n";
Swabey89 0:19febeae2548 40 *
Swabey89 0:19febeae2548 41 * int main() {
Swabey89 0:19febeae2548 42 * sd.init();
Swabey89 0:19febeae2548 43 * sd.write(block, 0, 512);
Swabey89 0:19febeae2548 44 * sd.read(block, 0, 512);
Swabey89 0:19febeae2548 45 * printf("%s", block);
Swabey89 0:19febeae2548 46 * sd.deinit();
Swabey89 0:19febeae2548 47 * }
Swabey89 0:19febeae2548 48 */
Swabey89 0:19febeae2548 49 class SDBlockDevice : public BlockDevice {
Swabey89 0:19febeae2548 50 public:
Swabey89 0:19febeae2548 51 /** Lifetime of an SD card
Swabey89 0:19febeae2548 52 */
Swabey89 0:19febeae2548 53 SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz=1000000);
Swabey89 0:19febeae2548 54 virtual ~SDBlockDevice();
Swabey89 0:19febeae2548 55
Swabey89 0:19febeae2548 56 /** Initialize a block device
Swabey89 0:19febeae2548 57 *
Swabey89 0:19febeae2548 58 * @return 0 on success or a negative error code on failure
Swabey89 0:19febeae2548 59 */
Swabey89 0:19febeae2548 60 virtual int init();
Swabey89 0:19febeae2548 61
Swabey89 0:19febeae2548 62 /** Deinitialize a block device
Swabey89 0:19febeae2548 63 *
Swabey89 0:19febeae2548 64 * @return 0 on success or a negative error code on failure
Swabey89 0:19febeae2548 65 */
Swabey89 0:19febeae2548 66 virtual int deinit();
Swabey89 0:19febeae2548 67
Swabey89 0:19febeae2548 68 /** Read blocks from a block device
Swabey89 0:19febeae2548 69 *
Swabey89 0:19febeae2548 70 * @param buffer Buffer to write blocks to
Swabey89 0:19febeae2548 71 * @param addr Address of block to begin reading from
Swabey89 0:19febeae2548 72 * @param size Size to read in bytes, must be a multiple of read block size
Swabey89 0:19febeae2548 73 * @return 0 on success, negative error code on failure
Swabey89 0:19febeae2548 74 */
Swabey89 0:19febeae2548 75 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
Swabey89 0:19febeae2548 76
Swabey89 0:19febeae2548 77 /** Program blocks to a block device
Swabey89 0:19febeae2548 78 *
Swabey89 0:19febeae2548 79 * The blocks must have been erased prior to being programmed
Swabey89 0:19febeae2548 80 *
Swabey89 0:19febeae2548 81 * @param buffer Buffer of data to write to blocks
Swabey89 0:19febeae2548 82 * @param addr Address of block to begin writing to
Swabey89 0:19febeae2548 83 * @param size Size to write in bytes, must be a multiple of program block size
Swabey89 0:19febeae2548 84 * @return 0 on success, negative error code on failure
Swabey89 0:19febeae2548 85 */
Swabey89 0:19febeae2548 86 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
Swabey89 0:19febeae2548 87
Swabey89 0:19febeae2548 88 /** Mark blocks as no longer in use
Swabey89 0:19febeae2548 89 *
Swabey89 0:19febeae2548 90 * This function provides a hint to the underlying block device that a region of blocks
Swabey89 0:19febeae2548 91 * is no longer in use and may be erased without side effects. Erase must still be called
Swabey89 0:19febeae2548 92 * before programming, but trimming allows flash-translation-layers to schedule erases when
Swabey89 0:19febeae2548 93 * the device is not busy.
Swabey89 0:19febeae2548 94 *
Swabey89 0:19febeae2548 95 * @param addr Address of block to mark as unused
Swabey89 0:19febeae2548 96 * @param size Size to mark as unused in bytes, must be a multiple of erase block size
Swabey89 0:19febeae2548 97 * @return 0 on success, negative error code on failure
Swabey89 0:19febeae2548 98 */
Swabey89 0:19febeae2548 99 virtual int trim(bd_addr_t addr, bd_size_t size);
Swabey89 0:19febeae2548 100
Swabey89 0:19febeae2548 101 /** Get the size of a readable block
Swabey89 0:19febeae2548 102 *
Swabey89 0:19febeae2548 103 * @return Size of a readable block in bytes
Swabey89 0:19febeae2548 104 */
Swabey89 0:19febeae2548 105 virtual bd_size_t get_read_size() const;
Swabey89 0:19febeae2548 106
Swabey89 0:19febeae2548 107 /** Get the size of a programable block
Swabey89 0:19febeae2548 108 *
Swabey89 0:19febeae2548 109 * @return Size of a programable block in bytes
Swabey89 0:19febeae2548 110 * @note Must be a multiple of the read size
Swabey89 0:19febeae2548 111 */
Swabey89 0:19febeae2548 112 virtual bd_size_t get_program_size() const;
Swabey89 0:19febeae2548 113
Swabey89 0:19febeae2548 114 /** Get the total size of the underlying device
Swabey89 0:19febeae2548 115 *
Swabey89 0:19febeae2548 116 * @return Size of the underlying device in bytes
Swabey89 0:19febeae2548 117 */
Swabey89 0:19febeae2548 118 virtual bd_size_t size() const;
Swabey89 0:19febeae2548 119
Swabey89 0:19febeae2548 120 /** Enable or disable debugging
Swabey89 0:19febeae2548 121 *
Swabey89 0:19febeae2548 122 * @param State of debugging
Swabey89 0:19febeae2548 123 */
Swabey89 0:19febeae2548 124 virtual void debug(bool dbg);
Swabey89 0:19febeae2548 125
Swabey89 0:19febeae2548 126 /** Set the transfer frequency
Swabey89 0:19febeae2548 127 *
Swabey89 0:19febeae2548 128 * @param Transfer frequency
Swabey89 0:19febeae2548 129 * @note Max frequency supported is 25MHZ
Swabey89 0:19febeae2548 130 */
Swabey89 0:19febeae2548 131 virtual int frequency(uint64_t freq);
Swabey89 0:19febeae2548 132
Swabey89 0:19febeae2548 133
Swabey89 0:19febeae2548 134 private:
Swabey89 0:19febeae2548 135 /* Commands : Listed below are commands supported
Swabey89 0:19febeae2548 136 * in SPI mode for SD card : Only Mandatory ones
Swabey89 0:19febeae2548 137 */
Swabey89 0:19febeae2548 138 enum cmdSupported {
Swabey89 0:19febeae2548 139 CMD_NOT_SUPPORTED = -1, /**< Command not supported error */
Swabey89 0:19febeae2548 140 CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */
Swabey89 0:19febeae2548 141 CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */
Swabey89 0:19febeae2548 142 CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */
Swabey89 0:19febeae2548 143 CMD8_SEND_IF_COND = 8, /**< Supply voltage info */
Swabey89 0:19febeae2548 144 CMD9_SEND_CSD = 9, /**< Provides Card Specific data */
Swabey89 0:19febeae2548 145 CMD10_SEND_CID = 10, /**< Provides Card Identification */
Swabey89 0:19febeae2548 146 CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */
Swabey89 0:19febeae2548 147 CMD13_SEND_STATUS = 13, /**< Card responds with status */
Swabey89 0:19febeae2548 148 CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */
Swabey89 0:19febeae2548 149 CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */
Swabey89 0:19febeae2548 150 CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted
Swabey89 0:19febeae2548 151 by a STOP_TRANSMISSION command */
Swabey89 0:19febeae2548 152 CMD24_WRITE_BLOCK = 24, /**< Write single block of data */
Swabey89 0:19febeae2548 153 CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until
Swabey89 0:19febeae2548 154 'Stop Tran' token is sent */
Swabey89 0:19febeae2548 155 CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */
Swabey89 0:19febeae2548 156 CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write
Swabey89 0:19febeae2548 157 block to be erased. */
Swabey89 0:19febeae2548 158 CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write
Swabey89 0:19febeae2548 159 block of the continuous range to be erased.*/
Swabey89 0:19febeae2548 160 CMD38_ERASE = 38, /**< Erases all previously selected write blocks */
Swabey89 0:19febeae2548 161 CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */
Swabey89 0:19febeae2548 162 CMD56_GEN_CMD = 56, /**< General Purpose Command */
Swabey89 0:19febeae2548 163 CMD58_READ_OCR = 58, /**< Read OCR register of card */
Swabey89 0:19febeae2548 164 CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/
Swabey89 0:19febeae2548 165 // App Commands
Swabey89 0:19febeae2548 166 ACMD6_SET_BUS_WIDTH = 6,
Swabey89 0:19febeae2548 167 ACMD13_SD_STATUS = 13,
Swabey89 0:19febeae2548 168 ACMD22_SEND_NUM_WR_BLOCKS = 22,
Swabey89 0:19febeae2548 169 ACMD23_SET_WR_BLK_ERASE_COUNT = 23,
Swabey89 0:19febeae2548 170 ACMD41_SD_SEND_OP_COND = 41,
Swabey89 0:19febeae2548 171 ACMD42_SET_CLR_CARD_DETECT = 42,
Swabey89 0:19febeae2548 172 ACMD51_SEND_SCR = 51,
Swabey89 0:19febeae2548 173 };
Swabey89 0:19febeae2548 174
Swabey89 0:19febeae2548 175 uint8_t _card_type;
Swabey89 0:19febeae2548 176 int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd=0, uint32_t *resp=NULL);
Swabey89 0:19febeae2548 177 int _cmd8();
Swabey89 0:19febeae2548 178
Swabey89 0:19febeae2548 179 /* Move the SDCard into the SPI Mode idle state
Swabey89 0:19febeae2548 180 *
Swabey89 0:19febeae2548 181 * The card is transitioned from SDCard mode to SPI mode by sending the
Swabey89 0:19febeae2548 182 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the
Swabey89 0:19febeae2548 183 * "SPI Startup" section of the comments at the head of the
Swabey89 0:19febeae2548 184 * implementation file for further details and specification references.
Swabey89 0:19febeae2548 185 *
Swabey89 0:19febeae2548 186 * @return Response form the card. R1_IDLE_STATE (0x1), the successful
Swabey89 0:19febeae2548 187 * response from CMD0. R1_XXX_XXX for more response
Swabey89 0:19febeae2548 188 */
Swabey89 0:19febeae2548 189 uint32_t _go_idle_state();
Swabey89 0:19febeae2548 190 int _initialise_card();
Swabey89 0:19febeae2548 191
Swabey89 0:19febeae2548 192 uint32_t _sectors;
Swabey89 0:19febeae2548 193 uint32_t _sd_sectors();
Swabey89 0:19febeae2548 194
Swabey89 0:19febeae2548 195 bool _is_valid_trim(bd_addr_t addr, bd_size_t size);
Swabey89 0:19febeae2548 196
Swabey89 0:19febeae2548 197 /* SPI functions */
Swabey89 0:19febeae2548 198 Timer _spi_timer; /**< Timer Class object used for busy wait */
Swabey89 0:19febeae2548 199 uint32_t _init_sck; /**< Intial SPI frequency */
Swabey89 0:19febeae2548 200 uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */
Swabey89 0:19febeae2548 201 SPI _spi; /**< SPI Class object */
Swabey89 0:19febeae2548 202
Swabey89 0:19febeae2548 203 /* SPI initialization function */
Swabey89 0:19febeae2548 204 void _spi_init();
Swabey89 0:19febeae2548 205 uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg);
Swabey89 0:19febeae2548 206 void _spi_wait(uint8_t count);
Swabey89 0:19febeae2548 207
Swabey89 0:19febeae2548 208 bool _wait_token(uint8_t token); /**< Wait for token */
Swabey89 0:19febeae2548 209 bool _wait_ready(uint16_t ms=300); /**< 300ms default wait for card to be ready */
Swabey89 0:19febeae2548 210 int _read(uint8_t * buffer, uint32_t length);
Swabey89 0:19febeae2548 211 int _read_bytes(uint8_t * buffer, uint32_t length);
Swabey89 0:19febeae2548 212 uint8_t _write(const uint8_t *buffer,uint8_t token, uint32_t length);
Swabey89 0:19febeae2548 213 int _freq(void);
Swabey89 0:19febeae2548 214
Swabey89 0:19febeae2548 215 /* Chip Select and SPI mode select */
Swabey89 0:19febeae2548 216 DigitalOut _cs;
Swabey89 0:19febeae2548 217 void _select();
Swabey89 0:19febeae2548 218 void _deselect();
Swabey89 0:19febeae2548 219
Swabey89 0:19febeae2548 220 mutable Mutex _lock;
Swabey89 0:19febeae2548 221 uint32_t _block_size;
Swabey89 0:19febeae2548 222 uint32_t _erase_size;
Swabey89 0:19febeae2548 223 bool _is_initialized;
Swabey89 0:19febeae2548 224 bool _dbg;
Swabey89 0:19febeae2548 225 };
Swabey89 0:19febeae2548 226
Swabey89 0:19febeae2548 227 #endif /* DEVICE_SPI */
Swabey89 0:19febeae2548 228
Swabey89 0:19febeae2548 229 #endif /* MBED_SD_BLOCK_DEVICE_H */