Reputation: 1
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.
Upvotes: 0
Views: 2288
Reputation: 1
You can use input signal u_derect and register callback function on event of input change.
Create one IPC variable for sdcard status change you can use that IPC variable inside your rtos task to check sdcard status.
Upvotes: 0
Reputation: 3255
You should unmount the card every time you are done writing if you want to remove the card. This is done by calling f_mount
with f_mount(0, "", 0);
as per elm-chan.
Please note that you then have to mount the drive also every time because it isn't accessible anymore.
A better solution would be to have the user request a safe removal by pressing a button and then your program has time to safely finish what it was doing and unmount the drive. It can then give confirmation by enabling a light whereupon the user removes the card. This program needs to check whether a card is currently mounted and if not mount it before trying to write or read something from the card.
I will be guessing the contents of MX_SDMMC2_SD_Init
as you haven't included this code here but you probably will need to call this function (and its deinit
counterpart every time you remove or try to access the card.
Upvotes: 1