[FL-1688] Incorrect SD read/write block address #649

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG 2021-08-17 06:14:27 +10:00 committed by GitHub
parent a024e470b7
commit 8d07e67dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -393,13 +393,12 @@ uint8_t BSP_SD_GetCardInfo(SD_CardInfo* pCardInfo) {
uint8_t uint8_t
BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) { BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) {
uint32_t offset = 0; uint32_t offset = 0;
uint32_t addr;
uint8_t retr = BSP_SD_ERROR; uint8_t retr = BSP_SD_ERROR;
uint8_t* ptr = NULL;
SD_CmdAnswer_typedef response; SD_CmdAnswer_typedef response;
uint16_t BlockSize = 512; uint16_t BlockSize = 512;
uint8_t* ptr = NULL;
// uint8_t ptr[512];
/* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and /* Send CMD16 (SD_CMD_SET_BLOCKLEN) to set the size of the block and
Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */ Check if the SD acknowledged the set block length command: R1 response (0x00: no errors) */
response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED); response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
@ -415,15 +414,14 @@ BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint
} }
memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t) * BlockSize); memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t) * BlockSize);
/* Initialize the address */
addr = (ReadAddr * ((flag_SDHC == 1) ? 1 : BlockSize));
/* Data transfer */ /* Data transfer */
while(NumOfBlocks--) { while(NumOfBlocks--) {
/* Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */ /* Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
/* Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */ /* Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
response = SD_SendCmd( response = SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_CMD_READ_SINGLE_BLOCK,
(ReadAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize),
0xFF,
SD_ANSWER_R1_EXPECTED);
if(response.r1 != SD_R1_NO_ERROR) { if(response.r1 != SD_R1_NO_ERROR) {
goto error; goto error;
} }
@ -435,6 +433,8 @@ BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint
/* Set next read address*/ /* Set next read address*/
offset += BlockSize; offset += BlockSize;
addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize));
/* get CRC bytes (not really needed by us, but required by SD) */ /* get CRC bytes (not really needed by us, but required by SD) */
SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE);
@ -471,6 +471,7 @@ error:
uint8_t uint8_t
BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) { BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) {
uint32_t offset = 0; uint32_t offset = 0;
uint32_t addr;
uint8_t retr = BSP_SD_ERROR; uint8_t retr = BSP_SD_ERROR;
uint8_t* ptr = NULL; uint8_t* ptr = NULL;
SD_CmdAnswer_typedef response; SD_CmdAnswer_typedef response;
@ -490,15 +491,14 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui
goto error; goto error;
} }
/* Initialize the address */
addr = (WriteAddr * ((flag_SDHC == 1) ? 1 : BlockSize));
/* Data transfer */ /* Data transfer */
while(NumOfBlocks--) { while(NumOfBlocks--) {
/* Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks and /* Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks and
Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */ Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
response = SD_SendCmd( response = SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_CMD_WRITE_SINGLE_BLOCK,
(WriteAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize),
0xFF,
SD_ANSWER_R1_EXPECTED);
if(response.r1 != SD_R1_NO_ERROR) { if(response.r1 != SD_R1_NO_ERROR) {
goto error; goto error;
} }
@ -515,7 +515,8 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui
/* Set next write address */ /* Set next write address */
offset += BlockSize; offset += BlockSize;
addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize));
/* Put CRC bytes (not really needed by us, but required by SD) */ /* Put CRC bytes (not really needed by us, but required by SD) */
SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE);