Page 1 of 2
Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:06 am
by Michail
Hello!
First of all, thanks for such a great OS!
I'm using ChibiOS 3.0 branch on STM32F050F4 with
GNU Tools for ARM Embedded Processors 4.8-2014-q2-update.
When using define ST2US(n), I'm getting:
Code: Select all
C:\Work\CoIDE\ChibiOS_3.0_Test\os\rt\include/chvt.h:173:32: warning: division by zero [-Wdiv-by-zero]
[cc] #define ST2US(n) ((((n) - 1UL) / (CH_CFG_ST_FREQUENCY / 1000000UL)) + 1UL)
Why not to change it (and other time conversion defines) as:
Code: Select all
#define ST2US(n) ((((n) - 1UL) * 1000000UL / CH_CFG_ST_FREQUENCY) + 1UL)
?
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:15 am
by Giovanni
Hi,
The first is wrong for CH_CFG_ST_FREQUENCY<1000000, the second limits the number of uS to about 4000 because an overflow limitation.
Probably the correct solution is to round up CH_CFG_ST_FREQUENCY / 1000000UL to the next integer in order to not let it be zero.
Giovanni
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:22 am
by Michail
Well, I see...
But if that way?
Code: Select all
#define ST2US(n) ((((n) - 1UL) * (1000000UL / CH_CFG_ST_FREQUENCY)) + 1UL)
Furthermore, multiplication to constant shoud be quicker than division...
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:55 am
by Giovanni
That one fails for CH_CFG_ST_FREQUENCY > resolution so would not work for ST2MS and ST2S. It is not easy to do a macro for arbitrary CH_CFG_ST_FREQUENCY and resolution values, there is always a zero result somewhere.
Giovanni
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 8:08 am
by Michail
We can change only ST2US(n)

Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 8:10 am
by Giovanni
That would not work for the others because ST2MS can fall in both cases depending on CH_CFG_ST_FREQUENCY, which can be greater or lower than 1000. I have to find a more general solution.
Giovanni
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 2:12 pm
by Giovanni
I decided to go for you initial suggestion until a better solution is found, it works except we lose about 20bits of range in the multiplication.
Giovanni
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:33 pm
by Michail
Hmm... 20 bits - sounds bad...
Doesn't compiler counts (1000000UL / CH_CFG_ST_FREQUENCY) as const before compilation?
Don't see changes on
svn://svn.code.sf.net/p/chibios/svn/bra ... rnel_3_dev. What's the latest 3.0-like branch?
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 7:45 pm
by Giovanni
Now 3.0 is the trunk so just checkout that.
The compiler should fold all the constant expressions but the precision is still 32bits even at compile time I believe.
Giovanni
Re: Time conversions (ST2US)
Posted: Mon Aug 04, 2014 8:17 pm
by Michail
Ok, thank you.
Tomorrow I'll try to update to trunk. Hope it will not break my apps too much
