Can we mount SD card and USB drive at a same time using fatfs in STM32f429ZI
Can we mount the two devices i.e sd card and usb at a same time using FATFS in STM32F429ZI. I want to store the sensor data in SD card and copy that data to USB drive when the usb is detected. I am able to mount one device at a time. i have used user define fatfs for SD card and USB disk FatFs for USB. When i used both mode at a time the SD card is not able to mount. when i disable the USB mode in fatfs SD card is mounting as expected but not able to mount two devices at a time.
void sd_card_write()
{
// f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(buffer));
fresult = f_mount(&sd_fs, "0:", 1);
if (fresult != FR_OK)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, 1);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, 0);
}
else
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, 1);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, 0);
}
/**************** The following operation is using f_write and f_read **************************/
/* Create second file with read write access and open it */
fresult = f_open(&sd_file, "0:FILE.TXT", FA_OPEN_EXISTING | FA_WRITE | FA_OPEN_APPEND);
/* Writing text */
sprintf(buffer,"Temperature:%f\n",Temp);
fresult = f_write(&sd_file, (const void *)buffer, strlen(buffer), &bw);
/* Close file */
f_close(&sd_file);
clear_buffer();
//unmount sd card
f_mount(&sd_fs, "0:", 0);
}
switch(Appli_state)
{
case APPLICATION_IDLE:
break;
case APPLICATION_START:
if(f_mount(&usb_fs, "1:", 0) == FR_OK)
{
//(TCHAR const*)USBH_Path
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_11, 1);
}
break;
case APPLICATION_READY:
{
UsbTest_Write();
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, 1);
HAL_Delay(5);
}
break;
case APPLICATION_DISCONNECT:
//unmount usb
f_mount(&usb_fs, "1:", 1);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_11, 0);
break;
}
bool UsbTest_Write(void)
{
//Open or Create file for writing
/* f_open(&sd_file, "FILE.TXT", FA_READ);
f_open(&usb_file, "Demo.TXT", FA_OPEN_EXISTING | FA_WRITE);
for (;;) {
fresult = f_read(&sd_file, buffer, sizeof(buffer), &br);
if (br == 0) break;
fresult = f_write(&usb_file, buffer, br, &bw);
if (bw < br) break;
}
f_close(&usb_file);
return 1; //Success
*/
if(f_open(&usb_file, "1:File.TXT", FA_OPEN_EXISTING | FA_WRITE | FA_OPEN_APPEND) != FR_OK)
{
return 0;
}
//Copy test Text to my temporary read/write buffer
sprintf(rwtext, "Temp:%f\n",Temp);
//Write to text file
fresult = f_write(&usb_file, (const void *)rwtext, strlen(rwtext), &bw);
if((fresult != FR_OK) || (bw == 0))
{
return 0;
}
f_close(&usb_file);
return 1; //Success
}
//2. USB test Read function
bool UsbTest_Read(void)
{
//Open file for reading
if(f_open(&usb_file, "1:File.TXT", FA_READ) != FR_OK)
{
return 0;
}
//Read text from files until NULL
for(uint8_t i=0; i<100; i++)
{
fresult = f_read(&usb_file, (uint8_t*)&rwtext[i], 1, &br);
if(rwtext[i] == 0x00) // NULL string
{
br = i;
break;
}
}
//Reading error handling
if(br==0) return 0;
//Close file
f_close(&usb_file);
return 1; // success
}
