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

Function rt_msd_sdhc_write

components/drivers/spi/dev_spi_msd.c:1484–1596  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1482}
1483
1484static rt_ssize_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
1485{
1486 struct msd_device *msd = (struct msd_device *)dev;
1487 uint8_t response[MSD_RESPONSE_MAX_LEN];
1488 rt_err_t result;
1489
1490 result = MSD_take_owner(msd->spi_device);
1491
1492 if (result != RT_EOK)
1493 {
1494 goto _exit;
1495 }
1496
1497 /* SINGLE_BLOCK? */
1498 if (size == 1)
1499 {
1500 rt_spi_take(msd->spi_device);
1501 result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos, 0x00, response_r1, response);
1502 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1503 {
1504 MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
1505 size = 0;
1506 goto _exit;
1507 }
1508
1509 result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
1510 if (result != RT_EOK)
1511 {
1512 MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
1513 size = 0;
1514 }
1515 }
1516 else if (size > 1)
1517 {
1518 struct rt_spi_message message;
1519 uint32_t i;
1520
1521 rt_spi_take(msd->spi_device);
1522
1523#ifdef MSD_USE_PRE_ERASED
1524 /* CMD55 APP_CMD */
1525 result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
1526 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1527 {
1528 MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
1529 size = 0;
1530 goto _exit;
1531 }
1532
1533 /* ACMD23 Pre-erased */
1534 result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
1535 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1536 {
1537 MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
1538 size = 0;
1539 goto _exit;
1540 }
1541#endif

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