Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_sd.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /**************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file SD.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V1.00 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 16 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 15/11/26 10:45a $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief NUC472/NUC442 SD driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include <stdlib.h> |
sahilmgandhi | 18:6a4db94011d3 | 13 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 14 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 15 | |
sahilmgandhi | 18:6a4db94011d3 | 16 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
sahilmgandhi | 18:6a4db94011d3 | 17 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 18 | */ |
sahilmgandhi | 18:6a4db94011d3 | 19 | |
sahilmgandhi | 18:6a4db94011d3 | 20 | /** @addtogroup NUC472_442_SD_Driver SD Driver |
sahilmgandhi | 18:6a4db94011d3 | 21 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 22 | */ |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | |
sahilmgandhi | 18:6a4db94011d3 | 25 | /** @addtogroup NUC472_442_SD_EXPORTED_FUNCTIONS SD Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 26 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 27 | */ |
sahilmgandhi | 18:6a4db94011d3 | 28 | #define SD_BLOCK_SIZE 512 |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | /// @cond HIDDEN_SYMBOLS |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | // global variables |
sahilmgandhi | 18:6a4db94011d3 | 33 | // For response R3 (such as ACMD41, CRC-7 is invalid; but SD controller will still |
sahilmgandhi | 18:6a4db94011d3 | 34 | // calculate CRC-7 and get an error result, software should ignore this error and clear SDISR [CRC_IF] flag |
sahilmgandhi | 18:6a4db94011d3 | 35 | // _sd_uR3_CMD is the flag for it. 1 means software should ignore CRC-7 error |
sahilmgandhi | 18:6a4db94011d3 | 36 | uint32_t _sd_uR3_CMD=0; |
sahilmgandhi | 18:6a4db94011d3 | 37 | uint32_t _sd_uR7_CMD=0; |
sahilmgandhi | 18:6a4db94011d3 | 38 | uint8_t volatile _sd_SDDataReady = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | uint8_t *_sd_pSDHCBuffer; |
sahilmgandhi | 18:6a4db94011d3 | 41 | uint32_t _sd_ReferenceClock; |
sahilmgandhi | 18:6a4db94011d3 | 42 | |
sahilmgandhi | 18:6a4db94011d3 | 43 | #if defined (__CC_ARM) |
sahilmgandhi | 18:6a4db94011d3 | 44 | __align(4096) uint8_t _sd_ucSDHCBuffer[512]; |
sahilmgandhi | 18:6a4db94011d3 | 45 | #elif defined ( __ICCARM__ ) /*!< IAR Compiler */ |
sahilmgandhi | 18:6a4db94011d3 | 46 | #pragma data_alignment = 4096 |
sahilmgandhi | 18:6a4db94011d3 | 47 | uint8_t _sd_ucSDHCBuffer[512]; |
sahilmgandhi | 18:6a4db94011d3 | 48 | #elif defined ( __GNUC__ ) |
sahilmgandhi | 18:6a4db94011d3 | 49 | uint8_t _sd_ucSDHCBuffer[512] __attribute__((aligned (4096))); |
sahilmgandhi | 18:6a4db94011d3 | 50 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | int sd0_ok = 0; |
sahilmgandhi | 18:6a4db94011d3 | 53 | int sd1_ok = 0; |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | uint8_t pSD0_offset = 0; |
sahilmgandhi | 18:6a4db94011d3 | 56 | uint8_t pSD1_offset = 0; |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | DISK_DATA_T SD_DiskInfo0; |
sahilmgandhi | 18:6a4db94011d3 | 59 | DISK_DATA_T SD_DiskInfo1; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | SD_INFO_T SD0; |
sahilmgandhi | 18:6a4db94011d3 | 62 | SD_INFO_T SD1; |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | void SD_CheckRB() |
sahilmgandhi | 18:6a4db94011d3 | 65 | { |
sahilmgandhi | 18:6a4db94011d3 | 66 | while(1) { |
sahilmgandhi | 18:6a4db94011d3 | 67 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 68 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 69 | if (SD->INTSTS & SDH_INTSTS_DAT0STS_Msk) |
sahilmgandhi | 18:6a4db94011d3 | 70 | break; |
sahilmgandhi | 18:6a4db94011d3 | 71 | } |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | int SD_SDCommand(SD_INFO_T *pSD, uint8_t ucCmd, uint32_t uArg) |
sahilmgandhi | 18:6a4db94011d3 | 76 | { |
sahilmgandhi | 18:6a4db94011d3 | 77 | volatile int buf; |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | SD->CMDARG = uArg; |
sahilmgandhi | 18:6a4db94011d3 | 80 | buf = (SD->CTL&(~SDH_CTL_CMDCODE_Msk))|(ucCmd << 8)|(SDH_CTL_COEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 81 | SD->CTL = buf; |
sahilmgandhi | 18:6a4db94011d3 | 82 | |
sahilmgandhi | 18:6a4db94011d3 | 83 | while(SD->CTL & SDH_CTL_COEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 84 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 85 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 86 | } |
sahilmgandhi | 18:6a4db94011d3 | 87 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 88 | } |
sahilmgandhi | 18:6a4db94011d3 | 89 | |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | int SD_SDCmdAndRsp(SD_INFO_T *pSD, uint8_t ucCmd, uint32_t uArg, int ntickCount) |
sahilmgandhi | 18:6a4db94011d3 | 92 | { |
sahilmgandhi | 18:6a4db94011d3 | 93 | volatile int buf; |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | SD->CMDARG = uArg; |
sahilmgandhi | 18:6a4db94011d3 | 96 | buf = (SD->CTL&(~SDH_CTL_CMDCODE_Msk))|(ucCmd << 8)|(SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 97 | SD->CTL = buf; |
sahilmgandhi | 18:6a4db94011d3 | 98 | |
sahilmgandhi | 18:6a4db94011d3 | 99 | if (ntickCount > 0) { |
sahilmgandhi | 18:6a4db94011d3 | 100 | while(SD->CTL & SDH_CTL_RIEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 101 | if(ntickCount-- == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 102 | SD->CTL |= SDH_CTL_CTLRST_Msk; // reset SD engine |
sahilmgandhi | 18:6a4db94011d3 | 103 | return 2; |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 106 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 107 | } |
sahilmgandhi | 18:6a4db94011d3 | 108 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 109 | while(SD->CTL & SDH_CTL_RIEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 110 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 111 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 112 | } |
sahilmgandhi | 18:6a4db94011d3 | 113 | } |
sahilmgandhi | 18:6a4db94011d3 | 114 | |
sahilmgandhi | 18:6a4db94011d3 | 115 | if (_sd_uR7_CMD) { |
sahilmgandhi | 18:6a4db94011d3 | 116 | if (((SD->RESP1 & 0xff) != 0x55) && ((SD->RESP0 & 0xf) != 0x01)) { |
sahilmgandhi | 18:6a4db94011d3 | 117 | _sd_uR7_CMD = 0; |
sahilmgandhi | 18:6a4db94011d3 | 118 | return SD_CMD8_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 119 | } |
sahilmgandhi | 18:6a4db94011d3 | 120 | } |
sahilmgandhi | 18:6a4db94011d3 | 121 | |
sahilmgandhi | 18:6a4db94011d3 | 122 | if (!_sd_uR3_CMD) { |
sahilmgandhi | 18:6a4db94011d3 | 123 | if (SD->INTSTS & SDH_INTSTS_CRC7_Msk) // check CRC7 |
sahilmgandhi | 18:6a4db94011d3 | 124 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 125 | else { |
sahilmgandhi | 18:6a4db94011d3 | 126 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 127 | } |
sahilmgandhi | 18:6a4db94011d3 | 128 | } else { // ignore CRC error for R3 case |
sahilmgandhi | 18:6a4db94011d3 | 129 | _sd_uR3_CMD = 0; |
sahilmgandhi | 18:6a4db94011d3 | 130 | SD->INTSTS = SDH_INTSTS_CRCIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 131 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 132 | } |
sahilmgandhi | 18:6a4db94011d3 | 133 | } |
sahilmgandhi | 18:6a4db94011d3 | 134 | |
sahilmgandhi | 18:6a4db94011d3 | 135 | |
sahilmgandhi | 18:6a4db94011d3 | 136 | int SD_Swap32(int val) |
sahilmgandhi | 18:6a4db94011d3 | 137 | { |
sahilmgandhi | 18:6a4db94011d3 | 138 | #if 1 |
sahilmgandhi | 18:6a4db94011d3 | 139 | int buf; |
sahilmgandhi | 18:6a4db94011d3 | 140 | |
sahilmgandhi | 18:6a4db94011d3 | 141 | buf = val; |
sahilmgandhi | 18:6a4db94011d3 | 142 | val <<= 24; |
sahilmgandhi | 18:6a4db94011d3 | 143 | val |= (buf<<8)&0xff0000; |
sahilmgandhi | 18:6a4db94011d3 | 144 | val |= (buf>>8)&0xff00; |
sahilmgandhi | 18:6a4db94011d3 | 145 | val |= (buf>>24)&0xff; |
sahilmgandhi | 18:6a4db94011d3 | 146 | return val; |
sahilmgandhi | 18:6a4db94011d3 | 147 | |
sahilmgandhi | 18:6a4db94011d3 | 148 | #else |
sahilmgandhi | 18:6a4db94011d3 | 149 | return ((val<<24) | ((val<<8)&0xff0000) | ((val>>8)&0xff00) | (val>>24)); |
sahilmgandhi | 18:6a4db94011d3 | 150 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 151 | } |
sahilmgandhi | 18:6a4db94011d3 | 152 | |
sahilmgandhi | 18:6a4db94011d3 | 153 | // Get 16 bytes CID or CSD |
sahilmgandhi | 18:6a4db94011d3 | 154 | int SD_SDCmdAndRsp2(SD_INFO_T *pSD, uint8_t ucCmd, uint32_t uArg, uint32_t *puR2ptr) |
sahilmgandhi | 18:6a4db94011d3 | 155 | { |
sahilmgandhi | 18:6a4db94011d3 | 156 | unsigned int i, buf; |
sahilmgandhi | 18:6a4db94011d3 | 157 | unsigned int tmpBuf[5]; |
sahilmgandhi | 18:6a4db94011d3 | 158 | |
sahilmgandhi | 18:6a4db94011d3 | 159 | SD->CMDARG = uArg; |
sahilmgandhi | 18:6a4db94011d3 | 160 | buf = (SD->CTL&(~SDH_CTL_CMDCODE_Msk))|(ucCmd << 8)|(SDH_CTL_COEN_Msk | SDH_CTL_R2EN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 161 | SD->CTL = buf; |
sahilmgandhi | 18:6a4db94011d3 | 162 | |
sahilmgandhi | 18:6a4db94011d3 | 163 | while(SD->CTL & SDH_CTL_R2EN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 164 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 165 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 166 | } |
sahilmgandhi | 18:6a4db94011d3 | 167 | |
sahilmgandhi | 18:6a4db94011d3 | 168 | if (SD->INTSTS & SDH_INTSTS_CRC7_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 169 | for (i=0; i<5; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 170 | tmpBuf[i] = SD_Swap32(*(int*)(SD_BASE+i*4)); |
sahilmgandhi | 18:6a4db94011d3 | 171 | } |
sahilmgandhi | 18:6a4db94011d3 | 172 | for (i=0; i<4; i++) |
sahilmgandhi | 18:6a4db94011d3 | 173 | *puR2ptr++ = ((tmpBuf[i] & 0x00ffffff)<<8) | ((tmpBuf[i+1] & 0xff000000)>>24); |
sahilmgandhi | 18:6a4db94011d3 | 174 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 175 | } else |
sahilmgandhi | 18:6a4db94011d3 | 176 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 177 | } |
sahilmgandhi | 18:6a4db94011d3 | 178 | |
sahilmgandhi | 18:6a4db94011d3 | 179 | |
sahilmgandhi | 18:6a4db94011d3 | 180 | int SD_SDCmdAndRspDataIn(SD_INFO_T *pSD, uint8_t ucCmd, uint32_t uArg) |
sahilmgandhi | 18:6a4db94011d3 | 181 | { |
sahilmgandhi | 18:6a4db94011d3 | 182 | volatile int buf; |
sahilmgandhi | 18:6a4db94011d3 | 183 | |
sahilmgandhi | 18:6a4db94011d3 | 184 | SD->CMDARG = uArg; |
sahilmgandhi | 18:6a4db94011d3 | 185 | buf = (SD->CTL&(~SDH_CTL_CMDCODE_Msk))|(ucCmd << 8)| |
sahilmgandhi | 18:6a4db94011d3 | 186 | (SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk | SDH_CTL_DIEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 187 | |
sahilmgandhi | 18:6a4db94011d3 | 188 | SD->CTL = buf; |
sahilmgandhi | 18:6a4db94011d3 | 189 | |
sahilmgandhi | 18:6a4db94011d3 | 190 | while (SD->CTL & SDH_CTL_RIEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 191 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 192 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 193 | } |
sahilmgandhi | 18:6a4db94011d3 | 194 | |
sahilmgandhi | 18:6a4db94011d3 | 195 | while (SD->CTL & SDH_CTL_DIEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 196 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 197 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 198 | } |
sahilmgandhi | 18:6a4db94011d3 | 199 | |
sahilmgandhi | 18:6a4db94011d3 | 200 | if (!(SD->INTSTS & SDH_INTSTS_CRC7_Msk)) { // check CRC7 |
sahilmgandhi | 18:6a4db94011d3 | 201 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 202 | } |
sahilmgandhi | 18:6a4db94011d3 | 203 | |
sahilmgandhi | 18:6a4db94011d3 | 204 | if (!(SD->INTSTS & SDH_INTSTS_CRC16_Msk)) { // check CRC16 |
sahilmgandhi | 18:6a4db94011d3 | 205 | return SD_CRC16_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 206 | } |
sahilmgandhi | 18:6a4db94011d3 | 207 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 208 | } |
sahilmgandhi | 18:6a4db94011d3 | 209 | |
sahilmgandhi | 18:6a4db94011d3 | 210 | // there are 8 bits for divider0, maximum is 256 |
sahilmgandhi | 18:6a4db94011d3 | 211 | #define SD_CLK_DIV0_MAX 256 |
sahilmgandhi | 18:6a4db94011d3 | 212 | |
sahilmgandhi | 18:6a4db94011d3 | 213 | void SD_Set_clock(uint32_t sd_clock_khz) |
sahilmgandhi | 18:6a4db94011d3 | 214 | { |
sahilmgandhi | 18:6a4db94011d3 | 215 | uint32_t rate, div1, i; |
sahilmgandhi | 18:6a4db94011d3 | 216 | uint32_t u32SD_ClkSrc; |
sahilmgandhi | 18:6a4db94011d3 | 217 | |
sahilmgandhi | 18:6a4db94011d3 | 218 | if(sd_clock_khz >= 24000) { |
sahilmgandhi | 18:6a4db94011d3 | 219 | sd_clock_khz = 24000; |
sahilmgandhi | 18:6a4db94011d3 | 220 | } |
sahilmgandhi | 18:6a4db94011d3 | 221 | |
sahilmgandhi | 18:6a4db94011d3 | 222 | u32SD_ClkSrc = (CLK->CLKSEL0 & CLK_CLKSEL0_SDHSEL_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 223 | |
sahilmgandhi | 18:6a4db94011d3 | 224 | if(u32SD_ClkSrc == CLK_CLKSEL0_SDHSEL_HXT) |
sahilmgandhi | 18:6a4db94011d3 | 225 | _sd_ReferenceClock = (CLK_GetHXTFreq() / 1000); |
sahilmgandhi | 18:6a4db94011d3 | 226 | else if(u32SD_ClkSrc == CLK_CLKSEL0_SDHSEL_HIRC) |
sahilmgandhi | 18:6a4db94011d3 | 227 | _sd_ReferenceClock = (__HIRC / 1000); |
sahilmgandhi | 18:6a4db94011d3 | 228 | else if(u32SD_ClkSrc == CLK_CLKSEL0_SDHSEL_PLL) |
sahilmgandhi | 18:6a4db94011d3 | 229 | _sd_ReferenceClock = (CLK_GetPLLClockFreq() / 1000); |
sahilmgandhi | 18:6a4db94011d3 | 230 | else if(u32SD_ClkSrc == CLK_CLKSEL0_SDHSEL_HCLK) |
sahilmgandhi | 18:6a4db94011d3 | 231 | _sd_ReferenceClock = (CLK_GetHCLKFreq() / 1000); |
sahilmgandhi | 18:6a4db94011d3 | 232 | |
sahilmgandhi | 18:6a4db94011d3 | 233 | rate = _sd_ReferenceClock / sd_clock_khz; |
sahilmgandhi | 18:6a4db94011d3 | 234 | |
sahilmgandhi | 18:6a4db94011d3 | 235 | // choose slower clock if system clock cannot divisible by wanted clock |
sahilmgandhi | 18:6a4db94011d3 | 236 | if (_sd_ReferenceClock % sd_clock_khz != 0) |
sahilmgandhi | 18:6a4db94011d3 | 237 | rate++; |
sahilmgandhi | 18:6a4db94011d3 | 238 | |
sahilmgandhi | 18:6a4db94011d3 | 239 | if(rate >= SD_CLK_DIV0_MAX) { |
sahilmgandhi | 18:6a4db94011d3 | 240 | rate = SD_CLK_DIV0_MAX; |
sahilmgandhi | 18:6a4db94011d3 | 241 | } |
sahilmgandhi | 18:6a4db94011d3 | 242 | |
sahilmgandhi | 18:6a4db94011d3 | 243 | //--- calculate the second divider CLKDIV0[SDHOST_N] |
sahilmgandhi | 18:6a4db94011d3 | 244 | div1 = ((rate -1) & 0xFF); |
sahilmgandhi | 18:6a4db94011d3 | 245 | |
sahilmgandhi | 18:6a4db94011d3 | 246 | //--- setup register |
sahilmgandhi | 18:6a4db94011d3 | 247 | CLK->CLKDIV0 &= ~CLK_CLKDIV0_SDHDIV_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 248 | CLK->CLKDIV0 |= (div1 << CLK_CLKDIV0_SDHDIV_Pos); |
sahilmgandhi | 18:6a4db94011d3 | 249 | |
sahilmgandhi | 18:6a4db94011d3 | 250 | for(i=0; i<1000; i++); // waiting for clock become stable |
sahilmgandhi | 18:6a4db94011d3 | 251 | return; |
sahilmgandhi | 18:6a4db94011d3 | 252 | } |
sahilmgandhi | 18:6a4db94011d3 | 253 | |
sahilmgandhi | 18:6a4db94011d3 | 254 | void SD_CardSelect(int cardSel) |
sahilmgandhi | 18:6a4db94011d3 | 255 | { |
sahilmgandhi | 18:6a4db94011d3 | 256 | if(cardSel == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 257 | SD->CTL |= (SD->CTL & ~SDH_CTL_SDPORT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 258 | } else if(cardSel == 1) { |
sahilmgandhi | 18:6a4db94011d3 | 259 | SD->CTL |= ((SD->CTL & ~SDH_CTL_SDPORT_Msk) | (1 << SDH_CTL_SDPORT_Pos)); |
sahilmgandhi | 18:6a4db94011d3 | 260 | } |
sahilmgandhi | 18:6a4db94011d3 | 261 | } |
sahilmgandhi | 18:6a4db94011d3 | 262 | |
sahilmgandhi | 18:6a4db94011d3 | 263 | uint32_t SD_CardDetection(uint32_t u32CardNum) |
sahilmgandhi | 18:6a4db94011d3 | 264 | { |
sahilmgandhi | 18:6a4db94011d3 | 265 | uint32_t i; |
sahilmgandhi | 18:6a4db94011d3 | 266 | |
sahilmgandhi | 18:6a4db94011d3 | 267 | if (u32CardNum == SD_PORT0) { |
sahilmgandhi | 18:6a4db94011d3 | 268 | if(SD->INTEN & SDH_INTEN_CDSRC0_Msk) { // Card detect pin from GPIO |
sahilmgandhi | 18:6a4db94011d3 | 269 | if(SD->INTSTS & SDH_INTSTS_CDSTS0_Msk) { // Card remove |
sahilmgandhi | 18:6a4db94011d3 | 270 | SD0.IsCardInsert = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 271 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 272 | } else |
sahilmgandhi | 18:6a4db94011d3 | 273 | SD0.IsCardInsert = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 274 | } else if(!(SD->INTEN & SDH_INTEN_CDSRC0_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 275 | SD->CTL |= SDH_CTL_CLKKEEP0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 276 | for(i= 0; i < 5000; i++); |
sahilmgandhi | 18:6a4db94011d3 | 277 | |
sahilmgandhi | 18:6a4db94011d3 | 278 | if(SD->INTSTS & SDH_INTSTS_CDSTS0_Msk) // Card insert |
sahilmgandhi | 18:6a4db94011d3 | 279 | SD0.IsCardInsert = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 280 | else { |
sahilmgandhi | 18:6a4db94011d3 | 281 | SD0.IsCardInsert = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 282 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 283 | } |
sahilmgandhi | 18:6a4db94011d3 | 284 | |
sahilmgandhi | 18:6a4db94011d3 | 285 | SD->CTL &= ~SDH_CTL_CLKKEEP0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 286 | } |
sahilmgandhi | 18:6a4db94011d3 | 287 | |
sahilmgandhi | 18:6a4db94011d3 | 288 | } else if (u32CardNum == SD_PORT1) { |
sahilmgandhi | 18:6a4db94011d3 | 289 | if(SD->INTEN & SDH_INTEN_CDSRC1_Msk) { // Card detect pin from GPIO |
sahilmgandhi | 18:6a4db94011d3 | 290 | if(SD->INTSTS & SDH_INTSTS_CDSTS1_Msk) { // Card remove |
sahilmgandhi | 18:6a4db94011d3 | 291 | SD1.IsCardInsert = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 292 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 293 | } else |
sahilmgandhi | 18:6a4db94011d3 | 294 | SD1.IsCardInsert = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 295 | } else if(!(SD->INTEN & SDH_INTEN_CDSRC1_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 296 | SD->CTL |= SDH_CTL_CLKKEEP1_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 297 | for(i= 0; i < 5000; i++); |
sahilmgandhi | 18:6a4db94011d3 | 298 | |
sahilmgandhi | 18:6a4db94011d3 | 299 | if(SD->INTSTS & SDH_INTSTS_CDSTS1_Msk) // Card insert |
sahilmgandhi | 18:6a4db94011d3 | 300 | SD1.IsCardInsert = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 301 | else { |
sahilmgandhi | 18:6a4db94011d3 | 302 | SD1.IsCardInsert = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 303 | return FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 304 | } |
sahilmgandhi | 18:6a4db94011d3 | 305 | |
sahilmgandhi | 18:6a4db94011d3 | 306 | SD->CTL &= ~SDH_CTL_CLKKEEP1_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 307 | } |
sahilmgandhi | 18:6a4db94011d3 | 308 | |
sahilmgandhi | 18:6a4db94011d3 | 309 | } |
sahilmgandhi | 18:6a4db94011d3 | 310 | |
sahilmgandhi | 18:6a4db94011d3 | 311 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 312 | } |
sahilmgandhi | 18:6a4db94011d3 | 313 | |
sahilmgandhi | 18:6a4db94011d3 | 314 | |
sahilmgandhi | 18:6a4db94011d3 | 315 | // Initial |
sahilmgandhi | 18:6a4db94011d3 | 316 | int SD_Init(SD_INFO_T *pSD) |
sahilmgandhi | 18:6a4db94011d3 | 317 | { |
sahilmgandhi | 18:6a4db94011d3 | 318 | int volatile i, status; |
sahilmgandhi | 18:6a4db94011d3 | 319 | unsigned int resp; |
sahilmgandhi | 18:6a4db94011d3 | 320 | unsigned int CIDBuffer[4]; |
sahilmgandhi | 18:6a4db94011d3 | 321 | unsigned int volatile u32CmdTimeOut; |
sahilmgandhi | 18:6a4db94011d3 | 322 | |
sahilmgandhi | 18:6a4db94011d3 | 323 | // set the clock to 200KHz |
sahilmgandhi | 18:6a4db94011d3 | 324 | //SD_Set_clock(200); |
sahilmgandhi | 18:6a4db94011d3 | 325 | SD_Set_clock(100); |
sahilmgandhi | 18:6a4db94011d3 | 326 | |
sahilmgandhi | 18:6a4db94011d3 | 327 | // power ON 74 clock |
sahilmgandhi | 18:6a4db94011d3 | 328 | SD->CTL |= SDH_CTL_CLK74OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 329 | |
sahilmgandhi | 18:6a4db94011d3 | 330 | while(SD->CTL & SDH_CTL_CLK74OEN_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 331 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 332 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 333 | } |
sahilmgandhi | 18:6a4db94011d3 | 334 | |
sahilmgandhi | 18:6a4db94011d3 | 335 | SD_SDCommand(pSD, 0, 0); // reset all cards |
sahilmgandhi | 18:6a4db94011d3 | 336 | for (i=0x1000; i>0; i--); |
sahilmgandhi | 18:6a4db94011d3 | 337 | |
sahilmgandhi | 18:6a4db94011d3 | 338 | // initial SDHC |
sahilmgandhi | 18:6a4db94011d3 | 339 | _sd_uR7_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 340 | //u32CmdTimeOut = 5000; |
sahilmgandhi | 18:6a4db94011d3 | 341 | u32CmdTimeOut = 0xFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 342 | //u32CmdTimeOut = 0; |
sahilmgandhi | 18:6a4db94011d3 | 343 | |
sahilmgandhi | 18:6a4db94011d3 | 344 | i = SD_SDCmdAndRsp(pSD, 8, 0x00000155, u32CmdTimeOut); |
sahilmgandhi | 18:6a4db94011d3 | 345 | if (i == Successful) { |
sahilmgandhi | 18:6a4db94011d3 | 346 | // SD 2.0 |
sahilmgandhi | 18:6a4db94011d3 | 347 | SD_SDCmdAndRsp(pSD, 55, 0x00, u32CmdTimeOut); |
sahilmgandhi | 18:6a4db94011d3 | 348 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 349 | SD_SDCmdAndRsp(pSD, 41, 0x40ff8000, u32CmdTimeOut); // 2.7v-3.6v |
sahilmgandhi | 18:6a4db94011d3 | 350 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 351 | |
sahilmgandhi | 18:6a4db94011d3 | 352 | while (!(resp & 0x00800000)) { // check if card is ready |
sahilmgandhi | 18:6a4db94011d3 | 353 | SD_SDCmdAndRsp(pSD, 55, 0x00, u32CmdTimeOut); |
sahilmgandhi | 18:6a4db94011d3 | 354 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 355 | SD_SDCmdAndRsp(pSD, 41, 0x40ff8000, u32CmdTimeOut); // 3.0v-3.4v |
sahilmgandhi | 18:6a4db94011d3 | 356 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 357 | } |
sahilmgandhi | 18:6a4db94011d3 | 358 | if (resp & 0x00400000) |
sahilmgandhi | 18:6a4db94011d3 | 359 | pSD->CardType = SD_TYPE_SD_HIGH; |
sahilmgandhi | 18:6a4db94011d3 | 360 | else |
sahilmgandhi | 18:6a4db94011d3 | 361 | pSD->CardType = SD_TYPE_SD_LOW; |
sahilmgandhi | 18:6a4db94011d3 | 362 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 363 | // SD 1.1 |
sahilmgandhi | 18:6a4db94011d3 | 364 | SD_SDCommand(pSD, 0, 0); // reset all cards |
sahilmgandhi | 18:6a4db94011d3 | 365 | for (i=0x100; i>0; i--); |
sahilmgandhi | 18:6a4db94011d3 | 366 | |
sahilmgandhi | 18:6a4db94011d3 | 367 | i = SD_SDCmdAndRsp(pSD, 55, 0x00, u32CmdTimeOut); |
sahilmgandhi | 18:6a4db94011d3 | 368 | if (i == 2) { // MMC memory |
sahilmgandhi | 18:6a4db94011d3 | 369 | |
sahilmgandhi | 18:6a4db94011d3 | 370 | SD_SDCommand(pSD, 0, 0); // reset |
sahilmgandhi | 18:6a4db94011d3 | 371 | for (i=0x100; i>0; i--); |
sahilmgandhi | 18:6a4db94011d3 | 372 | |
sahilmgandhi | 18:6a4db94011d3 | 373 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 374 | |
sahilmgandhi | 18:6a4db94011d3 | 375 | if (SD_SDCmdAndRsp(pSD, 1, 0x40ff8000, u32CmdTimeOut) != 2) { // eMMC memory |
sahilmgandhi | 18:6a4db94011d3 | 376 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 377 | while (!(resp & 0x00800000)) { // check if card is ready |
sahilmgandhi | 18:6a4db94011d3 | 378 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 379 | |
sahilmgandhi | 18:6a4db94011d3 | 380 | SD_SDCmdAndRsp(pSD, 1, 0x40ff8000, u32CmdTimeOut); // high voltage |
sahilmgandhi | 18:6a4db94011d3 | 381 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 382 | } |
sahilmgandhi | 18:6a4db94011d3 | 383 | |
sahilmgandhi | 18:6a4db94011d3 | 384 | if(resp & 0x00400000) |
sahilmgandhi | 18:6a4db94011d3 | 385 | pSD->CardType = SD_TYPE_EMMC; |
sahilmgandhi | 18:6a4db94011d3 | 386 | else |
sahilmgandhi | 18:6a4db94011d3 | 387 | pSD->CardType = SD_TYPE_MMC; |
sahilmgandhi | 18:6a4db94011d3 | 388 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 389 | pSD->CardType = SD_TYPE_UNKNOWN; |
sahilmgandhi | 18:6a4db94011d3 | 390 | return SD_ERR_DEVICE; |
sahilmgandhi | 18:6a4db94011d3 | 391 | } |
sahilmgandhi | 18:6a4db94011d3 | 392 | } else if (i == 0) { // SD Memory |
sahilmgandhi | 18:6a4db94011d3 | 393 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 394 | SD_SDCmdAndRsp(pSD, 41, 0x00ff8000, u32CmdTimeOut); // 3.0v-3.4v |
sahilmgandhi | 18:6a4db94011d3 | 395 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 396 | while (!(resp & 0x00800000)) { // check if card is ready |
sahilmgandhi | 18:6a4db94011d3 | 397 | SD_SDCmdAndRsp(pSD, 55, 0x00,u32CmdTimeOut); |
sahilmgandhi | 18:6a4db94011d3 | 398 | _sd_uR3_CMD = 1; |
sahilmgandhi | 18:6a4db94011d3 | 399 | SD_SDCmdAndRsp(pSD, 41, 0x00ff8000, u32CmdTimeOut); // 3.0v-3.4v |
sahilmgandhi | 18:6a4db94011d3 | 400 | resp = SD->RESP0; |
sahilmgandhi | 18:6a4db94011d3 | 401 | } |
sahilmgandhi | 18:6a4db94011d3 | 402 | pSD->CardType = SD_TYPE_SD_LOW; |
sahilmgandhi | 18:6a4db94011d3 | 403 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 404 | pSD->CardType = SD_TYPE_UNKNOWN; |
sahilmgandhi | 18:6a4db94011d3 | 405 | return SD_INIT_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 406 | } |
sahilmgandhi | 18:6a4db94011d3 | 407 | } |
sahilmgandhi | 18:6a4db94011d3 | 408 | |
sahilmgandhi | 18:6a4db94011d3 | 409 | // CMD2, CMD3 |
sahilmgandhi | 18:6a4db94011d3 | 410 | if (pSD->CardType != SD_TYPE_UNKNOWN) { |
sahilmgandhi | 18:6a4db94011d3 | 411 | SD_SDCmdAndRsp2(pSD, 2, 0x00, CIDBuffer); |
sahilmgandhi | 18:6a4db94011d3 | 412 | if ((pSD->CardType == SD_TYPE_MMC) || (pSD->CardType == SD_TYPE_EMMC)) { |
sahilmgandhi | 18:6a4db94011d3 | 413 | if ((status = SD_SDCmdAndRsp(pSD, 3, 0x10000, 0)) != Successful) // set RCA |
sahilmgandhi | 18:6a4db94011d3 | 414 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 415 | pSD->RCA = 0x10000; |
sahilmgandhi | 18:6a4db94011d3 | 416 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 417 | if ((status = SD_SDCmdAndRsp(pSD, 3, 0x00, 0)) != Successful) // get RCA |
sahilmgandhi | 18:6a4db94011d3 | 418 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 419 | else |
sahilmgandhi | 18:6a4db94011d3 | 420 | pSD->RCA = (SD->RESP0 << 8) & 0xffff0000; |
sahilmgandhi | 18:6a4db94011d3 | 421 | } |
sahilmgandhi | 18:6a4db94011d3 | 422 | } |
sahilmgandhi | 18:6a4db94011d3 | 423 | |
sahilmgandhi | 18:6a4db94011d3 | 424 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 425 | } |
sahilmgandhi | 18:6a4db94011d3 | 426 | |
sahilmgandhi | 18:6a4db94011d3 | 427 | |
sahilmgandhi | 18:6a4db94011d3 | 428 | int SD_SwitchToHighSpeed(SD_INFO_T *pSD) |
sahilmgandhi | 18:6a4db94011d3 | 429 | { |
sahilmgandhi | 18:6a4db94011d3 | 430 | int volatile status=0; |
sahilmgandhi | 18:6a4db94011d3 | 431 | uint16_t current_comsumption, busy_status0; |
sahilmgandhi | 18:6a4db94011d3 | 432 | |
sahilmgandhi | 18:6a4db94011d3 | 433 | SD->DMASA = (uint32_t)_sd_pSDHCBuffer; // set DMA transfer starting address |
sahilmgandhi | 18:6a4db94011d3 | 434 | SD->BLEN = 63; // 512 bit |
sahilmgandhi | 18:6a4db94011d3 | 435 | |
sahilmgandhi | 18:6a4db94011d3 | 436 | if ((status = SD_SDCmdAndRspDataIn(pSD, 6, 0x00ffff01)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 437 | return Fail; |
sahilmgandhi | 18:6a4db94011d3 | 438 | |
sahilmgandhi | 18:6a4db94011d3 | 439 | current_comsumption = _sd_pSDHCBuffer[0]<<8 | _sd_pSDHCBuffer[1]; |
sahilmgandhi | 18:6a4db94011d3 | 440 | if (!current_comsumption) |
sahilmgandhi | 18:6a4db94011d3 | 441 | return Fail; |
sahilmgandhi | 18:6a4db94011d3 | 442 | |
sahilmgandhi | 18:6a4db94011d3 | 443 | busy_status0 = _sd_pSDHCBuffer[28]<<8 | _sd_pSDHCBuffer[29]; |
sahilmgandhi | 18:6a4db94011d3 | 444 | |
sahilmgandhi | 18:6a4db94011d3 | 445 | if (!busy_status0) { // function ready |
sahilmgandhi | 18:6a4db94011d3 | 446 | SD->DMASA = (uint32_t)_sd_pSDHCBuffer; // set DMA transfer starting address |
sahilmgandhi | 18:6a4db94011d3 | 447 | SD->BLEN = 63; // 512 bit |
sahilmgandhi | 18:6a4db94011d3 | 448 | |
sahilmgandhi | 18:6a4db94011d3 | 449 | if ((status = SD_SDCmdAndRspDataIn(pSD, 6, 0x80ffff01)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 450 | return Fail; |
sahilmgandhi | 18:6a4db94011d3 | 451 | |
sahilmgandhi | 18:6a4db94011d3 | 452 | // function change timing: 8 clocks |
sahilmgandhi | 18:6a4db94011d3 | 453 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 454 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 455 | |
sahilmgandhi | 18:6a4db94011d3 | 456 | current_comsumption = _sd_pSDHCBuffer[0]<<8 | _sd_pSDHCBuffer[1]; |
sahilmgandhi | 18:6a4db94011d3 | 457 | if (!current_comsumption) |
sahilmgandhi | 18:6a4db94011d3 | 458 | return Fail; |
sahilmgandhi | 18:6a4db94011d3 | 459 | |
sahilmgandhi | 18:6a4db94011d3 | 460 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 461 | } else |
sahilmgandhi | 18:6a4db94011d3 | 462 | return Fail; |
sahilmgandhi | 18:6a4db94011d3 | 463 | } |
sahilmgandhi | 18:6a4db94011d3 | 464 | |
sahilmgandhi | 18:6a4db94011d3 | 465 | |
sahilmgandhi | 18:6a4db94011d3 | 466 | int SD_SelectCardType(SD_INFO_T *pSD) |
sahilmgandhi | 18:6a4db94011d3 | 467 | { |
sahilmgandhi | 18:6a4db94011d3 | 468 | int volatile status=0; |
sahilmgandhi | 18:6a4db94011d3 | 469 | unsigned int arg; |
sahilmgandhi | 18:6a4db94011d3 | 470 | |
sahilmgandhi | 18:6a4db94011d3 | 471 | if ((status = SD_SDCmdAndRsp(pSD, 7, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 472 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 473 | |
sahilmgandhi | 18:6a4db94011d3 | 474 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 475 | |
sahilmgandhi | 18:6a4db94011d3 | 476 | // if SD card set 4bit |
sahilmgandhi | 18:6a4db94011d3 | 477 | if (pSD->CardType == SD_TYPE_SD_HIGH) { |
sahilmgandhi | 18:6a4db94011d3 | 478 | _sd_pSDHCBuffer = (uint8_t *)((uint32_t)_sd_ucSDHCBuffer); |
sahilmgandhi | 18:6a4db94011d3 | 479 | SD->DMASA = (uint32_t)_sd_pSDHCBuffer; // set DMA transfer starting address |
sahilmgandhi | 18:6a4db94011d3 | 480 | SD->BLEN = 0x07; // 64 bit |
sahilmgandhi | 18:6a4db94011d3 | 481 | |
sahilmgandhi | 18:6a4db94011d3 | 482 | if ((status = SD_SDCmdAndRsp(pSD, 55, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 483 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 484 | if ((status = SD_SDCmdAndRspDataIn(pSD, 51, 0x00)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 485 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 486 | |
sahilmgandhi | 18:6a4db94011d3 | 487 | if ((_sd_ucSDHCBuffer[0] & 0xf) == 0x2) { |
sahilmgandhi | 18:6a4db94011d3 | 488 | status = SD_SwitchToHighSpeed(pSD); |
sahilmgandhi | 18:6a4db94011d3 | 489 | if (status == Successful) { |
sahilmgandhi | 18:6a4db94011d3 | 490 | /* divider */ |
sahilmgandhi | 18:6a4db94011d3 | 491 | SD_Set_clock(SDHC_FREQ); |
sahilmgandhi | 18:6a4db94011d3 | 492 | } |
sahilmgandhi | 18:6a4db94011d3 | 493 | } |
sahilmgandhi | 18:6a4db94011d3 | 494 | |
sahilmgandhi | 18:6a4db94011d3 | 495 | if ((status = SD_SDCmdAndRsp(pSD, 55, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 496 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 497 | if ((status = SD_SDCmdAndRsp(pSD, 6, 0x02, 0)) != Successful) // set bus width |
sahilmgandhi | 18:6a4db94011d3 | 498 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 499 | |
sahilmgandhi | 18:6a4db94011d3 | 500 | SD->CTL |= SDH_CTL_DBW_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 501 | } else if (pSD->CardType == SD_TYPE_SD_LOW) { |
sahilmgandhi | 18:6a4db94011d3 | 502 | _sd_pSDHCBuffer = (uint8_t *)((uint32_t)_sd_ucSDHCBuffer); |
sahilmgandhi | 18:6a4db94011d3 | 503 | SD->DMASA = (uint32_t) _sd_pSDHCBuffer; // set DMA transfer starting address |
sahilmgandhi | 18:6a4db94011d3 | 504 | SD->BLEN = 0x07; // 64 bit |
sahilmgandhi | 18:6a4db94011d3 | 505 | |
sahilmgandhi | 18:6a4db94011d3 | 506 | if ((status = SD_SDCmdAndRsp(pSD, 55, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 507 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 508 | if ((status = SD_SDCmdAndRspDataIn(pSD, 51, 0x00)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 509 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 510 | |
sahilmgandhi | 18:6a4db94011d3 | 511 | // set data bus width. ACMD6 for SD card, SDCR_DBW for host. |
sahilmgandhi | 18:6a4db94011d3 | 512 | if ((status = SD_SDCmdAndRsp(pSD, 55, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 513 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 514 | |
sahilmgandhi | 18:6a4db94011d3 | 515 | if ((status = SD_SDCmdAndRsp(pSD, 6, 0x02, 0)) != Successful) // set bus width |
sahilmgandhi | 18:6a4db94011d3 | 516 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 517 | |
sahilmgandhi | 18:6a4db94011d3 | 518 | SD->CTL |= SDH_CTL_DBW_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 519 | } else if ((pSD->CardType == SD_TYPE_MMC) ||(pSD->CardType == SD_TYPE_EMMC)) { |
sahilmgandhi | 18:6a4db94011d3 | 520 | |
sahilmgandhi | 18:6a4db94011d3 | 521 | if(pSD->CardType == SD_TYPE_MMC) |
sahilmgandhi | 18:6a4db94011d3 | 522 | SD->CTL &= ~SDH_CTL_DBW_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 523 | |
sahilmgandhi | 18:6a4db94011d3 | 524 | //--- sent CMD6 to MMC card to set bus width to 4 bits mode |
sahilmgandhi | 18:6a4db94011d3 | 525 | // set CMD6 argument Access field to 3, Index to 183, Value to 1 (4-bit mode) |
sahilmgandhi | 18:6a4db94011d3 | 526 | arg = (3 << 24) | (183 << 16) | (1 << 8); |
sahilmgandhi | 18:6a4db94011d3 | 527 | if ((status = SD_SDCmdAndRsp(pSD, 6, arg, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 528 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 529 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 530 | |
sahilmgandhi | 18:6a4db94011d3 | 531 | SD->CTL |= SDH_CTL_DBW_Msk;; // set bus width to 4-bit mode for SD host controller |
sahilmgandhi | 18:6a4db94011d3 | 532 | |
sahilmgandhi | 18:6a4db94011d3 | 533 | } |
sahilmgandhi | 18:6a4db94011d3 | 534 | |
sahilmgandhi | 18:6a4db94011d3 | 535 | if ((status = SD_SDCmdAndRsp(pSD, 16, SD_BLOCK_SIZE, 0)) != Successful) // set block length |
sahilmgandhi | 18:6a4db94011d3 | 536 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 537 | SD->BLEN = SD_BLOCK_SIZE - 1; // set the block size |
sahilmgandhi | 18:6a4db94011d3 | 538 | |
sahilmgandhi | 18:6a4db94011d3 | 539 | SD_SDCommand(pSD, 7, 0); |
sahilmgandhi | 18:6a4db94011d3 | 540 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 541 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 542 | |
sahilmgandhi | 18:6a4db94011d3 | 543 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 544 | SD->INTEN |= SDH_INTEN_BLKDIEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 545 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 546 | |
sahilmgandhi | 18:6a4db94011d3 | 547 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 548 | } |
sahilmgandhi | 18:6a4db94011d3 | 549 | |
sahilmgandhi | 18:6a4db94011d3 | 550 | void SD_Get_SD_info(SD_INFO_T *pSD, DISK_DATA_T *_info) |
sahilmgandhi | 18:6a4db94011d3 | 551 | { |
sahilmgandhi | 18:6a4db94011d3 | 552 | unsigned int R_LEN, C_Size, MULT, size; |
sahilmgandhi | 18:6a4db94011d3 | 553 | unsigned int Buffer[4]; |
sahilmgandhi | 18:6a4db94011d3 | 554 | unsigned char *ptr; |
sahilmgandhi | 18:6a4db94011d3 | 555 | |
sahilmgandhi | 18:6a4db94011d3 | 556 | SD_SDCmdAndRsp2(pSD, 9, pSD->RCA, Buffer); |
sahilmgandhi | 18:6a4db94011d3 | 557 | |
sahilmgandhi | 18:6a4db94011d3 | 558 | if ((pSD->CardType == SD_TYPE_MMC) || (pSD->CardType == SD_TYPE_EMMC)) { |
sahilmgandhi | 18:6a4db94011d3 | 559 | // for MMC/eMMC card |
sahilmgandhi | 18:6a4db94011d3 | 560 | if ((Buffer[0] & 0xc0000000) == 0xc0000000) { |
sahilmgandhi | 18:6a4db94011d3 | 561 | // CSD_STRUCTURE [127:126] is 3 |
sahilmgandhi | 18:6a4db94011d3 | 562 | // CSD version depend on EXT_CSD register in eMMC v4.4 for card size > 2GB |
sahilmgandhi | 18:6a4db94011d3 | 563 | SD_SDCmdAndRsp(pSD, 7, pSD->RCA, 0); |
sahilmgandhi | 18:6a4db94011d3 | 564 | |
sahilmgandhi | 18:6a4db94011d3 | 565 | ptr = (uint8_t *)((uint32_t)_sd_ucSDHCBuffer ); |
sahilmgandhi | 18:6a4db94011d3 | 566 | SD->DMASA = (uint32_t)ptr; // set DMA transfer starting address |
sahilmgandhi | 18:6a4db94011d3 | 567 | SD->BLEN = 511; // read 512 bytes for EXT_CSD |
sahilmgandhi | 18:6a4db94011d3 | 568 | |
sahilmgandhi | 18:6a4db94011d3 | 569 | if (SD_SDCmdAndRspDataIn(pSD, 8, 0x00) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 570 | return; |
sahilmgandhi | 18:6a4db94011d3 | 571 | |
sahilmgandhi | 18:6a4db94011d3 | 572 | SD_SDCommand(pSD, 7, 0); |
sahilmgandhi | 18:6a4db94011d3 | 573 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 574 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 575 | |
sahilmgandhi | 18:6a4db94011d3 | 576 | _info->totalSectorN = (*(uint32_t *)(ptr+212)); |
sahilmgandhi | 18:6a4db94011d3 | 577 | _info->diskSize = _info->totalSectorN / 2; |
sahilmgandhi | 18:6a4db94011d3 | 578 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 579 | // CSD version v1.0/1.1/1.2 in eMMC v4.4 spec for card size <= 2GB |
sahilmgandhi | 18:6a4db94011d3 | 580 | R_LEN = (Buffer[1] & 0x000f0000) >> 16; |
sahilmgandhi | 18:6a4db94011d3 | 581 | C_Size = ((Buffer[1] & 0x000003ff) << 2) | ((Buffer[2] & 0xc0000000) >> 30); |
sahilmgandhi | 18:6a4db94011d3 | 582 | MULT = (Buffer[2] & 0x00038000) >> 15; |
sahilmgandhi | 18:6a4db94011d3 | 583 | size = (C_Size+1) * (1<<(MULT+2)) * (1<<R_LEN); |
sahilmgandhi | 18:6a4db94011d3 | 584 | |
sahilmgandhi | 18:6a4db94011d3 | 585 | _info->diskSize = size / 1024; |
sahilmgandhi | 18:6a4db94011d3 | 586 | _info->totalSectorN = size / 512; |
sahilmgandhi | 18:6a4db94011d3 | 587 | } |
sahilmgandhi | 18:6a4db94011d3 | 588 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 589 | if (Buffer[0] & 0xc0000000) { |
sahilmgandhi | 18:6a4db94011d3 | 590 | C_Size = ((Buffer[1] & 0x0000003f) << 16) | ((Buffer[2] & 0xffff0000) >> 16); |
sahilmgandhi | 18:6a4db94011d3 | 591 | size = (C_Size+1) * 512; // Kbytes |
sahilmgandhi | 18:6a4db94011d3 | 592 | |
sahilmgandhi | 18:6a4db94011d3 | 593 | _info->diskSize = size; |
sahilmgandhi | 18:6a4db94011d3 | 594 | _info->totalSectorN = size << 1; |
sahilmgandhi | 18:6a4db94011d3 | 595 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 596 | R_LEN = (Buffer[1] & 0x000f0000) >> 16; |
sahilmgandhi | 18:6a4db94011d3 | 597 | C_Size = ((Buffer[1] & 0x000003ff) << 2) | ((Buffer[2] & 0xc0000000) >> 30); |
sahilmgandhi | 18:6a4db94011d3 | 598 | MULT = (Buffer[2] & 0x00038000) >> 15; |
sahilmgandhi | 18:6a4db94011d3 | 599 | size = (C_Size+1) * (1<<(MULT+2)) * (1<<R_LEN); |
sahilmgandhi | 18:6a4db94011d3 | 600 | |
sahilmgandhi | 18:6a4db94011d3 | 601 | _info->diskSize = size / 1024; |
sahilmgandhi | 18:6a4db94011d3 | 602 | _info->totalSectorN = size / 512; |
sahilmgandhi | 18:6a4db94011d3 | 603 | } |
sahilmgandhi | 18:6a4db94011d3 | 604 | } |
sahilmgandhi | 18:6a4db94011d3 | 605 | |
sahilmgandhi | 18:6a4db94011d3 | 606 | _info->sectorSize = 512; |
sahilmgandhi | 18:6a4db94011d3 | 607 | } |
sahilmgandhi | 18:6a4db94011d3 | 608 | |
sahilmgandhi | 18:6a4db94011d3 | 609 | int SD_ChipErase(SD_INFO_T *pSD, DISK_DATA_T *_info) |
sahilmgandhi | 18:6a4db94011d3 | 610 | { |
sahilmgandhi | 18:6a4db94011d3 | 611 | int status=0; |
sahilmgandhi | 18:6a4db94011d3 | 612 | |
sahilmgandhi | 18:6a4db94011d3 | 613 | status = SD_SDCmdAndRsp(pSD, 32, 512, 6000); |
sahilmgandhi | 18:6a4db94011d3 | 614 | if (status < 0) { |
sahilmgandhi | 18:6a4db94011d3 | 615 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 616 | } |
sahilmgandhi | 18:6a4db94011d3 | 617 | status = SD_SDCmdAndRsp(pSD, 33, _info->totalSectorN*512, 6000); |
sahilmgandhi | 18:6a4db94011d3 | 618 | if (status < 0) { |
sahilmgandhi | 18:6a4db94011d3 | 619 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 620 | } |
sahilmgandhi | 18:6a4db94011d3 | 621 | status = SD_SDCmdAndRsp(pSD, 38, 0, 6000); |
sahilmgandhi | 18:6a4db94011d3 | 622 | if (status < 0) { |
sahilmgandhi | 18:6a4db94011d3 | 623 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 624 | } |
sahilmgandhi | 18:6a4db94011d3 | 625 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 626 | |
sahilmgandhi | 18:6a4db94011d3 | 627 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 628 | } |
sahilmgandhi | 18:6a4db94011d3 | 629 | |
sahilmgandhi | 18:6a4db94011d3 | 630 | /// @endcond HIDDEN_SYMBOLS |
sahilmgandhi | 18:6a4db94011d3 | 631 | |
sahilmgandhi | 18:6a4db94011d3 | 632 | |
sahilmgandhi | 18:6a4db94011d3 | 633 | /** |
sahilmgandhi | 18:6a4db94011d3 | 634 | * @brief This function use to reset SD function and select card detection source and pin. |
sahilmgandhi | 18:6a4db94011d3 | 635 | * |
sahilmgandhi | 18:6a4db94011d3 | 636 | * @param[in] u32CardDetSrc Select card detection source from SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1) \n |
sahilmgandhi | 18:6a4db94011d3 | 637 | * And also select card detection pin from GPIO or DAT3 pin. ( \ref CardDetect_From_GPIO / \ref CardDetect_From_DAT3) |
sahilmgandhi | 18:6a4db94011d3 | 638 | * |
sahilmgandhi | 18:6a4db94011d3 | 639 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 640 | */ |
sahilmgandhi | 18:6a4db94011d3 | 641 | void SD_Open(uint32_t u32CardDetSrc) |
sahilmgandhi | 18:6a4db94011d3 | 642 | { |
sahilmgandhi | 18:6a4db94011d3 | 643 | // Enable SD Card Host Controller operation. |
sahilmgandhi | 18:6a4db94011d3 | 644 | //CLK->AHBCLK |= CLK_AHBCLK_SDHCKEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 645 | |
sahilmgandhi | 18:6a4db94011d3 | 646 | // enable DMAC |
sahilmgandhi | 18:6a4db94011d3 | 647 | SD->DMACTL = SDH_DMACTL_DMARST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 648 | while(SD->DMACTL & SDH_DMACTL_DMARST_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 649 | |
sahilmgandhi | 18:6a4db94011d3 | 650 | SD->DMACTL = SDH_DMACTL_DMAEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 651 | |
sahilmgandhi | 18:6a4db94011d3 | 652 | //Reset FMI |
sahilmgandhi | 18:6a4db94011d3 | 653 | SD->GCTL = SDH_GCTL_GCTLRST_Msk; // Start reset FMI controller. |
sahilmgandhi | 18:6a4db94011d3 | 654 | while(SD->GCTL & SDH_GCTL_GCTLRST_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 655 | |
sahilmgandhi | 18:6a4db94011d3 | 656 | |
sahilmgandhi | 18:6a4db94011d3 | 657 | //#ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 658 | // NVIC_EnableIRQ(SD_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 659 | //#endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 660 | |
sahilmgandhi | 18:6a4db94011d3 | 661 | // enable SD |
sahilmgandhi | 18:6a4db94011d3 | 662 | SD->GCTL = SDH_GCTL_SDEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 663 | |
sahilmgandhi | 18:6a4db94011d3 | 664 | if(u32CardDetSrc & SD_PORT0) { |
sahilmgandhi | 18:6a4db94011d3 | 665 | SD->CTL |= (SD->CTL & ~SDH_CTL_SDPORT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 666 | |
sahilmgandhi | 18:6a4db94011d3 | 667 | if(u32CardDetSrc & CardDetect_From_DAT3) { |
sahilmgandhi | 18:6a4db94011d3 | 668 | SD->INTEN &= ~SDH_INTEN_CDSRC0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 669 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 670 | SD->INTEN |= SDH_INTEN_CDSRC0_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 671 | } |
sahilmgandhi | 18:6a4db94011d3 | 672 | } else if(u32CardDetSrc & SD_PORT1) { |
sahilmgandhi | 18:6a4db94011d3 | 673 | SD->CTL |= ((SD->CTL & ~SDH_CTL_SDPORT_Msk) | (1 << SDH_CTL_SDPORT_Pos)); |
sahilmgandhi | 18:6a4db94011d3 | 674 | |
sahilmgandhi | 18:6a4db94011d3 | 675 | if(u32CardDetSrc & CardDetect_From_DAT3) { |
sahilmgandhi | 18:6a4db94011d3 | 676 | SD->INTEN &= ~SDH_INTEN_CDSRC1_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 677 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 678 | SD->INTEN |= SDH_INTEN_CDSRC1_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 679 | } |
sahilmgandhi | 18:6a4db94011d3 | 680 | } |
sahilmgandhi | 18:6a4db94011d3 | 681 | |
sahilmgandhi | 18:6a4db94011d3 | 682 | SD->CTL |= SDH_CTL_CTLRST_Msk; // SD software reset |
sahilmgandhi | 18:6a4db94011d3 | 683 | while(SD->CTL & SDH_CTL_CTLRST_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 684 | |
sahilmgandhi | 18:6a4db94011d3 | 685 | SD->CTL &= ~((0xFF) | (SDH_CTL_CLKKEEP1_Msk)); // disable SD clock output |
sahilmgandhi | 18:6a4db94011d3 | 686 | |
sahilmgandhi | 18:6a4db94011d3 | 687 | if(u32CardDetSrc & SD_PORT0) { |
sahilmgandhi | 18:6a4db94011d3 | 688 | memset(&SD0, 0, sizeof(SD_INFO_T)); |
sahilmgandhi | 18:6a4db94011d3 | 689 | } else if(u32CardDetSrc & SD_PORT1) { |
sahilmgandhi | 18:6a4db94011d3 | 690 | memset(&SD1, 0, sizeof(SD_INFO_T)); |
sahilmgandhi | 18:6a4db94011d3 | 691 | } |
sahilmgandhi | 18:6a4db94011d3 | 692 | |
sahilmgandhi | 18:6a4db94011d3 | 693 | } |
sahilmgandhi | 18:6a4db94011d3 | 694 | |
sahilmgandhi | 18:6a4db94011d3 | 695 | /** |
sahilmgandhi | 18:6a4db94011d3 | 696 | * @brief This function use to initial SD card. |
sahilmgandhi | 18:6a4db94011d3 | 697 | * |
sahilmgandhi | 18:6a4db94011d3 | 698 | * @param[in] u32CardNum Select initial SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1) |
sahilmgandhi | 18:6a4db94011d3 | 699 | * |
sahilmgandhi | 18:6a4db94011d3 | 700 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 701 | */ |
sahilmgandhi | 18:6a4db94011d3 | 702 | void SD_Probe(uint32_t u32CardNum) |
sahilmgandhi | 18:6a4db94011d3 | 703 | { |
sahilmgandhi | 18:6a4db94011d3 | 704 | // Disable FMI/SD host interrupt |
sahilmgandhi | 18:6a4db94011d3 | 705 | SD->GINTEN = 0; |
sahilmgandhi | 18:6a4db94011d3 | 706 | |
sahilmgandhi | 18:6a4db94011d3 | 707 | SD->CTL &= ~SDH_CTL_SDNWR_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 708 | SD->CTL |= 0x09 << SDH_CTL_SDNWR_Pos; // set SDNWR = 9 |
sahilmgandhi | 18:6a4db94011d3 | 709 | SD->CTL &= ~SDH_CTL_BLKCNT_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 710 | SD->CTL |= 0x01 << SDH_CTL_BLKCNT_Pos; // set BLKCNT = 1 |
sahilmgandhi | 18:6a4db94011d3 | 711 | SD->CTL &= ~SDH_CTL_DBW_Msk; // SD 1-bit data bus |
sahilmgandhi | 18:6a4db94011d3 | 712 | |
sahilmgandhi | 18:6a4db94011d3 | 713 | if(!(SD_CardDetection(u32CardNum))) |
sahilmgandhi | 18:6a4db94011d3 | 714 | return; |
sahilmgandhi | 18:6a4db94011d3 | 715 | |
sahilmgandhi | 18:6a4db94011d3 | 716 | if (u32CardNum == SD_PORT0) { |
sahilmgandhi | 18:6a4db94011d3 | 717 | if (SD_Init(&SD0) < 0) |
sahilmgandhi | 18:6a4db94011d3 | 718 | return; |
sahilmgandhi | 18:6a4db94011d3 | 719 | |
sahilmgandhi | 18:6a4db94011d3 | 720 | /* divider */ |
sahilmgandhi | 18:6a4db94011d3 | 721 | if (SD0.CardType == SD_TYPE_MMC) |
sahilmgandhi | 18:6a4db94011d3 | 722 | SD_Set_clock(20000); |
sahilmgandhi | 18:6a4db94011d3 | 723 | else |
sahilmgandhi | 18:6a4db94011d3 | 724 | SD_Set_clock(SD_FREQ); |
sahilmgandhi | 18:6a4db94011d3 | 725 | |
sahilmgandhi | 18:6a4db94011d3 | 726 | SD_Get_SD_info(&SD0, &SD_DiskInfo0); |
sahilmgandhi | 18:6a4db94011d3 | 727 | |
sahilmgandhi | 18:6a4db94011d3 | 728 | if (SD_SelectCardType(&SD0)) |
sahilmgandhi | 18:6a4db94011d3 | 729 | return; |
sahilmgandhi | 18:6a4db94011d3 | 730 | |
sahilmgandhi | 18:6a4db94011d3 | 731 | sd0_ok = 1; |
sahilmgandhi | 18:6a4db94011d3 | 732 | } else if (u32CardNum == SD_PORT1) { |
sahilmgandhi | 18:6a4db94011d3 | 733 | if (SD_Init(&SD1) < 0) |
sahilmgandhi | 18:6a4db94011d3 | 734 | return; |
sahilmgandhi | 18:6a4db94011d3 | 735 | |
sahilmgandhi | 18:6a4db94011d3 | 736 | /* divider */ |
sahilmgandhi | 18:6a4db94011d3 | 737 | if (SD1.CardType == SD_TYPE_MMC) |
sahilmgandhi | 18:6a4db94011d3 | 738 | SD_Set_clock(20000); |
sahilmgandhi | 18:6a4db94011d3 | 739 | else |
sahilmgandhi | 18:6a4db94011d3 | 740 | SD_Set_clock(SD_FREQ); |
sahilmgandhi | 18:6a4db94011d3 | 741 | |
sahilmgandhi | 18:6a4db94011d3 | 742 | SD_Get_SD_info(&SD1, &SD_DiskInfo1); |
sahilmgandhi | 18:6a4db94011d3 | 743 | |
sahilmgandhi | 18:6a4db94011d3 | 744 | if (SD_SelectCardType(&SD1)) |
sahilmgandhi | 18:6a4db94011d3 | 745 | return; |
sahilmgandhi | 18:6a4db94011d3 | 746 | |
sahilmgandhi | 18:6a4db94011d3 | 747 | sd1_ok = 1; |
sahilmgandhi | 18:6a4db94011d3 | 748 | } |
sahilmgandhi | 18:6a4db94011d3 | 749 | |
sahilmgandhi | 18:6a4db94011d3 | 750 | |
sahilmgandhi | 18:6a4db94011d3 | 751 | } |
sahilmgandhi | 18:6a4db94011d3 | 752 | |
sahilmgandhi | 18:6a4db94011d3 | 753 | /** |
sahilmgandhi | 18:6a4db94011d3 | 754 | * @brief This function use to read data from SD card. |
sahilmgandhi | 18:6a4db94011d3 | 755 | * |
sahilmgandhi | 18:6a4db94011d3 | 756 | * @param[in] u32CardNum Select card: SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1) |
sahilmgandhi | 18:6a4db94011d3 | 757 | * @param[out] pu8BufAddr The buffer to receive the data from SD card. |
sahilmgandhi | 18:6a4db94011d3 | 758 | * @param[in] u32StartSec The start read sector address. |
sahilmgandhi | 18:6a4db94011d3 | 759 | * @param[in] u32SecCount The the read sector number of data |
sahilmgandhi | 18:6a4db94011d3 | 760 | * |
sahilmgandhi | 18:6a4db94011d3 | 761 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 762 | */ |
sahilmgandhi | 18:6a4db94011d3 | 763 | uint32_t SD_Read(uint32_t u32CardNum, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount) |
sahilmgandhi | 18:6a4db94011d3 | 764 | { |
sahilmgandhi | 18:6a4db94011d3 | 765 | char volatile bIsSendCmd = FALSE, buf; |
sahilmgandhi | 18:6a4db94011d3 | 766 | unsigned int volatile reg; |
sahilmgandhi | 18:6a4db94011d3 | 767 | int volatile i, loop, status; |
sahilmgandhi | 18:6a4db94011d3 | 768 | uint32_t blksize = SD_BLOCK_SIZE; |
sahilmgandhi | 18:6a4db94011d3 | 769 | |
sahilmgandhi | 18:6a4db94011d3 | 770 | SD_INFO_T *pSD; |
sahilmgandhi | 18:6a4db94011d3 | 771 | |
sahilmgandhi | 18:6a4db94011d3 | 772 | if(u32CardNum == SD_PORT0) |
sahilmgandhi | 18:6a4db94011d3 | 773 | pSD = &SD0; |
sahilmgandhi | 18:6a4db94011d3 | 774 | else |
sahilmgandhi | 18:6a4db94011d3 | 775 | pSD = &SD1; |
sahilmgandhi | 18:6a4db94011d3 | 776 | |
sahilmgandhi | 18:6a4db94011d3 | 777 | //--- check input parameters |
sahilmgandhi | 18:6a4db94011d3 | 778 | if (u32SecCount == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 779 | return SD_SELECT_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 780 | } |
sahilmgandhi | 18:6a4db94011d3 | 781 | |
sahilmgandhi | 18:6a4db94011d3 | 782 | if ((status = SD_SDCmdAndRsp(pSD, 7, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 783 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 784 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 785 | |
sahilmgandhi | 18:6a4db94011d3 | 786 | SD->BLEN = blksize - 1; // the actual byte count is equal to (SDBLEN+1) |
sahilmgandhi | 18:6a4db94011d3 | 787 | |
sahilmgandhi | 18:6a4db94011d3 | 788 | if ( (pSD->CardType == SD_TYPE_SD_HIGH) || (pSD->CardType == SD_TYPE_EMMC) ) |
sahilmgandhi | 18:6a4db94011d3 | 789 | SD->CMDARG = u32StartSec; |
sahilmgandhi | 18:6a4db94011d3 | 790 | else |
sahilmgandhi | 18:6a4db94011d3 | 791 | SD->CMDARG = u32StartSec * blksize; |
sahilmgandhi | 18:6a4db94011d3 | 792 | |
sahilmgandhi | 18:6a4db94011d3 | 793 | SD->DMASA = (uint32_t)pu8BufAddr; |
sahilmgandhi | 18:6a4db94011d3 | 794 | |
sahilmgandhi | 18:6a4db94011d3 | 795 | loop = u32SecCount / 255; |
sahilmgandhi | 18:6a4db94011d3 | 796 | for (i=0; i<loop; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 797 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 798 | _sd_SDDataReady = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 799 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 800 | |
sahilmgandhi | 18:6a4db94011d3 | 801 | reg = SD->CTL & ~SDH_CTL_CMDCODE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 802 | reg = reg | 0xff0000; // set BLK_CNT to 255 |
sahilmgandhi | 18:6a4db94011d3 | 803 | if (bIsSendCmd == FALSE) { |
sahilmgandhi | 18:6a4db94011d3 | 804 | SD->CTL = reg|(18<<8)|(SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk | SDH_CTL_DIEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 805 | bIsSendCmd = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 806 | } else |
sahilmgandhi | 18:6a4db94011d3 | 807 | SD->CTL = reg | SDH_CTL_DIEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 808 | |
sahilmgandhi | 18:6a4db94011d3 | 809 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 810 | while(!_sd_SDDataReady) |
sahilmgandhi | 18:6a4db94011d3 | 811 | #else |
sahilmgandhi | 18:6a4db94011d3 | 812 | while(1) |
sahilmgandhi | 18:6a4db94011d3 | 813 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 814 | { |
sahilmgandhi | 18:6a4db94011d3 | 815 | if(_sd_SDDataReady) break; |
sahilmgandhi | 18:6a4db94011d3 | 816 | |
sahilmgandhi | 18:6a4db94011d3 | 817 | #ifndef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 818 | if ((SD->INTSTS & SDH_INTSTS_BLKDIF_Msk) && (!(SD->CTL & SDH_CTL_DIEN_Msk))) { |
sahilmgandhi | 18:6a4db94011d3 | 819 | SD->INTSTS = SDH_INTSTS_BLKDIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 820 | break; |
sahilmgandhi | 18:6a4db94011d3 | 821 | } |
sahilmgandhi | 18:6a4db94011d3 | 822 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 823 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 824 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 825 | } |
sahilmgandhi | 18:6a4db94011d3 | 826 | |
sahilmgandhi | 18:6a4db94011d3 | 827 | if (!(SD->INTSTS & SDH_INTSTS_CRC7_Msk)) { // check CRC7 |
sahilmgandhi | 18:6a4db94011d3 | 828 | //printf("sdioSD_Read_in_blksize(): response error!\n"); |
sahilmgandhi | 18:6a4db94011d3 | 829 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 830 | } |
sahilmgandhi | 18:6a4db94011d3 | 831 | |
sahilmgandhi | 18:6a4db94011d3 | 832 | if (!(SD->INTSTS & SDH_INTSTS_CRC16_Msk)) { // check CRC16 |
sahilmgandhi | 18:6a4db94011d3 | 833 | //printf("sdioSD_Read_in_blksize() :read data error!\n"); |
sahilmgandhi | 18:6a4db94011d3 | 834 | return SD_CRC16_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 835 | } |
sahilmgandhi | 18:6a4db94011d3 | 836 | } |
sahilmgandhi | 18:6a4db94011d3 | 837 | |
sahilmgandhi | 18:6a4db94011d3 | 838 | loop = u32SecCount % 255; |
sahilmgandhi | 18:6a4db94011d3 | 839 | if (loop != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 840 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 841 | _sd_SDDataReady = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 842 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 843 | |
sahilmgandhi | 18:6a4db94011d3 | 844 | reg = SD->CTL & (~SDH_CTL_CMDCODE_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 845 | reg = reg & (~SDH_CTL_BLKCNT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 846 | reg |= (loop << 16); // setup SDCR_BLKCNT |
sahilmgandhi | 18:6a4db94011d3 | 847 | |
sahilmgandhi | 18:6a4db94011d3 | 848 | if (bIsSendCmd == FALSE) { |
sahilmgandhi | 18:6a4db94011d3 | 849 | SD->CTL = reg|(18<<8)|(SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk | SDH_CTL_DIEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 850 | bIsSendCmd = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 851 | } else |
sahilmgandhi | 18:6a4db94011d3 | 852 | SD->CTL = reg | SDH_CTL_DIEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 853 | |
sahilmgandhi | 18:6a4db94011d3 | 854 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 855 | while(!_sd_SDDataReady) |
sahilmgandhi | 18:6a4db94011d3 | 856 | #else |
sahilmgandhi | 18:6a4db94011d3 | 857 | while(1) |
sahilmgandhi | 18:6a4db94011d3 | 858 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 859 | { |
sahilmgandhi | 18:6a4db94011d3 | 860 | |
sahilmgandhi | 18:6a4db94011d3 | 861 | #ifndef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 862 | if ((SD->INTSTS & SDH_INTSTS_BLKDIF_Msk) && (!(SD->CTL & SDH_CTL_DIEN_Msk))) { |
sahilmgandhi | 18:6a4db94011d3 | 863 | SD->INTSTS = SDH_INTSTS_BLKDIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 864 | break; |
sahilmgandhi | 18:6a4db94011d3 | 865 | } |
sahilmgandhi | 18:6a4db94011d3 | 866 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 867 | |
sahilmgandhi | 18:6a4db94011d3 | 868 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 869 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 870 | } |
sahilmgandhi | 18:6a4db94011d3 | 871 | |
sahilmgandhi | 18:6a4db94011d3 | 872 | if (!(SD->INTSTS & SDH_INTSTS_CRC7_Msk)) { // check CRC7 |
sahilmgandhi | 18:6a4db94011d3 | 873 | //printf("sdioSD_Read_in_blksize(): response error!\n"); |
sahilmgandhi | 18:6a4db94011d3 | 874 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 875 | } |
sahilmgandhi | 18:6a4db94011d3 | 876 | |
sahilmgandhi | 18:6a4db94011d3 | 877 | if (!(SD->INTSTS & SDH_INTSTS_CRC16_Msk)) { // check CRC16 |
sahilmgandhi | 18:6a4db94011d3 | 878 | //printf("sdioSD_Read_in_blksize(): read data error!\n"); |
sahilmgandhi | 18:6a4db94011d3 | 879 | return SD_CRC16_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 880 | } |
sahilmgandhi | 18:6a4db94011d3 | 881 | } |
sahilmgandhi | 18:6a4db94011d3 | 882 | |
sahilmgandhi | 18:6a4db94011d3 | 883 | if (SD_SDCmdAndRsp(pSD, 12, 0, 0)) { // stop command |
sahilmgandhi | 18:6a4db94011d3 | 884 | //printf("stop command fail !!\n"); |
sahilmgandhi | 18:6a4db94011d3 | 885 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 886 | } |
sahilmgandhi | 18:6a4db94011d3 | 887 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 888 | |
sahilmgandhi | 18:6a4db94011d3 | 889 | SD_SDCommand(pSD, 7, 0); |
sahilmgandhi | 18:6a4db94011d3 | 890 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 891 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 892 | |
sahilmgandhi | 18:6a4db94011d3 | 893 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 894 | } |
sahilmgandhi | 18:6a4db94011d3 | 895 | |
sahilmgandhi | 18:6a4db94011d3 | 896 | |
sahilmgandhi | 18:6a4db94011d3 | 897 | /** |
sahilmgandhi | 18:6a4db94011d3 | 898 | * @brief This function use to write data to SD card. |
sahilmgandhi | 18:6a4db94011d3 | 899 | * |
sahilmgandhi | 18:6a4db94011d3 | 900 | * @param[in] u32CardNum Select card: SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1) |
sahilmgandhi | 18:6a4db94011d3 | 901 | * @param[in] pu8BufAddr The buffer to send the data to SD card. |
sahilmgandhi | 18:6a4db94011d3 | 902 | * @param[in] u32StartSec The start write sector address. |
sahilmgandhi | 18:6a4db94011d3 | 903 | * @param[in] u32SecCount The the write sector number of data. |
sahilmgandhi | 18:6a4db94011d3 | 904 | * |
sahilmgandhi | 18:6a4db94011d3 | 905 | * @return \ref SD_SELECT_ERROR : u32SecCount is zero. \n |
sahilmgandhi | 18:6a4db94011d3 | 906 | * \ref SD_NO_SD_CARD : SD card be removed. \n |
sahilmgandhi | 18:6a4db94011d3 | 907 | * \ref SD_CRC_ERROR : CRC error happen. \n |
sahilmgandhi | 18:6a4db94011d3 | 908 | * \ref SD_CRC7_ERROR : CRC7 error happen. \n |
sahilmgandhi | 18:6a4db94011d3 | 909 | * \ref Successful : Write data to SD card success. |
sahilmgandhi | 18:6a4db94011d3 | 910 | */ |
sahilmgandhi | 18:6a4db94011d3 | 911 | uint32_t SD_Write(uint32_t u32CardNum, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount) |
sahilmgandhi | 18:6a4db94011d3 | 912 | { |
sahilmgandhi | 18:6a4db94011d3 | 913 | char volatile bIsSendCmd = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 914 | unsigned int volatile reg; |
sahilmgandhi | 18:6a4db94011d3 | 915 | int volatile i, loop, status; |
sahilmgandhi | 18:6a4db94011d3 | 916 | |
sahilmgandhi | 18:6a4db94011d3 | 917 | SD_INFO_T *pSD; |
sahilmgandhi | 18:6a4db94011d3 | 918 | |
sahilmgandhi | 18:6a4db94011d3 | 919 | if(u32CardNum == SD_PORT0) |
sahilmgandhi | 18:6a4db94011d3 | 920 | pSD = &SD0; |
sahilmgandhi | 18:6a4db94011d3 | 921 | else |
sahilmgandhi | 18:6a4db94011d3 | 922 | pSD = &SD1; |
sahilmgandhi | 18:6a4db94011d3 | 923 | |
sahilmgandhi | 18:6a4db94011d3 | 924 | |
sahilmgandhi | 18:6a4db94011d3 | 925 | //--- check input parameters |
sahilmgandhi | 18:6a4db94011d3 | 926 | if (u32SecCount == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 927 | return SD_SELECT_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 928 | } |
sahilmgandhi | 18:6a4db94011d3 | 929 | |
sahilmgandhi | 18:6a4db94011d3 | 930 | if ((status = SD_SDCmdAndRsp(pSD, 7, pSD->RCA, 0)) != Successful) |
sahilmgandhi | 18:6a4db94011d3 | 931 | return status; |
sahilmgandhi | 18:6a4db94011d3 | 932 | |
sahilmgandhi | 18:6a4db94011d3 | 933 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 934 | |
sahilmgandhi | 18:6a4db94011d3 | 935 | // According to SD Spec v2.0, the write CMD block size MUST be 512, and the start address MUST be 512*n. |
sahilmgandhi | 18:6a4db94011d3 | 936 | SD->BLEN = SD_BLOCK_SIZE - 1; // set the block size |
sahilmgandhi | 18:6a4db94011d3 | 937 | |
sahilmgandhi | 18:6a4db94011d3 | 938 | if ((pSD->CardType == SD_TYPE_SD_HIGH) || (pSD->CardType == SD_TYPE_EMMC)) |
sahilmgandhi | 18:6a4db94011d3 | 939 | SD->CMDARG = u32StartSec; |
sahilmgandhi | 18:6a4db94011d3 | 940 | else |
sahilmgandhi | 18:6a4db94011d3 | 941 | SD->CMDARG = u32StartSec * SD_BLOCK_SIZE; // set start address for SD CMD |
sahilmgandhi | 18:6a4db94011d3 | 942 | |
sahilmgandhi | 18:6a4db94011d3 | 943 | SD->DMASA = (uint32_t)pu8BufAddr; |
sahilmgandhi | 18:6a4db94011d3 | 944 | loop = u32SecCount / 255; // the maximum block count is 0xFF=255 for register SDCR[BLK_CNT] |
sahilmgandhi | 18:6a4db94011d3 | 945 | for (i=0; i<loop; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 946 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 947 | _sd_SDDataReady = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 948 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 949 | |
sahilmgandhi | 18:6a4db94011d3 | 950 | reg = SD->CTL & 0xff00c080; |
sahilmgandhi | 18:6a4db94011d3 | 951 | reg = reg | 0xff0000; // set BLK_CNT to 0xFF=255 |
sahilmgandhi | 18:6a4db94011d3 | 952 | if (!bIsSendCmd) { |
sahilmgandhi | 18:6a4db94011d3 | 953 | SD->CTL = reg|(25<<8)|(SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk | SDH_CTL_DOEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 954 | bIsSendCmd = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 955 | } else |
sahilmgandhi | 18:6a4db94011d3 | 956 | SD->CTL = reg | SDH_CTL_DOEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 957 | |
sahilmgandhi | 18:6a4db94011d3 | 958 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 959 | while(!_sd_SDDataReady) |
sahilmgandhi | 18:6a4db94011d3 | 960 | #else |
sahilmgandhi | 18:6a4db94011d3 | 961 | while(1) |
sahilmgandhi | 18:6a4db94011d3 | 962 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 963 | { |
sahilmgandhi | 18:6a4db94011d3 | 964 | #ifndef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 965 | if ((SD->INTSTS & SDH_INTSTS_BLKDIF_Msk) && (!(SD->CTL & SDH_CTL_DOEN_Msk))) { |
sahilmgandhi | 18:6a4db94011d3 | 966 | SD->INTSTS = SDH_INTSTS_BLKDIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 967 | break; |
sahilmgandhi | 18:6a4db94011d3 | 968 | } |
sahilmgandhi | 18:6a4db94011d3 | 969 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 970 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 971 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 972 | } |
sahilmgandhi | 18:6a4db94011d3 | 973 | |
sahilmgandhi | 18:6a4db94011d3 | 974 | if ((SD->INTSTS & SDH_INTSTS_CRCIF_Msk) != 0) { // check CRC |
sahilmgandhi | 18:6a4db94011d3 | 975 | SD->INTSTS = SDH_INTSTS_CRCIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 976 | return SD_CRC_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 977 | } |
sahilmgandhi | 18:6a4db94011d3 | 978 | } |
sahilmgandhi | 18:6a4db94011d3 | 979 | |
sahilmgandhi | 18:6a4db94011d3 | 980 | loop = u32SecCount % 255; |
sahilmgandhi | 18:6a4db94011d3 | 981 | if (loop != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 982 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 983 | _sd_SDDataReady = FALSE; |
sahilmgandhi | 18:6a4db94011d3 | 984 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 985 | |
sahilmgandhi | 18:6a4db94011d3 | 986 | reg = (SD->CTL & 0xff00c080) | (loop << 16); |
sahilmgandhi | 18:6a4db94011d3 | 987 | if (!bIsSendCmd) { |
sahilmgandhi | 18:6a4db94011d3 | 988 | SD->CTL = reg|(25<<8)|(SDH_CTL_COEN_Msk | SDH_CTL_RIEN_Msk | SDH_CTL_DOEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 989 | bIsSendCmd = TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 990 | } else |
sahilmgandhi | 18:6a4db94011d3 | 991 | SD->CTL = reg | SDH_CTL_DOEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 992 | |
sahilmgandhi | 18:6a4db94011d3 | 993 | #ifdef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 994 | while(!_sd_SDDataReady) |
sahilmgandhi | 18:6a4db94011d3 | 995 | #else |
sahilmgandhi | 18:6a4db94011d3 | 996 | while(1) |
sahilmgandhi | 18:6a4db94011d3 | 997 | #endif //_SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 998 | { |
sahilmgandhi | 18:6a4db94011d3 | 999 | #ifndef _SD_USE_INT_ |
sahilmgandhi | 18:6a4db94011d3 | 1000 | if ((SD->INTSTS & SDH_INTSTS_BLKDIF_Msk) && (!(SD->CTL & SDH_CTL_DOEN_Msk))) { |
sahilmgandhi | 18:6a4db94011d3 | 1001 | SD->INTSTS = SDH_INTSTS_BLKDIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 1002 | break; |
sahilmgandhi | 18:6a4db94011d3 | 1003 | } |
sahilmgandhi | 18:6a4db94011d3 | 1004 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 1005 | if (pSD->IsCardInsert == FALSE) |
sahilmgandhi | 18:6a4db94011d3 | 1006 | return SD_NO_SD_CARD; |
sahilmgandhi | 18:6a4db94011d3 | 1007 | } |
sahilmgandhi | 18:6a4db94011d3 | 1008 | |
sahilmgandhi | 18:6a4db94011d3 | 1009 | if ((SD->INTSTS & SDH_INTSTS_CRCIF_Msk) != 0) { // check CRC |
sahilmgandhi | 18:6a4db94011d3 | 1010 | SD->INTSTS = SDH_INTSTS_CRCIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 1011 | return SD_CRC_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 1012 | } |
sahilmgandhi | 18:6a4db94011d3 | 1013 | } |
sahilmgandhi | 18:6a4db94011d3 | 1014 | SD->INTSTS = SDH_INTSTS_CRCIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 1015 | |
sahilmgandhi | 18:6a4db94011d3 | 1016 | if (SD_SDCmdAndRsp(pSD, 12, 0, 0)) { // stop command |
sahilmgandhi | 18:6a4db94011d3 | 1017 | return SD_CRC7_ERROR; |
sahilmgandhi | 18:6a4db94011d3 | 1018 | } |
sahilmgandhi | 18:6a4db94011d3 | 1019 | SD_CheckRB(); |
sahilmgandhi | 18:6a4db94011d3 | 1020 | |
sahilmgandhi | 18:6a4db94011d3 | 1021 | SD_SDCommand(pSD, 7, 0); |
sahilmgandhi | 18:6a4db94011d3 | 1022 | SD->CTL |= SDH_CTL_CLK8OEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 1023 | while(SD->CTL & SDH_CTL_CLK8OEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 1024 | |
sahilmgandhi | 18:6a4db94011d3 | 1025 | return Successful; |
sahilmgandhi | 18:6a4db94011d3 | 1026 | } |
sahilmgandhi | 18:6a4db94011d3 | 1027 | |
sahilmgandhi | 18:6a4db94011d3 | 1028 | |
sahilmgandhi | 18:6a4db94011d3 | 1029 | /*@}*/ /* end of group NUC472_442_SD_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 1030 | |
sahilmgandhi | 18:6a4db94011d3 | 1031 | /*@}*/ /* end of group NUC472_442_SD_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 1032 | |
sahilmgandhi | 18:6a4db94011d3 | 1033 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 1034 | |
sahilmgandhi | 18:6a4db94011d3 | 1035 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
sahilmgandhi | 18:6a4db94011d3 | 1036 | |
sahilmgandhi | 18:6a4db94011d3 | 1037 | |
sahilmgandhi | 18:6a4db94011d3 | 1038 | |
sahilmgandhi | 18:6a4db94011d3 | 1039 | |
sahilmgandhi | 18:6a4db94011d3 | 1040 | |
sahilmgandhi | 18:6a4db94011d3 | 1041 | |
sahilmgandhi | 18:6a4db94011d3 | 1042 | |
sahilmgandhi | 18:6a4db94011d3 | 1043 |