cancel
Showing results for 
Search instead for 
Did you mean: 

Understand SD Card detect pin initialization

DYann.1
Senior II

Hello,

I would like to understand the code to carry out my software integration :

 

    /* Configure SD pin detect */
    __SD_DETECT_GPIO_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();

 

And This refers to lines of code that I am lost, for the first line for example

 

#define __SD_DETECT_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()

 

And after :

 

#define __HAL_RCC_GPIOG_CLK_ENABLE()           do { \
                                                    __IO uint32_t tmpreg; \
                                                    SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN); \
                                                    /* Delay after an RCC peripheral clock enabling */ \
                                                    tmpreg = READ_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN); \
                                                    UNUSED(tmpreg); \
                                                  } while(0)

 

I would just like to configure the SD card to be able to write the code from an example on the evaluation card. My real card is not the evaluation card and I would like the code just necessary. Do you have an example for STM32L5xx with the file .ioc for example.

Thank you 

 

 

16 REPLIES 16

@DYann.1 wrote:

This refers to lines of code that I am lost, for the first line for example


Where are you lost?

Those are just standard C macros.

So this code

 

    /* Configure SD pin detect */
    __SD_DETECT_GPIO_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();

 

expands to this:

 

__HAL_RCC_GPIOG_CLK_ENABLE();

do { 
   __IO uint32_t tmpreg; 
   SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN); 
   /* Delay after an RCC peripheral clock enabling */ 
   tmpreg = READ_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN); 
   UNUSED(tmpreg); 
} while(0);

 

which is equivalent to this:

 

__HAL_RCC_GPIOG_CLK_ENABLE();

__IO uint32_t tmpreg; 
SET_BIT( RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN ); 
/* Delay after an RCC peripheral clock enabling */ 
tmpreg = READ_BIT( RCC->AHB2ENR, RCC_AHB2ENR_GPIOGEN ); 
UNUSED(tmpreg); 

 

PS:

If  it's the  while(0) you're worried about, it's a standard C trick to make a multi-line macro which can be used anywhere - just like a function call:

https://c-faq.com/cpp/multistmt.html

https://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro

https://cboard.cprogramming.com/c-programming/82858-do-%7B%7D-while-0-;.html

 

 

 

on my personal card, I only have the microprocessor which is the same as on the evaluation card, can I use the files linked with the STM32L5xxx evaluation card. I don't have the specific library for the BSP evaluation card, drivers, prtotype etc...

 

As with any such software porting, you will have to adapt it to match the actual ports & pins you are using.


@Andrew Neil wrote:

As with any such software porting, you will have to adapt it to match the actual ports & pins you are using.


Nothing about the .ioc file with the ST example, It's not easy to do this software port. I've already done 2 ports but it doesn't work very well and I don't know what I'm missing compared to the ST examples. What is the most effective method ?


@DYann.1 wrote:


Nothing about the .ioc file with the ST example?


Not all examples have an .ioc file:

https://community.st.com/t5/stm32-mcus-products/some-example-projects-don-t-use-ioc-file/m-p/698338/highlight/true#M255312


@Andrew Neil wrote:

Not all examples have an .ioc file:


Thank you, so I'm going to try again !

I would suggest that you start by getting it working on an ST board; eg, Nucleo:

https://www.st.com/en/evaluation-tools/nucleo-l552ze-q.html

That's a simple & low-cost board ...

 

Or do you have the EV or DK board?

https://www.st.com/en/evaluation-tools/stm32l552e-ev.html

https://www.st.com/en/evaluation-tools/stm32l562e-dk.html

 


@Andrew Neil wrote:

I would suggest that you start by getting it working on an ST board; eg, Nucleo:

Or do you have the EV or DK board?


I have a STM32L552E-EV and I have a program that works very well on this evaluation card but now I would like to take just a portion of code necessary to work on my real card which has just the same microprocessor.

Hello @DYann.1 ,

Basically (on the eval board) the card detection is based on a GPIO. And the detection is done based on a switch on the SDcard embedded in the slot:

SofLit_0-1736256761367.png

From the Eval SDcard BSP:

 

int32_t BSP_SD_DetectITConfig(uint32_t Instance)

 

this API is responsible of the config of the GPIO.

And these line needs from stm32l552e_eval_sd.h are responsible to set the correct GPIO to be used/ the correct NVIC line and the IRQ handler using PG10 pin as indicated by the schematics above:

 

#define SD_DETECT_PIN                 GPIO_PIN_10
#define SD_DETECT_GPIO_PORT           GPIOG
#define __SD_DETECT_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()

#define SD_DETECT_IRQn                EXTI10_IRQn
#define SD_DETECT_IRQHandler          EXTI10_IRQHandler

#define SD_DETECT_EXTI_LINE           EXTI_LINE_10

 

If you need to use it in interrupt mode you need to use this callback:

 

BSP_SD_DetectCallback(uint32_t Instance, uint32_t Status)

 

In case of polling you need to use:

 

int32_t BSP_SD_IsDetected(uint32_t Instance

 

 

Hope that answers your question.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.