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

Hey,

I am using System Workbench for STM32 - Eclipse and I am using AC6.

Yes I am somehow trying to get the SD_Init working.

Greetings,

Trung

XZhen.2
Associate II

@Community member-Xuan​ Hi!

have you fixed this problem?

I have encountered the same problem, and I tried https://github.com/eziya/STM32_SPI_SDCARD

still couldn't get it work.

The thread relates to a 4-bit SDIO/SDMMC implementation, not an SPI one.

The SPI implementation can be achieved via the AdaFruit Shield code.

STM32Cube_FW_H7_V1.5.0\Drivers\BSP\Adafruit_Shield\stm32_adafruit_sd.c (see L4 equivalent)

ST generally provides workable / portable code examples, negating the need to provide a specific tutorial or re-explanation.

The best end-to-end examples tend to be for the EVAL series boards.

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