Skip to content

Commit 7eca2b0

Browse files
committed
chore: init only once
BSP_SD_Init() is also called by FatFs, avoid to init again if the SD handle state is different from HAL_SD_STATE_RESET. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 021b725 commit 7eca2b0

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/bsp_sd.c

+33-30
Original file line numberDiff line numberDiff line change
@@ -132,49 +132,52 @@ uint8_t BSP_SD_Init(void)
132132
{
133133
uint8_t sd_state = MSD_OK;
134134

135-
/* uSD device interface configuration */
136-
uSdHandle.Instance = SD_INSTANCE;
135+
/* Check if SD is not yet initialized */
136+
if (uSdHandle.State == HAL_SD_STATE_RESET) {
137+
/* uSD device interface configuration */
138+
uSdHandle.Instance = SD_INSTANCE;
137139

138-
uSdHandle.Init.ClockEdge = SD_CLK_EDGE;
140+
uSdHandle.Init.ClockEdge = SD_CLK_EDGE;
139141
#if defined(SD_CLK_BYPASS)
140-
uSdHandle.Init.ClockBypass = SD_CLK_BYPASS;
142+
uSdHandle.Init.ClockBypass = SD_CLK_BYPASS;
141143
#endif
142-
uSdHandle.Init.ClockPowerSave = SD_CLK_PWR_SAVE;
143-
uSdHandle.Init.BusWide = SD_BUS_WIDE_1B;
144-
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
145-
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
144+
uSdHandle.Init.ClockPowerSave = SD_CLK_PWR_SAVE;
145+
uSdHandle.Init.BusWide = SD_BUS_WIDE_1B;
146+
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
147+
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
146148
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
147149
#if defined(SDMMC_TRANSCEIVER_ENABLE)
148-
uSdHandle.Init.Transceiver = SD_TRANSCEIVER_ENABLE;
150+
uSdHandle.Init.Transceiver = SD_TRANSCEIVER_ENABLE;
149151
#else
150-
uSdHandle.Init.TranceiverPresent = SD_TRANSCEIVER_ENABLE;
152+
uSdHandle.Init.TranceiverPresent = SD_TRANSCEIVER_ENABLE;
151153
#endif
152-
BSP_SD_Transceiver_MspInit(&uSdHandle, NULL);
154+
BSP_SD_Transceiver_MspInit(&uSdHandle, NULL);
153155
#endif
154156

155-
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
156-
/* Msp SD Detect pin initialization */
157-
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
158-
if (BSP_SD_IsDetected() != SD_PRESENT) { /* Check if SD card is present */
159-
return MSD_ERROR_SD_NOT_PRESENT;
157+
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
158+
/* Msp SD Detect pin initialization */
159+
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
160+
if (BSP_SD_IsDetected() != SD_PRESENT) { /* Check if SD card is present */
161+
return MSD_ERROR_SD_NOT_PRESENT;
162+
}
160163
}
161-
}
162-
163-
/* Msp SD initialization */
164-
BSP_SD_MspInit(&uSdHandle, NULL);
165164

166-
/* HAL SD initialization */
167-
if (HAL_SD_Init(&uSdHandle) != HAL_OK) {
168-
sd_state = MSD_ERROR;
169-
}
165+
/* Msp SD initialization */
166+
BSP_SD_MspInit(&uSdHandle, NULL);
170167

171-
/* Configure SD Bus width */
172-
if (sd_state == MSD_OK) {
173-
/* Enable wide operation */
174-
if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SD_BUS_WIDE) != HAL_OK) {
168+
/* HAL SD initialization */
169+
if (HAL_SD_Init(&uSdHandle) != HAL_OK) {
175170
sd_state = MSD_ERROR;
176-
} else {
177-
sd_state = MSD_OK;
171+
}
172+
173+
/* Configure SD Bus width */
174+
if (sd_state == MSD_OK) {
175+
/* Enable wide operation */
176+
if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SD_BUS_WIDE) != HAL_OK) {
177+
sd_state = MSD_ERROR;
178+
} else {
179+
sd_state = MSD_OK;
180+
}
178181
}
179182
}
180183
return sd_state;

0 commit comments

Comments
 (0)