SD File System
Fork of SDFileSystem by
SDFileSystem.cpp@0:2a6d8a096edc, 2014-07-29 (annotated)
- Committer:
- neilt6
- Date:
- Tue Jul 29 20:12:23 2014 +0000
- Revision:
- 0:2a6d8a096edc
- Child:
- 1:25f4ba436b81
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:2a6d8a096edc | 1 | /* SD/MMC File System Library |
neilt6 | 0:2a6d8a096edc | 2 | * Copyright (c) 2014 Neil Thiessen |
neilt6 | 0:2a6d8a096edc | 3 | * |
neilt6 | 0:2a6d8a096edc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 0:2a6d8a096edc | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 0:2a6d8a096edc | 6 | * You may obtain a copy of the License at |
neilt6 | 0:2a6d8a096edc | 7 | * |
neilt6 | 0:2a6d8a096edc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:2a6d8a096edc | 9 | * |
neilt6 | 0:2a6d8a096edc | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 0:2a6d8a096edc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 0:2a6d8a096edc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 0:2a6d8a096edc | 13 | * See the License for the specific language governing permissions and |
neilt6 | 0:2a6d8a096edc | 14 | * limitations under the License. |
neilt6 | 0:2a6d8a096edc | 15 | */ |
neilt6 | 0:2a6d8a096edc | 16 | |
neilt6 | 0:2a6d8a096edc | 17 | #include "SDFileSystem.h" |
neilt6 | 0:2a6d8a096edc | 18 | #include "diskio.h" |
neilt6 | 0:2a6d8a096edc | 19 | #include "CRC7.h" |
neilt6 | 0:2a6d8a096edc | 20 | #include "CRC16.h" |
neilt6 | 0:2a6d8a096edc | 21 | |
neilt6 | 0:2a6d8a096edc | 22 | SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName cd, const char* name, int hz) : FATFileSystem(name), m_SPI(mosi, miso, sclk), m_CS(cs, 1), m_CD(cd) |
neilt6 | 0:2a6d8a096edc | 23 | { |
neilt6 | 0:2a6d8a096edc | 24 | //Initialize the member variables |
neilt6 | 0:2a6d8a096edc | 25 | m_SpiFreq = hz; |
neilt6 | 0:2a6d8a096edc | 26 | m_Status = STA_NOINIT; |
neilt6 | 0:2a6d8a096edc | 27 | m_CardType = CARD_NONE; |
neilt6 | 0:2a6d8a096edc | 28 | |
neilt6 | 0:2a6d8a096edc | 29 | //Configure the SPI bus |
neilt6 | 0:2a6d8a096edc | 30 | m_SPI.format(8, 0); |
neilt6 | 0:2a6d8a096edc | 31 | |
neilt6 | 0:2a6d8a096edc | 32 | //Configure the card detect pin |
neilt6 | 0:2a6d8a096edc | 33 | m_CD.mode(PullUp); |
neilt6 | 0:2a6d8a096edc | 34 | m_CD.fall(this, &SDFileSystem::checkSocket); |
neilt6 | 0:2a6d8a096edc | 35 | } |
neilt6 | 0:2a6d8a096edc | 36 | |
neilt6 | 0:2a6d8a096edc | 37 | SDFileSystem::CardType SDFileSystem::card_type() |
neilt6 | 0:2a6d8a096edc | 38 | { |
neilt6 | 0:2a6d8a096edc | 39 | //Check the card socket |
neilt6 | 0:2a6d8a096edc | 40 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 41 | |
neilt6 | 0:2a6d8a096edc | 42 | //If a card is present but not initialized, initialize it |
neilt6 | 0:2a6d8a096edc | 43 | if (!(m_Status & STA_NODISK) && (m_Status & STA_NOINIT)) |
neilt6 | 0:2a6d8a096edc | 44 | disk_initialize(); |
neilt6 | 0:2a6d8a096edc | 45 | |
neilt6 | 0:2a6d8a096edc | 46 | //Return the card type |
neilt6 | 0:2a6d8a096edc | 47 | return m_CardType; |
neilt6 | 0:2a6d8a096edc | 48 | } |
neilt6 | 0:2a6d8a096edc | 49 | |
neilt6 | 0:2a6d8a096edc | 50 | int SDFileSystem::disk_initialize() |
neilt6 | 0:2a6d8a096edc | 51 | { |
neilt6 | 0:2a6d8a096edc | 52 | char resp; |
neilt6 | 0:2a6d8a096edc | 53 | |
neilt6 | 0:2a6d8a096edc | 54 | //Make sure there's a card in the socket before proceeding |
neilt6 | 0:2a6d8a096edc | 55 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 56 | if (m_Status & STA_NODISK) |
neilt6 | 0:2a6d8a096edc | 57 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 58 | |
neilt6 | 0:2a6d8a096edc | 59 | //Make sure we're not already initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 60 | if (!(m_Status & STA_NOINIT)) |
neilt6 | 0:2a6d8a096edc | 61 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 62 | |
neilt6 | 0:2a6d8a096edc | 63 | //Set the SPI frequency to 100kHz for initialization |
neilt6 | 0:2a6d8a096edc | 64 | m_SPI.frequency(100000); |
neilt6 | 0:2a6d8a096edc | 65 | |
neilt6 | 0:2a6d8a096edc | 66 | //Send 80 dummy clocks with /CS and DI held high |
neilt6 | 0:2a6d8a096edc | 67 | m_CS = 1; |
neilt6 | 0:2a6d8a096edc | 68 | for (int i = 0; i < 10; i++) |
neilt6 | 0:2a6d8a096edc | 69 | m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 70 | |
neilt6 | 0:2a6d8a096edc | 71 | //Write CMD0(0), and check for a valid response |
neilt6 | 0:2a6d8a096edc | 72 | resp = writeCommand(CMD0, 0); |
neilt6 | 0:2a6d8a096edc | 73 | if (resp != 0x01) { |
neilt6 | 0:2a6d8a096edc | 74 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 75 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 76 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 77 | } |
neilt6 | 0:2a6d8a096edc | 78 | |
neilt6 | 0:2a6d8a096edc | 79 | //Write CMD8(0x000001AA) to see if this is an SDCv2 card |
neilt6 | 0:2a6d8a096edc | 80 | resp = writeCommand(CMD8, 0x000001AA); |
neilt6 | 0:2a6d8a096edc | 81 | if (resp == 0x01) { |
neilt6 | 0:2a6d8a096edc | 82 | //This is an SDCv2 card, get the 32-bit return value and verify the voltage range/check pattern |
neilt6 | 0:2a6d8a096edc | 83 | if ((readReturn() & 0xFFF) != 0x1AA) { |
neilt6 | 0:2a6d8a096edc | 84 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 85 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 86 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 87 | } |
neilt6 | 0:2a6d8a096edc | 88 | |
neilt6 | 0:2a6d8a096edc | 89 | //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V |
neilt6 | 0:2a6d8a096edc | 90 | resp = writeCommand(CMD58, 0); |
neilt6 | 0:2a6d8a096edc | 91 | if (resp != 0x01 || !(readReturn() & (1 << 20))) { |
neilt6 | 0:2a6d8a096edc | 92 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 93 | deselect(); |
neilt6 | 0:2a6d8a096edc | 94 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 95 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 96 | } |
neilt6 | 0:2a6d8a096edc | 97 | |
neilt6 | 0:2a6d8a096edc | 98 | //Send ACMD41(0x40100000) repeatedly for up to 1 second to initialize the card |
neilt6 | 0:2a6d8a096edc | 99 | for (int i = 0; i < 1000; i++) { |
neilt6 | 0:2a6d8a096edc | 100 | resp = writeCommand(ACMD41, 0x40100000); |
neilt6 | 0:2a6d8a096edc | 101 | if (resp != 0x01) |
neilt6 | 0:2a6d8a096edc | 102 | break; |
neilt6 | 0:2a6d8a096edc | 103 | wait_ms(1); |
neilt6 | 0:2a6d8a096edc | 104 | } |
neilt6 | 0:2a6d8a096edc | 105 | |
neilt6 | 0:2a6d8a096edc | 106 | //Check if the card initialized |
neilt6 | 0:2a6d8a096edc | 107 | if (resp != 0x00) { |
neilt6 | 0:2a6d8a096edc | 108 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 109 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 110 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 111 | } |
neilt6 | 0:2a6d8a096edc | 112 | |
neilt6 | 0:2a6d8a096edc | 113 | //Send CMD58(0) to read the OCR |
neilt6 | 0:2a6d8a096edc | 114 | resp = writeCommand(CMD58, 0); |
neilt6 | 0:2a6d8a096edc | 115 | if (resp == 0x00) { |
neilt6 | 0:2a6d8a096edc | 116 | //Check the CCS bit to determine if this is a high capacity card |
neilt6 | 0:2a6d8a096edc | 117 | if (readReturn() & 0x40000000) |
neilt6 | 0:2a6d8a096edc | 118 | m_CardType = CARD_SDHC; |
neilt6 | 0:2a6d8a096edc | 119 | else |
neilt6 | 0:2a6d8a096edc | 120 | m_CardType = CARD_SD; |
neilt6 | 0:2a6d8a096edc | 121 | } else { |
neilt6 | 0:2a6d8a096edc | 122 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 123 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 124 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 125 | } |
neilt6 | 0:2a6d8a096edc | 126 | } else { |
neilt6 | 0:2a6d8a096edc | 127 | //Didn't respond or illegal command, this is either an SDCv1 or MMC card |
neilt6 | 0:2a6d8a096edc | 128 | deselect(); |
neilt6 | 0:2a6d8a096edc | 129 | |
neilt6 | 0:2a6d8a096edc | 130 | //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V |
neilt6 | 0:2a6d8a096edc | 131 | resp = writeCommand(CMD58, 0); |
neilt6 | 0:2a6d8a096edc | 132 | if (resp != 0x01 || !(readReturn() & (1 << 20))) { |
neilt6 | 0:2a6d8a096edc | 133 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 134 | deselect(); |
neilt6 | 0:2a6d8a096edc | 135 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 136 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 137 | } |
neilt6 | 0:2a6d8a096edc | 138 | |
neilt6 | 0:2a6d8a096edc | 139 | //Try to initialize the card using ACMD41(0x00100000) for 1 second |
neilt6 | 0:2a6d8a096edc | 140 | for (int i = 0; i < 1000; i++) { |
neilt6 | 0:2a6d8a096edc | 141 | resp = writeCommand(ACMD41, 0x00100000); |
neilt6 | 0:2a6d8a096edc | 142 | if (resp != 0x01) |
neilt6 | 0:2a6d8a096edc | 143 | break; |
neilt6 | 0:2a6d8a096edc | 144 | wait_ms(1); |
neilt6 | 0:2a6d8a096edc | 145 | } |
neilt6 | 0:2a6d8a096edc | 146 | |
neilt6 | 0:2a6d8a096edc | 147 | //Check if the card initialized |
neilt6 | 0:2a6d8a096edc | 148 | if (resp == 0x00) { |
neilt6 | 0:2a6d8a096edc | 149 | //This is an SDCv1 standard capacity card |
neilt6 | 0:2a6d8a096edc | 150 | m_CardType = CARD_SD; |
neilt6 | 0:2a6d8a096edc | 151 | } else { |
neilt6 | 0:2a6d8a096edc | 152 | //Try to initialize the card using CMD1(0x00100000) for 1 second |
neilt6 | 0:2a6d8a096edc | 153 | for (int i = 0; i < 1000; i++) { |
neilt6 | 0:2a6d8a096edc | 154 | resp = writeCommand(CMD1, 0x00100000); |
neilt6 | 0:2a6d8a096edc | 155 | if (resp != 0x01) |
neilt6 | 0:2a6d8a096edc | 156 | break; |
neilt6 | 0:2a6d8a096edc | 157 | wait_ms(1); |
neilt6 | 0:2a6d8a096edc | 158 | } |
neilt6 | 0:2a6d8a096edc | 159 | |
neilt6 | 0:2a6d8a096edc | 160 | //Check if the card initialized |
neilt6 | 0:2a6d8a096edc | 161 | if (resp == 0x00) { |
neilt6 | 0:2a6d8a096edc | 162 | //This is an MMCv3 card |
neilt6 | 0:2a6d8a096edc | 163 | m_CardType = CARD_MMC; |
neilt6 | 0:2a6d8a096edc | 164 | } else { |
neilt6 | 0:2a6d8a096edc | 165 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 166 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 167 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 168 | } |
neilt6 | 0:2a6d8a096edc | 169 | } |
neilt6 | 0:2a6d8a096edc | 170 | } |
neilt6 | 0:2a6d8a096edc | 171 | |
neilt6 | 0:2a6d8a096edc | 172 | //Send CMD59(0x00000001) to re-enable CRC |
neilt6 | 0:2a6d8a096edc | 173 | resp = writeCommand(CMD59, 0x00000001); |
neilt6 | 0:2a6d8a096edc | 174 | if (resp != 0x00) { |
neilt6 | 0:2a6d8a096edc | 175 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 176 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 177 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 178 | } |
neilt6 | 0:2a6d8a096edc | 179 | |
neilt6 | 0:2a6d8a096edc | 180 | //Send CMD16(0x00000200) to force the block size to 512B if necessary |
neilt6 | 0:2a6d8a096edc | 181 | if (m_CardType != CARD_SDHC) { |
neilt6 | 0:2a6d8a096edc | 182 | resp = writeCommand(CMD16, 0x00000200); |
neilt6 | 0:2a6d8a096edc | 183 | if (resp != 0x00) { |
neilt6 | 0:2a6d8a096edc | 184 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 185 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 186 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 187 | } |
neilt6 | 0:2a6d8a096edc | 188 | } |
neilt6 | 0:2a6d8a096edc | 189 | |
neilt6 | 0:2a6d8a096edc | 190 | //The card is now initialized |
neilt6 | 0:2a6d8a096edc | 191 | m_Status &= ~STA_NOINIT; |
neilt6 | 0:2a6d8a096edc | 192 | |
neilt6 | 0:2a6d8a096edc | 193 | //Increase the SPI frequency to full speed (limited to 20MHz for MMC, or 25MHz for SDC) |
neilt6 | 0:2a6d8a096edc | 194 | if (m_CardType == CARD_MMC && m_SpiFreq > 20000000) |
neilt6 | 0:2a6d8a096edc | 195 | m_SPI.frequency(20000000); |
neilt6 | 0:2a6d8a096edc | 196 | else if (m_SpiFreq > 25000000) |
neilt6 | 0:2a6d8a096edc | 197 | m_SPI.frequency(25000000); |
neilt6 | 0:2a6d8a096edc | 198 | else |
neilt6 | 0:2a6d8a096edc | 199 | m_SPI.frequency(m_SpiFreq); |
neilt6 | 0:2a6d8a096edc | 200 | |
neilt6 | 0:2a6d8a096edc | 201 | //Return the device status |
neilt6 | 0:2a6d8a096edc | 202 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 203 | } |
neilt6 | 0:2a6d8a096edc | 204 | |
neilt6 | 0:2a6d8a096edc | 205 | int SDFileSystem::disk_status() |
neilt6 | 0:2a6d8a096edc | 206 | { |
neilt6 | 0:2a6d8a096edc | 207 | //Check if there's a card in the socket |
neilt6 | 0:2a6d8a096edc | 208 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 209 | |
neilt6 | 0:2a6d8a096edc | 210 | //Return the device status |
neilt6 | 0:2a6d8a096edc | 211 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 212 | } |
neilt6 | 0:2a6d8a096edc | 213 | |
neilt6 | 0:2a6d8a096edc | 214 | int SDFileSystem::disk_read(uint8_t* buffer, uint64_t sector) |
neilt6 | 0:2a6d8a096edc | 215 | { |
neilt6 | 0:2a6d8a096edc | 216 | //Make sure the device is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 217 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 218 | return RES_NOTRDY; |
neilt6 | 0:2a6d8a096edc | 219 | |
neilt6 | 0:2a6d8a096edc | 220 | //Convert from LBA to a byte address for standard capacity cards |
neilt6 | 0:2a6d8a096edc | 221 | if (m_CardType != CARD_SDHC) |
neilt6 | 0:2a6d8a096edc | 222 | sector *= 512; |
neilt6 | 0:2a6d8a096edc | 223 | |
neilt6 | 0:2a6d8a096edc | 224 | //Try to read the block up to 3 times |
neilt6 | 0:2a6d8a096edc | 225 | for (int i = 0; i < 3; i++) { |
neilt6 | 0:2a6d8a096edc | 226 | //Send CMD17(sector) to read a single block |
neilt6 | 0:2a6d8a096edc | 227 | char resp = writeCommand(CMD17, sector); |
neilt6 | 0:2a6d8a096edc | 228 | if (resp == 0x00) { |
neilt6 | 0:2a6d8a096edc | 229 | //Try to read the sector, and return if successful |
neilt6 | 0:2a6d8a096edc | 230 | if (readData((char*)buffer, 512)) |
neilt6 | 0:2a6d8a096edc | 231 | return RES_OK; |
neilt6 | 0:2a6d8a096edc | 232 | } else { |
neilt6 | 0:2a6d8a096edc | 233 | //The command failed |
neilt6 | 0:2a6d8a096edc | 234 | deselect(); |
neilt6 | 0:2a6d8a096edc | 235 | return RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 236 | } |
neilt6 | 0:2a6d8a096edc | 237 | } |
neilt6 | 0:2a6d8a096edc | 238 | |
neilt6 | 0:2a6d8a096edc | 239 | //The read operation failed 3 times (CRC most likely) |
neilt6 | 0:2a6d8a096edc | 240 | return RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 241 | } |
neilt6 | 0:2a6d8a096edc | 242 | |
neilt6 | 0:2a6d8a096edc | 243 | int SDFileSystem::disk_write(const uint8_t* buffer, uint64_t sector) |
neilt6 | 0:2a6d8a096edc | 244 | { |
neilt6 | 0:2a6d8a096edc | 245 | //Make sure the device is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 246 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 247 | return RES_NOTRDY; |
neilt6 | 0:2a6d8a096edc | 248 | |
neilt6 | 0:2a6d8a096edc | 249 | //Make sure the device isn't write protected before proceeding |
neilt6 | 0:2a6d8a096edc | 250 | if (m_Status & STA_PROTECT) |
neilt6 | 0:2a6d8a096edc | 251 | return RES_WRPRT; |
neilt6 | 0:2a6d8a096edc | 252 | |
neilt6 | 0:2a6d8a096edc | 253 | //Convert from LBA to a byte address for older cards |
neilt6 | 0:2a6d8a096edc | 254 | if (m_CardType != CARD_SDHC) |
neilt6 | 0:2a6d8a096edc | 255 | sector *= 512; |
neilt6 | 0:2a6d8a096edc | 256 | |
neilt6 | 0:2a6d8a096edc | 257 | //Try to write the block up to 3 times |
neilt6 | 0:2a6d8a096edc | 258 | for (int i = 0; i < 3; i++) { |
neilt6 | 0:2a6d8a096edc | 259 | //Send CMD24(sector) to write a single block |
neilt6 | 0:2a6d8a096edc | 260 | if (writeCommand(CMD24, sector) == 0x00) { |
neilt6 | 0:2a6d8a096edc | 261 | //Wait for up to 500ms for the card to become ready |
neilt6 | 0:2a6d8a096edc | 262 | if (!waitReady(500)) { |
neilt6 | 0:2a6d8a096edc | 263 | //We timed out |
neilt6 | 0:2a6d8a096edc | 264 | deselect(); |
neilt6 | 0:2a6d8a096edc | 265 | continue; |
neilt6 | 0:2a6d8a096edc | 266 | } |
neilt6 | 0:2a6d8a096edc | 267 | |
neilt6 | 0:2a6d8a096edc | 268 | //Send the write data token |
neilt6 | 0:2a6d8a096edc | 269 | m_SPI.write(0xFE); |
neilt6 | 0:2a6d8a096edc | 270 | |
neilt6 | 0:2a6d8a096edc | 271 | //Write the data block from the buffer |
neilt6 | 0:2a6d8a096edc | 272 | for (int b = 0; b < 512; b++) |
neilt6 | 0:2a6d8a096edc | 273 | m_SPI.write(buffer[b]); |
neilt6 | 0:2a6d8a096edc | 274 | |
neilt6 | 0:2a6d8a096edc | 275 | //Calculate the CRC16 checksum for the data block and send it |
neilt6 | 0:2a6d8a096edc | 276 | unsigned short crc = CRC16((char*)buffer, 512); |
neilt6 | 0:2a6d8a096edc | 277 | m_SPI.write(crc >> 8); |
neilt6 | 0:2a6d8a096edc | 278 | m_SPI.write(crc); |
neilt6 | 0:2a6d8a096edc | 279 | |
neilt6 | 0:2a6d8a096edc | 280 | //Receive the data response, and deselect the card |
neilt6 | 0:2a6d8a096edc | 281 | char resp = m_SPI.write(0xFF) & 0x1F; |
neilt6 | 0:2a6d8a096edc | 282 | deselect(); |
neilt6 | 0:2a6d8a096edc | 283 | |
neilt6 | 0:2a6d8a096edc | 284 | //Check the response |
neilt6 | 0:2a6d8a096edc | 285 | if (resp == 0x05) |
neilt6 | 0:2a6d8a096edc | 286 | return RES_OK; |
neilt6 | 0:2a6d8a096edc | 287 | else if (resp == 0x0D) |
neilt6 | 0:2a6d8a096edc | 288 | return RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 289 | } else { |
neilt6 | 0:2a6d8a096edc | 290 | //The command failed |
neilt6 | 0:2a6d8a096edc | 291 | deselect(); |
neilt6 | 0:2a6d8a096edc | 292 | return RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 293 | } |
neilt6 | 0:2a6d8a096edc | 294 | } |
neilt6 | 0:2a6d8a096edc | 295 | |
neilt6 | 0:2a6d8a096edc | 296 | //The operation either timed out 3 times, failed the CRC check 3 times, or experienced a write error |
neilt6 | 0:2a6d8a096edc | 297 | return RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 298 | } |
neilt6 | 0:2a6d8a096edc | 299 | |
neilt6 | 0:2a6d8a096edc | 300 | int SDFileSystem::disk_sync() |
neilt6 | 0:2a6d8a096edc | 301 | { |
neilt6 | 0:2a6d8a096edc | 302 | //Select the card so we're forced to wait for the end of any internal write processes |
neilt6 | 0:2a6d8a096edc | 303 | bool ret = select(); |
neilt6 | 0:2a6d8a096edc | 304 | deselect(); |
neilt6 | 0:2a6d8a096edc | 305 | |
neilt6 | 0:2a6d8a096edc | 306 | //Return success/failure |
neilt6 | 0:2a6d8a096edc | 307 | return (ret) ? RES_OK : RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 308 | } |
neilt6 | 0:2a6d8a096edc | 309 | |
neilt6 | 0:2a6d8a096edc | 310 | uint64_t SDFileSystem::disk_sectors() |
neilt6 | 0:2a6d8a096edc | 311 | { |
neilt6 | 0:2a6d8a096edc | 312 | //Make sure the device is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 313 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 314 | return 0; |
neilt6 | 0:2a6d8a096edc | 315 | |
neilt6 | 0:2a6d8a096edc | 316 | //Try to read the CSD register up to 3 times |
neilt6 | 0:2a6d8a096edc | 317 | for (int i = 0; i < 3; i++) { |
neilt6 | 0:2a6d8a096edc | 318 | //Send CMD9(0) to read the CSD register |
neilt6 | 0:2a6d8a096edc | 319 | if (writeCommand(CMD9, 0) == 0x00) { |
neilt6 | 0:2a6d8a096edc | 320 | //Receive the 16B CSD data |
neilt6 | 0:2a6d8a096edc | 321 | char csd[16]; |
neilt6 | 0:2a6d8a096edc | 322 | if (readData(csd, 16)) { |
neilt6 | 0:2a6d8a096edc | 323 | //Calculate the sector count based on the card type |
neilt6 | 0:2a6d8a096edc | 324 | if ((csd[0] >> 6) == 0x01) { |
neilt6 | 0:2a6d8a096edc | 325 | //Calculate the sector count a high capacity card |
neilt6 | 0:2a6d8a096edc | 326 | uint64_t sectors = (((csd[7] & 0x3F) << 16) | (csd[8] << 8) | csd[9]) + 1; |
neilt6 | 0:2a6d8a096edc | 327 | return sectors << 10; |
neilt6 | 0:2a6d8a096edc | 328 | } else { |
neilt6 | 0:2a6d8a096edc | 329 | //Calculate the sector count standard capacity card |
neilt6 | 0:2a6d8a096edc | 330 | uint64_t sectors = (((csd[6] & 0x03) << 10) | (csd[7] << 2) | ((csd[8] & 0xC0) >> 6)) + 1; |
neilt6 | 0:2a6d8a096edc | 331 | sectors <<= ((((csd[9] & 0x03) << 1) | ((csd[10] & 0x80) >> 7)) + 2); |
neilt6 | 0:2a6d8a096edc | 332 | sectors <<= (csd[5] & 0x0F); |
neilt6 | 0:2a6d8a096edc | 333 | return sectors >> 9; |
neilt6 | 0:2a6d8a096edc | 334 | } |
neilt6 | 0:2a6d8a096edc | 335 | } |
neilt6 | 0:2a6d8a096edc | 336 | } else { |
neilt6 | 0:2a6d8a096edc | 337 | //The command failed |
neilt6 | 0:2a6d8a096edc | 338 | deselect(); |
neilt6 | 0:2a6d8a096edc | 339 | return 0; |
neilt6 | 0:2a6d8a096edc | 340 | } |
neilt6 | 0:2a6d8a096edc | 341 | } |
neilt6 | 0:2a6d8a096edc | 342 | |
neilt6 | 0:2a6d8a096edc | 343 | //The read operation failed 3 times (CRC most likely) |
neilt6 | 0:2a6d8a096edc | 344 | return 0; |
neilt6 | 0:2a6d8a096edc | 345 | } |
neilt6 | 0:2a6d8a096edc | 346 | |
neilt6 | 0:2a6d8a096edc | 347 | void SDFileSystem::checkSocket() |
neilt6 | 0:2a6d8a096edc | 348 | { |
neilt6 | 0:2a6d8a096edc | 349 | //Check if a card is in the socket |
neilt6 | 0:2a6d8a096edc | 350 | if (m_CD) { |
neilt6 | 0:2a6d8a096edc | 351 | //The socket is occupied, clear the STA_NODISK flag |
neilt6 | 0:2a6d8a096edc | 352 | m_Status &= ~STA_NODISK; |
neilt6 | 0:2a6d8a096edc | 353 | } else { |
neilt6 | 0:2a6d8a096edc | 354 | //The socket is empty |
neilt6 | 0:2a6d8a096edc | 355 | m_Status |= (STA_NODISK | STA_NOINIT); |
neilt6 | 0:2a6d8a096edc | 356 | m_CardType = CARD_NONE; |
neilt6 | 0:2a6d8a096edc | 357 | } |
neilt6 | 0:2a6d8a096edc | 358 | } |
neilt6 | 0:2a6d8a096edc | 359 | |
neilt6 | 0:2a6d8a096edc | 360 | inline bool SDFileSystem::waitReady(int timeout) |
neilt6 | 0:2a6d8a096edc | 361 | { |
neilt6 | 0:2a6d8a096edc | 362 | //Wait for the specified timeout for the card to become ready |
neilt6 | 0:2a6d8a096edc | 363 | for (int i = 0; i < timeout; i++) { |
neilt6 | 0:2a6d8a096edc | 364 | if (m_SPI.write(0xFF) == 0xFF) |
neilt6 | 0:2a6d8a096edc | 365 | return true; |
neilt6 | 0:2a6d8a096edc | 366 | wait_ms(1); |
neilt6 | 0:2a6d8a096edc | 367 | } |
neilt6 | 0:2a6d8a096edc | 368 | |
neilt6 | 0:2a6d8a096edc | 369 | //We timed out |
neilt6 | 0:2a6d8a096edc | 370 | return false; |
neilt6 | 0:2a6d8a096edc | 371 | } |
neilt6 | 0:2a6d8a096edc | 372 | |
neilt6 | 0:2a6d8a096edc | 373 | inline bool SDFileSystem::select() |
neilt6 | 0:2a6d8a096edc | 374 | { |
neilt6 | 0:2a6d8a096edc | 375 | //Pull /CS low |
neilt6 | 0:2a6d8a096edc | 376 | m_CS = 0; |
neilt6 | 0:2a6d8a096edc | 377 | |
neilt6 | 0:2a6d8a096edc | 378 | //Send a dummy clock to enable DO |
neilt6 | 0:2a6d8a096edc | 379 | m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 380 | |
neilt6 | 0:2a6d8a096edc | 381 | //Wait for up to 500ms for the card to become ready |
neilt6 | 0:2a6d8a096edc | 382 | if (waitReady(500)) |
neilt6 | 0:2a6d8a096edc | 383 | return true; |
neilt6 | 0:2a6d8a096edc | 384 | |
neilt6 | 0:2a6d8a096edc | 385 | //We timed out, deselect and return false |
neilt6 | 0:2a6d8a096edc | 386 | deselect(); |
neilt6 | 0:2a6d8a096edc | 387 | return false; |
neilt6 | 0:2a6d8a096edc | 388 | } |
neilt6 | 0:2a6d8a096edc | 389 | |
neilt6 | 0:2a6d8a096edc | 390 | inline void SDFileSystem::deselect() |
neilt6 | 0:2a6d8a096edc | 391 | { |
neilt6 | 0:2a6d8a096edc | 392 | //Pull /CS high |
neilt6 | 0:2a6d8a096edc | 393 | m_CS = 1; |
neilt6 | 0:2a6d8a096edc | 394 | |
neilt6 | 0:2a6d8a096edc | 395 | //Send a dummy byte to release DO |
neilt6 | 0:2a6d8a096edc | 396 | m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 397 | } |
neilt6 | 0:2a6d8a096edc | 398 | |
neilt6 | 0:2a6d8a096edc | 399 | char SDFileSystem::writeCommand(char cmd, unsigned int arg) |
neilt6 | 0:2a6d8a096edc | 400 | { |
neilt6 | 0:2a6d8a096edc | 401 | char resp; |
neilt6 | 0:2a6d8a096edc | 402 | |
neilt6 | 0:2a6d8a096edc | 403 | //Try to send the command up to 3 times |
neilt6 | 0:2a6d8a096edc | 404 | for (int i = 0; i < 3; i++) { |
neilt6 | 0:2a6d8a096edc | 405 | //Send a CMD55 prior to an ACMD |
neilt6 | 0:2a6d8a096edc | 406 | if (cmd == ACMD41) { |
neilt6 | 0:2a6d8a096edc | 407 | resp = writeCommand(CMD55, 0); |
neilt6 | 0:2a6d8a096edc | 408 | if (resp > 0x01) |
neilt6 | 0:2a6d8a096edc | 409 | return resp; |
neilt6 | 0:2a6d8a096edc | 410 | } |
neilt6 | 0:2a6d8a096edc | 411 | |
neilt6 | 0:2a6d8a096edc | 412 | //Select the card and wait for ready |
neilt6 | 0:2a6d8a096edc | 413 | if (!select()) |
neilt6 | 0:2a6d8a096edc | 414 | return 0xFF; |
neilt6 | 0:2a6d8a096edc | 415 | |
neilt6 | 0:2a6d8a096edc | 416 | //Prepare the command packet |
neilt6 | 0:2a6d8a096edc | 417 | char cmdPacket[6]; |
neilt6 | 0:2a6d8a096edc | 418 | cmdPacket[0] = 0x40 | cmd; |
neilt6 | 0:2a6d8a096edc | 419 | cmdPacket[1] = arg >> 24; |
neilt6 | 0:2a6d8a096edc | 420 | cmdPacket[2] = arg >> 16; |
neilt6 | 0:2a6d8a096edc | 421 | cmdPacket[3] = arg >> 8; |
neilt6 | 0:2a6d8a096edc | 422 | cmdPacket[4] = arg; |
neilt6 | 0:2a6d8a096edc | 423 | cmdPacket[5] = (CRC7(cmdPacket, 5) << 1) | 0x01; |
neilt6 | 0:2a6d8a096edc | 424 | |
neilt6 | 0:2a6d8a096edc | 425 | //Send the command packet |
neilt6 | 0:2a6d8a096edc | 426 | for (int b = 0; b < 6; b++) |
neilt6 | 0:2a6d8a096edc | 427 | m_SPI.write(cmdPacket[b]); |
neilt6 | 0:2a6d8a096edc | 428 | |
neilt6 | 0:2a6d8a096edc | 429 | //Allow up to 10 bytes of delay for the command response |
neilt6 | 0:2a6d8a096edc | 430 | for (int b = 0; b < 10; b++) { |
neilt6 | 0:2a6d8a096edc | 431 | resp = m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 432 | if (!(resp & 0x80)) |
neilt6 | 0:2a6d8a096edc | 433 | break; |
neilt6 | 0:2a6d8a096edc | 434 | } |
neilt6 | 0:2a6d8a096edc | 435 | |
neilt6 | 0:2a6d8a096edc | 436 | //Deselect the card unless there's more data to read/write |
neilt6 | 0:2a6d8a096edc | 437 | if (resp == 0xFF || (resp & (1 << 3)) || !(cmd == CMD8 || cmd == CMD9 || cmd == CMD17 || cmd == CMD24 || cmd == CMD55 || cmd == CMD58)) |
neilt6 | 0:2a6d8a096edc | 438 | deselect(); |
neilt6 | 0:2a6d8a096edc | 439 | |
neilt6 | 0:2a6d8a096edc | 440 | //Return the response if there were no CRC errors |
neilt6 | 0:2a6d8a096edc | 441 | if (resp == 0xFF || !(resp & (1 << 3))) |
neilt6 | 0:2a6d8a096edc | 442 | return resp; |
neilt6 | 0:2a6d8a096edc | 443 | } |
neilt6 | 0:2a6d8a096edc | 444 | |
neilt6 | 0:2a6d8a096edc | 445 | //The command failed 3 times due to CRC errors |
neilt6 | 0:2a6d8a096edc | 446 | return 0xFF; |
neilt6 | 0:2a6d8a096edc | 447 | } |
neilt6 | 0:2a6d8a096edc | 448 | |
neilt6 | 0:2a6d8a096edc | 449 | unsigned int SDFileSystem::readReturn() |
neilt6 | 0:2a6d8a096edc | 450 | { |
neilt6 | 0:2a6d8a096edc | 451 | unsigned int ret; |
neilt6 | 0:2a6d8a096edc | 452 | |
neilt6 | 0:2a6d8a096edc | 453 | //Read the 32-bit response value |
neilt6 | 0:2a6d8a096edc | 454 | ret = (m_SPI.write(0xFF) << 24); |
neilt6 | 0:2a6d8a096edc | 455 | ret |= (m_SPI.write(0xFF) << 16); |
neilt6 | 0:2a6d8a096edc | 456 | ret |= (m_SPI.write(0xFF) << 8); |
neilt6 | 0:2a6d8a096edc | 457 | ret |= m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 458 | |
neilt6 | 0:2a6d8a096edc | 459 | //Deselect the card |
neilt6 | 0:2a6d8a096edc | 460 | deselect(); |
neilt6 | 0:2a6d8a096edc | 461 | |
neilt6 | 0:2a6d8a096edc | 462 | //Return the response value |
neilt6 | 0:2a6d8a096edc | 463 | return ret; |
neilt6 | 0:2a6d8a096edc | 464 | } |
neilt6 | 0:2a6d8a096edc | 465 | |
neilt6 | 0:2a6d8a096edc | 466 | bool SDFileSystem::readData(char* buffer, int length) |
neilt6 | 0:2a6d8a096edc | 467 | { |
neilt6 | 0:2a6d8a096edc | 468 | char token; |
neilt6 | 0:2a6d8a096edc | 469 | |
neilt6 | 0:2a6d8a096edc | 470 | //Wait for up to 200ms for the DataStart token to arrive |
neilt6 | 0:2a6d8a096edc | 471 | for (int i = 0; i < 200; i++) { |
neilt6 | 0:2a6d8a096edc | 472 | token = m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 473 | if (token != 0xFF) |
neilt6 | 0:2a6d8a096edc | 474 | break; |
neilt6 | 0:2a6d8a096edc | 475 | wait_ms(1); |
neilt6 | 0:2a6d8a096edc | 476 | } |
neilt6 | 0:2a6d8a096edc | 477 | |
neilt6 | 0:2a6d8a096edc | 478 | //Make sure the token is valid |
neilt6 | 0:2a6d8a096edc | 479 | if (token != 0xFE) |
neilt6 | 0:2a6d8a096edc | 480 | return false; |
neilt6 | 0:2a6d8a096edc | 481 | |
neilt6 | 0:2a6d8a096edc | 482 | //Read the data into the buffer |
neilt6 | 0:2a6d8a096edc | 483 | for (int i = 0; i < length; i++) |
neilt6 | 0:2a6d8a096edc | 484 | buffer[i] = m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 485 | |
neilt6 | 0:2a6d8a096edc | 486 | //Read the CRC16 checksum for the data block, and deselect the card |
neilt6 | 0:2a6d8a096edc | 487 | unsigned short crc = (m_SPI.write(0xFF) << 8); |
neilt6 | 0:2a6d8a096edc | 488 | crc |= m_SPI.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 489 | deselect(); |
neilt6 | 0:2a6d8a096edc | 490 | |
neilt6 | 0:2a6d8a096edc | 491 | //Indicate whether the CRC16 checksum was valid or not |
neilt6 | 0:2a6d8a096edc | 492 | if (crc == CRC16(buffer, length)) |
neilt6 | 0:2a6d8a096edc | 493 | return true; |
neilt6 | 0:2a6d8a096edc | 494 | else |
neilt6 | 0:2a6d8a096edc | 495 | return false; |
neilt6 | 0:2a6d8a096edc | 496 | } |