@@ -361,6 +361,7 @@ static int stm32_sdmmc_access_init(struct disk_info *disk)
361361{
362362 const struct device * dev = disk -> dev ;
363363 struct stm32_sdmmc_priv * priv = dev -> data ;
364+ HAL_StatusTypeDef hal_ret ;
364365 int err ;
365366
366367 err = stm32_sdmmc_pwr_on (priv );
@@ -412,20 +413,20 @@ static int stm32_sdmmc_access_init(struct disk_info *disk)
412413 }
413414
414415#ifdef CONFIG_SDMMC_STM32_EMMC
415- err = HAL_MMC_Init (& priv -> hsd );
416+ hal_ret = HAL_MMC_Init (& priv -> hsd );
416417#else
417- err = HAL_SD_Init (& priv -> hsd );
418+ hal_ret = HAL_SD_Init (& priv -> hsd );
418419#endif
419- if (err != HAL_OK ) {
420+ if (hal_ret != HAL_OK ) {
420421 LOG_ERR ("failed to init stm32_sdmmc (ErrorCode 0x%X)" , priv -> hsd .ErrorCode );
421422 err = - EIO ;
422423 goto error ;
423424 }
424425
425426 if (SDMMC_BUS_WIDTH != SDMMC_BUS_WIDE_1B ) {
426427 priv -> hsd .Init .BusWide = SDMMC_BUS_WIDTH ;
427- err = HAL_SD_ConfigWideBusOperation (& priv -> hsd , priv -> hsd .Init .BusWide );
428- if (err != HAL_OK ) {
428+ hal_ret = HAL_SD_ConfigWideBusOperation (& priv -> hsd , priv -> hsd .Init .BusWide );
429+ if (hal_ret != HAL_OK ) {
429430 LOG_ERR ("failed to configure wide bus operation (ErrorCode 0x%X)" ,
430431 priv -> hsd .ErrorCode );
431432 err = - EIO ;
@@ -501,23 +502,32 @@ static int stm32_sdmmc_is_card_in_transfer(HandleTypeDef *hsd)
501502static int stm32_sdmmc_read_blocks (HandleTypeDef * hsd , uint8_t * data_buf ,
502503 uint32_t start_sector , uint32_t num_sector )
503504{
505+ HAL_StatusTypeDef hal_ret ;
506+
504507#if STM32_SDMMC_USE_DMA || IS_ENABLED (DT_PROP (DT_DRV_INST (0 ), idma ))
505508
506509#ifdef CONFIG_SDMMC_STM32_EMMC
507- return HAL_MMC_ReadBlocks_DMA (hsd , data_buf , start_sector , num_sector );
510+ hal_ret = HAL_MMC_ReadBlocks_DMA (hsd , data_buf , start_sector , num_sector );
508511#else
509- return HAL_SD_ReadBlocks_DMA (hsd , data_buf , start_sector , num_sector );
512+ hal_ret = HAL_SD_ReadBlocks_DMA (hsd , data_buf , start_sector , num_sector );
510513#endif
511514
512- #else
515+ #else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
513516
514517#ifdef CONFIG_SDMMC_STM32_EMMC
515- return HAL_MMC_ReadBlocks_IT (hsd , data_buf , start_sector , num_sector );
518+ hal_ret = HAL_MMC_ReadBlocks_IT (hsd , data_buf , start_sector , num_sector );
516519#else
517- return HAL_SD_ReadBlocks_IT (hsd , data_buf , start_sector , num_sector );
520+ hal_ret = HAL_SD_ReadBlocks_IT (hsd , data_buf , start_sector , num_sector );
518521#endif
519522
520- #endif
523+ #endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
524+
525+ if (hal_ret != HAL_OK ) {
526+ LOG_ERR ("sd read block failed %d" , hal_ret );
527+ return - EIO ;
528+ }
529+
530+ return 0 ;
521531}
522532
523533static int stm32_sdmmc_access_read (struct disk_info * disk , uint8_t * data_buf ,
@@ -539,9 +549,7 @@ static int stm32_sdmmc_access_read(struct disk_info *disk, uint8_t *data_buf,
539549#endif
540550
541551 err = stm32_sdmmc_read_blocks (& priv -> hsd , data_buf , start_sector , num_sector );
542- if (err != HAL_OK ) {
543- LOG_ERR ("sd read block failed %d" , err );
544- err = - EIO ;
552+ if (err != 0 ) {
545553 goto end ;
546554 }
547555
@@ -569,23 +577,32 @@ static int stm32_sdmmc_write_blocks(HandleTypeDef *hsd,
569577 uint8_t * data_buf ,
570578 uint32_t start_sector , uint32_t num_sector )
571579{
580+ HAL_StatusTypeDef hal_ret ;
581+
572582#if STM32_SDMMC_USE_DMA || IS_ENABLED (DT_PROP (DT_DRV_INST (0 ), idma ))
573583
574584#ifdef CONFIG_SDMMC_STM32_EMMC
575- return HAL_MMC_WriteBlocks_DMA (hsd , data_buf , start_sector , num_sector );
585+ hal_ret = HAL_MMC_WriteBlocks_DMA (hsd , data_buf , start_sector , num_sector );
576586#else
577- return HAL_SD_WriteBlocks_DMA (hsd , data_buf , start_sector , num_sector );
587+ hal_ret = HAL_SD_WriteBlocks_DMA (hsd , data_buf , start_sector , num_sector );
578588#endif
579589
580- #else
590+ #else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
581591
582592#ifdef CONFIG_SDMMC_STM32_EMMC
583- return HAL_MMC_WriteBlocks_IT (hsd , data_buf , start_sector , num_sector );
593+ hal_ret = HAL_MMC_WriteBlocks_IT (hsd , data_buf , start_sector , num_sector );
584594#else
585- return HAL_SD_WriteBlocks_IT (hsd , data_buf , start_sector , num_sector );
595+ hal_ret = HAL_SD_WriteBlocks_IT (hsd , data_buf , start_sector , num_sector );
586596#endif
587597
588- #endif
598+ #endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */
599+
600+ if (hal_ret != HAL_OK ) {
601+ LOG_ERR ("sd write block failed %d" , hal_ret );
602+ return - EIO ;
603+ }
604+
605+ return 0 ;
589606}
590607
591608static int stm32_sdmmc_access_write (struct disk_info * disk ,
@@ -608,9 +625,7 @@ static int stm32_sdmmc_access_write(struct disk_info *disk,
608625#endif
609626
610627 err = stm32_sdmmc_write_blocks (& priv -> hsd , (uint8_t * )data_buf , start_sector , num_sector );
611- if (err != HAL_OK ) {
612- LOG_ERR ("sd write block failed %d" , err );
613- err = - EIO ;
628+ if (err != 0 ) {
614629 goto end ;
615630 }
616631
@@ -637,9 +652,9 @@ static int stm32_sdmmc_access_write(struct disk_info *disk,
637652static int stm32_sdmmc_get_card_info (HandleTypeDef * hsd , CardInfoTypeDef * info )
638653{
639654#ifdef CONFIG_SDMMC_STM32_EMMC
640- return HAL_MMC_GetCardInfo (hsd , info );
655+ return ( HAL_MMC_GetCardInfo (hsd , info ) == HAL_OK ) ? 0 : - EIO ;
641656#else
642- return HAL_SD_GetCardInfo (hsd , info );
657+ return ( HAL_SD_GetCardInfo (hsd , info ) == HAL_OK ) ? 0 : - EIO ;
643658#endif
644659}
645660
@@ -654,15 +669,15 @@ static int stm32_sdmmc_access_ioctl(struct disk_info *disk, uint8_t cmd,
654669 switch (cmd ) {
655670 case DISK_IOCTL_GET_SECTOR_COUNT :
656671 err = stm32_sdmmc_get_card_info (& priv -> hsd , & info );
657- if (err != HAL_OK ) {
658- return - EIO ;
672+ if (err != 0 ) {
673+ return err ;
659674 }
660675 * (uint32_t * )buff = info .LogBlockNbr ;
661676 break ;
662677 case DISK_IOCTL_GET_SECTOR_SIZE :
663678 err = stm32_sdmmc_get_card_info (& priv -> hsd , & info );
664- if (err != HAL_OK ) {
665- return - EIO ;
679+ if (err != 0 ) {
680+ return err ;
666681 }
667682 * (uint32_t * )buff = info .LogBlockSize ;
668683 break ;
0 commit comments