فهرست منبع

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

Co-authored-by: あく <alleteam@gmail.com>
SG 4 سال پیش
والد
کامیت
8d07e67dde
1فایلهای تغییر یافته به همراه15 افزوده شده و 14 حذف شده
  1. 15 14
      firmware/targets/f6/Src/fatfs/stm32_adafruit_sd.c

+ 15 - 14
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);