Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
ffxx68
Date:
Tue Jul 19 13:49:28 2022 +0000
Revision:
2:02f003d025a7
Parent:
1:3488faedd0d1
SD Card handling added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexVC97 0:3bdfc1556537 1 /* mbed Microcontroller Library
AlexVC97 0:3bdfc1556537 2 * Copyright (c) 2006-2012 ARM Limited
AlexVC97 0:3bdfc1556537 3 *
AlexVC97 0:3bdfc1556537 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
AlexVC97 0:3bdfc1556537 5 * of this software and associated documentation files (the "Software"), to deal
AlexVC97 0:3bdfc1556537 6 * in the Software without restriction, including without limitation the rights
AlexVC97 0:3bdfc1556537 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AlexVC97 0:3bdfc1556537 8 * copies of the Software, and to permit persons to whom the Software is
AlexVC97 0:3bdfc1556537 9 * furnished to do so, subject to the following conditions:
AlexVC97 0:3bdfc1556537 10 *
AlexVC97 0:3bdfc1556537 11 * The above copyright notice and this permission notice shall be included in
AlexVC97 0:3bdfc1556537 12 * all copies or substantial portions of the Software.
AlexVC97 0:3bdfc1556537 13 *
AlexVC97 0:3bdfc1556537 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AlexVC97 0:3bdfc1556537 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AlexVC97 0:3bdfc1556537 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AlexVC97 0:3bdfc1556537 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AlexVC97 0:3bdfc1556537 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AlexVC97 0:3bdfc1556537 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
AlexVC97 0:3bdfc1556537 20 * SOFTWARE.
AlexVC97 0:3bdfc1556537 21 */
AlexVC97 0:3bdfc1556537 22 /* Introduction
AlexVC97 0:3bdfc1556537 23 * ------------
AlexVC97 0:3bdfc1556537 24 * SD and MMC cards support a number of interfaces, but common to them all
AlexVC97 0:3bdfc1556537 25 * is one based on SPI. This is the one I'm implmenting because it means
AlexVC97 0:3bdfc1556537 26 * it is much more portable even though not so performant, and we already
AlexVC97 0:3bdfc1556537 27 * have the mbed SPI Interface!
AlexVC97 0:3bdfc1556537 28 *
AlexVC97 0:3bdfc1556537 29 * The main reference I'm using is Chapter 7, "SPI Mode" of:
AlexVC97 0:3bdfc1556537 30 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
AlexVC97 0:3bdfc1556537 31 *
ffxx68 1:3488faedd0d1 32 * SPI Startup
AlexVC97 0:3bdfc1556537 33 * -----------
ffxx68 1:3488faedd0d1 34 * The SD card powers up in SD mode. The start-up procedure is complicated
ffxx68 1:3488faedd0d1 35 * by the requirement to support older SDCards in a backwards compatible
ffxx68 1:3488faedd0d1 36 * way with the new higher capacity variants SDHC and SDHC.
ffxx68 1:3488faedd0d1 37 *
ffxx68 1:3488faedd0d1 38 * The following figures from the specification with associated text describe
ffxx68 1:3488faedd0d1 39 * the SPI mode initialisation process:
ffxx68 1:3488faedd0d1 40 * - Figure 7-1: SD Memory Card State Diagram (SPI mode)
ffxx68 1:3488faedd0d1 41 * - Figure 7-2: SPI Mode Initialization Flow
ffxx68 1:3488faedd0d1 42 *
ffxx68 1:3488faedd0d1 43 * Firstly, a low initial clock should be selected (in the range of 100-
ffxx68 1:3488faedd0d1 44 * 400kHZ). After initialisation has been completed, the switch to a
ffxx68 1:3488faedd0d1 45 * higher clock speed can be made (e.g. 1MHz). Newer cards will support
ffxx68 1:3488faedd0d1 46 * higher speeds than the default _transfer_sck defined here.
ffxx68 1:3488faedd0d1 47 *
ffxx68 1:3488faedd0d1 48 * After power up, we need to provide at least 1 msec delay and 74 clock cycles
ffxx68 1:3488faedd0d1 49 * before sending any command to an SD Card. Since we get 8 clock cycles with each byte,
ffxx68 1:3488faedd0d1 50 * we can send 10 bytes for a total of 80 clock cycles.
ffxx68 1:3488faedd0d1 51 * The SDCard spec also specifies that CS must be held high during this period.
ffxx68 1:3488faedd0d1 52 *
ffxx68 1:3488faedd0d1 53 * Next, note the following from the SDCard specification (note to
ffxx68 1:3488faedd0d1 54 * Figure 7-1):
ffxx68 1:3488faedd0d1 55 *
ffxx68 1:3488faedd0d1 56 * In any of the cases CMD1 is not recommended because it may be difficult for the host
ffxx68 1:3488faedd0d1 57 * to distinguish between MultiMediaCard and SD Memory Card
ffxx68 1:3488faedd0d1 58 *
ffxx68 1:3488faedd0d1 59 * Hence CMD1 is not used for the initialisation sequence.
ffxx68 1:3488faedd0d1 60 *
ffxx68 1:3488faedd0d1 61 * The SPI interface mode is selected by asserting CS low and sending the
ffxx68 1:3488faedd0d1 62 * reset command (CMD0). The card will respond with a (R1) response.
ffxx68 1:3488faedd0d1 63 * In practice many cards initially respond with 0xff or invalid data
ffxx68 1:3488faedd0d1 64 * which is ignored. Data is read until a valid response is received
ffxx68 1:3488faedd0d1 65 * or the number of re-reads has exceeded a maximim count. If a valid
ffxx68 1:3488faedd0d1 66 * response is not received then the CMD0 can be retried. This
ffxx68 1:3488faedd0d1 67 * has been found to successfully initialise cards where the SPI master
ffxx68 1:3488faedd0d1 68 * (on MCU) has been reset but the SDCard has not, so the first
ffxx68 1:3488faedd0d1 69 * CMD0 may be lost.
AlexVC97 0:3bdfc1556537 70 *
AlexVC97 0:3bdfc1556537 71 * CMD8 is optionally sent to determine the voltage range supported, and
AlexVC97 0:3bdfc1556537 72 * indirectly determine whether it is a version 1.x SD/non-SD card or
AlexVC97 0:3bdfc1556537 73 * version 2.x. I'll just ignore this for now.
AlexVC97 0:3bdfc1556537 74 *
AlexVC97 0:3bdfc1556537 75 * ACMD41 is repeatedly issued to initialise the card, until "in idle"
AlexVC97 0:3bdfc1556537 76 * (bit 0) of the R1 response goes to '0', indicating it is initialised.
AlexVC97 0:3bdfc1556537 77 *
AlexVC97 0:3bdfc1556537 78 * You should also indicate whether the host supports High Capicity cards,
AlexVC97 0:3bdfc1556537 79 * and check whether the card is high capacity - i'll also ignore this
AlexVC97 0:3bdfc1556537 80 *
AlexVC97 0:3bdfc1556537 81 * SPI Protocol
AlexVC97 0:3bdfc1556537 82 * ------------
AlexVC97 0:3bdfc1556537 83 * The SD SPI protocol is based on transactions made up of 8-bit words, with
AlexVC97 0:3bdfc1556537 84 * the host starting every bus transaction by asserting the CS signal low. The
AlexVC97 0:3bdfc1556537 85 * card always responds to commands, data blocks and errors.
AlexVC97 0:3bdfc1556537 86 *
AlexVC97 0:3bdfc1556537 87 * The protocol supports a CRC, but by default it is off (except for the
AlexVC97 0:3bdfc1556537 88 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
AlexVC97 0:3bdfc1556537 89 * I'll leave the CRC off I think!
AlexVC97 0:3bdfc1556537 90 *
AlexVC97 0:3bdfc1556537 91 * Standard capacity cards have variable data block sizes, whereas High
AlexVC97 0:3bdfc1556537 92 * Capacity cards fix the size of data block to 512 bytes. I'll therefore
AlexVC97 0:3bdfc1556537 93 * just always use the Standard Capacity cards with a block size of 512 bytes.
AlexVC97 0:3bdfc1556537 94 * This is set with CMD16.
AlexVC97 0:3bdfc1556537 95 *
AlexVC97 0:3bdfc1556537 96 * You can read and write single blocks (CMD17, CMD25) or multiple blocks
AlexVC97 0:3bdfc1556537 97 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
AlexVC97 0:3bdfc1556537 98 * the card gets a read command, it responds with a response token, and then
AlexVC97 0:3bdfc1556537 99 * a data token or an error.
AlexVC97 0:3bdfc1556537 100 *
AlexVC97 0:3bdfc1556537 101 * SPI Command Format
AlexVC97 0:3bdfc1556537 102 * ------------------
AlexVC97 0:3bdfc1556537 103 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
AlexVC97 0:3bdfc1556537 104 *
AlexVC97 0:3bdfc1556537 105 * +---------------+------------+------------+-----------+----------+--------------+
AlexVC97 0:3bdfc1556537 106 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
AlexVC97 0:3bdfc1556537 107 * +---------------+------------+------------+-----------+----------+--------------+
AlexVC97 0:3bdfc1556537 108 *
AlexVC97 0:3bdfc1556537 109 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
AlexVC97 0:3bdfc1556537 110 *
AlexVC97 0:3bdfc1556537 111 * All Application Specific commands shall be preceded with APP_CMD (CMD55).
AlexVC97 0:3bdfc1556537 112 *
AlexVC97 0:3bdfc1556537 113 * SPI Response Format
AlexVC97 0:3bdfc1556537 114 * -------------------
AlexVC97 0:3bdfc1556537 115 * The main response format (R1) is a status byte (normally zero). Key flags:
AlexVC97 0:3bdfc1556537 116 * idle - 1 if the card is in an idle state/initialising
AlexVC97 0:3bdfc1556537 117 * cmd - 1 if an illegal command code was detected
AlexVC97 0:3bdfc1556537 118 *
AlexVC97 0:3bdfc1556537 119 * +-------------------------------------------------+
AlexVC97 0:3bdfc1556537 120 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
AlexVC97 0:3bdfc1556537 121 * +-------------------------------------------------+
AlexVC97 0:3bdfc1556537 122 *
AlexVC97 0:3bdfc1556537 123 * R1b is the same, except it is followed by a busy signal (zeros) until
AlexVC97 0:3bdfc1556537 124 * the first non-zero byte when it is ready again.
AlexVC97 0:3bdfc1556537 125 *
AlexVC97 0:3bdfc1556537 126 * Data Response Token
AlexVC97 0:3bdfc1556537 127 * -------------------
AlexVC97 0:3bdfc1556537 128 * Every data block written to the card is acknowledged by a byte
AlexVC97 0:3bdfc1556537 129 * response token
AlexVC97 0:3bdfc1556537 130 *
AlexVC97 0:3bdfc1556537 131 * +----------------------+
AlexVC97 0:3bdfc1556537 132 * | xxx | 0 | status | 1 |
AlexVC97 0:3bdfc1556537 133 * +----------------------+
AlexVC97 0:3bdfc1556537 134 * 010 - OK!
AlexVC97 0:3bdfc1556537 135 * 101 - CRC Error
AlexVC97 0:3bdfc1556537 136 * 110 - Write Error
AlexVC97 0:3bdfc1556537 137 *
AlexVC97 0:3bdfc1556537 138 * Single Block Read and Write
AlexVC97 0:3bdfc1556537 139 * ---------------------------
AlexVC97 0:3bdfc1556537 140 *
AlexVC97 0:3bdfc1556537 141 * Block transfers have a byte header, followed by the data, followed
AlexVC97 0:3bdfc1556537 142 * by a 16-bit CRC. In our case, the data will always be 512 bytes.
AlexVC97 0:3bdfc1556537 143 *
AlexVC97 0:3bdfc1556537 144 * +------+---------+---------+- - - -+---------+-----------+----------+
AlexVC97 0:3bdfc1556537 145 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] |
AlexVC97 0:3bdfc1556537 146 * +------+---------+---------+- - - -+---------+-----------+----------+
ffxx68 1:3488faedd0d1 147 *
ffxx68 1:3488faedd0d1 148 *
ffxx68 1:3488faedd0d1 149 *
AlexVC97 0:3bdfc1556537 150 */
AlexVC97 0:3bdfc1556537 151 #include "SDFileSystem.h"
AlexVC97 0:3bdfc1556537 152 #include "mbed_debug.h"
AlexVC97 0:3bdfc1556537 153
ffxx68 2:02f003d025a7 154 #define SD_COMMAND_TIMEOUT 15/*5000*/
ffxx68 1:3488faedd0d1 155 #define SD_CMD0_GO_IDLE_STATE_RETRIES 3
ffxx68 1:3488faedd0d1 156 #define SD_CMD0_GO_IDLE_STATE 0x00
ffxx68 2:02f003d025a7 157 #define SD_DBG 0
AlexVC97 0:3bdfc1556537 158
AlexVC97 0:3bdfc1556537 159 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
AlexVC97 0:3bdfc1556537 160 FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0) {
AlexVC97 0:3bdfc1556537 161 _cs = 1;
AlexVC97 0:3bdfc1556537 162
ffxx68 1:3488faedd0d1 163 // Set default frquency, for initialisation and data transfer
ffxx68 1:3488faedd0d1 164 _init_sck = 400000;
AlexVC97 0:3bdfc1556537 165 _transfer_sck = 1000000;
AlexVC97 0:3bdfc1556537 166 }
AlexVC97 0:3bdfc1556537 167
ffxx68 1:3488faedd0d1 168 // R1 bits
AlexVC97 0:3bdfc1556537 169 #define R1_IDLE_STATE (1 << 0)
AlexVC97 0:3bdfc1556537 170 #define R1_ERASE_RESET (1 << 1)
AlexVC97 0:3bdfc1556537 171 #define R1_ILLEGAL_COMMAND (1 << 2)
AlexVC97 0:3bdfc1556537 172 #define R1_COM_CRC_ERROR (1 << 3)
AlexVC97 0:3bdfc1556537 173 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
AlexVC97 0:3bdfc1556537 174 #define R1_ADDRESS_ERROR (1 << 5)
AlexVC97 0:3bdfc1556537 175 #define R1_PARAMETER_ERROR (1 << 6)
AlexVC97 0:3bdfc1556537 176
AlexVC97 0:3bdfc1556537 177 // Types
AlexVC97 0:3bdfc1556537 178 // - v1.x Standard Capacity
AlexVC97 0:3bdfc1556537 179 // - v2.x Standard Capacity
AlexVC97 0:3bdfc1556537 180 // - v2.x High Capacity
AlexVC97 0:3bdfc1556537 181 // - Not recognised as an SD Card
AlexVC97 0:3bdfc1556537 182 #define SDCARD_FAIL 0
AlexVC97 0:3bdfc1556537 183 #define SDCARD_V1 1
AlexVC97 0:3bdfc1556537 184 #define SDCARD_V2 2
AlexVC97 0:3bdfc1556537 185 #define SDCARD_V2HC 3
AlexVC97 0:3bdfc1556537 186
ffxx68 1:3488faedd0d1 187 /* SDFileSystem::_go_idle_state()
ffxx68 1:3488faedd0d1 188 *
ffxx68 1:3488faedd0d1 189 * ARGUMENTS
ffxx68 1:3488faedd0d1 190 * None
ffxx68 1:3488faedd0d1 191 * DETAILS:
ffxx68 1:3488faedd0d1 192 * Put the SDCard into the SPI Mode idle state by sending the CMD0
ffxx68 1:3488faedd0d1 193 * (GO_IDLE_STATE) command. See the notes in the "SPI Startup" section
ffxx68 1:3488faedd0d1 194 * of the comments at the head of this file.
ffxx68 1:3488faedd0d1 195 *
ffxx68 1:3488faedd0d1 196 * RETURN:
ffxx68 1:3488faedd0d1 197 * -1 an error occured e.g. a valid response was not received.
ffxx68 1:3488faedd0d1 198 * R1_IDLE_STATE (0x1), the successful response from CMD0.
ffxx68 1:3488faedd0d1 199 */
ffxx68 1:3488faedd0d1 200 int SDFileSystem::_go_idle_state() {
ffxx68 1:3488faedd0d1 201 _spi.lock();
ffxx68 1:3488faedd0d1 202
ffxx68 1:3488faedd0d1 203 int cmd_arg = 0; /* CMD0 argument is just "stuff bits"*/
ffxx68 1:3488faedd0d1 204
ffxx68 1:3488faedd0d1 205 /* Resetting the MCU SPI master may not reset the on-board SDCard, in which
ffxx68 1:3488faedd0d1 206 * case when MCU power-on occurs the SDCard will resume operations as
ffxx68 1:3488faedd0d1 207 * though there was no reset. In this scenario the first CMD0 will
ffxx68 1:3488faedd0d1 208 * not be interpretted as a command and get lost. For some cards retrying
ffxx68 1:3488faedd0d1 209 * the command overcomes this situation. */
ffxx68 1:3488faedd0d1 210 for (int num_retries = 0; num_retries < SD_CMD0_GO_IDLE_STATE_RETRIES; num_retries++) {
ffxx68 1:3488faedd0d1 211
ffxx68 2:02f003d025a7 212 // Set to SCK for initialisation, and clock card with CS high
ffxx68 1:3488faedd0d1 213 _cs = 1;
ffxx68 1:3488faedd0d1 214 // Initial delay
ffxx68 1:3488faedd0d1 215 wait_us (1000);
ffxx68 1:3488faedd0d1 216 // Initial 74 cycles required for few cards, before selecting SPI mode
ffxx68 1:3488faedd0d1 217 for (int i = 0; i < 16; i++) {
ffxx68 1:3488faedd0d1 218 _spi.write(0xFF);
ffxx68 1:3488faedd0d1 219 }
ffxx68 1:3488faedd0d1 220
ffxx68 2:02f003d025a7 221 /* send a CMD0, with /CS asserted low */
ffxx68 1:3488faedd0d1 222 _cs = 0;
ffxx68 2:02f003d025a7 223 wait_us (1000);
ffxx68 1:3488faedd0d1 224 _spi.write(0x40 | SD_CMD0_GO_IDLE_STATE);
ffxx68 1:3488faedd0d1 225 _spi.write(cmd_arg >> 24);
ffxx68 1:3488faedd0d1 226 _spi.write(cmd_arg >> 16);
ffxx68 1:3488faedd0d1 227 _spi.write(cmd_arg >> 8);
ffxx68 1:3488faedd0d1 228 _spi.write(cmd_arg >> 0);
ffxx68 1:3488faedd0d1 229 _spi.write(0x95);
ffxx68 1:3488faedd0d1 230
ffxx68 1:3488faedd0d1 231 // wait for the response (response[7] == 0)
ffxx68 1:3488faedd0d1 232 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ffxx68 1:3488faedd0d1 233 int response = _spi.write(0xFF);
ffxx68 1:3488faedd0d1 234 /* Explicitly check for the R1_IDLE_STATE response rather that most significant bit
ffxx68 1:3488faedd0d1 235 * being 0 because invalid data can be returned. */
ffxx68 1:3488faedd0d1 236 if (response != R1_IDLE_STATE)
ffxx68 1:3488faedd0d1 237 debug_if(SD_DBG, "CMD0 #%d (retry #%d) response: 0x%.2X\n", i, num_retries, response);
ffxx68 1:3488faedd0d1 238 else {
ffxx68 1:3488faedd0d1 239 _cs = 1;
ffxx68 1:3488faedd0d1 240 _spi.write(0xFF);
ffxx68 1:3488faedd0d1 241 _spi.unlock();
ffxx68 1:3488faedd0d1 242 return response;
ffxx68 1:3488faedd0d1 243 }
ffxx68 2:02f003d025a7 244 wait_us(1000);
ffxx68 1:3488faedd0d1 245 }
ffxx68 1:3488faedd0d1 246 }
ffxx68 1:3488faedd0d1 247 _cs = 1;
ffxx68 1:3488faedd0d1 248 _spi.write(0xFF);
ffxx68 1:3488faedd0d1 249 _spi.unlock();
ffxx68 1:3488faedd0d1 250 return -1; // timeout
ffxx68 1:3488faedd0d1 251 }
ffxx68 1:3488faedd0d1 252
AlexVC97 0:3bdfc1556537 253 int SDFileSystem::initialise_card() {
ffxx68 1:3488faedd0d1 254
ffxx68 1:3488faedd0d1 255 _spi.lock();
ffxx68 1:3488faedd0d1 256 _spi.format(8, 0);
AlexVC97 0:3bdfc1556537 257 _spi.frequency(_init_sck);
ffxx68 1:3488faedd0d1 258 _spi.unlock();
ffxx68 1:3488faedd0d1 259
ffxx68 1:3488faedd0d1 260 /* moved within _go_idle_state
AlexVC97 0:3bdfc1556537 261 _cs = 1;
ffxx68 1:3488faedd0d1 262 // Initial delay
ffxx68 1:3488faedd0d1 263 wait_us (1000);
ffxx68 1:3488faedd0d1 264 // Initial 74 cycles required for few cards, before selecting SPI mode
AlexVC97 0:3bdfc1556537 265 for (int i = 0; i < 16; i++) {
AlexVC97 0:3bdfc1556537 266 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 267 }
ffxx68 1:3488faedd0d1 268 */
ffxx68 1:3488faedd0d1 269
ffxx68 1:3488faedd0d1 270 /* Transition from SD Card mode to SPI mode by sending CMD0 GO_IDLE_STATE command */
ffxx68 1:3488faedd0d1 271 if (_go_idle_state() != R1_IDLE_STATE) {
ffxx68 1:3488faedd0d1 272 debug_if(SD_DBG, "No disk, or could not put SD card in to SPI idle state\n");
ffxx68 1:3488faedd0d1 273 return SDCARD_FAIL;
ffxx68 1:3488faedd0d1 274 }
ffxx68 1:3488faedd0d1 275 /* send CMD0, should return with all zeros except IDLE STATE set (bit 0)
AlexVC97 0:3bdfc1556537 276 if (_cmd(0, 0) != R1_IDLE_STATE) {
AlexVC97 0:3bdfc1556537 277 debug("No disk, or could not put SD card in to SPI idle state\n");
AlexVC97 0:3bdfc1556537 278 return SDCARD_FAIL;
AlexVC97 0:3bdfc1556537 279 }
ffxx68 1:3488faedd0d1 280 */
AlexVC97 0:3bdfc1556537 281
AlexVC97 0:3bdfc1556537 282 // send CMD8 to determine whther it is ver 2.x
AlexVC97 0:3bdfc1556537 283 int r = _cmd8();
AlexVC97 0:3bdfc1556537 284 if (r == R1_IDLE_STATE) {
AlexVC97 0:3bdfc1556537 285 return initialise_card_v2();
AlexVC97 0:3bdfc1556537 286 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
AlexVC97 0:3bdfc1556537 287 return initialise_card_v1();
AlexVC97 0:3bdfc1556537 288 } else {
AlexVC97 0:3bdfc1556537 289 debug("Not in idle state after sending CMD8 (not an SD card?)\n");
AlexVC97 0:3bdfc1556537 290 return SDCARD_FAIL;
AlexVC97 0:3bdfc1556537 291 }
AlexVC97 0:3bdfc1556537 292 }
AlexVC97 0:3bdfc1556537 293
AlexVC97 0:3bdfc1556537 294 int SDFileSystem::initialise_card_v1() {
AlexVC97 0:3bdfc1556537 295 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
AlexVC97 0:3bdfc1556537 296 _cmd(55, 0);
AlexVC97 0:3bdfc1556537 297 if (_cmd(41, 0) == 0) {
AlexVC97 0:3bdfc1556537 298 cdv = 512;
AlexVC97 0:3bdfc1556537 299 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
AlexVC97 0:3bdfc1556537 300 return SDCARD_V1;
AlexVC97 0:3bdfc1556537 301 }
AlexVC97 0:3bdfc1556537 302 }
AlexVC97 0:3bdfc1556537 303
AlexVC97 0:3bdfc1556537 304 debug("Timeout waiting for v1.x card\n");
AlexVC97 0:3bdfc1556537 305 return SDCARD_FAIL;
AlexVC97 0:3bdfc1556537 306 }
AlexVC97 0:3bdfc1556537 307
AlexVC97 0:3bdfc1556537 308 int SDFileSystem::initialise_card_v2() {
AlexVC97 0:3bdfc1556537 309 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
AlexVC97 0:3bdfc1556537 310 wait_ms(50);
AlexVC97 0:3bdfc1556537 311 _cmd58();
AlexVC97 0:3bdfc1556537 312 _cmd(55, 0);
AlexVC97 0:3bdfc1556537 313 if (_cmd(41, 0x40000000) == 0) {
AlexVC97 0:3bdfc1556537 314 _cmd58();
AlexVC97 0:3bdfc1556537 315 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
AlexVC97 0:3bdfc1556537 316 cdv = 1;
AlexVC97 0:3bdfc1556537 317 return SDCARD_V2;
AlexVC97 0:3bdfc1556537 318 }
AlexVC97 0:3bdfc1556537 319 }
AlexVC97 0:3bdfc1556537 320
AlexVC97 0:3bdfc1556537 321 debug("Timeout waiting for v2.x card\n");
AlexVC97 0:3bdfc1556537 322 return SDCARD_FAIL;
AlexVC97 0:3bdfc1556537 323 }
AlexVC97 0:3bdfc1556537 324
AlexVC97 0:3bdfc1556537 325 int SDFileSystem::disk_initialize() {
AlexVC97 0:3bdfc1556537 326 _is_initialized = initialise_card();
AlexVC97 0:3bdfc1556537 327 if (_is_initialized == 0) {
AlexVC97 0:3bdfc1556537 328 debug("Fail to initialize card\n");
AlexVC97 0:3bdfc1556537 329 return 1;
AlexVC97 0:3bdfc1556537 330 }
AlexVC97 0:3bdfc1556537 331 debug_if(SD_DBG, "init card = %d\n", _is_initialized);
AlexVC97 0:3bdfc1556537 332 _sectors = _sd_sectors();
AlexVC97 0:3bdfc1556537 333
AlexVC97 0:3bdfc1556537 334 // Set block length to 512 (CMD16)
AlexVC97 0:3bdfc1556537 335 if (_cmd(16, 512) != 0) {
AlexVC97 0:3bdfc1556537 336 debug("Set 512-byte block timed out\n");
AlexVC97 0:3bdfc1556537 337 return 1;
AlexVC97 0:3bdfc1556537 338 }
AlexVC97 0:3bdfc1556537 339
AlexVC97 0:3bdfc1556537 340 // Set SCK for data transfer
AlexVC97 0:3bdfc1556537 341 _spi.frequency(_transfer_sck);
AlexVC97 0:3bdfc1556537 342 return 0;
AlexVC97 0:3bdfc1556537 343 }
AlexVC97 0:3bdfc1556537 344
AlexVC97 0:3bdfc1556537 345 int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) {
AlexVC97 0:3bdfc1556537 346 if (!_is_initialized) {
AlexVC97 0:3bdfc1556537 347 return -1;
AlexVC97 0:3bdfc1556537 348 }
AlexVC97 0:3bdfc1556537 349
AlexVC97 0:3bdfc1556537 350 for (uint32_t b = block_number; b < block_number + count; b++) {
AlexVC97 0:3bdfc1556537 351 // set write address for single block (CMD24)
AlexVC97 0:3bdfc1556537 352 if (_cmd(24, b * cdv) != 0) {
AlexVC97 0:3bdfc1556537 353 return 1;
AlexVC97 0:3bdfc1556537 354 }
AlexVC97 0:3bdfc1556537 355
AlexVC97 0:3bdfc1556537 356 // send the data block
AlexVC97 0:3bdfc1556537 357 _write(buffer, 512);
AlexVC97 0:3bdfc1556537 358 buffer += 512;
AlexVC97 0:3bdfc1556537 359 }
AlexVC97 0:3bdfc1556537 360
AlexVC97 0:3bdfc1556537 361 return 0;
AlexVC97 0:3bdfc1556537 362 }
AlexVC97 0:3bdfc1556537 363
AlexVC97 0:3bdfc1556537 364 int SDFileSystem::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) {
AlexVC97 0:3bdfc1556537 365 if (!_is_initialized) {
AlexVC97 0:3bdfc1556537 366 return -1;
AlexVC97 0:3bdfc1556537 367 }
AlexVC97 0:3bdfc1556537 368
AlexVC97 0:3bdfc1556537 369 for (uint32_t b = block_number; b < block_number + count; b++) {
AlexVC97 0:3bdfc1556537 370 // set read address for single block (CMD17)
AlexVC97 0:3bdfc1556537 371 if (_cmd(17, b * cdv) != 0) {
AlexVC97 0:3bdfc1556537 372 return 1;
AlexVC97 0:3bdfc1556537 373 }
AlexVC97 0:3bdfc1556537 374
AlexVC97 0:3bdfc1556537 375 // receive the data
AlexVC97 0:3bdfc1556537 376 _read(buffer, 512);
AlexVC97 0:3bdfc1556537 377 buffer += 512;
AlexVC97 0:3bdfc1556537 378 }
AlexVC97 0:3bdfc1556537 379
AlexVC97 0:3bdfc1556537 380 return 0;
AlexVC97 0:3bdfc1556537 381 }
AlexVC97 0:3bdfc1556537 382
AlexVC97 0:3bdfc1556537 383 int SDFileSystem::disk_status() {
AlexVC97 0:3bdfc1556537 384 // FATFileSystem::disk_status() returns 0 when initialized
AlexVC97 0:3bdfc1556537 385 if (_is_initialized) {
AlexVC97 0:3bdfc1556537 386 return 0;
AlexVC97 0:3bdfc1556537 387 } else {
AlexVC97 0:3bdfc1556537 388 return 1;
AlexVC97 0:3bdfc1556537 389 }
AlexVC97 0:3bdfc1556537 390 }
AlexVC97 0:3bdfc1556537 391
AlexVC97 0:3bdfc1556537 392 int SDFileSystem::disk_sync() { return 0; }
AlexVC97 0:3bdfc1556537 393 uint32_t SDFileSystem::disk_sectors() { return _sectors; }
AlexVC97 0:3bdfc1556537 394
AlexVC97 0:3bdfc1556537 395
AlexVC97 0:3bdfc1556537 396 // PRIVATE FUNCTIONS
AlexVC97 0:3bdfc1556537 397 int SDFileSystem::_cmd(int cmd, int arg) {
AlexVC97 0:3bdfc1556537 398 _cs = 0;
AlexVC97 0:3bdfc1556537 399
AlexVC97 0:3bdfc1556537 400 // send a command
AlexVC97 0:3bdfc1556537 401 _spi.write(0x40 | cmd);
AlexVC97 0:3bdfc1556537 402 _spi.write(arg >> 24);
AlexVC97 0:3bdfc1556537 403 _spi.write(arg >> 16);
AlexVC97 0:3bdfc1556537 404 _spi.write(arg >> 8);
AlexVC97 0:3bdfc1556537 405 _spi.write(arg >> 0);
AlexVC97 0:3bdfc1556537 406 _spi.write(0x95);
AlexVC97 0:3bdfc1556537 407
AlexVC97 0:3bdfc1556537 408 // wait for the repsonse (response[7] == 0)
AlexVC97 0:3bdfc1556537 409 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
AlexVC97 0:3bdfc1556537 410 int response = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 411 if (!(response & 0x80)) {
AlexVC97 0:3bdfc1556537 412 _cs = 1;
AlexVC97 0:3bdfc1556537 413 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 414 return response;
AlexVC97 0:3bdfc1556537 415 }
AlexVC97 0:3bdfc1556537 416 }
AlexVC97 0:3bdfc1556537 417 _cs = 1;
AlexVC97 0:3bdfc1556537 418 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 419 return -1; // timeout
AlexVC97 0:3bdfc1556537 420 }
AlexVC97 0:3bdfc1556537 421 int SDFileSystem::_cmdx(int cmd, int arg) {
AlexVC97 0:3bdfc1556537 422 _cs = 0;
AlexVC97 0:3bdfc1556537 423
AlexVC97 0:3bdfc1556537 424 // send a command
AlexVC97 0:3bdfc1556537 425 _spi.write(0x40 | cmd);
AlexVC97 0:3bdfc1556537 426 _spi.write(arg >> 24);
AlexVC97 0:3bdfc1556537 427 _spi.write(arg >> 16);
AlexVC97 0:3bdfc1556537 428 _spi.write(arg >> 8);
AlexVC97 0:3bdfc1556537 429 _spi.write(arg >> 0);
AlexVC97 0:3bdfc1556537 430 _spi.write(0x95);
AlexVC97 0:3bdfc1556537 431
AlexVC97 0:3bdfc1556537 432 // wait for the repsonse (response[7] == 0)
AlexVC97 0:3bdfc1556537 433 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
AlexVC97 0:3bdfc1556537 434 int response = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 435 if (!(response & 0x80)) {
AlexVC97 0:3bdfc1556537 436 return response;
AlexVC97 0:3bdfc1556537 437 }
AlexVC97 0:3bdfc1556537 438 }
AlexVC97 0:3bdfc1556537 439 _cs = 1;
AlexVC97 0:3bdfc1556537 440 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 441 return -1; // timeout
AlexVC97 0:3bdfc1556537 442 }
AlexVC97 0:3bdfc1556537 443
AlexVC97 0:3bdfc1556537 444
AlexVC97 0:3bdfc1556537 445 int SDFileSystem::_cmd58() {
AlexVC97 0:3bdfc1556537 446 _cs = 0;
AlexVC97 0:3bdfc1556537 447 int arg = 0;
AlexVC97 0:3bdfc1556537 448
AlexVC97 0:3bdfc1556537 449 // send a command
AlexVC97 0:3bdfc1556537 450 _spi.write(0x40 | 58);
AlexVC97 0:3bdfc1556537 451 _spi.write(arg >> 24);
AlexVC97 0:3bdfc1556537 452 _spi.write(arg >> 16);
AlexVC97 0:3bdfc1556537 453 _spi.write(arg >> 8);
AlexVC97 0:3bdfc1556537 454 _spi.write(arg >> 0);
AlexVC97 0:3bdfc1556537 455 _spi.write(0x95);
AlexVC97 0:3bdfc1556537 456
AlexVC97 0:3bdfc1556537 457 // wait for the repsonse (response[7] == 0)
AlexVC97 0:3bdfc1556537 458 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
AlexVC97 0:3bdfc1556537 459 int response = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 460 if (!(response & 0x80)) {
AlexVC97 0:3bdfc1556537 461 int ocr = _spi.write(0xFF) << 24;
AlexVC97 0:3bdfc1556537 462 ocr |= _spi.write(0xFF) << 16;
AlexVC97 0:3bdfc1556537 463 ocr |= _spi.write(0xFF) << 8;
AlexVC97 0:3bdfc1556537 464 ocr |= _spi.write(0xFF) << 0;
AlexVC97 0:3bdfc1556537 465 _cs = 1;
AlexVC97 0:3bdfc1556537 466 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 467 return response;
AlexVC97 0:3bdfc1556537 468 }
AlexVC97 0:3bdfc1556537 469 }
AlexVC97 0:3bdfc1556537 470 _cs = 1;
AlexVC97 0:3bdfc1556537 471 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 472 return -1; // timeout
AlexVC97 0:3bdfc1556537 473 }
AlexVC97 0:3bdfc1556537 474
AlexVC97 0:3bdfc1556537 475 int SDFileSystem::_cmd8() {
AlexVC97 0:3bdfc1556537 476 _cs = 0;
AlexVC97 0:3bdfc1556537 477
AlexVC97 0:3bdfc1556537 478 // send a command
AlexVC97 0:3bdfc1556537 479 _spi.write(0x40 | 8); // CMD8
AlexVC97 0:3bdfc1556537 480 _spi.write(0x00); // reserved
AlexVC97 0:3bdfc1556537 481 _spi.write(0x00); // reserved
AlexVC97 0:3bdfc1556537 482 _spi.write(0x01); // 3.3v
AlexVC97 0:3bdfc1556537 483 _spi.write(0xAA); // check pattern
AlexVC97 0:3bdfc1556537 484 _spi.write(0x87); // crc
AlexVC97 0:3bdfc1556537 485
AlexVC97 0:3bdfc1556537 486 // wait for the repsonse (response[7] == 0)
AlexVC97 0:3bdfc1556537 487 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
AlexVC97 0:3bdfc1556537 488 char response[5];
AlexVC97 0:3bdfc1556537 489 response[0] = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 490 if (!(response[0] & 0x80)) {
AlexVC97 0:3bdfc1556537 491 for (int j = 1; j < 5; j++) {
AlexVC97 0:3bdfc1556537 492 response[i] = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 493 }
AlexVC97 0:3bdfc1556537 494 _cs = 1;
AlexVC97 0:3bdfc1556537 495 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 496 return response[0];
AlexVC97 0:3bdfc1556537 497 }
AlexVC97 0:3bdfc1556537 498 }
AlexVC97 0:3bdfc1556537 499 _cs = 1;
AlexVC97 0:3bdfc1556537 500 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 501 return -1; // timeout
AlexVC97 0:3bdfc1556537 502 }
AlexVC97 0:3bdfc1556537 503
AlexVC97 0:3bdfc1556537 504 int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
AlexVC97 0:3bdfc1556537 505 _cs = 0;
AlexVC97 0:3bdfc1556537 506
AlexVC97 0:3bdfc1556537 507 // read until start byte (0xFF)
AlexVC97 0:3bdfc1556537 508 while (_spi.write(0xFF) != 0xFE);
AlexVC97 0:3bdfc1556537 509
AlexVC97 0:3bdfc1556537 510 // read data
AlexVC97 0:3bdfc1556537 511 for (uint32_t i = 0; i < length; i++) {
AlexVC97 0:3bdfc1556537 512 buffer[i] = _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 513 }
AlexVC97 0:3bdfc1556537 514 _spi.write(0xFF); // checksum
AlexVC97 0:3bdfc1556537 515 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 516
AlexVC97 0:3bdfc1556537 517 _cs = 1;
AlexVC97 0:3bdfc1556537 518 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 519 return 0;
AlexVC97 0:3bdfc1556537 520 }
AlexVC97 0:3bdfc1556537 521
AlexVC97 0:3bdfc1556537 522 int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
AlexVC97 0:3bdfc1556537 523 _cs = 0;
AlexVC97 0:3bdfc1556537 524
AlexVC97 0:3bdfc1556537 525 // indicate start of block
AlexVC97 0:3bdfc1556537 526 _spi.write(0xFE);
AlexVC97 0:3bdfc1556537 527
AlexVC97 0:3bdfc1556537 528 // write the data
AlexVC97 0:3bdfc1556537 529 for (uint32_t i = 0; i < length; i++) {
AlexVC97 0:3bdfc1556537 530 _spi.write(buffer[i]);
AlexVC97 0:3bdfc1556537 531 }
AlexVC97 0:3bdfc1556537 532
AlexVC97 0:3bdfc1556537 533 // write the checksum
AlexVC97 0:3bdfc1556537 534 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 535 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 536
AlexVC97 0:3bdfc1556537 537 // check the response token
AlexVC97 0:3bdfc1556537 538 if ((_spi.write(0xFF) & 0x1F) != 0x05) {
AlexVC97 0:3bdfc1556537 539 _cs = 1;
AlexVC97 0:3bdfc1556537 540 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 541 return 1;
AlexVC97 0:3bdfc1556537 542 }
AlexVC97 0:3bdfc1556537 543
AlexVC97 0:3bdfc1556537 544 // wait for write to finish
AlexVC97 0:3bdfc1556537 545 while (_spi.write(0xFF) == 0);
AlexVC97 0:3bdfc1556537 546
AlexVC97 0:3bdfc1556537 547 _cs = 1;
AlexVC97 0:3bdfc1556537 548 _spi.write(0xFF);
AlexVC97 0:3bdfc1556537 549 return 0;
AlexVC97 0:3bdfc1556537 550 }
AlexVC97 0:3bdfc1556537 551
AlexVC97 0:3bdfc1556537 552 static uint32_t ext_bits(unsigned char *data, int msb, int lsb) {
AlexVC97 0:3bdfc1556537 553 uint32_t bits = 0;
AlexVC97 0:3bdfc1556537 554 uint32_t size = 1 + msb - lsb;
AlexVC97 0:3bdfc1556537 555 for (uint32_t i = 0; i < size; i++) {
AlexVC97 0:3bdfc1556537 556 uint32_t position = lsb + i;
AlexVC97 0:3bdfc1556537 557 uint32_t byte = 15 - (position >> 3);
AlexVC97 0:3bdfc1556537 558 uint32_t bit = position & 0x7;
AlexVC97 0:3bdfc1556537 559 uint32_t value = (data[byte] >> bit) & 1;
AlexVC97 0:3bdfc1556537 560 bits |= value << i;
AlexVC97 0:3bdfc1556537 561 }
AlexVC97 0:3bdfc1556537 562 return bits;
AlexVC97 0:3bdfc1556537 563 }
AlexVC97 0:3bdfc1556537 564
AlexVC97 0:3bdfc1556537 565 uint32_t SDFileSystem::_sd_sectors() {
AlexVC97 0:3bdfc1556537 566 uint32_t c_size, c_size_mult, read_bl_len;
AlexVC97 0:3bdfc1556537 567 uint32_t block_len, mult, blocknr, capacity;
AlexVC97 0:3bdfc1556537 568 uint32_t hc_c_size;
AlexVC97 0:3bdfc1556537 569 uint32_t blocks;
AlexVC97 0:3bdfc1556537 570
AlexVC97 0:3bdfc1556537 571 // CMD9, Response R2 (R1 byte + 16-byte block read)
AlexVC97 0:3bdfc1556537 572 if (_cmdx(9, 0) != 0) {
AlexVC97 0:3bdfc1556537 573 debug("Didn't get a response from the disk\n");
AlexVC97 0:3bdfc1556537 574 return 0;
AlexVC97 0:3bdfc1556537 575 }
AlexVC97 0:3bdfc1556537 576
AlexVC97 0:3bdfc1556537 577 uint8_t csd[16];
AlexVC97 0:3bdfc1556537 578 if (_read(csd, 16) != 0) {
AlexVC97 0:3bdfc1556537 579 debug("Couldn't read csd response from disk\n");
AlexVC97 0:3bdfc1556537 580 return 0;
AlexVC97 0:3bdfc1556537 581 }
AlexVC97 0:3bdfc1556537 582
AlexVC97 0:3bdfc1556537 583 // csd_structure : csd[127:126]
AlexVC97 0:3bdfc1556537 584 // c_size : csd[73:62]
AlexVC97 0:3bdfc1556537 585 // c_size_mult : csd[49:47]
AlexVC97 0:3bdfc1556537 586 // read_bl_len : csd[83:80] - the *maximum* read block length
AlexVC97 0:3bdfc1556537 587
AlexVC97 0:3bdfc1556537 588 int csd_structure = ext_bits(csd, 127, 126);
AlexVC97 0:3bdfc1556537 589
AlexVC97 0:3bdfc1556537 590 switch (csd_structure) {
AlexVC97 0:3bdfc1556537 591 case 0:
AlexVC97 0:3bdfc1556537 592 cdv = 512;
AlexVC97 0:3bdfc1556537 593 c_size = ext_bits(csd, 73, 62);
AlexVC97 0:3bdfc1556537 594 c_size_mult = ext_bits(csd, 49, 47);
AlexVC97 0:3bdfc1556537 595 read_bl_len = ext_bits(csd, 83, 80);
AlexVC97 0:3bdfc1556537 596
AlexVC97 0:3bdfc1556537 597 block_len = 1 << read_bl_len;
AlexVC97 0:3bdfc1556537 598 mult = 1 << (c_size_mult + 2);
AlexVC97 0:3bdfc1556537 599 blocknr = (c_size + 1) * mult;
AlexVC97 0:3bdfc1556537 600 capacity = blocknr * block_len;
AlexVC97 0:3bdfc1556537 601 blocks = capacity / 512;
AlexVC97 0:3bdfc1556537 602 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
AlexVC97 0:3bdfc1556537 603 break;
AlexVC97 0:3bdfc1556537 604
AlexVC97 0:3bdfc1556537 605 case 1:
AlexVC97 0:3bdfc1556537 606 cdv = 1;
AlexVC97 0:3bdfc1556537 607 hc_c_size = ext_bits(csd, 63, 48);
AlexVC97 0:3bdfc1556537 608 blocks = (hc_c_size+1)*1024;
AlexVC97 0:3bdfc1556537 609 debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
AlexVC97 0:3bdfc1556537 610 break;
AlexVC97 0:3bdfc1556537 611
AlexVC97 0:3bdfc1556537 612 default:
AlexVC97 0:3bdfc1556537 613 debug("CSD struct unsupported\r\n");
AlexVC97 0:3bdfc1556537 614 return 0;
AlexVC97 0:3bdfc1556537 615 };
AlexVC97 0:3bdfc1556537 616 return blocks;
AlexVC97 0:3bdfc1556537 617 }