diff --git a/src/SD.cpp b/src/SD.cpp index d22ddec..f220f47 100644 --- a/src/SD.cpp +++ b/src/SD.cpp @@ -72,6 +72,20 @@ bool SDClass::begin(uint32_t detect, uint32_t level) return false; } +/** + * @brief UnLink SD, unregister the file system object and unconfigure + * relatives SD IOs including SD Detect Pin and level if any + * @retval true or false + */ +bool SDClass::end(void) +{ + /*##-1- DeInitializes SD IOs ###########################################*/ + if (_fatFs.deinit()) { + return _card.deinit(); + } + return false; +} + /** * @brief Check if a file or folder exist on the SD disk * @param filename: File name diff --git a/src/STM32SD.h b/src/STM32SD.h index 9b7f451..ed52396 100644 --- a/src/STM32SD.h +++ b/src/STM32SD.h @@ -87,6 +87,8 @@ class SDClass { public: /* Initialize the SD peripheral */ bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL); + /* Call this when a card is removed. It will allow to insert and initialise a new card. */ + bool end(void); // 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 5b96782..0b06b17 100644 --- a/src/Sd2Card.cpp +++ b/src/Sd2Card.cpp @@ -78,6 +78,14 @@ bool Sd2Card::init(uint32_t detect, uint32_t level) return false; } +bool Sd2Card::deinit(void) +{ + if (BSP_SD_DeInit() == MSD_OK) { + return true; + } + return false; +} + uint8_t Sd2Card::type(void) const { uint8_t cardType = SD_CARD_TYPE_UKN; diff --git a/src/Sd2Card.h b/src/Sd2Card.h index 2dfbd5f..69d6818 100644 --- a/src/Sd2Card.h +++ b/src/Sd2Card.h @@ -55,6 +55,7 @@ class Sd2Card { Sd2Card(); bool init(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL); + bool deinit(void); // 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/SdFatFs.cpp b/src/SdFatFs.cpp index 9dea4bd..c59c6cc 100644 --- a/src/SdFatFs.cpp +++ b/src/SdFatFs.cpp @@ -51,6 +51,19 @@ bool SdFatFs::init(void) return false; } +bool SdFatFs::deinit(void) +{ + /*##-1- Unregister the file system object to the FatFs module ##############*/ + if (f_unmount((TCHAR const *)_SDPath) == FR_OK) { + /*##-2- Unlink the SD disk I/O driver ####################################*/ + if (FATFS_UnLinkDriver(_SDPath) == 0) { + /* FatFs deInitialization done */ + return true; + } + } + return false; +} + uint8_t SdFatFs::fatType(void) { switch (_SDFatFs.fs_type) { diff --git a/src/SdFatFs.h b/src/SdFatFs.h index fdef3ba..6f7be73 100644 --- a/src/SdFatFs.h +++ b/src/SdFatFs.h @@ -82,6 +82,7 @@ class SdFatFs { public: bool init(void); + bool deinit(void); /** Return the FatFs type: 12, 16, 32 (0: unknown)*/ uint8_t fatType(void); diff --git a/src/bsp_sd.c b/src/bsp_sd.c index d624125..054f28a 100644 --- a/src/bsp_sd.c +++ b/src/bsp_sd.c @@ -354,6 +354,15 @@ uint8_t BSP_SD_DeInit(void) /* Msp SD deinitialization */ BSP_SD_MspDeInit(&uSdHandle, NULL); + + if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) { + BSP_SD_Detect_MspDeInit(&uSdHandle, NULL); + } +#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) + BSP_SD_Transceiver_MspDeInit(&uSdHandle, NULL); +#endif + + return sd_state; } @@ -523,26 +532,6 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) #endif } -/** - * @brief Initializes the SD Detect pin MSP. - * @param hsd: SD handle - * @param Params : pointer on additional configuration parameters, can be NULL. - */ -__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params) -{ - UNUSED(hsd); - UNUSED(Params); - - /* GPIO configuration in input for uSD_Detect signal */ -#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH - LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH); -#else - LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH); -#endif - LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT); - LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP); -} - /** * @brief DeInitializes the SD MSP. * @param hsd: SD handle @@ -608,6 +597,47 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params) #endif } +/** + * @brief Initializes the SD Detect pin MSP. + * @param hsd: SD handle + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params) +{ + UNUSED(hsd); + UNUSED(Params); + + /* GPIO configuration in input for uSD_Detect signal */ +#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH + LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH); +#else + LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH); +#endif + LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT); + LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP); +} + +/** + * @brief DeInitializes the SD Detect pin MSP. + * @param hsd: SD handle + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_SD_Detect_MspDeInit(SD_HandleTypeDef *hsd, void *Params) +{ + UNUSED(hsd); + UNUSED(Params); + + /* GPIO configuration in analog to saves the consumption */ + LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW); + #ifndef LL_GPIO_PULL_NO + /* For STM32F1xx */ + LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING); + #else + LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_NO); + #endif + LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_ANALOG); +} + #if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) /** * @brief Initializes the SD Transceiver pin MSP. @@ -621,12 +651,20 @@ __weak void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params) LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH); LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_OUTPUT); - LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO); - + #ifndef LL_GPIO_PULL_NO + /* For STM32F1xx */ + LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING); + #else + LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO); + #endif LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH); LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_OUTPUT); - LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO); - + #ifndef LL_GPIO_PULL_NO + /* For STM32F1xx */ + LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING); + #else + LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO); + #endif /* Enable the level shifter */ LL_GPIO_SetOutputPin(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin); @@ -634,6 +672,25 @@ __weak void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params) LL_GPIO_ResetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin); } +/** + * @brief DeInitializes the SD Transceiver pin MSP. + * @param hsd: SD handle + * @param Params : pointer on additional configuration parameters, can be NULL. + */ +__weak void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params) +{ + UNUSED(hsd); + UNUSED(Params); + + LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW); + LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_ANALOG); + LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO); + + LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW); + LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_ANALOG); + LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO); +} + /** * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback. * @param status: Voltage Switch State diff --git a/src/bsp_sd.h b/src/bsp_sd.h index bf95f49..3828ee6 100644 --- a/src/bsp_sd.h +++ b/src/bsp_sd.h @@ -185,10 +185,12 @@ uint8_t BSP_SD_IsDetected(void); /* These __weak function can be surcharged by application code in case the current settings (e.g. DMA stream) need to be changed for specific needs */ void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params); -void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params); void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params); +void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params); +void BSP_SD_Detect_MspDeInit(SD_HandleTypeDef *hsd, void *Params); #if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U) void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params); +void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params); #endif #ifdef __cplusplus