cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-L476RG - SD card not creating any .txt files or any files at all. Does anyone have an example code to let the sd card work ?

TNguy.9
Associate II

Hey guys,

I am pretty new to stm32 (-> beginner) and I am trying to create a .txt file on my sd card. My pc is connected by usb to the Nucleo-L476RG board, which is connected to an onboard sd card.

These are the connected pins:

   /**SDMMC1 GPIO Configuration   

   PC8    ------> SDMMC1_D0

   PC9    ------> SDMMC1_D1

   PC10    ------> SDMMC1_D2

   PC11    ------> SDMMC1_D3

   PC12    ------> SDMMC1_CK

   PD2    ------> SDMMC1_CMD

   */

I already checked the PC12 pin and saw a good working sd clk.

I have uploaded my project, which is created by CubeMX.

I appreciate any kind of help.

Thank you in advance,

Greetings

#include "main.h"
 
#include "fatfs.h"
 
 
ADC_HandleTypeDef hadc1;
 
ADC_HandleTypeDef hadc2;
 
DMA_HandleTypeDef hdma_adc1;
 
DMA_HandleTypeDef hdma_adc2;
 
 
SD_HandleTypeDef hsd1;
 
 
SPI_HandleTypeDef hspi2;
 
 
FATFS fs;          /* Filesystem object */
 
FIL fil;           /* File object */
 
FRESULT res;       /* API result code */
 
 
int main(void)
 
{
 
 /* USER CODE BEGIN 1 */
 
 
 /* USER CODE END 1 */
 
 
 
 /* MCU Configuration--------------------------------------------------------*/
 
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 
 HAL_Init();
 
 
 /* USER CODE BEGIN Init */
 
 
 /* USER CODE END Init */
 
 
 /* Configure the system clock */
 
 SystemClock_Config();
 
 
 /* USER CODE BEGIN SysInit */
 
 
 /* USER CODE END SysInit */
 
 
 /* Initialize all configured peripherals */
 
 MX_GPIO_Init();
 
 MX_DMA_Init();
 
 MX_ADC1_Init();
 
 MX_ADC2_Init();
 
 MX_SDMMC1_SD_Init();
 
 MX_SPI2_Init();
 
 MX_FATFS_Init();
 
 /* USER CODE BEGIN 2 */
 
 HAL_Delay(500);
 
 
 /*PA12 = SD Detecteion Pin*/
 
 HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);
 
 
 f_mount(&fs,SDPath,1);
 
 
 f_open(&fil,"text.txt",FA_CREATE_ALWAYS);
 
 
 f_close(&fil);
 
 

12 REPLIES 12
Curtis B.
Senior

Hi,

do you have pull up resistors in your hardware design or at least the internal pullups enables for the SDIO Interface? Do you operate the interface in DMA mode? Does your controller have cache and if he does, is it enabled?

Regards,

Daniel

TNguy.9
Associate II

Hi Curtis,

thank you for trying to help me !

I have pull up resistors on my SDIO Interface.

I do not operate in DMA mode, the only thing running in DMA is my ADC.

I looked it up and it says I have no cache inside my controller.

Greetings,

Trung

Curtis B.
Senior

Hi Trung,

Did you step through the code and did you observed if the speficic functions return an error?

Greetings

TNguy.9
Associate II

Hi Curtis,

I tried this code in debug mode:

  if(f_mount(&fs,SDPath,1) != FR_OK)
  {
	  test = 1;
  }
 
  HAL_Delay(500);
 
  if(f_open(&fil,"text.txt",FA_CREATE_ALWAYS) != FR_OK)
  {
	  test = 2;
  }
 
  HAL_Delay(500);
 
  if(f_close(&fil) != FR_OK)
  {
	  test = 3;
  }

And the test variable went from 1 to 2 and 3. So it is somehow not working.

Greetings,

Trung

Curtis B.
Senior

Ok, ist already fails during the mounting process. Is the SD card formatted correctly? Which Clock frequency did you set? Did you step through the code in f_mount. This would held to see where exactly it fails.

TNguy.9
Associate II

Hey,

yes my sd card is on fat32, 4096 bytes. My HCLK is set to 25 MHz and my SDMMC1 controler is set to 25 MHz too. I also read the clock on my pin PC12 set as alternate function to my sd_clk which is about 210kHz.

I somehow cant step through the f_mount function in debug mode, my ide doesnt allow me to. But I have tested this code:

void Error_Handler1(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
	error1 = 1;
  /* USER CODE END Error_Handler_Debug */
}
 
void Error_Handler2(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
	error2 = 1;
  /* USER CODE END Error_Handler_Debug */
}
static void MX_SDMMC1_SD_Init(void)
{
 
  /* USER CODE BEGIN SDMMC1_Init 0 */
 
  /* USER CODE END SDMMC1_Init 0 */
 
  /* USER CODE BEGIN SDMMC1_Init 1 */
 
  /* USER CODE END SDMMC1_Init 1 */
  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;
  if (HAL_SD_Init(&hsd1) != HAL_OK)
  {
    Error_Handler1();
  }
  if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)
  {
    Error_Handler2();
  }
  /* USER CODE BEGIN SDMMC1_Init 2 */
 
  /* USER CODE END SDMMC1_Init 2 */
 
}

And it looks like, my function HDL_DS_Init(&hsd1) and HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) are not working. Any clue how to see what in particular is not working?

Thank you

Curtis B.
Senior

Hi,

210 Khz for the clock signal seems queit low. But I don't know if this will lead to a fault. Not being able to step debug your code makes it quite hard to find the error. Which IDE do you use? I bet the Init-function fails in the subfunction HAL_SD_InitCard. You should focus on that function an see where exactly the first error arises.

Regards,

Daniel

Working SDMMC+FATFS demo for NUCLEO-L476RG, diagnostic output to VCP at 115200 8N1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TNguy.9
Associate II

Hey Clive

thanks for replying. Is there any possibility to get this code in .c instead of .hex ?

Greetings