SDB

Dependents:   20180621_FT813 IOT_Lec7_SD_with_Acclerometer_empty GPS-Tracking-Velo IOT_Lec9_SD

Committer:
JackB
Date:
Mon Jul 23 12:22:39 2018 +0000
Revision:
0:6c7012898e59
SD

Who changed what in which revision?

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