Skip to content

Commit

Permalink
chore: add SD detect pin level
Browse files Browse the repository at this point in the history
Default: LOW

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Sep 19, 2024
1 parent d8cf24c commit 6bdc8fd
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ By default, if no pins are explicitly defined, the first one from each array is

#### SD detect and timeout
* `SD_DETECT_PIN` pin number

* `SD_DETECT_LEVEL` default `LOW`
* `SD_DATATIMEOUT` constant for Read/Write block
7 changes: 4 additions & 3 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ SDClass SD;

/**
* @brief Link SD, register the file system object to the FatFs mode and configure
* relatives SD IOs including SD Detect Pin if any
* relatives SD IOs including SD Detect Pin and level if any
* @param detect: detect pin number (default SD_DETECT_NONE)
* @param level: detect pin level (default SD_DETECT_LEVEL)
* @retval true or false
*/
bool SDClass::begin(uint32_t detect)
bool SDClass::begin(uint32_t detect, uint32_t level)
{
/*##-1- Initializes SD IOs #############################################*/
if (_card.init(detect)) {
if (_card.init(detect, level)) {
return _fatFs.init();
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/STM32SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SDClass {

public:
/* Initialize the SD peripheral */
bool begin(uint32_t detect = SD_DETECT_NONE);
bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);

// set* have to be called before begin()
void setDx(uint32_t data0, uint32_t data1 = PNUM_NOT_DEFINED, uint32_t data2 = PNUM_NOT_DEFINED, uint32_t data3 = PNUM_NOT_DEFINED)
Expand Down
4 changes: 2 additions & 2 deletions src/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ Sd2Card::Sd2Card()
#endif
}

bool Sd2Card::init(uint32_t detect)
bool Sd2Card::init(uint32_t detect, uint32_t level)
{
if (detect != SD_DETECT_NONE) {
PinName p = digitalPinToPinName(detect);
if ((p == NC) || \
BSP_SD_DetectPin(set_GPIO_Port_Clock(STM_PORT(p)),
STM_LL_GPIO_PIN(p)) != MSD_OK) {
STM_LL_GPIO_PIN(p), level) != MSD_OK) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Sd2Card {
public:
Sd2Card();

bool init(uint32_t detect = SD_DETECT_NONE);
bool init(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);

// set* have to be called before init()
void setDx(uint32_t data0, uint32_t data1 = PNUM_NOT_DEFINED, uint32_t data2 = PNUM_NOT_DEFINED, uint32_t data3 = PNUM_NOT_DEFINED)
Expand Down
13 changes: 6 additions & 7 deletions src/bsp_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
static SD_HandleTypeDef uSdHandle;
static uint32_t SD_detect_ll_gpio_pin = LL_GPIO_PIN_ALL;
static GPIO_TypeDef *SD_detect_gpio_port = GPIOA;
static uint32_t SD_detect_level = SD_DETECT_LEVEL;
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
static uint32_t SD_trans_en_ll_gpio_pin = LL_GPIO_PIN_ALL;
static GPIO_TypeDef *SD_trans_en_gpio_port = GPIOA;
Expand Down Expand Up @@ -379,16 +380,18 @@ uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef
#endif

/**
* @brief Set the SD card device detect pin and port.
* @brief Set the SD card device detect pin, port and level.
* @param port one of the gpio port
* @param pin one of the gpio pin
* @param level the level of the detect pin (HIGH or LOW)
* @retval SD status
*/
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin)
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin, uint32_t level)
{
if (port != 0) {
SD_detect_ll_gpio_pin = pin;
SD_detect_gpio_port = port;
SD_detect_level = level;
return MSD_OK;
}
return MSD_ERROR;
Expand Down Expand Up @@ -469,12 +472,8 @@ uint8_t BSP_SD_DetectITConfig(void (*callback)(void))
*/
uint8_t BSP_SD_IsDetected(void)
{
uint8_t status = SD_NOT_PRESENT;
/* Check SD card detect pin */
if (!LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin)) {
status = SD_PRESENT;
}
return status;
return (LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin) == SD_detect_level) ? SD_PRESENT : SD_NOT_PRESENT;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/bsp_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern "C" {
#include "PinNames.h"
#include "stm32_def.h"
#include "variant.h"
#include "wiring_constants.h"
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01060100)
#error "This library version required a STM32 core version > 1.6.1.\
Please update the core or install previous library version."
Expand All @@ -73,6 +74,9 @@ Please update the core or install previous library version."
#define SD_DETECT_NONE NUM_DIGITAL_PINS

/* Could be redefined in variant.h or using build_opt.h */
#ifndef SD_DETECT_LEVEL
#define SD_DETECT_LEVEL LOW
#endif
#ifndef SD_DATATIMEOUT
#define SD_DATATIMEOUT 100000000U
#endif
Expand Down Expand Up @@ -170,7 +174,7 @@ uint8_t BSP_SD_DeInit(void);
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef *selport, uint32_t selpin);
#endif
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin);
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin, uint32_t level);
uint8_t BSP_SD_DetectITConfig(void (*callback)(void));
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);
Expand Down

0 comments on commit 6bdc8fd

Please sign in to comment.