Fatfs timeout period and system tick frequency

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
gallegojm
Posts: 42
Joined: Mon Apr 06, 2015 2:02 am

Fatfs timeout period and system tick frequency

Post by gallegojm »

Working to my ftpserver, I stumbled on an error when two threads was writing to the SD card.
Because of my little experience debugging ChibiOs, it took me some time to find that function f_write() in fatfs was returning a 'time out' error and that I can solve it modifying parameter _FS_TIMEOUT (Timeout period in unit of time tick) in ffconf.h.

But that do not explains why I got this error with ChibiOs 3 and not with version 2.
Until I notice that the system tick frequency defined by CH_FREQUENCY in version 2 and equal to 1000 for almost all the boards was now defined by CH_CFG_ST_FREQUENCY and that this value has increased for many boards. Especially, for the STM32-E407, his value is now 10000.
So, without modifying any parameter in ffconf.h, the time out period was divided by 10. :cry:

To avoid this inconvenience, I would like to know if it is a good idea to define _FS_TIMEOUT in function of system tick frequency.
For example:

Code: Select all

#define _FS_TIMEOUT   CH_CFG_ST_FREQUENCY  // timeout period of 1 second

Or if it is considered a bad programming practice?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Fatfs timeout period and system tick frequency

Post by Giovanni »

Hi,

Timeouts should not change depending on the system tick clock, in ChibiOS the system tick can have any value between 10 and 16000000Hz (o even more).

You need to use the time conversion macros, for example MS2ST(1000), converts from 1000 milliseconds to a number of system ticks depending on the current frequency. Your timeout will be the same regardless the frequency.

Giovanni
gallegojm
Posts: 42
Joined: Mon Apr 06, 2015 2:02 am

Re: Fatfs timeout period and system tick frequency

Post by gallegojm »

Hi Giovanni
I think I expressed myself badly.
The original define for _FS_TIMEOUT in ffconf.h is:

Code: Select all

#define _FS_TIMEOUT     1000    /* Timeout period in unit of time tick */


and it is used in file chibios3\os\various\fatfs_bindings\fatfs_syscall.c

Code: Select all

int ff_req_grant(_SYNC_t sobj) {
  msg_t msg = chSemWaitTimeout(sobj, (systime_t)_FS_TIMEOUT);
  return msg == MSG_OK;
}


Did you think we have to change this to:

Code: Select all

#define _FS_TIMEOUT     1000    /* Timeout period in milliseconds */


and:

Code: Select all

  msg_t msg = chSemWaitTimeout(sobj, MS2ST(_FS_TIMEOUT));


Jean-Michel
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Fatfs timeout period and system tick frequency

Post by Giovanni »

Hi,

You could simply do:

#define _FS_TIMEOUT MS2ST(1000)

Giovanni
gallegojm
Posts: 42
Joined: Mon Apr 06, 2015 2:02 am

Re: Fatfs timeout period and system tick frequency

Post by gallegojm »

Hi Giovanni
Yes, it is more elegant. Thank you.
It would be nice to update in next release that define in files
chibios3\ext\fatfs\src\ffconf_template.h
and
chibios3\demos\STM32\RT-STM32F407-OLIMEX_E407-LWIP-FATFS-USB\ffconf.h
Jean-Michel
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Fatfs timeout period and system tick frequency

Post by Giovanni »

Will do :)

Giovanni
Post Reply