1 month, 1 week ago.

STM32 DISCO-F469NI board - is there a HAL bug in SD card demo program?

I'm trying to get the SD card demo program working on a STM32F469NI Discovery Board. I installed the latest version of the example "DISCO-F469NI_SD_demo" into the online compiler but I am having problems with execution hanging at the call to initialise the SD card.

The program is not returning from a call in "main.cpp" at line 33: SD_state = sd.Init();

I traced this call to "BSP_SD_Init()" function in the board support file: "stm32469i_discovery_sd.c". In this function the SD card structure is first initialised with 1bit bus width but the problem occurs later on when a call is made to see if the bus width can be increased to 4-bit mode using a HAL function "HAL_SD_ConfigWideBusOperation()":

  /* Configure SD Bus width */
  if(sd_state == MSD_OK)
  {
    /* Enable wide operation */
    if(HAL_SD_ConfigWideBusOperation(&uSdHandle, SDIO_BUS_WIDE_4B) != HAL_OK)  //** NOT RETURNING FROM THIS FUNCTION !!
    {
      sd_state = MSD_ERROR;
    }
    else
    {
      sd_state = MSD_OK;
    }
  }

I tried changing the parameter SDIO_BUS_WIDE_4B to SDIO_BUS_WIDE_1B and this also didn't return, therefore making me think there's something fundamentally wrong with the HAL function.

If I comment this routine out then the program will execute and initialise, presumably still in the slow 1 bit bus mode.

All my SD cards will initialise, but only a select few (2GB SD) will successfully write and read blocks. Most newer SDHC cards give write failure errors.

Ideally I'm trying to get a file system working on top of this basic read/write - does anyone have a code example for the STM32F469 Disco board? It's a great piece of hardware, but I'm finding it difficult to get started with code examples.

Many thanks in advance.

2 Answers

1 month, 1 week ago.

Hello Andy,

Maybe the default SPI frequency for the SD cad initialization is too low. You can try to increase it by adding a mbed_app.json file for example as follows:

mbed_app.json

{
    "target_overrides": {
        "*": {
            "sd.INIT_FREQUENCY": 350000
        }
    }
}

Hi Zoltan, Changing SD clock speeds definitely helped - the program doesn't hang at SD initialisation now.

I'm using the onling compiler and couldn't see how to use the "mbed_app.json" file. Is this for the mbed offline compiler?

I couldn't see the clock rate defined values in online compiler so I exported the project and found values in "stm32f4xx_ll_sdmmc.h"

/* SDIO Initialization Frequency (400KHz max) */

  1. define SDIO_INIT_CLK_DIV ((uint8_t)0x76)

/* SDIO Data Transfer Frequency (25MHz max) */

  1. define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x0)

I think the formula for initialisation clock rate is: 400000 = 48000000 (0x76+2)

Thereafter I could enter hard codes values in the "stm32469i_discovery_sd.c" file to recompile and run some experiments. I found a slower clock rate helped.

This isn't a perfect solution - I've still got problem writing to some SDHC cards - but this it's a big step forward - many thanks.

posted by Andy Bentley 03 Nov 2019

Hello Andy,

I'm glad it helped at least for some SD cards. It took some time also for me to figure out how to add an mbed_app.json configuration file when building with the online compiler. Luckily the following worked:

  • Right-click on the project's name and in the pop-up menu select New File .... Then type mbed_app.json into the File Name edit box.

or

  • On the toolbar, click on the small v arrow next to New and then proceed as above.
posted by Zoltan Hudak 03 Nov 2019
1 week, 3 days ago.

Hello Andy, I had written a SDIO driver component for STM32 F4/F7 boards: https://github.com/JojoS62/Test-lvgl/tree/master/components/sdio-driver I made also a PR, but it is still pending because I had no time to finish the requests last year. But it is working quite well for this board. With the BSP I had also trouble, you need to use the DMA version, the software polling for the data transfer is too slow with HAL. The whole lvgl project is not up to date, I have reworked and added drivers for the lvgl graphics lib also for this board.