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

Function rt_msd_read

components/drivers/spi/dev_spi_msd.c:1212–1286  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1210}
1211
1212static rt_ssize_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
1213{
1214 struct msd_device *msd = (struct msd_device *)dev;
1215 uint8_t response[MSD_RESPONSE_MAX_LEN];
1216 rt_err_t result = RT_EOK;
1217
1218 result = MSD_take_owner(msd->spi_device);
1219
1220 if (result != RT_EOK)
1221 {
1222 goto _exit;
1223 }
1224
1225 /* SINGLE_BLOCK? */
1226 if (size == 1)
1227 {
1228 rt_spi_take(msd->spi_device);
1229
1230 result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1231 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1232 {
1233 MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1234 size = 0;
1235 goto _exit;
1236 }
1237
1238 result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
1239 if (result != RT_EOK)
1240 {
1241 MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1242 size = 0;
1243 }
1244 }
1245 else if (size > 1)
1246 {
1247 uint32_t i;
1248
1249 rt_spi_take(msd->spi_device);
1250
1251 result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1252 if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1253 {
1254 MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1255 size = 0;
1256 goto _exit;
1257 }
1258
1259 for (i = 0; i < size; i++)
1260 {
1261 result = _read_block(msd->spi_device,
1262 (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
1263 msd->geometry.bytes_per_sector);
1264 if (result != RT_EOK)
1265 {
1266 MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1267 size = i;
1268 break;
1269 }

Callers

nothing calls this directly

Calls 6

MSD_take_ownerFunction · 0.85
rt_spi_takeFunction · 0.85
_send_cmdFunction · 0.85
_read_blockFunction · 0.85
rt_spi_releaseFunction · 0.85
rt_mutex_releaseFunction · 0.85

Tested by

no test coverage detected