2024-12-03 01:27 PM
I have followed How to create a file system on a SD card using STM32CubeIDE the best I can using the STM32H7B3I-DK. With this board I do not see the DMA tab under SDMMC1, so I have skipped that. I am failing the f_mount(). I am using a touchGFX project and a 128GB microSD card. The failure occurs while calling fmount(). I have debugged and got the following stack:
HardFault_Handler() at stm32h7xx_it.c:93 0x8007d6e
<signal handler called>() at 0xfffffff1
memcpy() at 0x8036658
prvCopyDataToQueue() at queue.c:2,102 0x8020a04
xQueueGenericSendFromISR() at queue.c:995 0x80202d4
osMessageQueuePut() at cmsis_os2.c:1,766 0x801ef94
touchgfx::OSWrappers::signalVSync() at OSWrappers.cpp:95 0x8002de2
HAL_LTDC_LineEventCallback() at TouchGFXGeneratedHAL.cpp:196 0x8005a2e
HAL_LTDC_IRQHandler() at stm32h7xx_hal_ltdc.c:713 0x8011446
LTDC_IRQHandler() at stm32h7xx_it.c:233 0x8007df2
<signal handler called>() at 0xffffffed
SDMMC_GetCmdResp1() at stm32h7xx_ll_sdmmc.c:1,273 0x801acf8
SDMMC_CmdAppCommand() at stm32h7xx_ll_sdmmc.c:885 0x801aaa0
SD_PowerON() at stm32h7xx_hal_sd.c:3,186 0x8017a6a
HAL_SD_InitCard() at stm32h7xx_hal_sd.c:523 0x8016a08
HAL_SD_Init() at stm32h7xx_hal_sd.c:404 0x8016894
BSP_SD_Init() at bsp_driver_sd.c:51 0x8000a9e
SD_initialize() at sd_diskio.c:192 0x8000c7e
disk_initialize() at diskio.c:59 0x801b208
find_volume() at ff.c:3,036 0x801c82a
f_mount() at ff.c:3,297 0x801cd5c
SDInit() at main.c:817 0x800704a
StartDefaultTask() at main.c:892 0x800719a
pxPortInitialiseStack() at port.c:214 0x801f6d8
With no SD card inserted the system just prints the failure messages, but with an sd card the system hard faults and locks up ... I am having a hard time understanding what is causing the issue.
I have modified the code slightly to troublseshoot:
void SDInit(void)
{
// Mount the file system on the SD card (assumes SDPath is defined)
if (f_mount(&SDFatFS, (TCHAR const*)SDPath, 1) != FR_OK)
{
printf("Failed to mount\r\n");
}
else{
printf("Mount Good\r\n");
}
}
void WriteSDCard(void)
{
if (f_open(&SDFile, "STM32.TXT", FA_OPEN_ALWAYS | FA_WRITE) != FR_OK)
{
printf("Failed to open file on write\r\n");
}
else
{
printf("File Opened on write\r\n");
f_lseek(&SDFile, f_size(&SDFile));
res = f_write(&SDFile, wtext, sizeof(wtext), &byteswritten);
if (res != FR_OK)
{
printf("Failed to write\r\n");
}
else
{
printf("%u bytes written to STM32.TXT\r\n", byteswritten);
}
f_close(&SDFile);
}
}
void ReadSDCard(void)
{
if (f_open(&SDFile, "STM32.TXT", FA_READ) != FR_OK)
{
printf("Failed to open file on read\r\n");
}
else
{
printf("File Opened on read\r\n");
res = f_read(&SDFile, rtext, sizeof(rtext) - 1, &bytesread);
if (res == FR_OK)
{
rtext[bytesread] = '\0';
printf("File contents: \n%s\n", rtext);
}
else
{
printf("Failed to read\r\n");
}
f_close(&SDFile);
}
}
void SDDeInit(void)
{
// Unmount the SD card (optional step to release resources)
f_mount(NULL, (TCHAR const*)NULL, 0);
}
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN 5 */
printf("Starting SD Card Program\r\n");
SDInit();
f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext));
/* Infinite loop */
for(;;)
{
// if(HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_8) == GPIO_PIN_SET){
// printf("Pin high\r\n");
// }
// if(HAL_GPIO_ReadPin(GPIOI, GPIO_PIN_8)== GPIO_PIN_RESET){
// printf("Pin low\r\n");
// }
if(writeFlag){
printf("Attempt Write\r\n");
writeFlag = 0;
WriteSDCard();
}
if(readFlag){
printf("Attempt Read\r\n");
readFlag = 0;
ReadSDCard();
}
osDelay(100);
}
SDDeInit();
/* USER CODE END 5 */
}