MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_msd_write

Function rt_msd_write

components/drivers/spi/dev_spi_msd.c:1364–1482  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1362}
1363
1364static rt_ssize_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
1365{
1366 struct msd_device *msd = (struct msd_device *)dev;
1367 uint8_t response[MSD_RESPONSE_MAX_LEN];
1368 rt_err_t result;
1369
1370 result = MSD_take_owner(msd->spi_device);
1371
1372 if (result != RT_EOK)
1373 {
1374 MSD_DEBUG("[err] get SPI owner fail!\r\n");
1375 goto _exit;
1376 }
1377
1378
1379 /* SINGLE_BLOCK? */
1380 if (size == 1)
1381 {
1382 rt_spi_take(msd->spi_device);
1383 result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1384 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1385 {
1386 MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
1387 size = 0;
1388 goto _exit;
1389 }
1390
1391 result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
1392 if (result != RT_EOK)
1393 {
1394 MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
1395 size = 0;
1396 }
1397 }
1398 else if (size > 1)
1399 {
1400 struct rt_spi_message message;
1401 uint32_t i;
1402
1403 rt_spi_take(msd->spi_device);
1404
1405#ifdef MSD_USE_PRE_ERASED
1406 if (msd->card_type != MSD_CARD_TYPE_MMC)
1407 {
1408 /* CMD55 APP_CMD */
1409 result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
1410 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1411 {
1412 MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
1413 size = 0;
1414 goto _exit;
1415 }
1416
1417 /* ACMD23 Pre-erased */
1418 result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
1419 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1420 {
1421 MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");

Callers

nothing calls this directly

Calls 8

MSD_take_ownerFunction · 0.85
rt_spi_takeFunction · 0.85
_send_cmdFunction · 0.85
_write_blockFunction · 0.85
rt_memsetFunction · 0.85
_wait_readyFunction · 0.85
rt_spi_releaseFunction · 0.85
rt_mutex_releaseFunction · 0.85

Tested by

no test coverage detected