MCPcopy Create free account
hub / github.com/DISTORTEC/distortos / initialize

Method initialize

source/devices/memory/SdCardSpiBased.cpp:1449–1603  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1447}
1448
1449int SdCardSpiBased::initialize()
1450{
1451 const SpiMasterHandle spiMasterHandle {spiMaster_};
1452 spiMasterHandle.configure(SpiMode::_0, 400000, 8, false, UINT32_MAX);
1453
1454 {
1455 static const SpiMasterTransfer transfer {nullptr, nullptr, (74 + CHAR_BIT - 1) / CHAR_BIT};
1456 const auto ret = spiMasterHandle.executeTransaction(SpiMasterTransfersRange{transfer});
1457 if (ret != 0)
1458 return ret;
1459 }
1460 {
1461 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1462
1463 // 3500 milliseconds - max time of single AU partial erase
1464 const auto ret = waitWhileBusy(spiMasterHandle, std::chrono::milliseconds{3500});
1465 if (ret != 0)
1466 return ret;
1467 }
1468 {
1469 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1470
1471 const auto ret = executeCmd0(spiMasterHandle);
1472 if (ret.first != 0)
1473 return ret.first;
1474 if (ret.second != r1InIdleStateMask)
1475 return EIO;
1476 }
1477 {
1478 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1479
1480 const auto ret = executeCmd8(spiMasterHandle);
1481 if (std::get<0>(ret) != 0)
1482 return std::get<0>(ret);
1483 if (std::get<1>(ret) != r1InIdleStateMask)
1484 return EIO;
1485 if (std::get<2>(ret) == false)
1486 return EIO; // voltage range not supported
1487 }
1488 {
1489 const auto deadline = TickClock::now() + std::chrono::seconds{1};
1490 while (1)
1491 {
1492 {
1493 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1494
1495 const auto ret = executeAcmd41(spiMasterHandle, true);
1496 if (ret.first != 0)
1497 return ret.first;
1498 if (ret.second == 0)
1499 break;
1500 if (ret.second != r1InIdleStateMask)
1501 return EIO;
1502 if (TickClock::now() >= deadline)
1503 return ETIMEDOUT;
1504 }
1505
1506 ThisThread::sleepFor({});

Callers

nothing calls this directly

Calls 13

waitWhileBusyFunction · 0.85
executeCmd58Function · 0.85
decodeCsdFunction · 0.85
decodeSdStatusFunction · 0.85
executeCmd0Function · 0.70
executeCmd8Function · 0.70
executeAcmd41Function · 0.70
executeCmd16Function · 0.70
executeCmd9Function · 0.70
executeAcmd13Function · 0.70
sleepForFunction · 0.50
configureMethod · 0.45

Tested by

no test coverage detected