Skip to content

Commit 063cdab

Browse files
committed
chore: add end() method
Fixes #71 Signed-off-by: Frederic Pillon <[email protected]>
1 parent f4bf655 commit 063cdab

File tree

8 files changed

+121
-23
lines changed

8 files changed

+121
-23
lines changed

src/SD.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ bool SDClass::begin(uint32_t detect, uint32_t level)
7272
return false;
7373
}
7474

75+
/**
76+
* @brief UnLink SD, unregister the file system object and unconfigure
77+
* relatives SD IOs including SD Detect Pin and level if any
78+
* @retval true or false
79+
*/
80+
bool SDClass::end(void)
81+
{
82+
/*##-1- DeInitializes SD IOs ###########################################*/
83+
if (_fatFs.deinit()) {
84+
return _card.deinit();
85+
}
86+
return false;
87+
}
88+
7589
/**
7690
* @brief Check if a file or folder exist on the SD disk
7791
* @param filename: File name

src/STM32SD.h

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class SDClass {
8787
public:
8888
/* Initialize the SD peripheral */
8989
bool begin(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);
90+
/* Call this when a card is removed. It will allow to insert and initialise a new card. */
91+
bool end(void);
9092

9193
// set* have to be called before begin()
9294
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

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ bool Sd2Card::init(uint32_t detect, uint32_t level)
7878
return false;
7979
}
8080

81+
bool Sd2Card::deinit(void)
82+
{
83+
if (BSP_SD_DeInit() == MSD_OK) {
84+
return true;
85+
}
86+
return false;
87+
}
88+
8189
uint8_t Sd2Card::type(void) const
8290
{
8391
uint8_t cardType = SD_CARD_TYPE_UKN;

src/Sd2Card.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Sd2Card {
5555
Sd2Card();
5656

5757
bool init(uint32_t detect = SD_DETECT_NONE, uint32_t level = SD_DETECT_LEVEL);
58+
bool deinit(void);
5859

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

src/SdFatFs.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ bool SdFatFs::init(void)
5151
return false;
5252
}
5353

54+
bool SdFatFs::deinit(void)
55+
{
56+
/*##-1- Unregister the file system object to the FatFs module ##############*/
57+
if (f_unmount((TCHAR const *)_SDPath) == FR_OK) {
58+
/*##-2- Unlink the SD disk I/O driver ####################################*/
59+
if (FATFS_UnLinkDriver(_SDPath) == 0) {
60+
/* FatFs deInitialization done */
61+
return true;
62+
}
63+
}
64+
return false;
65+
}
66+
5467
uint8_t SdFatFs::fatType(void)
5568
{
5669
switch (_SDFatFs.fs_type) {

src/SdFatFs.h

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class SdFatFs {
8282
public:
8383

8484
bool init(void);
85+
bool deinit(void);
8586

8687
/** Return the FatFs type: 12, 16, 32 (0: unknown)*/
8788
uint8_t fatType(void);

src/bsp_sd.c

+79-22
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ uint8_t BSP_SD_DeInit(void)
354354
/* Msp SD deinitialization */
355355
BSP_SD_MspDeInit(&uSdHandle, NULL);
356356

357+
358+
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
359+
BSP_SD_Detect_MspDeInit(&uSdHandle, NULL);
360+
}
361+
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
362+
BSP_SD_Transceiver_MspDeInit(&uSdHandle, NULL);
363+
#endif
364+
365+
357366
return sd_state;
358367
}
359368

@@ -523,26 +532,6 @@ __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
523532
#endif
524533
}
525534

526-
/**
527-
* @brief Initializes the SD Detect pin MSP.
528-
* @param hsd: SD handle
529-
* @param Params : pointer on additional configuration parameters, can be NULL.
530-
*/
531-
__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
532-
{
533-
UNUSED(hsd);
534-
UNUSED(Params);
535-
536-
/* GPIO configuration in input for uSD_Detect signal */
537-
#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH
538-
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
539-
#else
540-
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
541-
#endif
542-
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT);
543-
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP);
544-
}
545-
546535
/**
547536
* @brief DeInitializes the SD MSP.
548537
* @param hsd: SD handle
@@ -608,6 +597,47 @@ __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
608597
#endif
609598
}
610599

600+
/**
601+
* @brief Initializes the SD Detect pin MSP.
602+
* @param hsd: SD handle
603+
* @param Params : pointer on additional configuration parameters, can be NULL.
604+
*/
605+
__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
606+
{
607+
UNUSED(hsd);
608+
UNUSED(Params);
609+
610+
/* GPIO configuration in input for uSD_Detect signal */
611+
#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH
612+
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
613+
#else
614+
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
615+
#endif
616+
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT);
617+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP);
618+
}
619+
620+
/**
621+
* @brief DeInitializes the SD Detect pin MSP.
622+
* @param hsd: SD handle
623+
* @param Params : pointer on additional configuration parameters, can be NULL.
624+
*/
625+
__weak void BSP_SD_Detect_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
626+
{
627+
UNUSED(hsd);
628+
UNUSED(Params);
629+
630+
/* GPIO configuration in analog to saves the consumption */
631+
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
632+
#ifndef LL_GPIO_PULL_NO
633+
/* For STM32F1xx */
634+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
635+
#else
636+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_NO);
637+
#endif
638+
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
639+
}
640+
611641
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
612642
/**
613643
* @brief Initializes the SD Transceiver pin MSP.
@@ -621,19 +651,46 @@ __weak void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params)
621651

622652
LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
623653
LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_OUTPUT);
654+
#ifndef LL_GPIO_PULL_NO
655+
/* For STM32F1xx */
656+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
657+
#else
624658
LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO);
625-
659+
#endif
626660
LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
627661
LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_OUTPUT);
662+
#ifndef LL_GPIO_PULL_NO
663+
/* For STM32F1xx */
664+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
665+
#else
628666
LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO);
629-
667+
#endif
630668
/* Enable the level shifter */
631669
LL_GPIO_SetOutputPin(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin);
632670

633671
/* By default start with the default voltage */
634672
LL_GPIO_ResetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin);
635673
}
636674

675+
/**
676+
* @brief DeInitializes the SD Transceiver pin MSP.
677+
* @param hsd: SD handle
678+
* @param Params : pointer on additional configuration parameters, can be NULL.
679+
*/
680+
__weak void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
681+
{
682+
UNUSED(hsd);
683+
UNUSED(Params);
684+
685+
LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
686+
LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
687+
LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO);
688+
689+
LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
690+
LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
691+
LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO);
692+
}
693+
637694
/**
638695
* @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
639696
* @param status: Voltage Switch State

src/bsp_sd.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,12 @@ uint8_t BSP_SD_IsDetected(void);
185185
/* These __weak function can be surcharged by application code in case the current settings (e.g. DMA stream)
186186
need to be changed for specific needs */
187187
void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params);
188-
void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params);
189188
void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params);
189+
void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params);
190+
void BSP_SD_Detect_MspDeInit(SD_HandleTypeDef *hsd, void *Params);
190191
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
191192
void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params);
193+
void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params);
192194
#endif
193195

194196
#ifdef __cplusplus

0 commit comments

Comments
 (0)