Skip to main content
Explorer
January 7, 2025
Solved

Understand SD Card detect pin initialization

  • January 7, 2025
  • 2 replies
  • 4743 views

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 

 

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    As you are using CubeMx I invite you to watch that video especially at 5:59 on how to configure SDcard pin detection on CubeMx.

    Hope that gives you more visibility on the subject.

    2 replies

    Super User
    January 7, 2025

    @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

     

     

     

    DYann.1Author
    Explorer
    January 7, 2025

    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...

     

    Super User
    January 7, 2025

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

    mƎALLEmAnswer
    Technical Moderator
    January 7, 2025

    As you are using CubeMx I invite you to watch that video especially at 5:59 on how to configure SDcard pin detection on CubeMx.

    Hope that gives you more visibility on the subject.