Skip to content

Commit d9fcc1e

Browse files
committed
drivers: disk: sdmmc_stm32: don't assume HAL return value is an int
Clarify HAL return value is of type HAL_StatusTypeDef and may not be a int. This change aims preventing one from mixing standard "errno" int return values and STM32 HAL return value finding misleading implementation in existing code. No functional change. Signed-off-by: Etienne Carriere <[email protected]>
1 parent 8c13c07 commit d9fcc1e

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

drivers/disk/sdmmc_stm32.c

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ static int stm32_sdmmc_access_init(struct disk_info *disk)
353353
{
354354
const struct device *dev = disk->dev;
355355
struct stm32_sdmmc_priv *priv = dev->data;
356+
HAL_StatusTypeDef hal_ret;
356357
int err;
357358

358359
err = stm32_sdmmc_pwr_on(priv);
@@ -404,11 +405,11 @@ static int stm32_sdmmc_access_init(struct disk_info *disk)
404405
}
405406

406407
#ifdef CONFIG_SDMMC_STM32_EMMC
407-
err = HAL_MMC_Init(&priv->hsd);
408+
hal_ret = HAL_MMC_Init(&priv->hsd);
408409
#else
409-
err = HAL_SD_Init(&priv->hsd);
410+
hal_ret = HAL_SD_Init(&priv->hsd);
410411
#endif
411-
if (err != HAL_OK) {
412+
if (hal_ret != HAL_OK) {
412413
LOG_ERR("failed to init stm32_sdmmc (ErrorCode 0x%X)", priv->hsd.ErrorCode);
413414
err = -EIO;
414415
goto error;
@@ -480,23 +481,32 @@ static int stm32_sdmmc_is_card_in_transfer(HandleTypeDef *hsd)
480481
static int stm32_sdmmc_read_blocks(HandleTypeDef *hsd, uint8_t *data_buf,
481482
uint32_t start_sector, uint32_t num_sector)
482483
{
484+
HAL_StatusTypeDef hal_ret;
485+
483486
#if STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma))
484487

485488
#ifdef CONFIG_SDMMC_STM32_EMMC
486-
return HAL_MMC_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector);
489+
hal_ret = HAL_MMC_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector);
487490
#else
488-
return HAL_SD_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector);
491+
hal_ret = HAL_SD_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector);
489492
#endif
490493

491-
#else
494+
#else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
492495

493496
#ifdef CONFIG_SDMMC_STM32_EMMC
494-
return HAL_MMC_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector);
497+
hal_ret = HAL_MMC_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector);
495498
#else
496-
return HAL_SD_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector);
499+
hal_ret = HAL_SD_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector);
497500
#endif
498501

499-
#endif
502+
#endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
503+
504+
if (hal_ret != HAL_OK) {
505+
LOG_ERR("sd read block failed %d", hal_ret);
506+
return -EIO;
507+
}
508+
509+
return 0;
500510
}
501511

502512
static int stm32_sdmmc_access_read(struct disk_info *disk, uint8_t *data_buf,
@@ -518,9 +528,7 @@ static int stm32_sdmmc_access_read(struct disk_info *disk, uint8_t *data_buf,
518528
#endif
519529

520530
err = stm32_sdmmc_read_blocks(&priv->hsd, data_buf, start_sector, num_sector);
521-
if (err != HAL_OK) {
522-
LOG_ERR("sd read block failed %d", err);
523-
err = -EIO;
531+
if (err != 0) {
524532
goto end;
525533
}
526534

@@ -548,23 +556,32 @@ static int stm32_sdmmc_write_blocks(HandleTypeDef *hsd,
548556
uint8_t *data_buf,
549557
uint32_t start_sector, uint32_t num_sector)
550558
{
559+
HAL_StatusTypeDef hal_ret;
560+
551561
#if STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma))
552562

553563
#ifdef CONFIG_SDMMC_STM32_EMMC
554-
return HAL_MMC_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector);
564+
hal_ret = HAL_MMC_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector);
555565
#else
556-
return HAL_SD_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector);
566+
hal_ret = HAL_SD_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector);
557567
#endif
558568

559-
#else
569+
#else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
560570

561571
#ifdef CONFIG_SDMMC_STM32_EMMC
562-
return HAL_MMC_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector);
572+
hal_ret = HAL_MMC_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector);
563573
#else
564-
return HAL_SD_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector);
574+
hal_ret = HAL_SD_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector);
565575
#endif
566576

567-
#endif
577+
#endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
578+
579+
if (hal_ret != HAL_OK) {
580+
LOG_ERR("sd write block failed %d", hal_ret);
581+
return -EIO;
582+
}
583+
584+
return 0;
568585
}
569586

570587
static int stm32_sdmmc_access_write(struct disk_info *disk,
@@ -587,9 +604,7 @@ static int stm32_sdmmc_access_write(struct disk_info *disk,
587604
#endif
588605

589606
err = stm32_sdmmc_write_blocks(&priv->hsd, (uint8_t *)data_buf, start_sector, num_sector);
590-
if (err != HAL_OK) {
591-
LOG_ERR("sd write block failed %d", err);
592-
err = -EIO;
607+
if (err != 0) {
593608
goto end;
594609
}
595610

@@ -616,9 +631,9 @@ static int stm32_sdmmc_access_write(struct disk_info *disk,
616631
static int stm32_sdmmc_get_card_info(HandleTypeDef *hsd, CardInfoTypeDef *info)
617632
{
618633
#ifdef CONFIG_SDMMC_STM32_EMMC
619-
return HAL_MMC_GetCardInfo(hsd, info);
634+
return (HAL_MMC_GetCardInfo(hsd, info) == HAL_OK) ? 0 : -EIO;
620635
#else
621-
return HAL_SD_GetCardInfo(hsd, info);
636+
return (HAL_SD_GetCardInfo(hsd, info) == HAL_OK) ? 0 : -EIO;
622637
#endif
623638
}
624639

@@ -633,15 +648,15 @@ static int stm32_sdmmc_access_ioctl(struct disk_info *disk, uint8_t cmd,
633648
switch (cmd) {
634649
case DISK_IOCTL_GET_SECTOR_COUNT:
635650
err = stm32_sdmmc_get_card_info(&priv->hsd, &info);
636-
if (err != HAL_OK) {
637-
return -EIO;
651+
if (err != 0) {
652+
return err;
638653
}
639654
*(uint32_t *)buff = info.LogBlockNbr;
640655
break;
641656
case DISK_IOCTL_GET_SECTOR_SIZE:
642657
err = stm32_sdmmc_get_card_info(&priv->hsd, &info);
643-
if (err != HAL_OK) {
644-
return -EIO;
658+
if (err != 0) {
659+
return err;
645660
}
646661
*(uint32_t *)buff = info.LogBlockSize;
647662
break;

0 commit comments

Comments
 (0)