I'm using a SD Card with the SDMMC interface on a STM32F7 board. I'm following this video and the project going well. https://www.youtube.com/watch?v=0NbBem8U80Y
FATFS SDFatFs;
FIL MyFile;
FRESULT res;
uint32_t byteswritten;
uint8_t wtext[] = "This is test of Fatfs with STM32F7-Disco DMA&RTOS\n";
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_SDMMC2_SD_Init();
MX_FATFS_Init();
/* Call init function for freertos objects (in freertos.c) */
MX_FREERTOS_Init();
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void StartDefaultTask(void const * argument)
{
/* init code for FATFS */
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
if(f_open(&MyFile, "sd_test.txt", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
res = f_write(&MyFile, wtext, sizeof(wtext)-1, (void *)&byteswritten);
char buf[]="Hello World";
res = f_write(&MyFile,buf, sizeof(buf)-1, (void *)&byteswritten);
char buf1[]="I am GEHAD";
res = f_write(&MyFile,buf1, sizeof(buf1)-1, (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
Error_Handler();
}
else
{
f_close(&MyFile);
}
}
}
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for(;;)
{
if(f_open(&MyFile, "sd_test2.txt", FA_OPEN_APPEND | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
res = f_write(&MyFile, wtext, sizeof(wtext)-1, (void *)&byteswritten);
char buf[]="Hello World";
res = f_write(&MyFile,buf, sizeof(buf)-1, (void *)&byteswritten);
char buf1[]="I am gehad";
res = f_write(&MyFile,buf1, sizeof(buf1)-1, (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
Error_Handler();
}
else
{
f_close(&MyFile);
}
}
osDelay(1);
}
/* USER CODE END StartDefaultTask */
}
I wan't to be able to safely remove the SD Card and reinsert it while to program is running and possibly writing to it. I'm able to to remove the card safely but it doesn't work when I reconnect it during the same run.