cancel
Showing results for 
Search instead for 
Did you mean: 

Can we mount SD card and USB drive at a same time using fatfs in STM32f429ZI

Nikhil Gayke
Associate III

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

}

6 REPLIES 6
Haithem Rahmani
ST Employee

Hi @Nikhil Gayke​ 

yes you can, have a look at this project it links the SD & SDRAM drives using FatFs, you can do the same using the USB & SD.

the main trick is to define "_VOLUMES = 2" in the ffconf.h

regards

Haithem.

Nikhil Gayke
Associate III

yes i have define _VOLUMES = 2 still its not working. How to link the USB and SD drive Using FatFs

Nikhil Gayke
Associate III

This is the code

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "fatfs.h"

#include "usb_host.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "fatfs_sd.h"

#include "string.h"

#include "stdio.h"

#include <stdbool.h>

ADC_HandleTypeDef hadc3;

SPI_HandleTypeDef hspi1;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_SPI1_Init(void);

static void MX_ADC3_Init(void);

void MX_USB_HOST_Process(void);

/* USER CODE BEGIN PFP */

//1. USB test Write function

bool UsbTest_Write(void);

//2. USB test Read function

bool UsbTest_Read(void);

/* USER CODE END PFP */

extern ApplicationTypeDef Appli_state;

//USB Logical path

char USBH_Path[4]; /* USBH logical drive path */

char rwtext[100];

FATFS sd_fs,usb_fs; // file system

FIL sd_file,usb_file; // File

FILINFO fno;

FRESULT fresult; // result

UINT br, bw; // File read/write count

float adc_val=0;

float Temp;

float resolution;

#define BUFFER_SIZE 100

char buffer[BUFFER_SIZE]; // to store strings..

int i=0;

int bufsize (char *buf)

{

int i=0;

while (*buf++ != '\0') i++;

return i;

}

void clear_buffer (void)

{

for (int i=0; i<BUFFER_SIZE; i++) buffer[i] = '\0';

}

void adc_value()

{

  HAL_ADC_Start(&hadc3);

  HAL_ADC_PollForConversion(&hadc3, 10);

  adc_val=HAL_ADC_GetValue(&hadc3);

  resolution=( 3.0/4095.0);

  Temp=(adc_val * resolution * 100);

  HAL_Delay(10);

}

void sd_card_write()

{

  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);

   }

   /* 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);

}

int main(void)

{

 HAL_Init();

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SPI1_Init();

 MX_FATFS_Init();

 MX_ADC3_Init();

 MX_USB_HOST_Init();

 /* USER CODE BEGIN 2 */

 HAL_ADC_Start(&hadc3);

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  MX_USB_HOST_Process();

  /* USER CODE BEGIN 3 */

   adc_value();

   sd_card_write();

  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;

    }

 }

 /* USER CODE END 3 */

}

void SystemClock_Config(void)

{

}

static void MX_ADC3_Init(void)

{

}

static void MX_SPI1_Init(void)

{

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

}

//1. USB test Write function

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

}

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @retval None

 */

void Error_Handler(void)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 __disable_irq();

 while (1)

 {

 }

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *     where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t *file, uint32_t line)

{

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

char RAMpath[4], SDpath[4]; /* RAM disk and SD card logical drives paths */    
 
  if((FATFS_LinkDriver(&SDRAMDISK_Driver, RAMpath) == 0) && (FATFS_LinkDriver(&SD_Driver, SDpath) == 0))
  {  
    /*##-2- Register the file system object to the FatFs module ##############*/
    res1 = f_mount(&RAMFatFs, (TCHAR const*)RAMpath, 0);
    res2 = f_mount(&SDFatFs, (TCHAR const*)SDpath, 0);
    
    if((res1 != FR_OK) || (res2 != FR_OK))
    {
      /* FatFs Initialization Error */
      Error_Handler();
    }

Above how the RAM & SD are linked you'll need to replace the SRAM with USB drive, below the code snippet extracted from FatFs_USBDisk_RTOS

 /* Link the USB Host disk I/O driver */
  if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
  {
    /* Init Host Library */
    USBH_Init(&hUSBHost, USBH_UserProcess, 0);
    
    /* Add Supported Class */
    USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
    
    /* Start Host Process */
    USBH_Start(&hUSBHost);

I think in new version or cubemx by default link the driver at the time of initialization

void MX_FATFS_Init(void)

{

 retUSBH = FATFS_LinkDriver(&USBH_Driver, USBHPath);

 retUSER = FATFS_LinkDriver(&USER_Driver, USERPath);

}

but i tried to linked it again but its not working i am using STM32F429ZIT6U board does the board support the mounting of SD card and USB at a time because the there is difference in mounting of SD card & RAM and SD card & USB.

The main problem is both the devices get detected or get mounted when i use f_mount but the file that is get be stored in SD card is will be get stored in USB and SD card not have any file

void sd_card_write()

{

  FATFS_LinkDriver(&USER_Driver, USERPath);

fresult = f_mount(&sd_fs, (TCHAR const*)USERPath, 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);

}

   fresult = f_open(&sd_file, "0:FILE.TXT", FA_OPEN_EXISTING | FA_WRITE| FA_WRITE | FA_OPEN_APPEND);

   sprintf(buffer,"Temperature:%f\n",Temp);

   fresult = f_write(&sd_file, (const void *)buffer, strlen(buffer), &bw);

   f_close(&sd_file);

   clear_buffer();

   FATFS_UnLinkDriver(USERPath);

}

void UsbTest_Write(void)

{

f_open(&sd_file, "0:FILE.TXT", FA_READ);

f_open(&usb_file, "1:FILE1.TXT", FA_OPEN_EXISTING |FA_WRITE | FA_OPEN_APPEND);

f_lseek(&sd_file, 0);

f_read(&sd_file, (void *)buffer1, strlen(buffer1), (void *)&bw1 );

f_write(&usb_file, buffer1, strlen(buffer1), (void *)&bw1);

f_close(&sd_file);

f_close(&usb_file);

}

switch(Appli_state)

    {

     case APPLICATION_IDLE:

     break;

     case APPLICATION_START:

        FATFS_LinkDriver(&USBH_Driver, USBHPath);

     if(f_mount(&usb_fs, (TCHAR const*)USBHPath, 0) == FR_OK)

     {

     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:

        HAL_GPIO_WritePin(GPIOG, GPIO_PIN_11, 0);

        FATFS_UnLinkDriver(USBHPath);

     break;

    }