Skip to content

Commit 7d9f2ac

Browse files
committed
Fix SD detect pin usage
SD detect pin was hardcoded in sd_conf.h while API provide a way to specify it. This requires to provide the pin number instead of the GPIO_PIN_x pin and GPIOX port. That's why sd_conf.h has been removed from the core. Moreove several definition for the BSP are common or only dependent to SD instance. SD instance could be SDMMC1 or SDIO depending of the STM32 series. Only the SD detect definition should be redefined thanks the variant.h if the board has one. Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 8fd6424 commit 7d9f2ac

File tree

6 files changed

+107
-27
lines changed

6 files changed

+107
-27
lines changed

src/SD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ uint8_t SDClass::begin()
8181
* @param None
8282
* @retval TRUE or FALSE
8383
*/
84-
uint8_t SDClass::begin(uint8_t cspin)
84+
uint8_t SDClass::begin(uint32_t cspin)
8585
{
8686
/*##-1- Initializes SD IOs #############################################*/
8787
if (_card.init(cspin)) {

src/STM32SD.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class SDClass {
7979

8080
/* Initialize the SD peripheral */
8181
uint8_t begin();
82-
uint8_t begin(uint8_t cspin);
82+
uint8_t begin(uint32_t cspin);
8383
static File open(const char *filepath, uint8_t mode);
8484
static File open(const char *filepath);
8585
static uint8_t exists(const char *filepath);

src/Sd2Card.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ uint8_t Sd2Card::init(void) {
4646
}
4747
}
4848

49-
uint8_t Sd2Card::init(uint8_t cspin) {
50-
if (BSP_SD_CSInit() == MSD_OK) {
51-
BSP_SD_GetCardInfo(&_SdCardInfo);
52-
return TRUE;
53-
} else {
54-
return FALSE;
49+
uint8_t Sd2Card::init(uint32_t cspin) {
50+
PinName p = digitalPinToPinName(cspin);
51+
if(p != NC) {
52+
if (BSP_SD_CSInit(set_GPIO_Port_Clock(STM_PORT(p)), STM_GPIO_PIN(p)) == MSD_OK) {
53+
BSP_SD_GetCardInfo(&_SdCardInfo);
54+
return TRUE;
55+
}
5556
}
57+
return FALSE;
5658
}
5759

5860
uint8_t Sd2Card::type(void) const {

src/Sd2Card.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Sd2Card {
5757
public:
5858

5959
uint8_t init(void);
60-
uint8_t init(uint8_t cspin);
60+
uint8_t init(uint32_t cspin);
6161

6262
/** Return the card type: SD V1, SD V2 or SDHC */
6363
uint8_t type(void) const;

src/bsp_sd.c

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,42 @@
8181
/* Includes ------------------------------------------------------------------*/
8282
#include "bsp_sd.h"
8383

84+
#ifdef SDMMC1
85+
/* Definition for BSP SD */
86+
#define SD_INSTANCE SDMMC1
87+
#define SD_CLK_ENABLE __HAL_RCC_SDMMC1_CLK_ENABLE
88+
#define SD_CLK_DISABLE __HAL_RCC_SDMMC1_CLK_DISABLE
89+
#define SD_CLK_EDGE SDMMC_CLOCK_EDGE_RISING
90+
#define SD_CLK_BYPASS SDMMC_CLOCK_BYPASS_DISABLE
91+
#define SD_CLK_PWR_SAVE SDMMC_CLOCK_POWER_SAVE_DISABLE
92+
#define SD_BUS_WIDE_1B SDMMC_BUS_WIDE_1B
93+
#define SD_BUS_WIDE_4B SDMMC_BUS_WIDE_4B
94+
#define SD_HW_FLOW_CTRL SDMMC_HARDWARE_FLOW_CONTROL_DISABLE
95+
#define SD_CLK_DIV SDMMC_TRANSFER_CLK_DIV
96+
/* Definition for MSP SD */
97+
#define SD_AF GPIO_AF12_SDMMC1
98+
#elif defined(SDIO)
99+
/* Definition for BSP SD */
100+
#define SD_INSTANCE SDIO
101+
#define SD_CLK_ENABLE __HAL_RCC_SDIO_CLK_ENABLE
102+
#define SD_CLK_DISABLE __HAL_RCC_SDIO_CLK_DISABLE
103+
#define SD_CLK_EDGE SDIO_CLOCK_EDGE_RISING
104+
#define SD_CLK_BYPASS SDIO_CLOCK_BYPASS_DISABLE
105+
#define SD_CLK_PWR_SAVE SDIO_CLOCK_POWER_SAVE_DISABLE
106+
#define SD_BUS_WIDE_1B SDIO_BUS_WIDE_1B
107+
#define SD_BUS_WIDE_4B SDIO_BUS_WIDE_4B
108+
#define SD_HW_FLOW_CTRL SDIO_HARDWARE_FLOW_CONTROL_DISABLE
109+
#define SD_CLK_DIV SDIO_TRANSFER_CLK_DIV
110+
/* Definition for MSP SD */
111+
#define SD_AF GPIO_AF12_SDIO
112+
#else
113+
#error "Unknown SD_INSTANCE"
114+
#endif
115+
84116
/* BSP SD Private Variables */
85117
static SD_HandleTypeDef uSdHandle;
118+
static uint32_t SD_detect_gpio_pin = GPIO_PIN_All;
119+
static GPIO_TypeDef *SD_detect_gpio_port = GPIOA;
86120
#if defined (STM32F4xx) || defined(STM32F7xx) || defined(STM32L4xx)
87121
#define SD_OK HAL_OK
88122
#define SD_TRANSFER_OK ((uint8_t)0x00)
@@ -145,10 +179,11 @@ uint8_t BSP_SD_Init(void)
145179
* @brief Initializes the SD card device with CS initialization.
146180
* @retval SD status
147181
*/
148-
uint8_t BSP_SD_CSInit(void)
182+
uint8_t BSP_SD_CSInit(GPIO_TypeDef *csport, uint32_t cspin)
149183
{
150184
uint8_t sd_state = MSD_OK;
151-
185+
SD_detect_gpio_pin = cspin;
186+
SD_detect_gpio_port = csport;
152187
/* PLLSAI is dedicated to LCD periph. Do not use it to get 48MHz*/
153188

154189
/* uSD device interface configuration */
@@ -226,20 +261,61 @@ uint8_t BSP_SD_DeInit(void)
226261
*/
227262
uint8_t BSP_SD_ITConfig(void)
228263
{
264+
uint8_t sd_state = MSD_OK;
229265
GPIO_InitTypeDef gpio_init_structure;
266+
IRQn_Type sd_detect_EXTI_IRQn = EXTI0_IRQn;
230267

231268
/* Configure Interrupt mode for SD detection pin */
232-
gpio_init_structure.Pin = SD_DETECT_PIN;
269+
gpio_init_structure.Pin = SD_detect_gpio_pin;
233270
gpio_init_structure.Pull = GPIO_PULLUP;
234271
gpio_init_structure.Speed = GPIO_SPEED_FAST;
235272
gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
236-
HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
237-
238-
/* Enable and set SD detect EXTI Interrupt to the lowest priority */
239-
HAL_NVIC_SetPriority((IRQn_Type)(SD_DETECT_EXTI_IRQn), 0x0F, 0x00);
240-
HAL_NVIC_EnableIRQ((IRQn_Type)(SD_DETECT_EXTI_IRQn));
241-
242-
return MSD_OK;
273+
HAL_GPIO_Init(SD_detect_gpio_port, &gpio_init_structure);
274+
275+
if(SD_detect_gpio_pin == GPIO_PIN_0) {
276+
sd_detect_EXTI_IRQn = EXTI0_IRQn;
277+
} else {
278+
if(SD_detect_gpio_pin == GPIO_PIN_1) {
279+
sd_detect_EXTI_IRQn = EXTI1_IRQn;
280+
} else {
281+
if(SD_detect_gpio_pin == GPIO_PIN_2) {
282+
sd_detect_EXTI_IRQn = EXTI2_IRQn;
283+
} else {
284+
if(SD_detect_gpio_pin == GPIO_PIN_3) {
285+
sd_detect_EXTI_IRQn = EXTI3_IRQn;
286+
} else {
287+
if(SD_detect_gpio_pin == GPIO_PIN_4) {
288+
sd_detect_EXTI_IRQn = EXTI4_IRQn;
289+
} else {
290+
if((SD_detect_gpio_pin == GPIO_PIN_5) ||\
291+
(SD_detect_gpio_pin == GPIO_PIN_6) ||\
292+
(SD_detect_gpio_pin == GPIO_PIN_7) ||\
293+
(SD_detect_gpio_pin == GPIO_PIN_8) ||\
294+
(SD_detect_gpio_pin == GPIO_PIN_9)) {
295+
sd_detect_EXTI_IRQn = EXTI9_5_IRQn;
296+
} else {
297+
if((SD_detect_gpio_pin == GPIO_PIN_10) ||\
298+
(SD_detect_gpio_pin == GPIO_PIN_11) ||\
299+
(SD_detect_gpio_pin == GPIO_PIN_12) ||\
300+
(SD_detect_gpio_pin == GPIO_PIN_13) ||\
301+
(SD_detect_gpio_pin == GPIO_PIN_14) ||\
302+
(SD_detect_gpio_pin == GPIO_PIN_15)) {
303+
sd_detect_EXTI_IRQn = EXTI15_10_IRQn;
304+
} else {
305+
sd_state = MSD_ERROR;
306+
}
307+
}
308+
}
309+
}
310+
}
311+
}
312+
}
313+
if(sd_state == MSD_OK) {
314+
/* Enable and set SD detect EXTI Interrupt to the lowest priority */
315+
HAL_NVIC_SetPriority(sd_detect_EXTI_IRQn, 0x0F, 0x00);
316+
HAL_NVIC_EnableIRQ(sd_detect_EXTI_IRQn);
317+
}
318+
return sd_state;
243319
}
244320

245321
/**
@@ -251,7 +327,7 @@ uint8_t BSP_SD_IsDetected(void)
251327
uint8_t status = SD_PRESENT;
252328

253329
/* Check SD card detect pin */
254-
if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET)
330+
if (HAL_GPIO_ReadPin(SD_detect_gpio_port, SD_detect_gpio_pin) == GPIO_PIN_SET)
255331
{
256332
status = SD_NOT_PRESENT;
257333
}
@@ -363,14 +439,12 @@ __weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
363439
UNUSED(Params);
364440
GPIO_InitTypeDef gpio_init_structure;
365441

366-
SD_DETECT_GPIO_CLK_ENABLE();
367-
368442
/* GPIO configuration in input for uSD_Detect signal */
369-
gpio_init_structure.Pin = SD_DETECT_PIN;
443+
gpio_init_structure.Pin = SD_detect_gpio_pin;
370444
gpio_init_structure.Mode = GPIO_MODE_INPUT;
371445
gpio_init_structure.Pull = GPIO_PULLUP;
372446
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
373-
HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
447+
HAL_GPIO_Init(SD_detect_gpio_port, &gpio_init_structure);
374448
}
375449

376450
/**
@@ -389,7 +463,7 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
389463
(by surcharging this __weak function) */
390464

391465
/* Disable SDIO clock */
392-
__HAL_RCC_SDIO_CLK_DISABLE();
466+
SD_CLK_DISABLE();
393467

394468
/* GPOI pins clock and DMA cloks can be shut down in the applic
395469
by surcgarging this __weak function */

src/bsp_sd.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
#endif
4545

4646
/* Includes ------------------------------------------------------------------*/
47-
#include "sd_conf.h"
47+
#include "variant.h"
48+
/* Could be redefined in variant.h or using build_opt.h */
49+
#ifndef SD_DATATIMEOUT
50+
#define SD_DATATIMEOUT 100000000U
51+
#endif
4852

4953
/*SD Card information structure */
5054
#if defined (STM32F4xx) || defined(STM32F7xx) || defined(STM32L4xx)
@@ -67,7 +71,7 @@
6771

6872
/* SD Exported Functions */
6973
uint8_t BSP_SD_Init(void);
70-
uint8_t BSP_SD_CSInit(void);
74+
uint8_t BSP_SD_CSInit(GPIO_TypeDef *csport, uint32_t cspin);
7175
uint8_t BSP_SD_DeInit(void);
7276
uint8_t BSP_SD_ITConfig(void);
7377

0 commit comments

Comments
 (0)