| 340 | /*-----------------------------------------------------------------------*/ |
| 341 | |
| 342 | DSTATUS disk_initialize(BYTE drv /* Physical drive nmuber (0) */ |
| 343 | ) |
| 344 | { |
| 345 | if(drv != 0) { |
| 346 | return RES_NOTRDY; |
| 347 | } |
| 348 | |
| 349 | SPISettings initSettings(spiInitFreq, MSBFIRST, SPI_MODE0); |
| 350 | SDCardSPI->beginTransaction(initSettings); |
| 351 | |
| 352 | dly_us(10000); |
| 353 | |
| 354 | debug_i("disk_initialize: send 80 0xFF cycles"); |
| 355 | BYTE tmp[80 / 8]; |
| 356 | memset(tmp, 0xff, sizeof(tmp)); |
| 357 | SDCardSPI->transfer(tmp, sizeof(tmp)); |
| 358 | |
| 359 | // debug_i("disk_initialize: send n send_cmd(CMD0, 0)"); |
| 360 | BYTE retCmd; |
| 361 | BYTE n = 5; |
| 362 | do { |
| 363 | retCmd = send_cmd(CMD0, 0); |
| 364 | n--; |
| 365 | } while(n && retCmd != 1); |
| 366 | debug_i("disk_initialize: until n = 5 && ret != 1"); |
| 367 | |
| 368 | BYTE ty = 0; |
| 369 | if(retCmd == 1) { |
| 370 | debug_i("disk_initialize: Enter Idle state, send_cmd(CMD8, 0x1AA) == 1"); |
| 371 | /* Enter Idle state */ |
| 372 | if(send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */ |
| 373 | debug_i("[SDCard] Sdv2 ?"); |
| 374 | BYTE buf[4]{0xff, 0xff, 0xff, 0xff}; |
| 375 | SDCardSPI->transfer(buf, sizeof(buf)); |
| 376 | debug_hex(INFO, "[SDCard]", buf, sizeof(buf)); |
| 377 | if(buf[2] == 0x01 && buf[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */ |
| 378 | UINT tmr; |
| 379 | for(tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state (ACMD41 with HCS bit) */ |
| 380 | if(send_cmd(ACMD41, 1UL << 30) == 0) { |
| 381 | debug_i("[SDCard] ACMD41 OK"); |
| 382 | break; |
| 383 | } |
| 384 | dly_us(1000); |
| 385 | } |
| 386 | if(tmr == 0) { |
| 387 | debug_i("[SDCard] ACMD41 FAIL"); |
| 388 | } |
| 389 | if(tmr != 0 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */ |
| 390 | // SDCardSPI->setMOSI(HIGH); /* Send 0xFF */ |
| 391 | // SDCardSPI->recv(buf, 4); |
| 392 | // ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */ |
| 393 | memset(buf, 0xFF, sizeof(buf)); |
| 394 | SDCardSPI->transfer(buf, sizeof(buf)); |
| 395 | ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */ |
| 396 | debug_hex(INFO, "[SDCard]", buf, sizeof(buf)); |
| 397 | } |
| 398 | } |
| 399 | } else { /* SDv1 or MMCv3 */ |
no test coverage detected