From 8534c38232b587c33e0802580dcedca84f6fe7ae Mon Sep 17 00:00:00 2001 From: Pisit Sawangvonganan Date: Wed, 24 Jan 2024 01:37:39 +0700 Subject: [PATCH] sd: omit zero-initialization for sdhc_command and sdhc_data structures The decision to omit zero-initialization is driven by a desire to enhance Zephyr's compactness and efficiency. This is achieved by omitting zero-initialization, thereby reducing the instruction count and, as a byproduct, the code size. After a thorough review of the usage of struct sdhc_command and sdhc_data, it has been determined that zero-initialization can be omitted. Only a portion of the fields need to be manually initialized. (e.g. cmd.retries, data.block_addr) For the uninitialized fields, it can be expected from successful operations that data will be appropriately written back from the underlying layer. Signed-off-by: Pisit Sawangvonganan --- subsys/sd/sd.c | 6 +++--- subsys/sd/sd_ops.c | 31 +++++++++++++++++-------------- subsys/sd/sd_ops.h | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/subsys/sd/sd.c b/subsys/sd/sd.c index f4859b25a4398d..f075203601d620 100644 --- a/subsys/sd/sd.c +++ b/subsys/sd/sd.c @@ -21,7 +21,7 @@ LOG_MODULE_REGISTER(sd, CONFIG_SD_LOG_LEVEL); /* Idle all cards on bus. Can be used to clear errors on cards */ static inline int sd_idle(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; /* Reset card with CMD0 */ cmd.opcode = SD_GO_IDLE_STATE; @@ -35,7 +35,7 @@ static inline int sd_idle(struct sd_card *card) /* Sends CMD8 during SD initialization */ static int sd_send_interface_condition(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; uint32_t resp; @@ -72,7 +72,7 @@ static int sd_send_interface_condition(struct sd_card *card) /* Sends CMD59 to enable CRC checking for SD card in SPI mode */ static int sd_enable_crc(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; /* CMD59 for CRC mode is only valid for SPI hosts */ __ASSERT_NO_MSG(card->host_props.is_spi); diff --git a/subsys/sd/sd_ops.c b/subsys/sd/sd_ops.c index bf804679d685da..cf8bd01c146593 100644 --- a/subsys/sd/sd_ops.c +++ b/subsys/sd/sd_ops.c @@ -19,10 +19,11 @@ LOG_MODULE_DECLARE(sd, CONFIG_SD_LOG_LEVEL); /* Read card status. Return 0 if card is inactive */ int sdmmc_read_status(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; cmd.opcode = SD_SEND_STATUS; + cmd.arg = 0; if (!card->host_props.is_spi) { cmd.arg = (card->relative_addr << 16U); } @@ -203,8 +204,8 @@ static inline void sdmmc_decode_cid(struct sd_cid *cid, uint32_t *raw_cid) /* Reads card id/csd register (in SPI mode) */ static int sdmmc_spi_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t *cxd) { - struct sdhc_command cmd = {0}; - struct sdhc_data data = {0}; + struct sdhc_command cmd; + struct sdhc_data data; int ret, i; /* Use internal card buffer for data transfer */ uint32_t *cxd_be = (uint32_t *)card->card_buffer; @@ -216,6 +217,7 @@ static int sdmmc_spi_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t *c cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; /* CID/CSD is 16 bytes */ + data.block_addr = 0; /* Unused set to 0 */ data.block_size = 16; data.blocks = 1U; data.data = cxd_be; @@ -235,7 +237,7 @@ static int sdmmc_spi_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t *c /* Reads card id/csd register (native SD mode */ static int sdmmc_read_cxd(struct sd_card *card, uint32_t opcode, uint32_t rca, uint32_t *cxd) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; cmd.opcode = opcode; @@ -323,7 +325,7 @@ int card_read_cid(struct sd_card *card) int sdmmc_switch_voltage(struct sd_card *card) { int ret, sd_clock; - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; /* Check to make sure card supports 1.8V */ if (!(card->flags & SD_1800MV_FLAG)) { @@ -408,7 +410,7 @@ int sdmmc_switch_voltage(struct sd_card *card) */ int sdmmc_request_rca(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; cmd.opcode = SD_SEND_RELATIVE_ADDR; @@ -435,7 +437,7 @@ int sdmmc_request_rca(struct sd_card *card) */ int sdmmc_select_card(struct sd_card *card) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; cmd.opcode = SD_SELECT_CARD; @@ -460,7 +462,7 @@ int sdmmc_select_card(struct sd_card *card) /* Helper to send SD app command */ int card_app_command(struct sd_card *card, int relative_card_address) { - struct sdhc_command cmd = {0}; + struct sdhc_command cmd; int ret; cmd.opcode = SD_APP_CMD; @@ -489,8 +491,8 @@ int card_app_command(struct sd_card *card, int relative_card_address) static int card_read(struct sd_card *card, uint8_t *rbuf, uint32_t start_block, uint32_t num_blocks) { int ret; - struct sdhc_command cmd = {0}; - struct sdhc_data data = {0}; + struct sdhc_command cmd; + struct sdhc_data data; /* * Note: The SD specification allows for CMD23 to be sent before a @@ -611,8 +613,8 @@ int card_read_blocks(struct sd_card *card, uint8_t *rbuf, uint32_t start_block, static int card_query_written(struct sd_card *card, uint32_t *num_written) { int ret; - struct sdhc_command cmd = {0}; - struct sdhc_data data = {0}; + struct sdhc_command cmd; + struct sdhc_data data; uint32_t *blocks = (uint32_t *)card->card_buffer; ret = card_app_command(card, card->relative_addr); @@ -627,6 +629,7 @@ static int card_query_written(struct sd_card *card, uint32_t *num_written) cmd.retries = CONFIG_SD_CMD_RETRIES; cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT; + data.block_addr = 0; /* Unused set to 0 */ data.block_size = 4U; data.blocks = 1U; data.data = blocks; @@ -653,8 +656,8 @@ static int card_write(struct sd_card *card, const uint8_t *wbuf, uint32_t start_ { int ret; uint32_t blocks; - struct sdhc_command cmd = {0}; - struct sdhc_data data = {0}; + struct sdhc_command cmd; + struct sdhc_data data; /* * See the note in card_read() above. We will not issue CMD23 diff --git a/subsys/sd/sd_ops.h b/subsys/sd/sd_ops.h index ecd04f48a62622..f6c382244ef80f 100644 --- a/subsys/sd/sd_ops.h +++ b/subsys/sd/sd_ops.h @@ -10,7 +10,7 @@ /* * Switches voltage of SD card to 1.8V, as described by - * "Signal volatage switch procedure" in section 3.6.1 of SD specification. + * "Signal voltage switch procedure" in section 3.6.1 of SD specification. */ int sdmmc_switch_voltage(struct sd_card *card);