cancel
Showing results for 
Search instead for 
Did you mean: 

SD card not working on STM32F746G-DISC0, error "R/W Error" found when "disk_write()" called?

LLart.1
Associate

I have been trying to implement SD card function using SDMMC1 onto the STM32F746G-DISC0 dev board in my custom code which uses FreeRTOS and the TouchGFX. These later functions are working fine, however after mounting the SD Card, any other FatFS functions fails.

Currently I cannot get past f_mkfs() due to an error I traced to disk_write() in diskio.c. It return an error 1 which is:

RES_ERROR,		/* 1: R/W Error */

The FatFS function, f_mkfs(), in the next layer up (ff.c), return as error code 1 which is:

FR_DISK_ERR,			/* (1) A hard error occurred in the low level disk I/O layer */

My current setup is:

  • STM32F746G-DISC0 (C01) dev board
  • FreeRTOS (CMSIS_V1) Version: 10.2.1(according to CubeMX)
  • SDHC 8GB, SDHCi 16GB (both have the same issue)
  • STMCubeIDE Version: 1.5.0
  • STMCubeMX Version: 6.1.0
  • FatFS Version: R0.12c (according to CubeMX)

In CubeMX I have set the SDMMC clock to 48MHz in Clock Configuration with SDMMCCLK clock divide factor set to 0 (was default). DMA, Interrupt and uSD_Detect with pull up are also setup. I have tried both enabling/disabling FS_EXFAT as well but issue persists.

My SD card task is as follows:

/* USER CODE BEGIN PV */
extern char SDPath[4]; /* SD logical drive path */
extern FATFS SDFatFS; /* File system object for SD logical drive */
extern FIL SDFile; /* File object for SD */
uint8_t workBuffer[2 * _MAX_SS];
/* USER CODE END PV */
.
.
.
void StartSDTask(void const * argument)
{
  /* USER CODE BEGIN StartSDTask */
	FRESULT res; /* FatFs function common result code */
	uint32_t byteswritten, bytesread; /* File write/read counts */
	uint8_t wtext[] = "Hello World"; /* File write buffer */
	uint8_t rtext[100]; /* File read buffer */
 
	printf("SD card Task started...\n");
	/*##-2- Register the file system object to the FatFs module ##############*/
	res = f_mount(&SDFatFS, (TCHAR const*) SDPath, 0);
	if (res != FR_OK) {
		/* FatFs Initialization Error */
		printf("FatFs Initialization Error %d\n", res);
		Error_Handler();
	} else {
		printf("FatFs Initialization Successful\n");
		/*##-3- Create a FAT file system (format) on the logical drive #########*/
		/* WARNING: Formatting the uSD card will delete all content on the device */
		res = f_mkfs((TCHAR const*) SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer));
		if (res != FR_OK) {
			/* FatFs Format Error */
			printf("FatFs Format Error %d\n", res);
			Error_Handler();
		} else {
			/*##-4- Create and Open a new text file object with write access #####*/
			res = f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
			if (res	!= FR_OK) {
				/* 'STM32.TXT' file Open for write Error */
				printf("STM32.TXT file Open for write Error %d\n", res);
				Error_Handler();
			} else {
				/*##-5- Write data to the text file ################################*/
				res = f_write(&SDFile, wtext, sizeof(wtext),
						(void*) &byteswritten);
 
				if ((byteswritten == 0) || (res != FR_OK)) {
					/* 'STM32.TXT' file Write or EOF Error */
					Error_Handler();
				} else {
					/*##-6- Close the open text file #################################*/
					f_close(&SDFile);
 
					/*##-7- Open the text file object with read access ###############*/
					if (f_open(&SDFile, "STM32.TXT", FA_READ) != FR_OK) {
						/* 'STM32.TXT' file Open for read Error */
						Error_Handler();
					} else {
						/*##-8- Read data from the text file ###########################*/
						res = f_read(&SDFile, rtext, sizeof(rtext),
								(UINT*) &bytesread);
 
						if ((bytesread == 0) || (res != FR_OK)) {
							/* 'STM32.TXT' file Read or EOF Error */
							Error_Handler();
						} else {
							/*##-9- Close the open text file #############################*/
							f_close(&SDFile);
 
							/*##-10- Compare read data with the expected data ############*/
							if ((bytesread != byteswritten)) {
								/* Read data is different from the expected data */
								Error_Handler();
							} else {
								/* Success of the demo: no error occurrence */
								HAL_GPIO_WritePin(LED_D13_GPIO_Port,
										LED_D13_Pin, GPIO_PIN_SET);
							}
						}
					}
				}
			}
		}
	}
 
	/* Infinite loop */
	for (;;) {
		osDelay(1);
	}
  /* USER CODE END StartSDTask */
}

Hoping anyone has a fix for this as the dev board should "just work". Thanks in advace.

1 REPLY 1
vishnu_illikkal
Associate III

I also had the same problem , it seems we can't use freertos with fatfs. they haven't figured it out yet maybe!