cancel
Showing results for 
Search instead for 
Did you mean: 

Link_driver failure

con3
Senior
Posted on January 17, 2018 at 12:27

Hey everyone,

For some reason I can mount, open and write to a SDcard. Although for some reason I cant use the LinkDriver function.

I have the function setup as if(FATFS_LinkDriver(&SD_Driver, SDPath)==0) and when it enters, it fails because disk.nbr seems to always be 1 no matter what I do.

Does anyone know what could be causing this? I have one sdcard connected in 4bit sdio mode.

A little stumped as it seems to be linked before I even call the function. I also dont have the MX_FATFS_Init(); in the code, so it couldn;t be linked there. Completely Cube generated.

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* Enable I-Cache-------------------------------------------------------------*/

  SCB_EnableICache();

  /* Enable D-Cache-------------------------------------------------------------*/

  SCB_EnableDCache();

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */

  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_DMA_Init();

  MX_SDMMC1_SD_Init();

  /* USER CODE BEGIN 2 */

uint32_t wbytes;

uint32_t bytesread;

const char wtext [] = 'Hello\0';

char rtext[20]= {0};

 volatile FRESULT res;

 volatile FRESULT res1;

 volatile FRESULT res2;

 //HAL_SD_Init(&hsd1);

  if(FATFS_LinkDriver(&SD_Driver, SDPath)==0){

      res = f_mount(&SDFatFS,SDPath,1);

      if(res == FR_OK){

        res2 =f_open(&SDFile, 'lat11.txt',FA_CREATE_ALWAYS | FA_WRITE);

       res1 = f_write(&SDFile, wtext, sizeof(wtext), (void *)&wbytes);

  //     res = f_read(&SDFile, rtext, sizeof(wtext), (void *)&bytesread);

          f_close(&SDFile);

      }

}

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

#sdmmc #sdio #sdmmc1 #cubemx-project #stm32f7
6 REPLIES 6
feuerwolf
Senior
Posted on January 18, 2018 at 10:07

I think your problem is related becase it got linked before. In my case this was the reason for a similar problem before.

I have serious problems with cube generated code + SD card (SDMMC) + fatFs as well. I succesfully use USB MSC & SD Card via SPI on a STM32L073 but litterally all SDMMC attempts with Cube generated Code failed so far.

For example a STML496 Discovery board which features a SD Card holder every Cube generated Code fails. The FatFs Example applications in Cube MCU folders works, but these Examples always look very different to Cube generated code, so implementing  until now fails. Also this Examples seems to be few years old and due to library changes they are not adoptable as well.

There is an example which is supposed to very easy:

https://community.st.com/0D50X00009bMM7JSAW

 

But it doesnt work, or at least i could not make it work. Also because of library changes and poor documentation.

Posted on January 20, 2018 at 12:55

It just randomly started working again. My SDcard doesn't seem to have any issues anymore.

I spent weeks debugging cube and have found the errors with the SDIO communication, so that I can regenerate cube and have it working. please have a look at this, I really hope it helps.

https://community.st.com/0D50X00009XkXg5SAF

https://community.st.com/0D50X00009XkXgUSAV

and finally in sd_diskio.c this function should look like this:

DSTATUS SD_initialize(BYTE lun)

{

Stat = STA_NOINIT;

if(BSP_SD_Init() == MSD_OK)

{

return SD_CheckStatus(lun);

}

return Stat;

}

Remember your pullup resistors and setting the gpio pins as pullups.

Hope this helps

Posted on January 20, 2018 at 19:41

thanks for sharing your work. I got all the same problems you are facing.

1. Linker Driver error.

2. Doesnt matter if DMA for SDMMC is disabled or enabled in Cubemx, alwas the write/read DMA functions got called and stalls in this while loop:

    /* Wait that the reading process is completed or a timeout occurs */

    timeout = HAL_GetTick();

    while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))

    {

    }

so writing/reading to SD card doesnt work with cube generated code.

3. Also this SMMC - DMA bug might be a problem in USB Mass Storage code generated by Cube. Which i can not get work, as well.

Internal vs external Pull Up Resistors? I mean hard wired Resistors vs internal Resistors. On Discovery board there are hard wired resistors and all example scematics i found use real resistors from 22k to 47k to 100k. Therefore internal Pull up resistors are not enabled in STM the example codes i found. Do you succesfully use internal Pull ups for SDIO?

Posted on February 10, 2018 at 21:32

Hi Alexander,

My apologies, I forgot to reply to your comment. For number 2, cube seems to generate callbacks that always call the DMA. This can be seen in sd_diskio.c

RESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)

{

  DRESULT res = RES_ERROR;

  ReadStatus = 0;

  uint32_t timeout;

&sharpif (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

  uint32_t alignedAddr;

&sharpendif

  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,

                           (uint32_t) (sector),

                           count) == MSD_OK)

  {ReadStatus = 1;

    /* Wait that the reading process is completed or a timeout occurs */

    timeout = HAL_GetTick();

    while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))

    {

    }

    /* incase of a timeout return error */

    if (ReadStatus == 0)

    {

      res = RES_ERROR;

    }

    else

    {

      ReadStatus = 0;

      timeout = HAL_GetTick();

      while((HAL_GetTick() - timeout) < SD_TIMEOUT)

      {

        if (BSP_SD_GetCardState() == SD_TRANSFER_OK)

        {

          res = RES_OK;

&sharpif (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

            /*

               the SCB_InvalidateDCache_by_Addr() requires a 32-Bit aligned address,

               adjust the address and the D-Cache size to invalidate accordingly.

             */

            alignedAddr = (uint32_t)buff & ~3;

            SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint32_t)buff - alignedAddr));

&sharpendif

           break;

        }

      }

    }

  }

  return res;

}

Without DMA this functin should be:

DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)

{

  DRESULT res = RES_ERROR;

  if(BSP_SD_ReadBlocks((uint32_t*)buff,

                       (uint32_t) (sector),

                       count, SD_TIMEOUT) == MSD_OK)

  {

    /* wait until the read operation is finished */

    while(BSP_SD_GetCardState()!= MSD_OK)

    {

    }

    res = RES_OK;

  }

  return res;

}

Note the change from sd_readblock.

and also the write function should be:

&sharpif _USE_WRITE == 1

DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)

{

  DRESULT res = RES_ERROR;

  if(BSP_SD_WriteBlocks((uint32_t*)buff,

                        (uint32_t)(sector),

                        count, SD_TIMEOUT) == MSD_OK)

  {

    /* wait until the Write operation is finished */

    while(BSP_SD_GetCardState() != MSD_OK)

    {

    }

    res = RES_OK;

  }

  return res;

}

&sharpendif /* _USE_WRITE == 1 */

Now the DMA won't be called at all.

If you have external pullups it should not be a problem!

feuerwolf
Senior
Posted on February 11, 2018 at 13:36

Hi, so i found out, that in order to use Blocked non DMA SDIO communication, there is a dropdown menue hidden in CubeMx

0690X00000604HqQAI.jpg

There its possible to deactivate the DMA template, which is always on as standard. Deactivate it an USB & FAT works fine without any tweaks.

Posted on February 11, 2018 at 13:39

Nice! They really hide this one well