Author Topic: SDMMC_WriteFIFO Hardfault on STM32F7  (Read 510 times)

0 Members and 1 Guest are viewing this topic.

Offline Boscoe

  • Regular Contributor
  • *
  • Posts: 171
SDMMC_WriteFIFO Hardfault on STM32F7
« on: September 12, 2019, 07:24:27 pm »
Hi all,

I've got a strange one here, perhaps something wrong with the STM32 HAL. I'm trying to demo FATFS that has been created through CubeMX. I have used SDIO 4 bits and enabled FATFS with the default settings. I use the code below to test. I can create the file on the disk however when I try to write to the file I get a Hardfault in the SDMMC_WriteFIFO function. The memory address for the data it's trying to write to the card is out of range at 0x20050000. I find this very strange. Has anyone else had this issue? I see one person has posted it on the ST forums without a proper solution.

Thanks

Code: [Select]

f_mount(&FatFs, "", 0);

fr = f_open(&Fil, "george.txt", FA_WRITE | FA_CREATE_ALWAYS);

if (fr == FR_OK)
{
f_write(&Fil, "It works!\r\n", 11, &bw);

fr = f_close(&Fil);

if (fr == FR_OK && bw == 11)
{
asm("NOP");
}
}
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 1593
  • Country: ca
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #1 on: September 12, 2019, 09:35:48 pm »
So the file is actually created on the SD card, you've verified this by reading it on a PC?
Tried with code optimizations disabled?
SD was formatted manually by you on PC to a compatible type, or with fatfs? Maybe look at the blocksize/filesystem/etc info, see if it matches up, I think there was a structure for that somewhere..

Fairly sure it doesn't matter, but could try FA_READ | FA_WRITE for permissions as well.


http://elm-chan.org/fsw/ff/doc/open.html
http://elm-chan.org/fsw/ff/doc/mount.html
 


Offline Boscoe

  • Regular Contributor
  • *
  • Posts: 171
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #3 on: September 13, 2019, 02:10:05 pm »
Thank you both. I used the patch and got it working however have run into some other issues. Will be putting this on hold.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 3765
  • Country: fr
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #4 on: September 13, 2019, 02:42:54 pm »
No problem and good luck.

I don't know if you're using a recent version of ST's library or not. If so, and the problem has not been officially fixed yet, I think this is really bad of ST.
This tells that you should usually NOT trust vendor's libraries (except maybe for the very low-level ones) and example code. They are often full of bugs and are just released "as is".
 

Offline Boscoe

  • Regular Contributor
  • *
  • Posts: 171
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #5 on: September 14, 2019, 08:41:39 pm »
No problem and good luck.

I don't know if you're using a recent version of ST's library or not. If so, and the problem has not been officially fixed yet, I think this is really bad of ST.
This tells that you should usually NOT trust vendor's libraries (except maybe for the very low-level ones) and example code. They are often full of bugs and are just released "as is".

Yes they certainly have quirks however this is the first issue I've come across that is outright broken. Essentially the problem is you have to write a block even if you just want to right one byte. So you have your one byte to write and pass this as a pointer to the write function and the write function just keeps incrementing that pointer for one whole block size regardless of how much memory that pointer has been allocated!!!
 

Online techman-001

  • Frequent Contributor
  • **
  • Posts: 524
  • Country: au
  • Electronics technician for the last 47 years
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #6 on: September 15, 2019, 01:14:34 am »
Essentially the problem is you have to write a block even if you just want to right one byte. So you have your one byte to write and pass this as a pointer to the write function and the write function just keeps incrementing that pointer for one whole block size regardless of how much memory that pointer has been allocated!!!

Perhaps they have to write a whole block at a time as it's a block file system and not RAM ?

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 3765
  • Country: fr
Re: SDMMC_WriteFIFO Hardfault on STM32F7
« Reply #7 on: September 15, 2019, 04:27:32 pm »
This is a bug in the FATFS code and not in SDMMC_WriteFIFO() itself.

SDMMC_WriteFIFO() takes a pointer to uint32_t. The parameter being a pointer is sort of misleading; it actually only copies the pointed 32-bit word to some register. This is why there is no additional "block length" parameter. The only acceptable way of using it is passing a pointer to uint32_t, pointing to a single uint32_t variable, or a buffer (array) of uint32_t's, from which it will only use the first word. Of course it could also be a buffer of bytes, as long as it's properly aligned and points to at least 4 bytes.

This is an instance of this function I got:

Code: [Select]
HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
{
  /* Write data to FIFO */
  SDMMCx->FIFO = *pWriteData;

  return HAL_OK;
}

Some developer adapting FATFS probably played tricks passing it a pointer to a buffer of bytes instead. Of course, if the pointed-to buffer has less than 4 bytes, it's a bug. If it's not aligned to a 32-bit word, it may also crash depending on the CPU. A basic misuse of pointers.

« Last Edit: September 15, 2019, 05:00:00 pm by SiliconWizard »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf