Skip to main content
TNguy.9
Associate II
April 14, 2020
Question

Possible CubeMX Bug on Nucleo-L467RG : SD Card.

  • April 14, 2020
  • 4 replies
  • 1607 views

Hey guys,

I'm pretty new to Stm32 controllers, but I might find a bug. Just want to report it, in case I am right.

Im using Stm32 Workbench Eclipse, Stm32CubeMX and a Nucleo-L476RG.

I tried to get a sd card working on my board. So I had a SD-Card onboard with a 4-bit bus on my Nucleo with following configs:

         PC8    ------> SDMMC1_D0

          PC9    ------> SDMMC1_D1

          PC10    ------> SDMMC1_D2

          PC11    ------> SDMMC1_D3

          PC12    ------> SDMMC1_CK

          PD2    ------> SDMMC1_CMD

The moment I set the settings in CubeMX I get this standard code as my SDMMC settings:

 hsd1.Instance = SDMMC1;

 hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;

 hsd1.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;

 hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;

 hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;

 hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;

 hsd1.Init.ClockDiv = 0;

First of all the (HAL_SD_Init(&hsd1) is missing.

Secondly the  hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B; is not set to 4B.

When I change it to 4B my whole IDE is not working in debug mode.

Is it me getting something wrong or why can't I use my sd card on a 4Bit bus?

Greetings,

Trung

This topic has been closed for replies.

4 replies

Mohamed Aymen HZAMI
ST Employee
April 16, 2020

Hello,

During initialization , the SD card must be configured with SDMMC_BUS_WIDE_1B, so it is normal to see this configuration during the initialization , after that we can switch the bus wide to 4B with the following function : 

if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)
 {
 Error_Handler();
 } 

 Of course all this configuration must be automatically generated by the STM32CubeMx.

Concerning the missing (HAL_SD_Init(&hsd1) , it is strange and i am not able to reproduce , can you send me your ioc file ?

Best Regards,

Mohamed Aymen.

TNguy.9
TNguy.9Author
Associate II
April 16, 2020

Hi Mohamed,

of course, I will upload my ioc file.

In which case do I use the other two possibilities to initialize my sd ?

#define SDMMC_BUS_WIDE_1B                     ((uint32_t)0x00000000U)

-> #define SDMMC_BUS_WIDE_4B                     SDMMC_CLKCR_WIDBUS_0

-> #define SDMMC_BUS_WIDE_8B                     SDMMC_CLKCR_WIDBUS_1

It took me quite a while to understand that SDMMC_BUS_WIDE_1B is the only option working for me, since I didn't find any advices in any ref manual or datasheet.

Best Regards,

Trung Nguyen-Xuan

Tesla DeLorean
Guru
April 16, 2020

ST brings the interface up at 400 KHz 1-bit initially, and then increases speed and bus width.

The 4-bit wide bus is sensitive to noise, ringing and skewing, and has significantly less tolerance to how the socket/card is interfaced.

If the previously furnished demo code does not work, it suggests an interface level issue. If it did work, you need to double check the GPIO slew-rate and pull-up configurations being used in the failing code.

Known, working, build of code posted in the previous thread

https://community.st.com/s/question/0D53W000003yg0DSAQ/nucleol476rg-sd-card-not-creating-any-txt-files-or-any-files-at-all-does-anyone-have-an-example-code-to-let-the-sd-card-work-

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
chaaalyy
Senior II
April 20, 2020

The problem maybe isn´t one ;) If you enable FatFS, the according HAL_SD_Init function isn´t inside the generated MX init function anymore. It´s somewhere inside the FatFS files (esp., when you enable the FatFS DMA template in cubeMX). These are copied over from predefined templates, but cubeMX is intelligent enough (exeptionally...), not to generate the HAL_SD_Init anymore because it already exists somewhere else...

chaaalyy
Senior II
April 20, 2020

Just found it: It´s inside FATFS->Target. The file is bsp_driver_sd.c and the function is:

__weak uint8_t BSP_SD_Init(void)

{

 uint8_t sd_state = MSD_OK;

 /* Check if the SD card is plugged in the slot */

 if (BSP_SD_IsDetected() != SD_PRESENT)

 {

  return MSD_ERROR_SD_NOT_PRESENT;

 }

 /* HAL SD initialization */

 sd_state = HAL_SD_Init(&hsd1);

 /* Configure SD Bus width (4 bits mode selected) */

 if (sd_state == MSD_OK)

 {

  /* Enable wide operation */

  if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)

  {

   sd_state = MSD_ERROR;

  }

 }

/Charly