From 8d07e67dded1e043c9690c5d5ce6f0047bade5f2 Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 17 Aug 2021 06:14:27 +1000 Subject: [PATCH] [FL-1688] Incorrect SD read/write block address #649 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- .../targets/f6/Src/fatfs/stm32_adafruit_sd.c | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/firmware/targets/f6/Src/fatfs/stm32_adafruit_sd.c b/firmware/targets/f6/Src/fatfs/stm32_adafruit_sd.c index 9c328d30..e90ebd62 100644 --- a/firmware/targets/f6/Src/fatfs/stm32_adafruit_sd.c +++ b/firmware/targets/f6/Src/fatfs/stm32_adafruit_sd.c @@ -393,13 +393,12 @@ uint8_t BSP_SD_GetCardInfo(SD_CardInfo* pCardInfo) { uint8_t BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) { uint32_t offset = 0; + uint32_t addr; uint8_t retr = BSP_SD_ERROR; + uint8_t* ptr = NULL; SD_CmdAnswer_typedef response; 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 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); @@ -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); + /* Initialize the address */ + addr = (ReadAddr * ((flag_SDHC == 1) ? 1 : BlockSize)); + /* Data transfer */ while(NumOfBlocks--) { /* 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) */ - response = SD_SendCmd( - SD_CMD_READ_SINGLE_BLOCK, - (ReadAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize), - 0xFF, - SD_ANSWER_R1_EXPECTED); + response = SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED); if(response.r1 != SD_R1_NO_ERROR) { goto error; } @@ -435,6 +433,8 @@ BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint /* Set next read address*/ offset += BlockSize; + addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize)); + /* get CRC bytes (not really needed by us, but required by SD) */ SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE); @@ -471,6 +471,7 @@ error: uint8_t BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) { uint32_t offset = 0; + uint32_t addr; uint8_t retr = BSP_SD_ERROR; uint8_t* ptr = NULL; SD_CmdAnswer_typedef response; @@ -490,15 +491,14 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui goto error; } + /* Initialize the address */ + addr = (WriteAddr * ((flag_SDHC == 1) ? 1 : BlockSize)); + /* Data transfer */ while(NumOfBlocks--) { /* 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) */ - response = SD_SendCmd( - SD_CMD_WRITE_SINGLE_BLOCK, - (WriteAddr + offset) * (flag_SDHC == 1 ? 1 : BlockSize), - 0xFF, - SD_ANSWER_R1_EXPECTED); + response = SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED); if(response.r1 != SD_R1_NO_ERROR) { goto error; } @@ -515,7 +515,8 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui /* Set next write address */ offset += BlockSize; - + addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize)); + /* Put CRC bytes (not really needed by us, but required by SD) */ SD_IO_WriteByte(SD_DUMMY_BYTE); SD_IO_WriteByte(SD_DUMMY_BYTE);