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

Method write

source/devices/memory/SdCardSpiBased.cpp:1363–1433  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1361}
1362
1363int SdCardSpiBased::write(const uint64_t address, const void* const buffer, const size_t size)
1364{
1365 const std::lock_guard<Mutex> lockGuard {mutex_};
1366
1367 assert(openCount_ != 0);
1368 assert(buffer != nullptr && address % blockSize == 0 && size % blockSize == 0);
1369
1370 const auto firstBlock = address / blockSize;
1371 const auto blocks = size / blockSize;
1372 assert(firstBlock + blocks <= blocksCount_);
1373
1374 if (size == 0)
1375 return {};
1376
1377 const SpiMasterHandle spiMasterHandle {spiMaster_};
1378 spiMasterHandle.configure(SpiMode::_0, clockFrequency_, 8, false, UINT32_MAX);
1379
1380 if (blocks != 1)
1381 {
1382 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1383
1384 const auto ret = executeAcmd23(spiMasterHandle, blocks);
1385 if (ret.first != 0)
1386 return ret.first;
1387 if (ret.second != 0)
1388 return EIO;
1389 }
1390
1391 const SelectGuard selectGuard {slaveSelectPin_ ,spiMasterHandle};
1392
1393 {
1394 const auto commandAddress = blockAddressing_ == true ? firstBlock : address;
1395 const auto ret = blocks == 1 ? executeCmd24(spiMasterHandle, commandAddress) :
1396 executeCmd25(spiMasterHandle, commandAddress);
1397 if (ret.first != 0)
1398 return ret.first;
1399 if (ret.second != 0)
1400 return EIO;
1401 }
1402
1403 const auto bufferUint8 = static_cast<const uint8_t*>(buffer);
1404 for (size_t block {}; block < blocks; ++block)
1405 {
1406 const auto ret = writeDataBlock(spiMasterHandle, blocks == 1 ? startBlockToken : startBlockWriteToken,
1407 bufferUint8 + block * blockSize, blockSize, std::chrono::milliseconds{writeTimeoutMs_});
1408 if (ret != 0)
1409 return ret;
1410 }
1411
1412 if (blocks != 1)
1413 {
1414 {
1415 static const uint8_t stopTransfer[]
1416 {
1417 stopTranToken,
1418 0xff,
1419 };
1420 static const SpiMasterTransfer transfer {stopTransfer, nullptr, sizeof(stopTransfer)};

Callers

nothing calls this directly

Calls 7

writeDataBlockFunction · 0.85
waitWhileBusyFunction · 0.85
executeAcmd23Function · 0.70
executeCmd24Function · 0.70
executeCmd25Function · 0.70
configureMethod · 0.45
executeTransactionMethod · 0.45

Tested by

no test coverage detected