From 338f6007809b238d5ca3745d381f3847c678357c Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 5 Jul 2023 15:59:56 +0200 Subject: [PATCH 1/5] fix: restore SD_CLK_ENABLE Wrong endif preprocessor placement when introducing new SD pins management. Fixes #60 Signed-off-by: Frederic Pillon --- src/bsp_sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index ee767cc..3a08b44 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -515,6 +515,7 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) pinmap_pinout(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR); pinmap_pinout(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); } +#endif #endif /* Enable SD clock */ #if defined(SDMMC1) && defined(SDMMC2) @@ -527,7 +528,6 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) UNUSED(hsd); SD_CLK_ENABLE(); #endif -#endif } /** From e656bc81c57a61f5fe77b5552dafcd6bfb31cd1e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 5 Jul 2023 16:41:02 +0200 Subject: [PATCH 2/5] fix: wrong preprocessor tests Signed-off-by: Frederic Pillon --- src/bsp_sd.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index 3a08b44..a79def7 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -160,12 +160,12 @@ uint8_t BSP_SD_GetInstance(void) SD_PinNames.pin_d3 = PinMap_SD_DATA3[0].pin; SD_PinNames.pin_cmd = PinMap_SD_CMD[0].pin; SD_PinNames.pin_ck = PinMap_SD_CK[0].pin; -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) SD_PinNames.pin_ckin = PinMap_SD_CKIN[0].pin; SD_PinNames.pin_cdir = PinMap_SD_CDIR[0].pin; SD_PinNames.pin_d0dir = PinMap_SD_D0DIR[0].pin; SD_PinNames.pin_d123dir = PinMap_SD_D123DIR[0].pin; -#endif /* SDMMC1 && SDMMC2 */ +#endif /* SDMMC1 || SDMMC2 */ } /* Get SD instance from pins */ sd_d0 = pinmap_peripheral(SD_PinNames.pin_d0, PinMap_SD_DATA0); @@ -192,7 +192,7 @@ uint8_t BSP_SD_GetInstance(void) return MSD_ERROR; } uSdHandle.Instance = sd_base; -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) if (SD_PinNames.pin_ckin != NC) { SD_TypeDef *sd_ckin = pinmap_peripheral(SD_PinNames.pin_ckin, PinMap_SD_CKIN); SD_TypeDef *sd_cdir = pinmap_peripheral(SD_PinNames.pin_cdir, PinMap_SD_CDIR); @@ -508,7 +508,7 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) pinmap_pinout(SD_PinNames.pin_d3, PinMap_SD_DATA3); pinmap_pinout(SD_PinNames.pin_cmd, PinMap_SD_CMD); pinmap_pinout(SD_PinNames.pin_ck, PinMap_SD_CK); -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) if (SD_PinNames.pin_ckin != NC) { pinmap_pinout(SD_PinNames.pin_ckin, PinMap_SD_CKIN); pinmap_pinout(SD_PinNames.pin_cdir, PinMap_SD_CDIR); @@ -573,7 +573,7 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params) HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_d3), STM_GPIO_PIN(SD_PinNames.pin_d3)); HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_cmd), STM_GPIO_PIN(SD_PinNames.pin_cmd)); HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_ck), STM_GPIO_PIN(SD_PinNames.pin_ck)); -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) if (SD_PinNames.pin_ckin != NC) { HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_ckin), STM_GPIO_PIN(SD_PinNames.pin_ckin)); HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_cdir), STM_GPIO_PIN(SD_PinNames.pin_cdir)); From 29208ce6c3cb14f2e7faf47c99c798e3d387926b Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 6 Jul 2023 09:10:23 +0200 Subject: [PATCH 3/5] fix: SDMMC2 can be available without SDMMC1 Example for L4P5C(E-G)(T-U)_L4Q5CG(T-U). So ensure to have correct setup in this case. Signed-off-by: Frederic Pillon --- src/bsp_sd.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index a79def7..327ac68 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -43,14 +43,11 @@ /* Definition for BSP SD */ #if defined(SDMMC1) || defined(SDMMC2) #ifndef SD_INSTANCE - #define SD_INSTANCE SDMMC1 - #endif - - #define SD_CLK_ENABLE __HAL_RCC_SDMMC1_CLK_ENABLE - #define SD_CLK_DISABLE __HAL_RCC_SDMMC1_CLK_DISABLE - #ifdef SDMMC2 - #define SD_CLK2_ENABLE __HAL_RCC_SDMMC2_CLK_ENABLE - #define SD_CLK2_DISABLE __HAL_RCC_SDMMC2_CLK_DISABLE + #if defined(SDMMC1) + #define SD_INSTANCE SDMMC1 + #else + #define SD_INSTANCE SDMMC2 + #endif #endif #define SD_CLK_EDGE SDMMC_CLOCK_EDGE_RISING @@ -84,8 +81,6 @@ #elif defined(SDIO) #define SD_INSTANCE SDIO - #define SD_CLK_ENABLE __HAL_RCC_SDIO_CLK_ENABLE - #define SD_CLK_DISABLE __HAL_RCC_SDIO_CLK_DISABLE #define SD_CLK_EDGE SDIO_CLOCK_EDGE_RISING #if defined(SDIO_CLOCK_BYPASS_DISABLE) #define SD_CLK_BYPASS SDIO_CLOCK_BYPASS_DISABLE @@ -518,15 +513,20 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) #endif #endif /* Enable SD clock */ -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) +#if defined(SDMMC1) if (hsd->Instance == SDMMC1) { - SD_CLK_ENABLE(); - } else { - SD_CLK2_ENABLE(); + __HAL_RCC_SDMMC1_CLK_ENABLE(); + } +#endif +#if defined(SDMMC2) + if (hsd->Instance == SDMMC2) { + __HAL_RCC_SDMMC2_CLK_ENABLE(); } +#endif #else UNUSED(hsd); - SD_CLK_ENABLE(); + __HAL_RCC_SDIO_CLK_ENABLE(); #endif } @@ -584,15 +584,20 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params) #endif /* Disable SD clock */ -#if defined(SDMMC1) && defined(SDMMC2) +#if defined(SDMMC1) || defined(SDMMC2) +#if defined(SDMMC1) if (hsd->Instance == SDMMC1) { - SD_CLK_DISABLE(); - } else { - SD_CLK2_DISABLE(); + __HAL_RCC_SDMMC1_CLK_DISABLE(); } +#endif +#if defined(SDMMC2) + if (hsd->Instance == SDMMC2) { + __HAL_RCC_SDMMC2_CLK_DISABLE(); + } +#endif #else UNUSED(hsd); - SD_CLK_DISABLE(); + __HAL_RCC_SDIO_CLK_DISABLE(); #endif } From e7516f17532feaabd1cd12cea26ba6727b1a7c60 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 6 Jul 2023 19:23:58 +0200 Subject: [PATCH 4/5] fix: manage SDMMCx signals availability For some series some PinMap_SD* are not available. Signed-off-by: Frederic Pillon --- src/bsp_sd.c | 82 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index 327ac68..8d797b5 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -156,10 +156,18 @@ uint8_t BSP_SD_GetInstance(void) SD_PinNames.pin_cmd = PinMap_SD_CMD[0].pin; SD_PinNames.pin_ck = PinMap_SD_CK[0].pin; #if defined(SDMMC1) || defined(SDMMC2) +#if !defined(SDMMC_CKIN_NA) SD_PinNames.pin_ckin = PinMap_SD_CKIN[0].pin; +#endif +#if !defined(SDMMC_CDIR_NA) SD_PinNames.pin_cdir = PinMap_SD_CDIR[0].pin; +#endif +#if !defined(SDMMC_D0DIR_NA) SD_PinNames.pin_d0dir = PinMap_SD_D0DIR[0].pin; +#endif +#if !defined(SDMMC_D123DIR_NA) SD_PinNames.pin_d123dir = PinMap_SD_D123DIR[0].pin; +#endif #endif /* SDMMC1 || SDMMC2 */ } /* Get SD instance from pins */ @@ -188,27 +196,43 @@ uint8_t BSP_SD_GetInstance(void) } uSdHandle.Instance = sd_base; #if defined(SDMMC1) || defined(SDMMC2) +#if !defined(SDMMC_CKIN_NA) if (SD_PinNames.pin_ckin != NC) { SD_TypeDef *sd_ckin = pinmap_peripheral(SD_PinNames.pin_ckin, PinMap_SD_CKIN); + if (pinmap_merge_peripheral(sd_ckin, sd_base) == NP) { + core_debug("ERROR: SD CKIN pin mismatch\n"); + return MSD_ERROR; + } + } +#endif +#if !defined(SDMMC_CDIR_NA) + if (SD_PinNames.pin_cdir != NC) { SD_TypeDef *sd_cdir = pinmap_peripheral(SD_PinNames.pin_cdir, PinMap_SD_CDIR); + if (pinmap_merge_peripheral(sd_cdir, sd_base) == NP) { + core_debug("ERROR: SD CDIR pin mismatch\n"); + return MSD_ERROR; + } + } +#endif +#if !defined(SDMMC_D0DIR_NA) + if (SD_PinNames.pin_cdir != NC) { SD_TypeDef *sd_d0dir = pinmap_peripheral(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR); - SD_TypeDef *sd_d123dir = pinmap_peripheral(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); - - /* Pins Dx/cmd/CK must not be NP. */ - if (sd_ckin == NP || sd_cdir == NP || sd_d0dir == NP || sd_d123dir == NP) { - core_debug("ERROR: at least one SDMMC pin has no peripheral\n"); + if (pinmap_merge_peripheral(sd_d0dir, sd_base) == NP) { + core_debug("ERROR: SD DODIR pin mismatch\n"); return MSD_ERROR; } - SD_TypeDef *sdmmc_cx = pinmap_merge_peripheral(sd_ckin, sd_cdir); - SD_TypeDef *sdmmc_dx = pinmap_merge_peripheral(sd_d0dir, sd_d123dir); - SD_TypeDef *sdmmc_base = pinmap_merge_peripheral(sdmmc_cx, sdmmc_dx); - if (sdmmc_cx == NP || sdmmc_dx == NP || sdmmc_base == NP) { - core_debug("ERROR: SD pins mismatch\n"); + } +#endif +#if !defined(SDMMC_D123DIR_NA) + if (SD_PinNames.pin_cdir != NC) { + SD_TypeDef *sd_d123dir = pinmap_peripheral(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); + if (pinmap_merge_peripheral(sd_d123dir, sd_base) == NP) { + core_debug("ERROR: SD D123DIR pin mismatch\n"); return MSD_ERROR; } - uSdHandle.Instance = pinmap_merge_peripheral(sd_base, sdmmc_base); } #endif +#endif /* SDMMC1 || SDMMC2 */ /* Are all pins connected to the same SDx instance? */ if (uSdHandle.Instance == NP) { core_debug("ERROR: SD pins mismatch\n"); @@ -216,7 +240,7 @@ uint8_t BSP_SD_GetInstance(void) } return MSD_OK; } -#endif /* STM32_CORE_VERSION */ +#endif /* STM32_CORE_VERSION && (STM32_CORE_VERSION > 0x02050000) */ /** * @brief Initializes the SD card device with CS check if any. @@ -235,7 +259,7 @@ uint8_t BSP_SD_Init(void) if (BSP_SD_GetInstance() == MSD_ERROR) { return MSD_ERROR; } -#endif +#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */ uSdHandle.Init.ClockEdge = SD_CLK_EDGE; #if defined(SD_CLK_BYPASS) @@ -504,14 +528,28 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) pinmap_pinout(SD_PinNames.pin_cmd, PinMap_SD_CMD); pinmap_pinout(SD_PinNames.pin_ck, PinMap_SD_CK); #if defined(SDMMC1) || defined(SDMMC2) +#if !defined(SDMMC_CKIN_NA) if (SD_PinNames.pin_ckin != NC) { pinmap_pinout(SD_PinNames.pin_ckin, PinMap_SD_CKIN); + } +#endif +#if !defined(SDMMC_CDIR_NA) + if (SD_PinNames.pin_cdir != NC) { pinmap_pinout(SD_PinNames.pin_cdir, PinMap_SD_CDIR); + } +#endif +#if !defined(SDMMC_D0DIR_NA) + if (SD_PinNames.pin_d0dir != NC) { pinmap_pinout(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR); - pinmap_pinout(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); } #endif +#if !defined(SDMMC_D123DIR_NA) + if (SD_PinNames.pin_d123dir != NC) { + pinmap_pinout(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR); + } #endif +#endif /* SDMMC1 || SDMMC2 */ +#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */ /* Enable SD clock */ #if defined(SDMMC1) || defined(SDMMC2) #if defined(SDMMC1) @@ -574,14 +612,28 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params) HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_cmd), STM_GPIO_PIN(SD_PinNames.pin_cmd)); HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_ck), STM_GPIO_PIN(SD_PinNames.pin_ck)); #if defined(SDMMC1) || defined(SDMMC2) +#if !defined(SDMMC_CKIN_NA) if (SD_PinNames.pin_ckin != NC) { HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_ckin), STM_GPIO_PIN(SD_PinNames.pin_ckin)); + } +#endif +#if !defined(SDMMC_CDIR_NA) + if (SD_PinNames.pin_cdir != NC) { HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_cdir), STM_GPIO_PIN(SD_PinNames.pin_cdir)); + } +#endif +#if !defined(SDMMC_D0DIR_NA) + if (SD_PinNames.pin_d0dir != NC) { HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_d0dir), STM_GPIO_PIN(SD_PinNames.pin_d0dir)); - HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_d123dir), STM_GPIO_PIN(SD_PinNames.pin_d123dir)); } #endif +#if !defined(SDMMC_D123DIR_NA) + if (SD_PinNames.pin_d123dir != NC) { + HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(SD_PinNames.pin_d123dir), STM_GPIO_PIN(SD_PinNames.pin_d123dir)); + } #endif +#endif /* SDMMC1 || SDMMC2 */ +#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */ /* Disable SD clock */ #if defined(SDMMC1) || defined(SDMMC2) From 67baf6e54b5d8df82e52544724a1b834856df413 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Fri, 7 Jul 2023 10:42:11 +0200 Subject: [PATCH 5/5] workaround: prevent build issue with missing PinMap_SD* for SDMMC This commit is a workaround waiting update of the core to specify which target have or not SDMMC signals. Signed-off-by: Frederic Pillon --- src/bsp_sd.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/bsp_sd.h b/src/bsp_sd.h index a651c21..03443ad 100644 --- a/src/bsp_sd.h +++ b/src/bsp_sd.h @@ -90,6 +90,28 @@ Please update the core or install previous library version." #define GPIO_PIN_All GPIO_PIN_ALL #endif +/* Workaround while core does not defined *_NA for SDMMCx signals availability */ +#if defined(SDMMC1) || defined(SDMMC2) +#if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) ||\ + defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx) +#define STM32L4xx_PLUS +#endif +#if defined(STM32F7xx) || (defined(STM32L4xx) && !defined(STM32L4xx_PLUS)) +#if !defined(SDMMC_CKIN_NA) +#define SDMMC_CKIN_NA +#endif +#if !defined(SDMMC_CDIR_NA) +#define SDMMC_CDIR_NA +#endif +#if !defined(SDMMC_D0DIR_NA) +#define SDMMC_D0DIR_NA +#endif +#if !defined(SDMMC_D123DIR_NA) +#define SDMMC_D123DIR_NA +#endif +#endif /* STM32F7xx || STM32L4xx_PLUS */ +#endif /* SDMMC1 || SDMMC2 */ + /* Default SDx pins definitions */ #ifndef SDX_D0 #define SDX_D0 NUM_DIGITAL_PINS