Skip to content

Commit 6bdc8fd

Browse files
committed
chore: add SD detect pin level
Default: LOW Signed-off-by: Frederic Pillon <[email protected]>
1 parent d8cf24c commit 6bdc8fd

File tree

7 files changed

+20
-16
lines changed

7 files changed

+20
-16
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ By default, if no pins are explicitly defined, the first one from each array is
139139

140140
#### SD detect and timeout
141141
* `SD_DETECT_PIN` pin number
142-
142+
* `SD_DETECT_LEVEL` default `LOW`
143143
* `SD_DATATIMEOUT` constant for Read/Write block

src/SD.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ SDClass SD;
5858

5959
/**
6060
* @brief Link SD, register the file system object to the FatFs mode and configure
61-
* relatives SD IOs including SD Detect Pin if any
61+
* relatives SD IOs including SD Detect Pin and level if any
6262
* @param detect: detect pin number (default SD_DETECT_NONE)
63+
* @param level: detect pin level (default SD_DETECT_LEVEL)
6364
* @retval true or false
6465
*/
65-
bool SDClass::begin(uint32_t detect)
66+
bool SDClass::begin(uint32_t detect, uint32_t level)
6667
{
6768
/*##-1- Initializes SD IOs #############################################*/
68-
if (_card.init(detect)) {
69+
if (_card.init(detect, level)) {
6970
return _fatFs.init();
7071
}
7172
return false;

src/STM32SD.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class SDClass {
8686

8787
public:
8888
/* Initialize the SD peripheral */
89-
bool begin(uint32_t detect = SD_DETECT_NONE);
89+
bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);
9090

9191
// set* have to be called before begin()
9292
void setDx(uint32_t data0, uint32_t data1 = PNUM_NOT_DEFINED, uint32_t data2 = PNUM_NOT_DEFINED, uint32_t data3 = PNUM_NOT_DEFINED)

src/Sd2Card.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ Sd2Card::Sd2Card()
5252
#endif
5353
}
5454

55-
bool Sd2Card::init(uint32_t detect)
55+
bool Sd2Card::init(uint32_t detect, uint32_t level)
5656
{
5757
if (detect != SD_DETECT_NONE) {
5858
PinName p = digitalPinToPinName(detect);
5959
if ((p == NC) || \
6060
BSP_SD_DetectPin(set_GPIO_Port_Clock(STM_PORT(p)),
61-
STM_LL_GPIO_PIN(p)) != MSD_OK) {
61+
STM_LL_GPIO_PIN(p), level) != MSD_OK) {
6262
return false;
6363
}
6464
}

src/Sd2Card.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Sd2Card {
5454
public:
5555
Sd2Card();
5656

57-
bool init(uint32_t detect = SD_DETECT_NONE);
57+
bool init(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);
5858

5959
// set* have to be called before init()
6060
void setDx(uint32_t data0, uint32_t data1 = PNUM_NOT_DEFINED, uint32_t data2 = PNUM_NOT_DEFINED, uint32_t data3 = PNUM_NOT_DEFINED)

src/bsp_sd.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
static SD_HandleTypeDef uSdHandle;
109109
static uint32_t SD_detect_ll_gpio_pin = LL_GPIO_PIN_ALL;
110110
static GPIO_TypeDef *SD_detect_gpio_port = GPIOA;
111+
static uint32_t SD_detect_level = SD_DETECT_LEVEL;
111112
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
112113
static uint32_t SD_trans_en_ll_gpio_pin = LL_GPIO_PIN_ALL;
113114
static GPIO_TypeDef *SD_trans_en_gpio_port = GPIOA;
@@ -379,16 +380,18 @@ uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef
379380
#endif
380381

381382
/**
382-
* @brief Set the SD card device detect pin and port.
383+
* @brief Set the SD card device detect pin, port and level.
383384
* @param port one of the gpio port
384385
* @param pin one of the gpio pin
386+
* @param level the level of the detect pin (HIGH or LOW)
385387
* @retval SD status
386388
*/
387-
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin)
389+
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin, uint32_t level)
388390
{
389391
if (port != 0) {
390392
SD_detect_ll_gpio_pin = pin;
391393
SD_detect_gpio_port = port;
394+
SD_detect_level = level;
392395
return MSD_OK;
393396
}
394397
return MSD_ERROR;
@@ -469,12 +472,8 @@ uint8_t BSP_SD_DetectITConfig(void (*callback)(void))
469472
*/
470473
uint8_t BSP_SD_IsDetected(void)
471474
{
472-
uint8_t status = SD_NOT_PRESENT;
473475
/* Check SD card detect pin */
474-
if (!LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin)) {
475-
status = SD_PRESENT;
476-
}
477-
return status;
476+
return (LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin) == SD_detect_level) ? SD_PRESENT : SD_NOT_PRESENT;
478477
}
479478

480479
/**

src/bsp_sd.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "C" {
4747
#include "PinNames.h"
4848
#include "stm32_def.h"
4949
#include "variant.h"
50+
#include "wiring_constants.h"
5051
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100)
5152
#error "This library version required a STM32 core version > 1.6.1.\
5253
Please update the core or install previous library version."
@@ -73,6 +74,9 @@ Please update the core or install previous library version."
7374
#define SD_DETECT_NONE NUM_DIGITAL_PINS
7475

7576
/* Could be redefined in variant.h or using build_opt.h */
77+
#ifndef SD_DETECT_LEVEL
78+
#define SD_DETECT_LEVEL LOW
79+
#endif
7680
#ifndef SD_DATATIMEOUT
7781
#define SD_DATATIMEOUT 100000000U
7882
#endif
@@ -170,7 +174,7 @@ uint8_t BSP_SD_DeInit(void);
170174
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
171175
uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef *selport, uint32_t selpin);
172176
#endif
173-
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin);
177+
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin, uint32_t level);
174178
uint8_t BSP_SD_DetectITConfig(void (*callback)(void));
175179
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
176180
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);

0 commit comments

Comments
 (0)