Fork of https://github.com/ARMmbed/sd-driver. Added pin config for MAX32630FTHR

Dependents:   CircularBufferSDCardLib time_between_inerupt

Committer:
DVLevine
Date:
Tue Mar 20 17:35:00 2018 +0000
Revision:
0:69bfc1595ae5
Initial commit

Who changed what in which revision?

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