Skip to content

Commit

Permalink
Merge pull request #10512 from bkleiner/bkleiner/fix-h7-sdcard
Browse files Browse the repository at this point in the history
fix h7 sdcard init
  • Loading branch information
mmosca authored Dec 8, 2024
2 parents bd4561f + 1c288a8 commit abc286d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/main/drivers/sdcard/sdcard_sdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,20 @@ static bool sdcardSdio_poll(void)
break; // Timeout not reached yet so keep waiting
}
// Timeout has expired, so fall through to convert to a fatal error
FALLTHROUGH;

case SDCARD_RECEIVE_ERROR:
sdcardSdio_reset();

if (sdcard.pendingOperation.callback) {
sdcard.pendingOperation.callback(
SDCARD_BLOCK_OPERATION_READ,
sdcard.pendingOperation.blockIndex,
NULL,
sdcard.pendingOperation.callbackData
);
}

goto doMore;
break;
}
Expand Down
26 changes: 15 additions & 11 deletions src/main/drivers/sdcard/sdmmc_sdio_h7xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ void HAL_SD_MspInit(SD_HandleTypeDef* hsd)

if (sdioHardware->instance == SDMMC1) {
__HAL_RCC_SDMMC1_CLK_DISABLE();
__HAL_RCC_SDMMC1_CLK_ENABLE();

__HAL_RCC_SDMMC1_FORCE_RESET();
__HAL_RCC_SDMMC1_RELEASE_RESET();
__HAL_RCC_SDMMC1_CLK_ENABLE();
} else if (sdioHardware->instance == SDMMC2) {
__HAL_RCC_SDMMC2_CLK_DISABLE();
__HAL_RCC_SDMMC2_CLK_ENABLE();

__HAL_RCC_SDMMC2_FORCE_RESET();
__HAL_RCC_SDMMC2_RELEASE_RESET();
__HAL_RCC_SDMMC2_CLK_ENABLE();
}

const IO_t clk = IOGetByTag(sdioPin[SDIO_PIN_CK].pin);
Expand Down Expand Up @@ -248,28 +250,30 @@ bool SD_GetState(void)

bool SD_Init(void)
{
HAL_StatusTypeDef status;

memset(&hsd1, 0, sizeof(hsd1));

hsd1.Instance = sdioHardware->instance;

hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_ENABLE;
// falling seems to work better ?? no idea whats "right" here
hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_FALLING;

// drastically increases the time to respond from the sdcard
// lets leave it off
hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
#ifdef SDCARD_SDIO_4BIT
hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B;
#else
hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B; // FIXME untested
hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;
#endif
hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
#ifdef SDCARD_SDIO_NORMAL_SPEED
hsd1.Init.ClockDiv = SDMMC_NSpeed_CLK_DIV;
hsd1.Init.ClockDiv = SDMMC_NSpeed_CLK_DIV;
#else
hsd1.Init.ClockDiv = 1; // 200Mhz / (2 * 1 ) = 100Mhz, used for "UltraHigh speed SD card" only, see HAL_SD_ConfigWideBusOperation, SDMMC_HSpeed_CLK_DIV, SDMMC_NSpeed_CLK_DIV
hsd1.Init.ClockDiv = SDMMC_HSpeed_CLK_DIV; // lets not go too crazy :)
#endif

status = HAL_SD_Init(&hsd1); // Will call HAL_SD_MspInit

// Will call HAL_SD_MspInit
const HAL_StatusTypeDef status = HAL_SD_Init(&hsd1);
if (status != HAL_OK) {
return SD_ERROR;
}
Expand Down

0 comments on commit abc286d

Please sign in to comment.