我正在使用带有 Arduino Mega 的 Petit FatFS。我已经实现了所有必要的功能,但是我遇到了该pf_read()
功能的问题。我正在尝试读取一个长度为 568 (512 + 56) 字节的文件。第一个扇区可以毫无问题地读取(并且是正确的),但第二个部分扇区不会被读取。
我尝试调试pf_read()
并发现以下内容:对于它调用的第一个扇区disk_readp()
,sector = 41104
和offset = 0
,count = 512
这似乎是正确的。然而,第二次,它用sector = -538935151
, offset = 512
and调用count = 56
(至少计数是正确的)。
奇怪的是,在pf_read()
完成之后,*br
is 97
,即使该函数实际上已经读取了 512 个字节。
以防万一有问题disk_readp()
,这是我的代码:
DRESULT disk_readp (
BYTE* buff, /* Pointer to the destination object */
DWORD sector, /* Sector number (LBA) */
UINT offset, /* Offset in the sector */
UINT count /* Byte count (bit15:destination) */
)
{
uint8_t r1;
uint8_t res_token;
uint16_t readAttempts;
DRESULT res = RES_ERROR;
CS_HIGH();
spi_transmit(0xFF);
CS_LOW();
spi_transmit(0xFF);
r1 = send_command(CMD17, sector);
if(r1 != 0xFF)
{
readAttempts = 0;
while(++readAttempts != SD_MAX_READ_ATTEMPTS)
{
if((res_token = spi_transmit(0xFF)) != 0xFF) break;
}
if(res_token == 0xFE)
{
// read 512 byte block
for(uint16_t i = 0; i < offset; i++) {
spi_transmit(0xFF); // discard data from (0) to (offset)
}
for(uint16_t i = offset; i < offset + count; i++)
{
*(buff++) = spi_transmit(0xFF); // safe data from (offset) to (offset + count)
}
for(uint16_t i = offset + count; i < 512; i++) {
spi_transmit(0xFF); // discard data from (offset + count) to (512)
}
// read and ignore 16-bit CRC
spi_transmit(0xFF);
spi_transmit(0xFF);
res = RES_OK;
}
}
CS_LOW();
spi_transmit(0xFF);
CS_HIGH();
spi_transmit(0xFF);
return res;
}
以及 Petit FatFS 文档的链接:http ://elm-chan.org/fsw/ff/00index_p.html
谢谢!