1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
| /*****************************************************************************
Function:
MEDIA_INFORMATION * MDD_SDSPI_MediaInitialize (void)
Summary:
Initializes the SD card.
Conditions:
The MDD_MediaInitialize function pointer must be pointing to this function.
Input:
None.
Return Values:
The function returns a pointer to the MEDIA_INFORMATION structure. The
errorCode member may contain the following values:
* MEDIA_NO_ERROR - The media initialized successfully
* MEDIA_CANNOT_INITIALIZE - Cannot initialize the media.
Side Effects:
None.
Description:
This function will send initialization commands to and SD card.
Remarks:
None.
***************************************************************************************/
MEDIA_INFORMATION * MDD_SDSPI_MediaInitialize(void)
{
WORD timeout;
MMC_RESPONSE response;
#if defined __C30__ || defined __C32__
WORD spiconvalue = 0x0003;
#endif
mediaInformation.errorCode = MEDIA_NO_ERROR;
mediaInformation.validityFlags.value = 0;
SD_CS = 1; //Initialize Chip Select line
//Media powers up in the open-drain mode and cannot handle a clock faster
//than 400kHz. Initialize SPI port to slower than 400kHz
#if defined __C30__ || defined __C32__
#ifdef __PIC32MX__
OpenSPI(SPI_START_CFG_1, SPI_START_CFG_2);
SPIBRG = SPICalutateBRG(GetPeripheralClock(), 400000);
#else
// Calculate the prescaler needed for the clock
timeout = GetSystemClock() / 400000;
// if timeout is less than 400k and greater than 100k use a 1:1 prescaler
if (timeout == 0)
{
OpenSPIM (MASTER_ENABLE_ON | PRI_PRESCAL_1_1 | SEC_PRESCAL_1_1);
}
else
{
while (timeout != 0)
{
if (timeout > 8)
{
spiconvalue--;
// round up
if ((timeout % 4) != 0)
timeout += 4;
timeout /= 4;
}
else
{
break;
}
}
timeout--;
OpenSPIM (MASTER_ENABLE_ON | spiconvalue | ((~(timeout << 2)) & 0x1C));
}
#endif
// let the card power on and initialize
Delayms(1);
//Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
for(timeout=0; timeout<10; timeout++)
mSend8ClkCycles();
SD_CS = 0;
Delayms(1);
// Send CMD0 to reset the media
response = SendMMCCmd(GO_IDLE_STATE,0x0);
if((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
{
SD_CS = 1; // deselect the devices
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
return &mediaInformation;
}
response = SendMMCCmd(SEND_IF_COND, 0x1AA);
if (((response.r7.bytewise._returnVal & 0xFFF) == 0x1AA) && (!response.r7.bitwise.bits.ILLEGAL_CMD))
{
timeout = 0xFFF;
do
{
response = SendMMCCmd(SEND_OP_COND, 0x40000000);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
response = SendMMCCmd(READ_OCR, 0x0);
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0x80000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_NORMAL;
}
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0xC0000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_;
}
}
else
{
gSDMode = SD_MODE_NORMAL;
// According to spec cmd1 must be repeated until the card is fully initialized
timeout = 0xFFF;
do
{
response = SendMMCCmd(SEND_OP_COND,0x0);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
}
// see if it failed
if(timeout == 0)
{
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
SD_CS = 1; // deselect the devices
}
else
{
#else
// let the card power on and initialize
Delayms(1);
#if (GetSystemClock() < 25600000)
#if (GetSystemClock() < 1600000)
OpenSPIM (SYNC_MODE_FAST);
#elif (GetSystemClock() < 6400000)
OpenSPIM (SYNC_MODE_MED);
#else
OpenSPIM (SYNC_MODE_SLOW);
#endif
// let the card power on and initialize
Delayms(1);
//Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
for(timeout=0; timeout<10; timeout++)
mSend8ClkCycles();
SD_CS = 0;
Delayms(1);
// Send CMD0 to reset the media
response = SendMMCCmd(GO_IDLE_STATE,0x0);
if((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
{
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
SD_CS = 1; // deselect the devices
return &mediaInformation;
}
response = SendMMCCmd(SEND_IF_COND, 0x1AA);
if (((response.r7.bytewise._returnVal & 0xFFF) == 0x1AA) && (!response.r7.bitwise.bits.ILLEGAL_CMD))
{
timeout = 0xFFF;
do
{
response = SendMMCCmd(SEND_OP_COND, 0x40000000);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
response = SendMMCCmd(READ_OCR, 0x0);
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0x80000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_NORMAL;
}
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0xC0000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_HC;
}
}
else
{
gSDMode = SD_MODE_NORMAL;
// According to spec cmd1 must be repeated until the card is fully initialized
timeout = 0xFFF;
do
{
response = SendMMCCmd(SEND_OP_COND,0x0);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
}
#else
// Make sure the SPI module doesn't control the bus
SPICON1 = 0x00;
//Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
for(timeout=0; timeout<10; timeout++)
WriteSPIManual(0xFF);
SD_CS = 0;
Delayms(1);
// Send CMD0 to reset the media
response = SendMMCCmdManual (GO_IDLE_STATE, 0x0);
if ((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
{
SD_CS = 1; // deselect the devices
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
return &mediaInformation;
}
response = SendMMCCmdManual(SEND_IF_COND, 0x1AA);
if (((response.r7.bytewise._returnVal & 0xFFF) == 0x1AA) && (!response.r7.bitwise.bits.ILLEGAL_CMD))
{
timeout = 0xFFF;
do
{
response = SendMMCCmdManual(SEND_OP_COND, 0x40000000);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
response = SendMMCCmdManual(READ_OCR, 0x0);
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0x80000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_NORMAL;
}
if (((response.r7.bytewise._returnVal & 0xC0000000) == 0xC0000000) && (response.r7.bytewise._byte == 0))
{
gSDMode = SD_MODE_HC;
}
}
else
{
gSDMode = SD_MODE_NORMAL;
// According to the spec cmd1 must be repeated until the card is fully initialized
timeout = 0xFFF;
do
{
response = SendMMCCmdManual (SEND_OP_COND, 0x0);
timeout--;
}while(response.r1._byte != 0x00 && timeout != 0);
}
#endif
// see if it failed
if (timeout == 0)
{
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
SD_CS = 1; // deselect the devices
}
else
{
#endif
Delayms (2);
#ifdef __PIC32MX__
#if (GetSystemClock() <= 20000000)
SPIBRG = SPICalutateBRG(GetPeripheralClock(), 10000);
#else
SPIBRG = SPICalutateBRG(GetPeripheralClock(), SPI_FREQUENCY);
#endif
#else
OpenSPIM(SYNC_MODE_FAST);
#endif
// Turn off CRC7 if we can, might be an invalid cmd on some cards (CMD59)
response = SendMMCCmd(CRC_ON_OFF,0x0);
// Now set the block length to media sector size. It should be already
response = SendMMCCmd(SET_BLOCKLEN,MEDIA_SECTOR_SIZE);
for(timeout = 0xFF; timeout > 0 && MDD_SDSPI_SectorRead(0x0,NULL) != TRUE; timeout--)
{;}
// see if we had an issue
if(timeout == 0)
{
mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
SD_CS = 1; // deselect the devices
}
}
return &mediaInformation;
}
}//end MediaInitialize |
Partager