GroupU - 05012018 1543

Fork of 352 by Elec351 MMB

Committer:
mslade
Date:
Tue Jan 09 16:39:28 2018 +0000
Revision:
4:019309e6090e
Parent:
1:84581acd1333
Update for DLE

Who changed what in which revision?

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