diff --git a/README.md b/README.md index 897faa7..7e3059b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/SD.cpp b/src/SD.cpp index 9da60b7..4322a19 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -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; diff --git a/src/STM32SD.h b/src/STM32SD.h index feb874c..9b7f451 100644 --- a/src/STM32SD.h +++ b/src/STM32SD.h @@ -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) diff --git a/src/Sd2Card.cpp b/src/Sd2Card.cpp index 4598d5d..4178f84 100644 --- a/src/Sd2Card.cpp +++ b/src/Sd2Card.cpp @@ -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; } } diff --git a/src/Sd2Card.h b/src/Sd2Card.h index a8b863c..2dfbd5f 100644 --- a/src/Sd2Card.h +++ b/src/Sd2Card.h @@ -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) diff --git a/src/bsp_sd.c b/src/bsp_sd.c index 13b904a..1cabd56 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -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; @@ -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; @@ -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; } /** diff --git a/src/bsp_sd.h b/src/bsp_sd.h index 9333024..e964403 100644 --- a/src/bsp_sd.h +++ b/src/bsp_sd.h @@ -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." @@ -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 @@ -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);