Page 1 of 1

Fatfs timeout period and system tick frequency

Posted: Wed Jun 03, 2015 2:18 am
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?

Re: Fatfs timeout period and system tick frequency

Posted: Wed Jun 03, 2015 8:14 am
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

Re: Fatfs timeout period and system tick frequency

Posted: Fri Jun 05, 2015 6:00 am
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

Re: Fatfs timeout period and system tick frequency

Posted: Fri Jun 05, 2015 7:49 am
by Giovanni
Hi,

You could simply do:

#define _FS_TIMEOUT MS2ST(1000)

Giovanni

Re: Fatfs timeout period and system tick frequency

Posted: Fri Jun 05, 2015 4:24 pm
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

Re: Fatfs timeout period and system tick frequency

Posted: Fri Jun 05, 2015 7:05 pm
by Giovanni
Will do :)

Giovanni