cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS Multi Logic Device Support with single SPI Bus and Multiple CS pins

eenis
Associate

Hello..

I have a few problems, can you help me?

I am using FatFS on stm32f103RB MCU. I have a Single SPI bus and 2 CS pins for 2 SD Card.

used STM32CUBEIDE. No problem when I use 1 SD card and 1 CS pin. But Problem occurs when I use 2 SD Cards. Problem is SD Card 2 not mounting

DSTATUS USER_initialize (
	BYTE pdrv           /* Physical drive nmuber to identify the drive */
)
{
  /* USER CODE BEGIN INIT */
	return SD_disk_initialize(pdrv);
  /* USER CODE END INIT */
}

I am using the "pdrv" parameter here for CS pin control. But "pdrv" parameter allway 0. For this reason, SD Card 0 remains selected.

I mount like this:

FATFS fs0,fs1;  // file systems
FIL fil0,fil1; // Files
 
// mount the device with fs
  fresult = f_mount(&fs0, "0:/", 1);
  fresult = f_mount(&fs1, "1:/", 1);
 
  // create files
  fresult = f_open(&fil0, "0:/test_file_for_sd0.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
  fresult = f_open(&fil1, "1:/test_file_for_sd1.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
 
  // Write text
  f_puts("Test DATA to SD 0", &fil0);
  f_puts("Test DATA to SD 1", &fil1);
 
  // Close files
  fresult = f_close(&fil0);
  fresult = f_close(&fil1);

my FatFS library parameters :

0693W00000ANnJXQA1.pngI will be very happy if you can help :)

Thanks..

2 REPLIES 2

You'll have to manage this, and the selection/deselection, in the DISKIO layer code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eenis
Associate

Thank you for quickly replay.

Inside the discio.c file there is the "disk_initialize" function. The "pdrv" parameter here is always seen as 0 in debug mode. I've closed all the "fil0" and "fs0" related lines. I only worked with "fil1" and "fil2" but "pdrv" was 0 again. I was able to follow this parameter in the above f_mount function. The value that is 1 then changes to 0. as a result, I always read the value 0 in user-side functions.

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
  DSTATUS stat = RES_OK;
  
  if(disk.is_initialized[pdrv] == 0)
  { 
    disk.is_initialized[pdrv] = 1;
    stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);
  }
  return stat;
}