SD card write in STM32F7 Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

SD card write in STM32F7  Topic is solved

Postby josesimoes » Mon Mar 04, 2019 7:33 pm

Hi,

I'm trying to write to SD Card on STM32F769I-DISCO1 board.
Had to adjust the the configurations because the SD Card connector on this board is wired to SDMMC 2-.

This requires hacking "halconf.h" to force FATFS_HAL_DEVICE.

Code: Select all

#if !defined(FATFS_HAL_DEVICE) || defined(__DOXYGEN__)
//this board requires SDCD2 not SDCD1
#define FATFS_HAL_DEVICE SDCD2
#endif



Moving on. I can create folders and files (empty files).

When writting data to the files, the file content - depite the size is correct - is always garbled. Either rubbish content or unrelated strings that I can recognize as bits of data that exist on the code.
I can see that the file size is correct. If I later open that SD Card on the PC and open the file in a binary editor, the file size is correct too, but the content is rubbish.
I've tried set the write buffer to a malloced pointer and to a constant string in flash. All this trying to determine if any chache or DMA issues could be explain this (being an F7).

The test code is as simple as this:

Code: Select all

    if(f_puts("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", &file) == 445)
    {
        // expected number of bytes written
        operationResult = FR_OK;
    }

    // close file
    f_close(&file);



I've step in the calls to f_puts and f_close and the calls inside all seem to be OK and no errors are being missed.
If this helps, I've reformatted the SD cards with FAT32 both on the PC as in the device itself by calling f_mkfs.
I've also tried with two different cards: a 4GB SANDISK and a 2GB VERBATIN.

Where should I look further to debug this?

TIA!

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD card write in STM32F7

Postby Giovanni » Tue Mar 05, 2019 8:27 am

Probably a cache coherence problem, you need to perform buffers invalidation/flushing. Best place to do that should be the FatFS wrapper. Aren't FatFS buffers in BSS? that section is by default placed in TCM so not cacheable.

Giovanni

josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Re: SD card write in STM32F7

Postby josesimoes » Thu Mar 07, 2019 5:15 pm

Hi Giovanni,

Following your suggestion I've added:

Code: Select all

  // invalidate cache on buffer
  cacheBufferFlush(buff, count * MMCSD_BLOCK_SIZE);


at line 166 of fatfs_diskio.c and the writes on the SD card are now working perfectily on the STM32F769I.

I now understand that my previous attempts to invalidate the cache were failing because I was doing it right before calling f_puts and f_write.
As the buffer still undergo some processing before actually loading the DMA, so the cache kicks in again making that useless.

Could you please include this fix on the repo?

Thanks!

josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Re: SD card write in STM32F7

Postby josesimoes » Sun Mar 10, 2019 12:31 pm

Actually this requires a bit more than the above fix.
For SPI access the writes are done in batches so invalidating all the buffer at start won't work.
This should be the correct fix:

Code: Select all

{
  switch (pdrv) {
#if HAL_USE_MMC_SPI
  case MMC:
    if (blkGetDriverState(&FATFS_HAL_DEVICE) != BLK_READY)
        return RES_NOTRDY;
    if (mmcIsWriteProtected(&FATFS_HAL_DEVICE))
        return RES_WRPRT;
    if (mmcStartSequentialWrite(&FATFS_HAL_DEVICE, sector))
        return RES_ERROR;

    while (count > 0) {
      // invalidate cache on buffer
      cacheBufferFlush(buff, MMCSD_BLOCK_SIZE);

      if (mmcSequentialWrite(&FATFS_HAL_DEVICE, buff))
          return RES_ERROR;
      buff += MMCSD_BLOCK_SIZE;
      count--;
    }
    if (mmcStopSequentialWrite(&FATFS_HAL_DEVICE))
        return RES_ERROR;
    return RES_OK;
#else
  case SDC:
    if (blkGetDriverState(&FATFS_HAL_DEVICE) != BLK_READY)
      return RES_NOTRDY;

    // invalidate cache on buffer
    cacheBufferFlush(buff, count * MMCSD_BLOCK_SIZE);

    if (sdcWrite(&FATFS_HAL_DEVICE, sector, buff, count))
      return RES_ERROR;
    return RES_OK;
#endif
  }
  return RES_PARERR;
}

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD card write in STM32F7

Postby Giovanni » Sun Mar 10, 2019 1:30 pm

I wonder if this is sufficient, this patch flushes the buffer before SD writes but does not invalidate the cache after SD reads, the CPU could read cached data instead of what DMA just wrote into the buffer.

Giovanni

josesimoes
Posts: 91
Joined: Sat Feb 18, 2017 11:50 am
Has thanked: 43 times
Been thanked: 23 times

Re: SD card write in STM32F7

Postby josesimoes » Sun Mar 10, 2019 9:24 pm

That has crossed my mind... I thought that should probably the callers responsibility. But, then again, maybe it should be in the wrappers too...

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD card write in STM32F7

Postby Giovanni » Sun Mar 31, 2019 8:30 am

It could be in the wrapper only if buffers could be guaranteed to be aligned to 32 bytes addresses, invalidating cache requires an aligned buffer or it would also invalidate adjacent data.

In this case the buffer is handled in FatFS, aligning the buffer would require patching that code.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD card write in STM32F7

Postby Giovanni » Sun Nov 10, 2019 10:24 am

Closing, cannot be fixed in the driver nor in the bindings. It has to be application care to allocate FatFS buffers in non-cacheable memory or to patch FatFS.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 21 guests