Skip to content

Commit

Permalink
Merge pull request #61 from fpistm/fix
Browse files Browse the repository at this point in the history
fix: wrong preprocessor definitions
  • Loading branch information
fpistm committed Jul 7, 2023
2 parents c000dfe + 67baf6e commit 4755239
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 40 deletions.
137 changes: 97 additions & 40 deletions src/bsp_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -160,12 +155,20 @@ 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)
#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 /* SDMMC1 && SDMMC2 */
#endif
#endif /* SDMMC1 || SDMMC2 */
}
/* Get SD instance from pins */
sd_d0 = pinmap_peripheral(SD_PinNames.pin_d0, PinMap_SD_DATA0);
Expand All @@ -192,36 +195,52 @@ uint8_t BSP_SD_GetInstance(void)
return MSD_ERROR;
}
uSdHandle.Instance = sd_base;
#if defined(SDMMC1) && defined(SDMMC2)
#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");
return MSD_ERROR;
}
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.
Expand All @@ -240,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)
Expand Down Expand Up @@ -508,25 +527,44 @@ __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 !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);
}
#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) || 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();
#endif
__HAL_RCC_SDIO_CLK_ENABLE();
#endif
}

Expand Down Expand Up @@ -573,26 +611,45 @@ __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 !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)
#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
}

Expand Down
22 changes: 22 additions & 0 deletions src/bsp_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4755239

Please sign in to comment.