| 1 |
//***************************************************************************** |
| 2 |
// sd_diskio.c |
| 3 |
// |
| 4 |
// Low level SD Card access hookup for FatFS |
| 5 |
// |
| 6 |
// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ |
| 7 |
// |
| 8 |
// |
| 9 |
// Redistribution and use in source and binary forms, with or without |
| 10 |
// modification, are permitted provided that the following conditions |
| 11 |
// are met: |
| 12 |
// |
| 13 |
// Redistributions of source code must retain the above copyright |
| 14 |
// notice, this list of conditions and the following disclaimer. |
| 15 |
// |
| 16 |
// Redistributions in binary form must reproduce the above copyright |
| 17 |
// notice, this list of conditions and the following disclaimer in the |
| 18 |
// documentation and/or other materials provided with the |
| 19 |
// distribution. |
| 20 |
// |
| 21 |
// Neither the name of Texas Instruments Incorporated nor the names of |
| 22 |
// its contributors may be used to endorse or promote products derived |
| 23 |
// from this software without specific prior written permission. |
| 24 |
// |
| 25 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 28 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 29 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 30 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 31 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 35 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
// |
| 37 |
//***************************************************************************** |
| 38 |
#include <stdbool.h> |
| 39 |
|
| 40 |
#include "py/mpconfig.h" |
| 41 |
#include "py/mphal.h" |
| 42 |
#include "lib/oofatfs/ff.h" |
| 43 |
#include "lib/oofatfs/diskio.h" |
| 44 |
#include "hw_types.h" |
| 45 |
#include "hw_memmap.h" |
| 46 |
#include "hw_ints.h" |
| 47 |
#include "rom_map.h" |
| 48 |
#include "sd_diskio.h" |
| 49 |
#include "sdhost.h" |
| 50 |
#include "pin.h" |
| 51 |
#include "prcm.h" |
| 52 |
#include "stdcmd.h" |
| 53 |
#include "utils.h" |
| 54 |
|
| 55 |
//***************************************************************************** |
| 56 |
// Macros |
| 57 |
//***************************************************************************** |
| 58 |
#define DISKIO_RETRY_TIMEOUT 0xFFFFFFFF |
| 59 |
|
| 60 |
#define CARD_TYPE_UNKNOWN 0 |
| 61 |
#define CARD_TYPE_MMC 1 |
| 62 |
#define CARD_TYPE_SDCARD 2 |
| 63 |
|
| 64 |
#define CARD_CAP_CLASS_SDSC 0 |
| 65 |
#define CARD_CAP_CLASS_SDHC 1 |
| 66 |
|
| 67 |
#define CARD_VERSION_1 0 |
| 68 |
#define CARD_VERSION_2 1 |
| 69 |
|
| 70 |
//***************************************************************************** |
| 71 |
// Disk Info for attached disk |
| 72 |
//***************************************************************************** |
| 73 |
DiskInfo_t sd_disk_info = {CARD_TYPE_UNKNOWN, CARD_VERSION_1, CARD_CAP_CLASS_SDSC, 0, 0, STA_NOINIT, 0}; |
| 74 |
|
| 75 |
//***************************************************************************** |
| 76 |
// |
| 77 |
//! Send Command to card |
| 78 |
//! |
| 79 |
//! \param ulCmd is the command to be send |
| 80 |
//! \paran ulArg is the command argument |
| 81 |
//! |
| 82 |
//! This function sends command to attached card and check the response status |
| 83 |
//! if any. |
| 84 |
//! |
| 85 |
//! \return Returns 0 on success, 1 otherwise |
| 86 |
// |
| 87 |
//***************************************************************************** |
| 88 |
static unsigned int CardSendCmd (unsigned int ulCmd, unsigned int ulArg) { |
| 89 |
unsigned long ulStatus; |
| 90 |
|
| 91 |
// Clear the interrupt status |
| 92 |
MAP_SDHostIntClear(SDHOST_BASE,0xFFFFFFFF); |
| 93 |
|
| 94 |
// Send command |
| 95 |
MAP_SDHostCmdSend(SDHOST_BASE,ulCmd,ulArg); |
| 96 |
|
| 97 |
// Wait for command complete or error |
| 98 |
do { |
| 99 |
ulStatus = MAP_SDHostIntStatus(SDHOST_BASE); |
| 100 |
ulStatus = (ulStatus & (SDHOST_INT_CC | SDHOST_INT_ERRI)); |
| 101 |
} while (!ulStatus); |
| 102 |
|
| 103 |
// Check error status |
| 104 |
if (ulStatus & SDHOST_INT_ERRI) { |
| 105 |
// Reset the command line |
| 106 |
MAP_SDHostCmdReset(SDHOST_BASE); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
else { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
//***************************************************************************** |
| 115 |
// |
| 116 |
//! Get the capacity of specified card |
| 117 |
//! |
| 118 |
//! \param ulRCA is the Relative Card Address (RCA) |
| 119 |
//! |
| 120 |
//! This function gets the capacity of card addressed by \e ulRCA paramaeter. |
| 121 |
//! |
| 122 |
//! \return Returns 0 on success, 1 otherwise. |
| 123 |
// |
| 124 |
//***************************************************************************** |
| 125 |
static unsigned int CardCapacityGet(DiskInfo_t *psDiskInfo) { |
| 126 |
unsigned long ulRet; |
| 127 |
unsigned long ulResp[4]; |
| 128 |
unsigned long ulBlockSize; |
| 129 |
unsigned long ulBlockCount; |
| 130 |
unsigned long ulCSizeMult; |
| 131 |
unsigned long ulCSize; |
| 132 |
|
| 133 |
// Read the CSD register |
| 134 |
ulRet = CardSendCmd(CMD_SEND_CSD, (psDiskInfo->usRCA << 16)); |
| 135 |
|
| 136 |
if(ulRet == 0) { |
| 137 |
// Read the response |
| 138 |
MAP_SDHostRespGet(SDHOST_BASE,ulResp); |
| 139 |
|
| 140 |
// 136 bit CSD register is read into an array of 4 words. |
| 141 |
// ulResp[0] = CSD[31:0] |
| 142 |
// ulResp[1] = CSD[63:32] |
| 143 |
// ulResp[2] = CSD[95:64] |
| 144 |
// ulResp[3] = CSD[127:96] |
| 145 |
if(ulResp[3] >> 30) { |
| 146 |
ulBlockSize = SD_SECTOR_SIZE * 1024; |
| 147 |
ulBlockCount = (ulResp[1] >> 16 | ((ulResp[2] & 0x3F) << 16)) + 1; |
| 148 |
} |
| 149 |
else { |
| 150 |
ulBlockSize = 1 << ((ulResp[2] >> 16) & 0xF); |
| 151 |
ulCSizeMult = ((ulResp[1] >> 15) & 0x7); |
| 152 |
ulCSize = ((ulResp[1] >> 30) | (ulResp[2] & 0x3FF) << 2); |
| 153 |
ulBlockCount = (ulCSize + 1) * (1 << (ulCSizeMult + 2)); |
| 154 |
} |
| 155 |
|
| 156 |
// Calculate the card capacity in bytes |
| 157 |
psDiskInfo->ulBlockSize = ulBlockSize; |
| 158 |
psDiskInfo->ulNofBlock = ulBlockCount; |
| 159 |
} |
| 160 |
|
| 161 |
return ulRet; |
| 162 |
} |
| 163 |
|
| 164 |
//***************************************************************************** |
| 165 |
// |
| 166 |
//! Select a card for reading or writing |
| 167 |
//! |
| 168 |
//! \param Card is the pointer to card attribute structure. |
| 169 |
//! |
| 170 |
//! This function selects a card for reading or writing using its RCA from |
| 171 |
//! \e Card parameter. |
| 172 |
//! |
| 173 |
//! \return Returns 0 success, 1 otherwise. |
| 174 |
// |
| 175 |
//***************************************************************************** |
| 176 |
static unsigned int CardSelect (DiskInfo_t *sDiskInfo) { |
| 177 |
unsigned long ulRCA; |
| 178 |
unsigned long ulRet; |
| 179 |
|
| 180 |
ulRCA = sDiskInfo->usRCA; |
| 181 |
|
| 182 |
// Send select command with card's RCA. |
| 183 |
ulRet = CardSendCmd(CMD_SELECT_CARD, (ulRCA << 16)); |
| 184 |
|
| 185 |
if (ulRet == 0) { |
| 186 |
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); |
| 187 |
} |
| 188 |
|
| 189 |
// Delay 250ms for the card to become ready |
| 190 |
mp_hal_delay_ms(250); |
| 191 |
|
| 192 |
return ulRet; |
| 193 |
} |
| 194 |
|
| 195 |
//***************************************************************************** |
| 196 |
// |
| 197 |
//! Initializes physical drive |
| 198 |
//! |
| 199 |
//! This function initializes the physical drive |
| 200 |
//! |
| 201 |
//! \return Returns 0 on succeeded. |
| 202 |
//***************************************************************************** |
| 203 |
DSTATUS sd_disk_init (void) { |
| 204 |
unsigned long ulRet; |
| 205 |
unsigned long ulResp[4]; |
| 206 |
|
| 207 |
if (sd_disk_info.bStatus != 0) { |
| 208 |
sd_disk_info.bStatus = STA_NODISK; |
| 209 |
// Send std GO IDLE command |
| 210 |
if (CardSendCmd(CMD_GO_IDLE_STATE, 0) == 0) { |
| 211 |
// Get interface operating condition for the card |
| 212 |
ulRet = CardSendCmd(CMD_SEND_IF_COND,0x000001A5); |
| 213 |
MAP_SDHostRespGet(SDHOST_BASE,ulResp); |
| 214 |
|
| 215 |
// It's a SD ver 2.0 or higher card |
| 216 |
if (ulRet == 0 && ((ulResp[0] & 0xFF) == 0xA5)) { |
| 217 |
// Version 1 card do not respond to this command |
| 218 |
sd_disk_info.ulVersion = CARD_VERSION_2; |
| 219 |
sd_disk_info.ucCardType = CARD_TYPE_SDCARD; |
| 220 |
|
| 221 |
// Wait for card to become ready. |
| 222 |
do { |
| 223 |
// Send ACMD41 |
| 224 |
CardSendCmd(CMD_APP_CMD, 0); |
| 225 |
ulRet = CardSendCmd(CMD_SD_SEND_OP_COND, 0x40E00000); |
| 226 |
|
| 227 |
// Response contains 32-bit OCR register |
| 228 |
MAP_SDHostRespGet(SDHOST_BASE, ulResp); |
| 229 |
|
| 230 |
} while (((ulResp[0] >> 31) == 0)); |
| 231 |
|
| 232 |
if (ulResp[0] & (1UL<<30)) { |
| 233 |
sd_disk_info.ulCapClass = CARD_CAP_CLASS_SDHC; |
| 234 |
} |
| 235 |
sd_disk_info.bStatus = 0; |
| 236 |
} |
| 237 |
//It's a MMC or SD 1.x card |
| 238 |
else { |
| 239 |
// Wait for card to become ready. |
| 240 |
do { |
| 241 |
CardSendCmd(CMD_APP_CMD, 0); |
| 242 |
ulRet = CardSendCmd(CMD_SD_SEND_OP_COND,0x00E00000); |
| 243 |
if (ulRet == 0) { |
| 244 |
// Response contains 32-bit OCR register |
| 245 |
MAP_SDHostRespGet(SDHOST_BASE, ulResp); |
| 246 |
} |
| 247 |
} while (((ulRet == 0) && (ulResp[0] >> 31) == 0)); |
| 248 |
|
| 249 |
if (ulRet == 0) { |
| 250 |
sd_disk_info.ucCardType = CARD_TYPE_SDCARD; |
| 251 |
sd_disk_info.bStatus = 0; |
| 252 |
} |
| 253 |
else { |
| 254 |
if (CardSendCmd(CMD_SEND_OP_COND, 0) == 0) { |
| 255 |
// MMC not supported by the controller |
| 256 |
sd_disk_info.ucCardType = CARD_TYPE_MMC; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// Get the RCA of the attached card |
| 263 |
if (sd_disk_info.bStatus == 0) { |
| 264 |
ulRet = CardSendCmd(CMD_ALL_SEND_CID, 0); |
| 265 |
if (ulRet == 0) { |
| 266 |
CardSendCmd(CMD_SEND_REL_ADDR,0); |
| 267 |
MAP_SDHostRespGet(SDHOST_BASE, ulResp); |
| 268 |
|
| 269 |
// Fill in the RCA |
| 270 |
sd_disk_info.usRCA = (ulResp[0] >> 16); |
| 271 |
|
| 272 |
// Get the card capacity |
| 273 |
CardCapacityGet(&sd_disk_info); |
| 274 |
} |
| 275 |
|
| 276 |
// Select the card. |
| 277 |
ulRet = CardSelect(&sd_disk_info); |
| 278 |
if (ulRet == 0) { |
| 279 |
sd_disk_info.bStatus = 0; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return sd_disk_info.bStatus; |
| 285 |
} |
| 286 |
|
| 287 |
//***************************************************************************** |
| 288 |
// |
| 289 |
//! De-initializes the physical drive |
| 290 |
//! |
| 291 |
//! This function de-initializes the physical drive |
| 292 |
//***************************************************************************** |
| 293 |
void sd_disk_deinit (void) { |
| 294 |
sd_disk_info.ucCardType = CARD_TYPE_UNKNOWN; |
| 295 |
sd_disk_info.ulVersion = CARD_VERSION_1; |
| 296 |
sd_disk_info.ulCapClass = CARD_CAP_CLASS_SDSC; |
| 297 |
sd_disk_info.ulNofBlock = 0; |
| 298 |
sd_disk_info.ulBlockSize = 0; |
| 299 |
sd_disk_info.bStatus = STA_NOINIT; |
| 300 |
sd_disk_info.usRCA = 0; |
| 301 |
} |
| 302 |
|
| 303 |
//***************************************************************************** |
| 304 |
// |
| 305 |
//! Reads sector(s) from the disk drive. |
| 306 |
//! |
| 307 |
//! |
| 308 |
//! This function reads specified number of sectors from the drive |
| 309 |
//! |
| 310 |
//! \return Returns RES_OK on success. |
| 311 |
// |
| 312 |
//***************************************************************************** |
| 313 |
DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { |
| 314 |
DRESULT Res = RES_ERROR; |
| 315 |
unsigned long ulSize; |
| 316 |
|
| 317 |
if (SectorCount > 0) { |
| 318 |
// Return if disk not initialized |
| 319 |
if (sd_disk_info.bStatus & STA_NOINIT) { |
| 320 |
return RES_NOTRDY; |
| 321 |
} |
| 322 |
|
| 323 |
// SDSC uses linear address, SDHC uses block address |
| 324 |
if (sd_disk_info.ulCapClass == CARD_CAP_CLASS_SDSC) { |
| 325 |
ulSectorNumber = ulSectorNumber * SD_SECTOR_SIZE; |
| 326 |
} |
| 327 |
|
| 328 |
// Set the block count |
| 329 |
MAP_SDHostBlockCountSet(SDHOST_BASE, SectorCount); |
| 330 |
|
| 331 |
// Compute the number of words |
| 332 |
ulSize = (SD_SECTOR_SIZE * SectorCount) / 4; |
| 333 |
|
| 334 |
// Check if 1 block or multi block transfer |
| 335 |
if (SectorCount == 1) { |
| 336 |
// Send single block read command |
| 337 |
if (CardSendCmd(CMD_READ_SINGLE_BLK, ulSectorNumber) == 0) { |
| 338 |
// Read the block of data |
| 339 |
while (ulSize--) { |
| 340 |
MAP_SDHostDataRead(SDHOST_BASE, (unsigned long *)pBuffer); |
| 341 |
pBuffer += 4; |
| 342 |
} |
| 343 |
Res = RES_OK; |
| 344 |
} |
| 345 |
} |
| 346 |
else { |
| 347 |
// Send multi block read command |
| 348 |
if (CardSendCmd(CMD_READ_MULTI_BLK, ulSectorNumber) == 0) { |
| 349 |
// Read the data |
| 350 |
while (ulSize--) { |
| 351 |
MAP_SDHostDataRead(SDHOST_BASE, (unsigned long *)pBuffer); |
| 352 |
pBuffer += 4; |
| 353 |
} |
| 354 |
CardSendCmd(CMD_STOP_TRANS, 0); |
| 355 |
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); |
| 356 |
Res = RES_OK; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return Res; |
| 362 |
} |
| 363 |
|
| 364 |
//***************************************************************************** |
| 365 |
// |
| 366 |
//! Writes sector(s) to the disk drive. |
| 367 |
//! |
| 368 |
//! |
| 369 |
//! This function writes specified number of sectors to the drive |
| 370 |
//! |
| 371 |
//! \return Returns RES_OK on success. |
| 372 |
// |
| 373 |
//***************************************************************************** |
| 374 |
DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT SectorCount) { |
| 375 |
DRESULT Res = RES_ERROR; |
| 376 |
unsigned long ulSize; |
| 377 |
|
| 378 |
if (SectorCount > 0) { |
| 379 |
// Return if disk not initialized |
| 380 |
if (sd_disk_info.bStatus & STA_NOINIT) { |
| 381 |
return RES_NOTRDY; |
| 382 |
} |
| 383 |
|
| 384 |
// SDSC uses linear address, SDHC uses block address |
| 385 |
if (sd_disk_info.ulCapClass == CARD_CAP_CLASS_SDSC) { |
| 386 |
ulSectorNumber = ulSectorNumber * SD_SECTOR_SIZE; |
| 387 |
} |
| 388 |
|
| 389 |
// Set the block count |
| 390 |
MAP_SDHostBlockCountSet(SDHOST_BASE, SectorCount); |
| 391 |
|
| 392 |
// Compute the number of words |
| 393 |
ulSize = (SD_SECTOR_SIZE * SectorCount) / 4; |
| 394 |
|
| 395 |
// Check if 1 block or multi block transfer |
| 396 |
if (SectorCount == 1) { |
| 397 |
// Send single block write command |
| 398 |
if (CardSendCmd(CMD_WRITE_SINGLE_BLK, ulSectorNumber) == 0) { |
| 399 |
// Write the data |
| 400 |
while (ulSize--) { |
| 401 |
MAP_SDHostDataWrite (SDHOST_BASE, (*(unsigned long *)pBuffer)); |
| 402 |
pBuffer += 4; |
| 403 |
} |
| 404 |
// Wait for data transfer complete |
| 405 |
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); |
| 406 |
Res = RES_OK; |
| 407 |
} |
| 408 |
} |
| 409 |
else { |
| 410 |
// Set the card write block count |
| 411 |
if (sd_disk_info.ucCardType == CARD_TYPE_SDCARD) { |
| 412 |
CardSendCmd(CMD_APP_CMD,sd_disk_info.usRCA << 16); |
| 413 |
CardSendCmd(CMD_SET_BLK_CNT, SectorCount); |
| 414 |
} |
| 415 |
|
| 416 |
// Send multi block write command |
| 417 |
if (CardSendCmd(CMD_WRITE_MULTI_BLK, ulSectorNumber) == 0) { |
| 418 |
// Write the data buffer |
| 419 |
while (ulSize--) { |
| 420 |
MAP_SDHostDataWrite(SDHOST_BASE, (*(unsigned long *)pBuffer)); |
| 421 |
pBuffer += 4; |
| 422 |
} |
| 423 |
// Wait for transfer complete |
| 424 |
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); |
| 425 |
CardSendCmd(CMD_STOP_TRANS, 0); |
| 426 |
while (!(MAP_SDHostIntStatus(SDHOST_BASE) & SDHOST_INT_TC)); |
| 427 |
Res = RES_OK; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
return Res; |
| 433 |
} |