Skip to main content
DYann.1
Senior II
January 7, 2025
Solved

Understand SD Card detect pin initialization

  • January 7, 2025
  • 36 replies
  • 4856 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.

36 replies

Andrew Neil
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

 

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
DYann.1
DYann.1Author
Senior II
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...

 

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
mƎALLEmBest answer
ST 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.

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