Skip to content

Commit 835bb65

Browse files
committed
Unify begin() methods and BSP_SD_*init() functions
Use methods with default parameters value. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent ee2fa02 commit 835bb65

File tree

6 files changed

+34
-92
lines changed

6 files changed

+34
-92
lines changed

src/SD.cpp

+2-23
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,7 @@ SDClass SD;
5959

6060
/**
6161
* @brief Link SD, register the file system object to the FatFs mode and configure
62-
* relatives SD IOs except SD Detect Pin
63-
* @param None
64-
* @retval TRUE or FALSE
65-
*/
66-
uint8_t SDClass::begin()
67-
{
68-
/*##-1- Initializes SD IOs #############################################*/
69-
if (_card.init()) {
70-
return _fatFs.init();
71-
}
72-
else
73-
{
74-
return FALSE;
75-
}
76-
}
77-
78-
/**
79-
* @brief Link SD, register the file system object to the FatFs mode and configure
80-
* relatives SD IOs including SD Detect Pin
62+
* relatives SD IOs including SD Detect Pin if any
8163
* @param None
8264
* @retval TRUE or FALSE
8365
*/
@@ -87,10 +69,7 @@ uint8_t SDClass::begin(uint32_t cspin)
8769
if (_card.init(cspin)) {
8870
return _fatFs.init();
8971
}
90-
else
91-
{
92-
return FALSE;
93-
}
72+
return FALSE;
9473
}
9574

9675
/**

src/STM32SD.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ class SDClass {
7878
public:
7979

8080
/* Initialize the SD peripheral */
81-
uint8_t begin();
82-
uint8_t begin(uint32_t cspin);
81+
uint8_t begin(uint32_t cspin = SD_DETECT_NONE);
8382
static File open(const char *filepath, uint8_t mode);
8483
static File open(const char *filepath);
8584
static uint8_t exists(const char *filepath);

src/Sd2Card.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,18 @@
3737
#include <Arduino.h>
3838
#include "Sd2Card.h"
3939

40-
uint8_t Sd2Card::init(void) {
40+
uint8_t Sd2Card::init(uint32_t cspin) {
41+
if(cspin != SD_DETECT_NONE) {
42+
PinName p = digitalPinToPinName(cspin);
43+
if((p == NC) ||\
44+
BSP_SD_CSSet(set_GPIO_Port_Clock(STM_PORT(p)),
45+
STM_GPIO_PIN(p)) != MSD_OK) {
46+
return FALSE;
47+
}
48+
}
4149
if (BSP_SD_Init() == MSD_OK) {
4250
BSP_SD_GetCardInfo(&_SdCardInfo);
4351
return TRUE;
44-
} else {
45-
return FALSE;
46-
}
47-
}
48-
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-
}
5652
}
5753
return FALSE;
5854
}

src/Sd2Card.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
class Sd2Card {
5757
public:
5858

59-
uint8_t init(void);
60-
uint8_t init(uint32_t cspin);
59+
uint8_t init(uint32_t cspin = SD_DETECT_NONE);
6160

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

src/bsp_sd.c

+19-51
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static SD_CardInfo uSdCardInfo;
127127

128128

129129
/**
130-
* @brief Initializes the SD card device without CS initialization.
130+
* @brief Initializes the SD card device with CS check if any.
131131
* @retval SD status
132132
*/
133133
uint8_t BSP_SD_Init(void)
@@ -146,6 +146,15 @@ uint8_t BSP_SD_Init(void)
146146
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
147147
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
148148

149+
if(SD_detect_gpio_pin != GPIO_PIN_All) {
150+
/* Msp SD Detect pin initialization */
151+
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
152+
if(BSP_SD_IsDetected() != SD_PRESENT) /* Check if SD card is present */
153+
{
154+
return MSD_ERROR_SD_NOT_PRESENT;
155+
}
156+
}
157+
149158
/* Msp SD initialization */
150159
BSP_SD_MspInit(&uSdHandle, NULL);
151160

@@ -176,60 +185,19 @@ uint8_t BSP_SD_Init(void)
176185
}
177186

178187
/**
179-
* @brief Initializes the SD card device with CS initialization.
188+
* @brief Set the SD card device detect pin and port.
189+
* @param csport one of the gpio port
190+
* @param cspin one of the gpio pin
180191
* @retval SD status
181192
*/
182-
uint8_t BSP_SD_CSInit(GPIO_TypeDef *csport, uint32_t cspin)
193+
uint8_t BSP_SD_CSSet(GPIO_TypeDef *csport, uint32_t cspin)
183194
{
184-
uint8_t sd_state = MSD_OK;
185-
SD_detect_gpio_pin = cspin;
186-
SD_detect_gpio_port = csport;
187-
/* PLLSAI is dedicated to LCD periph. Do not use it to get 48MHz*/
188-
189-
/* uSD device interface configuration */
190-
uSdHandle.Instance = SD_INSTANCE;
191-
192-
uSdHandle.Init.ClockEdge = SD_CLK_EDGE;
193-
uSdHandle.Init.ClockBypass = SD_CLK_BYPASS;
194-
uSdHandle.Init.ClockPowerSave = SD_CLK_PWR_SAVE;
195-
uSdHandle.Init.BusWide = SD_BUS_WIDE_1B;
196-
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
197-
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
198-
199-
/* Msp SD Detect pin initialization */
200-
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
201-
if(BSP_SD_IsDetected() != SD_PRESENT) /* Check if SD card is present */
202-
{
203-
return MSD_ERROR_SD_NOT_PRESENT;
204-
}
205-
206-
/* Msp SD initialization */
207-
BSP_SD_MspInit(&uSdHandle, NULL);
208-
209-
/* HAL SD initialization */
210-
#if defined (STM32F4xx) || defined(STM32F7xx) || defined(STM32L4xx)
211-
if(HAL_SD_Init(&uSdHandle) != SD_OK)
212-
#else /* (STM32F1xx) || defined(STM32F2xx) || defined(STM32L1xx) */
213-
if(HAL_SD_Init(&uSdHandle, &uSdCardInfo) != SD_OK)
214-
#endif
215-
{
216-
sd_state = MSD_ERROR;
195+
if(csport != 0) {
196+
SD_detect_gpio_pin = cspin;
197+
SD_detect_gpio_port = csport;
198+
return MSD_OK;
217199
}
218-
219-
/* Configure SD Bus width */
220-
if(sd_state == MSD_OK)
221-
{
222-
/* Enable wide operation */
223-
if(HAL_SD_WideBusOperation_Config(&uSdHandle, SD_BUS_WIDE_4B) != SD_OK)
224-
{
225-
sd_state = MSD_ERROR;
226-
}
227-
else
228-
{
229-
sd_state = MSD_OK;
230-
}
231-
}
232-
return sd_state;
200+
return MSD_ERROR;
233201
}
234202

235203
/**

src/bsp_sd.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@
6868
/* SD Exported Constants */
6969
#define SD_PRESENT ((uint8_t)0x01)
7070
#define SD_NOT_PRESENT ((uint8_t)0x00)
71+
#define SD_DETECT_NONE NUM_DIGITAL_PINS
7172

7273
/* SD Exported Functions */
7374
uint8_t BSP_SD_Init(void);
74-
uint8_t BSP_SD_CSInit(GPIO_TypeDef *csport, uint32_t cspin);
75+
uint8_t BSP_SD_CSSet(GPIO_TypeDef *csport, uint32_t cspin);
7576
uint8_t BSP_SD_DeInit(void);
7677
uint8_t BSP_SD_ITConfig(void);
7778

0 commit comments

Comments
 (0)