Skip to content

Commit 9a0ac74

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

File tree

8 files changed

+106
-21
lines changed

8 files changed

+106
-21
lines changed

src/SD.cpp

Lines changed: 14 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 64 additions & 20 deletions
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,42 @@ __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+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_NO);
633+
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
634+
}
635+
611636
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
612637
/**
613638
* @brief Initializes the SD Transceiver pin MSP.
@@ -634,6 +659,25 @@ __weak void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params)
634659
LL_GPIO_ResetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin);
635660
}
636661

662+
/**
663+
* @brief DeInitializes the SD Transceiver pin MSP.
664+
* @param hsd: SD handle
665+
* @param Params : pointer on additional configuration parameters, can be NULL.
666+
*/
667+
__weak void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
668+
{
669+
UNUSED(hsd);
670+
UNUSED(Params);
671+
672+
LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
673+
LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
674+
LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO);
675+
676+
LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
677+
LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
678+
LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO);
679+
}
680+
637681
/**
638682
* @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
639683
* @param status: Voltage Switch State

src/bsp_sd.h

Lines changed: 3 additions & 1 deletion
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)