+++ /dev/null
-/* mbed SDFileSystem Library, for providing file access to SD cards\r
- * Copyright (c) 2008-2010, sford\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy\r
- * of this software and associated documentation files (the "Software"), to deal\r
- * in the Software without restriction, including without limitation the rights\r
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
- * copies of the Software, and to permit persons to whom the Software is\r
- * furnished to do so, subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in\r
- * all copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
- * THE SOFTWARE.\r
- */\r
-\r
-/* Introduction\r
- * ------------\r
- * SD and MMC cards support a number of interfaces, but common to them all\r
- * is one based on SPI. This is the one I'm implmenting because it means\r
- * it is much more portable even though not so performant, and we already \r
- * have the mbed SPI Interface!\r
- *\r
- * The main reference I'm using is Chapter 7, "SPI Mode" of: \r
- * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf\r
- *\r
- * SPI Startup\r
- * -----------\r
- * The SD card powers up in SD mode. The SPI interface mode is selected by\r
- * asserting CS low and sending the reset command (CMD0). The card will \r
- * respond with a (R1) response.\r
- *\r
- * CMD8 is optionally sent to determine the voltage range supported, and \r
- * indirectly determine whether it is a version 1.x SD/non-SD card or \r
- * version 2.x. I'll just ignore this for now.\r
- *\r
- * ACMD41 is repeatedly issued to initialise the card, until "in idle"\r
- * (bit 0) of the R1 response goes to '0', indicating it is initialised.\r
- *\r
- * You should also indicate whether the host supports High Capicity cards,\r
- * and check whether the card is high capacity - i'll also ignore this\r
- *\r
- * SPI Protocol\r
- * ------------\r
- * The SD SPI protocol is based on transactions made up of 8-bit words, with\r
- * the host starting every bus transaction by asserting the CS signal low. The\r
- * card always responds to commands, data blocks and errors.\r
- * \r
- * The protocol supports a CRC, but by default it is off (except for the \r
- * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)\r
- * I'll leave the CRC off I think! \r
- * \r
- * Standard capacity cards have variable data block sizes, whereas High \r
- * Capacity cards fix the size of data block to 512 bytes. I'll therefore\r
- * just always use the Standard Capacity cards with a block size of 512 bytes.\r
- * This is set with CMD16.\r
- *\r
- * You can read and write single blocks (CMD17, CMD25) or multiple blocks \r
- * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When\r
- * the card gets a read command, it responds with a response token, and then \r
- * a data token or an error.\r
- * \r
- * SPI Command Format\r
- * ------------------\r
- * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.\r
- *\r
- * +---------------+------------+------------+-----------+----------+--------------+\r
- * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |\r
- * +---------------+------------+------------+-----------+----------+--------------+\r
- *\r
- * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)\r
- *\r
- * All Application Specific commands shall be preceded with APP_CMD (CMD55).\r
- *\r
- * SPI Response Format\r
- * -------------------\r
- * The main response format (R1) is a status byte (normally zero). Key flags:\r
- * idle - 1 if the card is in an idle state/initialising \r
- * cmd - 1 if an illegal command code was detected\r
- *\r
- * +-------------------------------------------------+\r
- * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |\r
- * +-------------------------------------------------+\r
- *\r
- * R1b is the same, except it is followed by a busy signal (zeros) until\r
- * the first non-zero byte when it is ready again.\r
- *\r
- * Data Response Token\r
- * -------------------\r
- * Every data block written to the card is acknowledged by a byte \r
- * response token\r
- *\r
- * +----------------------+\r
- * | xxx | 0 | status | 1 |\r
- * +----------------------+\r
- * 010 - OK!\r
- * 101 - CRC Error\r
- * 110 - Write Error\r
- *\r
- * Single Block Read and Write\r
- * ---------------------------\r
- *\r
- * Block transfers have a byte header, followed by the data, followed\r
- * by a 16-bit CRC. In our case, the data will always be 512 bytes.\r
- * \r
- * +------+---------+---------+- - - -+---------+-----------+----------+\r
- * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] | \r
- * +------+---------+---------+- - - -+---------+-----------+----------+\r
- */\r
- \r
-#include "SDFileSystem.h"\r
-\r
-#define SD_COMMAND_TIMEOUT 5000\r
-\r
-SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* pName) :\r
- FATFileSystem(pName), _spi(mosi, miso, sclk), _cs(cs) {\r
- _cs = 1; \r
-}\r
-\r
-#define R1_IDLE_STATE (1 << 0)\r
-#define R1_ERASE_RESET (1 << 1)\r
-#define R1_ILLEGAL_COMMAND (1 << 2)\r
-#define R1_COM_CRC_ERROR (1 << 3)\r
-#define R1_ERASE_SEQUENCE_ERROR (1 << 4)\r
-#define R1_ADDRESS_ERROR (1 << 5)\r
-#define R1_PARAMETER_ERROR (1 << 6)\r
-\r
-// Types\r
-// - v1.x Standard Capacity\r
-// - v2.x Standard Capacity\r
-// - v2.x High Capacity\r
-// - Not recognised as an SD Card\r
-\r
-#define SDCARD_FAIL 0\r
-#define SDCARD_V1 1\r
-#define SDCARD_V2 2\r
-#define SDCARD_V2HC 3\r
-\r
-int SDFileSystem::initialise_card() {\r
- // Set to 100kHz for initialisation, and clock card with cs = 1\r
- _spi.frequency(100000); \r
- _cs = 1;\r
- for(int i=0; i<16; i++) { \r
- _spi.write(0xFF);\r
- }\r
-\r
- // send CMD0, should return with all zeros except IDLE STATE set (bit 0)\r
- if(_cmd(0, 0) != R1_IDLE_STATE) { \r
- fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");\r
- return SDCARD_FAIL;\r
- }\r
-\r
- // send CMD8 to determine whther it is ver 2.x\r
- int r = _cmd8();\r
- if(r == R1_IDLE_STATE) {\r
- return initialise_card_v2();\r
- } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {\r
- return initialise_card_v1();\r
- } else {\r
- fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n");\r
- return SDCARD_FAIL;\r
- }\r
-}\r
-\r
-int SDFileSystem::initialise_card_v1() {\r
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
- _cmd(55, 0); \r
- if(_cmd(41, 0) == 0) { \r
- return SDCARD_V1;\r
- }\r
- }\r
-\r
- fprintf(stderr, "Timeout waiting for v1.x card\n");\r
- return SDCARD_FAIL;\r
-}\r
-\r
-int SDFileSystem::initialise_card_v2() {\r
- \r
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
- _cmd(55, 0); \r
- if(_cmd(41, 0) == 0) { \r
- _cmd58();\r
- return SDCARD_V2;\r
- }\r
- }\r
-\r
- fprintf(stderr, "Timeout waiting for v2.x card\n");\r
- return SDCARD_FAIL;\r
-}\r
-\r
-int SDFileSystem::disk_initialize() {\r
-\r
- initialise_card();\r
-// printf("init card = %d\n", i);\r
-// printf("OK\n");\r
-\r
- _sectors = _sd_sectors();\r
-\r
- // Set block length to 512 (CMD16)\r
- if(_cmd(16, 512) != 0) {\r
- fprintf(stderr, "Set 512-byte block timed out\n");\r
- return 1;\r
- }\r
- \r
- _spi.frequency(1000000); // Set to 1MHz for data transfer\r
- return 0;\r
-}\r
-\r
-int SDFileSystem::disk_write(const char *buffer, int block_number) {\r
- // set write address for single block (CMD24)\r
- if(_cmd(24, block_number * 512) != 0) {\r
- return 1;\r
- }\r
-\r
- // send the data block\r
- _write(buffer, 512); \r
- return 0; \r
-}\r
-\r
-int SDFileSystem::disk_read(char *buffer, int block_number) { \r
- // set read address for single block (CMD17)\r
- if(_cmd(17, block_number * 512) != 0) {\r
- return 1;\r
- }\r
- \r
- // receive the data\r
- _read(buffer, 512);\r
- return 0;\r
-}\r
-\r
-int SDFileSystem::disk_status() { return 0; }\r
-int SDFileSystem::disk_sync() { return 0; }\r
-int SDFileSystem::disk_sectors() { return _sectors; }\r
-\r
-// PRIVATE FUNCTIONS\r
-\r
-int SDFileSystem::_cmd(int cmd, int arg) {\r
- _cs = 0; \r
-\r
- // send a command\r
- _spi.write(0x40 | cmd);\r
- _spi.write(arg >> 24);\r
- _spi.write(arg >> 16);\r
- _spi.write(arg >> 8);\r
- _spi.write(arg >> 0);\r
- _spi.write(0x95);\r
-\r
- // wait for the repsonse (response[7] == 0)\r
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
- int response = _spi.write(0xFF);\r
- if(!(response & 0x80)) {\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return response;\r
- }\r
- }\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return -1; // timeout\r
-}\r
-int SDFileSystem::_cmdx(int cmd, int arg) {\r
- _cs = 0; \r
-\r
- // send a command\r
- _spi.write(0x40 | cmd);\r
- _spi.write(arg >> 24);\r
- _spi.write(arg >> 16);\r
- _spi.write(arg >> 8);\r
- _spi.write(arg >> 0);\r
- _spi.write(0x95);\r
-\r
- // wait for the repsonse (response[7] == 0)\r
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
- int response = _spi.write(0xFF);\r
- if(!(response & 0x80)) {\r
- return response;\r
- }\r
- }\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return -1; // timeout\r
-}\r
-\r
-\r
-int SDFileSystem::_cmd58() {\r
- _cs = 0; \r
- int arg = 0;\r
- \r
- // send a command\r
- _spi.write(0x40 | 58);\r
- _spi.write(arg >> 24);\r
- _spi.write(arg >> 16);\r
- _spi.write(arg >> 8);\r
- _spi.write(arg >> 0);\r
- _spi.write(0x95);\r
-\r
- // wait for the repsonse (response[7] == 0)\r
- for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {\r
- int response = _spi.write(0xFF);\r
- if(!(response & 0x80)) {\r
- int ocr = _spi.write(0xFF) << 24;\r
- ocr |= _spi.write(0xFF) << 16;\r
- ocr |= _spi.write(0xFF) << 8;\r
- ocr |= _spi.write(0xFF) << 0;\r
-// printf("OCR = 0x%08X\n", ocr);\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return response;\r
- }\r
- }\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return -1; // timeout\r
-}\r
-\r
-int SDFileSystem::_cmd8() {\r
- _cs = 0; \r
- \r
- // send a command\r
- _spi.write(0x40 | 8); // CMD8\r
- _spi.write(0x00); // reserved\r
- _spi.write(0x00); // reserved\r
- _spi.write(0x01); // 3.3v\r
- _spi.write(0xAA); // check pattern\r
- _spi.write(0x87); // crc\r
-\r
- // wait for the repsonse (response[7] == 0)\r
- for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {\r
- char response[5];\r
- response[0] = _spi.write(0xFF);\r
- if(!(response[0] & 0x80)) {\r
- for(int j=1; j<5; j++) {\r
- response[i] = _spi.write(0xFF);\r
- }\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return response[0];\r
- }\r
- }\r
- _cs = 1;\r
- _spi.write(0xFF);\r
- return -1; // timeout\r
-}\r
-\r
-int SDFileSystem::_read(char *buffer, int length) {\r
- _cs = 0;\r
-\r
- // read until start byte (0xFF)\r
- while(_spi.write(0xFF) != 0xFE);\r
-\r
- // read data\r
- for(int i=0; i<length; i++) {\r
- buffer[i] = _spi.write(0xFF);\r
- }\r
- _spi.write(0xFF); // checksum\r
- _spi.write(0xFF);\r
-\r
- _cs = 1; \r
- _spi.write(0xFF);\r
- return 0;\r
-}\r
-\r
-int SDFileSystem::_write(const char *buffer, int length) {\r
- _cs = 0;\r
- \r
- // indicate start of block\r
- _spi.write(0xFE);\r
- \r
- // write the data\r
- for(int i=0; i<length; i++) {\r
- _spi.write(buffer[i]);\r
- }\r
- \r
- // write the checksum\r
- _spi.write(0xFF); \r
- _spi.write(0xFF);\r
-\r
- // check the repsonse token\r
- if((_spi.write(0xFF) & 0x1F) != 0x05) {\r
- _cs = 1;\r
- _spi.write(0xFF); \r
- return 1;\r
- }\r
-\r
- // wait for write to finish\r
- while(_spi.write(0xFF) == 0);\r
-\r
- _cs = 1; \r
- _spi.write(0xFF);\r
- return 0;\r
-}\r
-\r
-static int ext_bits(char *data, int msb, int lsb) {\r
- int bits = 0;\r
- int size = 1 + msb - lsb; \r
- for(int i=0; i<size; i++) {\r
- int position = lsb + i;\r
- int byte = 15 - (position >> 3);\r
- int bit = position & 0x7;\r
- int value = (data[byte] >> bit) & 1;\r
- bits |= value << i;\r
- }\r
- return bits;\r
-}\r
-\r
-int SDFileSystem::_sd_sectors() {\r
-\r
- // CMD9, Response R2 (R1 byte + 16-byte block read)\r
- if(_cmdx(9, 0) != 0) {\r
- fprintf(stderr, "Didn't get a response from the disk\n");\r
- return 0;\r
- }\r
- \r
- char csd[16]; \r
- if(_read(csd, 16) != 0) {\r
- fprintf(stderr, "Couldn't read csd response from disk\n");\r
- return 0;\r
- }\r
-\r
- // csd_structure : csd[127:126]\r
- // c_size : csd[73:62]\r
- // c_size_mult : csd[49:47]\r
- // read_bl_len : csd[83:80] - the *maximum* read block length\r
-\r
- int csd_structure = ext_bits(csd, 127, 126);\r
- int c_size = ext_bits(csd, 73, 62);\r
- int c_size_mult = ext_bits(csd, 49, 47);\r
- int read_bl_len = ext_bits(csd, 83, 80);\r
-\r
-// printf("CSD_STRUCT = %d\n", csd_structure);\r
- \r
- if(csd_structure != 0) {\r
- fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");\r
- return 0;\r
- }\r
- \r
- // memory capacity = BLOCKNR * BLOCK_LEN\r
- // where\r
- // BLOCKNR = (C_SIZE+1) * MULT\r
- // MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)\r
- // BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12) \r
- \r
- int block_len = 1 << read_bl_len;\r
- int mult = 1 << (c_size_mult + 2);\r
- int blocknr = (c_size + 1) * mult;\r
- int capacity = blocknr * block_len;\r
- \r
- int blocks = capacity / 512;\r
- \r
- return blocks;\r
-}\r