Skip to main content
Visitor II
January 27, 2020
Question

Nucleo L476RG: Interface SD card using SDMMC

  • January 27, 2020
  • 2 replies
  • 1956 views

HI, I have STM32L476RG nucleo board. I have interface a SD card using SDMMC. I have configured SDMMC as 4bit mode with TX, RX in DMA mode. I have configured FATFS. I have given 47K pull up to DO to D3 and CMD line. I am using CUbeMX 5.5.0. The FATFS version is R0.12c.

0690X00000BwcW5QAJ.jpgWhen I generate the code and compile, I get error of

void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)

weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)

weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)

as defined twice in BSP_driver_SD.c and stm32l4xx_hal_sd.c

SO I commented in BSP_driver_SD.c and the code complied.

I flashed the code in my nucleo-l476RG board.

the code is as below

MX_GPIO_Init();

 MX_DMA_Init();

 MX_RTC_Init();

 MX_SDMMC1_SD_Init();

 MX_USART2_UART_Init();

 MX_FATFS_Init();

 /* USER CODE BEGIN 2 */

 

 res = BSP_SD_Init();

 if(res != FR_OK)

 {

   Error_Handler();

 }

 res = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);

 if(res != FR_OK)

 {

   Error_Handler();

 }

 if(f_open(&SDFile, "L4.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

 {

  printf("Failed to open Write file\r\n");

 }

 else

 {

  printf("Opened Write file successfully\r\n");

  //Write data to text file

  res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);

  if((byteswritten == 0) || (res != FR_OK))

  {

   printf("Failed to write file!\r\n");

  }

 

but on ruuning the code, I am getting

Failed to open Write file

 

Please can you help with the configuration and code.

 

Thanks,

Hitesh

  else

  {

   printf("File written successfully\r\n");

   printf("Write Content: %s\r\n", wtext);

  }   

  f_close(&SDFile);

 }

but the

 

    This topic has been closed for replies.

    2 replies

    HMaje.1Author
    Visitor II
    January 31, 2020

    I uploaded the below hex file STM32L476RG_NUCLEO_TEST_SDMMC_001.hex and I am getting

    Core=80000000, 80 MHz     CPUID 410FC241 DEVID 415 REVID 1007                                                                                              

    Cortex M4 r0p1                STM32L475xx, L476xx or L486xx                                                                                                    

    C0000000 00000000 00000000 10110021 11000011 00000000                                                                                                       

    FPU-S Single-precision only HCLK=80000000                                                                                                                    

    APB1=80000000                      APB2=80000000                                                                                                                    

    MCLK=4000000                                                                                                                     

    FatFs Testing (87030)                                                                                                            

    SystemCoreClock: 80000000                                                                                                        

    Mounting FatFs                                                                                                                   

    Switch to 4-Bit                                                                                                                  

    R 8192 1 (00000002)                                                                                                            

    res = 13 f_getfree                                                                                                               

    Infinite loop...  

    Nothing is written on the SD card.

    Graduate II
    January 31, 2020

    How large is the card? Is it using EXFAT or NTFS?

    Try a FAT formatted card of 32GB or less.​

    HMaje.1Author
    Visitor II
    February 3, 2020

    I have programmed by nucleo-L476RG board from STM32Cube_FW_L4_V1.15.0 ( provided by STM)

    D:\en.stm32cubel4_v1-15-0\STM32Cube_FW_L4_V1.15.0\Projects\STM32L476G-EVAL\Applications\FatFs\FatFs_uSD_Standalone

    the f_open function creates the a text file STM32.txt in my SD card. but f_write function return error. SO f_write is not working.

    Since I am able to create a STM32.TXT in my SD card, I assume my electrical connections are OK. Also I am getting SDMMC_CK as 24MHZ on the oscilloscope.

    (SDMMC_CK = SDMMCCLK / [CLKDIV + 2])

    MY SD card is FAT32 format 16GB SDHC.