cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get FatFS working on a NucleoH7A3?

magene
Senior II

I've been struggling to get FatFs working on a NucleoH7A3 board for a while. I built a little board with a uSD card connector that plugs into the Arduino connectors on the Nucleo board. The pins for the uSD card connector are connected to the same GPIO lines on the NucleoH7A3 as are used on the H7B3I Discovery board for its built in uSD connector. My Nucleo/uSD board works with the H7B3I Discovery board FatFs_uSD_DMA_Standalone example. But I haven't been able to build a project that works from scratch using STM32CubeIDE as in this video: https://www.youtube.com/watch?v=I9KDN1o6924 . f_mount returns FR_OK but f_mkfs returns FR_DISK_ERR. If I format the uSD card on my PC and comment out the f_mkfs command, the f_open command returns FR_DISK_ERR.

One difference from the video example is there doesn't seem to be an option to enable DMA for the H7 in the SDMMC1 configuration. According to the H7 data sheet, the SDMMC interface has a dedicated DMA controller. Does that mean there's no configuration required and I don't have to do anything to get DMA on the SDMMC? Just in case, I've tired enabling and disabling the DMA template in the FATFS configuration but neither way works.

Is there something unique about the H7 that makes it different than the F7?

int main(void)
{
  /* USER CODE BEGIN 1 */
	FRESULT res; /* FatFs function common result code */
	uint32_t byteswritten, bytesread; /* File write/read counts */
	uint8_t wtext[] = "STM32 FATFS works great!"; /* File write buffer */
	uint8_t rtext[_MAX_SS];/* File read buffer */
  /* 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_SDMMC1_SD_Init();
  MX_FATFS_Init();
//  MX_UART4_Init();
//  MX_USART2_UART_Init();
//  MX_USART3_UART_Init();
//  MX_UART5_Init();
//  MX_USART6_UART_Init();
//  MX_UART7_Init();
//  MX_UART9_Init();
//  MX_USART10_UART_Init();
//  MX_BDMA2_Init();
//  MX_I2C4_Init();
//  MX_I2C2_Init();
//  MX_LPUART1_UART_Init();
  /* USER CODE BEGIN 2 */
 
  res = f_mount(&SDFatFS, (TCHAR const*)SDPath, 0);
  if(res != FR_OK)
  {
		Error_Handler();
  }
  else
  {
//	  res = f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext));
//	  if(res != FR_OK)
//	  {
//			Error_Handler();
//	  }
//	 else
	{
			//Open file for writing (Create)
			res = f_open(&SDFile, "mySTM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
			if(res != FR_OK)
			{
				Error_Handler();
			}
			else
			{
 
				//Write to the text file
				res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
				if((byteswritten == 0) || (res != FR_OK))
				{
					Error_Handler();
				}
				else
				{
 
					f_close(&SDFile);
				}
			}
		}
	}
	f_mount(&SDFatFS, (TCHAR const*)NULL, 0);

3 REPLIES 3
Imen.D
ST Employee

Hello @magene​ ,

There is a known issue with CubeMx v6.3.0 with DMA initialization that will be fixed in the next release:

The DMA initialization needs to be before any other peripheral initialization.

(Ticket ID 112040: this is an internal tracking number and is not accessible or usable by customers).

So, please try update these lines, then keep me informed about your progress:

  1.  MX_GPIO_Init();
  2. MX_DMA_Init();

Hope this resolve your issue.

  

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
magene
Senior II

I rebuilt my project with just USART2, SDMMC1 and the FatFs middleware, I moved MX_DMA_Init() ahead of MX_GPIO_Init() but I get the same error. HAL_SD_ReadBlocks(... in stm32h7xx_had_sd.c gets a SDMMC_FLAG_RX)VERR error. Which eventually winds up as a FR_DISK_ERR in my main(). Here's the call stack that lead to that error.

0693W00000FDqzqQAD.png 

Here's my new project. Did I do what you were suggesting correctly?

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_DMA_Init();
  MX_GPIO_Init();
  MX_SDMMC1_SD_Init();
  MX_USART2_UART_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
 
	FRESULT fatReturn;
	uint8_t workBuffer[_MAX_SS];
	uint32_t byteswritten, bytesread; /* File write/read counts */
	uint8_t rtext[100]; /* File read buffer */
	FATFS SDFatFs;
 
	uint8_t wtext[] = "line1"; /* File write buffer */
	TCHAR fileName[] = "file1.txt";
 
	//fatReturn = f_mkfs(SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer));
 
	/* Register the file system object to the FatFs module */
	fatReturn = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);
 
	/* Create and Open a new text file object with write access */
	fatReturn = f_open(&SDFile, fileName, FA_CREATE_ALWAYS | FA_WRITE);
 
	/* Write data to the text file */
	fatReturn = f_write(&SDFile, wtext, sizeof(wtext), (void *)&byteswritten);
	fatReturn = f_sync(&SDFile);
 
	fatReturn = f_close(&SDFile);
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

I've been able to get a simple program working with FatFs and one or more UARTs working on other Nucleo and Discovery boards. However, I've been trying to get a program working on the STM32H7A3 Nucleo board with FatFS and at least one UART using STM32CubeIDE and all the relevant app notes I can find for quite a while. Have you or anyone else at ST done this successfully on this Nucelo board? If so, is there any chance of getting that project with the .ioc file?

magene
Senior II

Hello @Imen DAHMEN​  - Can youi confirm that I made the change you suggested correctly?

Thanks