Skip to content

Commit

Permalink
chore: add end() method
Browse files Browse the repository at this point in the history
Fixes #71

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Sep 27, 2024
1 parent f4bf655 commit 006b9d5
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/STM32SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/SdFatFs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/SdFatFs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
105 changes: 81 additions & 24 deletions src/bsp_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -621,19 +651,46 @@ __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);

/* By default start with the default voltage */
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
Expand Down
4 changes: 3 additions & 1 deletion src/bsp_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 006b9d5

Please sign in to comment.