From b524eba3b99bd074316c18760dfdaccac46d90e2 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Andrea Date: Sat, 5 Oct 2019 23:06:53 +0200 Subject: [PATCH 1/2] Fix position of c_size_high in struct csd2_t Byte 7 of register CSD v2.00 is so composed: the first 2 bits for "reserved3" and the remaining 6 bits for "c_size_high". Because of how the bits are arranged on little-endian systems, "c_size_high" must be positioned before "reserved3" in order to have "c_size_high", "c_size_mid" and "c_size_low" arraged contiguously. --- src/utility/SdInfo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/SdInfo.h b/src/utility/SdInfo.h index 6a0e087..6e04bc1 100644 --- a/src/utility/SdInfo.h +++ b/src/utility/SdInfo.h @@ -190,8 +190,8 @@ typedef struct CSDV2 { unsigned write_blk_misalign : 1; unsigned read_bl_partial : 1; // byte 7 - unsigned reserved3 : 2; unsigned c_size_high : 6; + unsigned reserved3 : 2; // byte 8 uint8_t c_size_mid; // byte 9 From c48ea98be40aa82e8068f41b07181602e4fb42c6 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Andrea Date: Sat, 5 Oct 2019 23:07:27 +0200 Subject: [PATCH 2/2] Cast 8 bits variable to allow left shift by 8 Type of csd.v2.c_size_mid is uint8_t (8 bits). In order to left shift by 8 bits csd.v2.c_size_mid without losing informations (bits), is necessary to cast the variable to uint32_t. --- src/utility/Sd2Card.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 7cfbe73..61337a6 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -164,7 +164,7 @@ uint32_t Sd2Card::cardSize(void) { return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7); } else if (csd.v2.csd_ver == 1) { uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16) - | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low; + | ((uint32_t)csd.v2.c_size_mid << 8) | csd.v2.c_size_low; return (c_size + 1) << 10; } else { error(SD_CARD_ERROR_BAD_CSD);