2025-08-10 4:58 AM - edited 2025-08-10 5:00 AM
Hello Community,
I am trying to initialize SD card with FileX middleware standalone with my Nucleo-U545RET6Q board.
I have initialized SDMMC1 with SD 4 bit wide , 48MHz with 0 divider. Later i had to put divider by 2 because the code was stuck at fx_media_open(); here is the picture:
Later I have initialized the FileX middleware with SD interface, without changing any parameters. I then, edited the app_filex.c using code found with RTOS Based SD File edit program in U5 families.
This is my app_filex.c file
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file app_filex.c
* @author MCD Application Team
* @brief FileX applicative file
******************************************************************************
* @attention
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "app_filex.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "main.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define FX_STM32_SD_DEFAULT_SECTOR_SIZE 4096
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Define FileX global data structures. */
/* FileX SRAM disk media instance */
FX_MEDIA sdio_disk;
/* FileX file instance */
FX_FILE fx_file;
ALIGN_32BYTES (uint32_t fx_sd_media_memory[FX_STM32_SD_DEFAULT_SECTOR_SIZE / sizeof(uint32_t)]);
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/**
* @brief Application FileX Initialization.
* None
* @retval int
*/
UINT MX_FileX_Init(void)
{
UINT ret = FX_SUCCESS;
/* USER CODE BEGIN MX_FileX_Init */
/* USER CODE END MX_FileX_Init */
/* Initialize FileX. */
fx_system_initialize();
/* USER CODE BEGIN MX_FileX_Init 1*/
/* USER CODE END MX_FileX_Init 1*/
return ret;
}
/* USER CODE BEGIN 1 */
VOID MX_FileX_Process(void)
{
UINT status;
ULONG bytes_read;
CHAR read_buffer[32];
CHAR data[] = "This is FileX working on STM32";
/* Start application */
printf("FileX SD Standalone Application Start.\n");
/* Open the sd_disk driver. */
status = fx_media_open(&sdio_disk, "STM32_SD_DISK", fx_stm32_sd_driver, (VOID *)FX_NULL, (VOID *) fx_sd_media_memory, sizeof(fx_sd_media_memory));
/* Check the media open status. */
if (status != FX_SUCCESS)
{
Error_Handler();
}
printf("SD Disk successfully opened.\n");
/* Create a file called STM32.TXT in the root directory. */
status = fx_file_create(&sdio_disk, "STM33.TXT");
/* Check the create status. */
if (status != FX_SUCCESS)
{
/* Check for an already created status. This is expected on the
second pass of this loop! */
if (status != FX_ALREADY_CREATED)
{
/* Create error, call error handler. */
Error_Handler();
}
}
/* Open the test file. */
status = fx_file_open(&sdio_disk, &fx_file, "STM33.TXT", FX_OPEN_FOR_WRITE);
/* Check the file open status. */
if (status != FX_SUCCESS)
{
/* Error opening file, call error handler. */
Error_Handler();
}
/* Seek to the beginning of the test file. */
status = fx_file_seek(&fx_file, 0);
/* Check the file seek status. */
if (status != FX_SUCCESS)
{
/* Error performing file seek, call error handler. */
Error_Handler();
}
printf("Writing data into the file. \n");
/* Write a string to the test file. */
status = fx_file_write(&fx_file, data, sizeof(data));
/* Check the file write status. */
if (status != FX_SUCCESS)
{
/* Error writing to a file, call error handler. */
Error_Handler();
}
/* Close the test file. */
status = fx_file_close(&fx_file);
/* Check the file close status. */
if (status != FX_SUCCESS)
{
/* Error closing the file, call error handler. */
Error_Handler();
}
status = fx_media_flush(&sdio_disk);
/* Check the media flush status. */
if (status != FX_SUCCESS)
{
/* Error closing the file, call error handler. */
Error_Handler();
}
/* Open the test file. */
status = fx_file_open(&sdio_disk, &fx_file, "STM33.TXT", FX_OPEN_FOR_READ);
/* Check the file open status. */
if (status != FX_SUCCESS)
{
/* Error opening file, call error handler. */
Error_Handler();
}
/* Seek to the beginning of the test file. */
status = fx_file_seek(&fx_file, 0);
/* Check the file seek status. */
if (status != FX_SUCCESS)
{
/* Error performing file seek, call error handler. */
Error_Handler();
}
/* Read the first 28 bytes of the test file. */
status = fx_file_read(&fx_file, read_buffer, sizeof(data), &bytes_read);
/* Check the file read status. */
if ((status != FX_SUCCESS) || (bytes_read != sizeof(data)))
{
/* Error reading file, call error handler. */
Error_Handler();
}
/* Close the test file. */
status = fx_file_close(&fx_file);
/* Check the file close status. */
if (status != FX_SUCCESS)
{
/* Error closing the file, call error handler. */
Error_Handler();
}
/* Close the media. */
status = fx_media_close(&sdio_disk);
/* Check the media close status. */
if (status != FX_SUCCESS)
{
/* Error closing the media, call error handler. */
Error_Handler();
}
printf("Data successfully written.\n");
/* Infinite loop */
while (1)
{
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
HAL_Delay(500);
}
}
/* USER CODE END 1 */
Note that default #define FX_STM32_SD_DEFAULT_SECTOR_SIZE 512 doesn't work until i make it 2*512 in my program, later i make it 4096.
My program runs until i go to :
status = fx_media_flush(&sdio_disk);
/* Check the media flush status. */
if (status != FX_SUCCESS)
{
/* Error closing the file, call error handler. */
Error_Handler();
}
Here the Errror_Handler(); is invoked, and from the debugger i get these statues with status code = 144,
There was once it could successfully write the text file onto the sd card
but again that was once and even that time the fx_media_flush() error occurred.
I wanted to know how can i get around this error, and reliably read write delete files into sd card for a datalogger project.