Time conversions (ST2US)

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Time conversions (ST2US)

Post 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)
?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Time conversions (ST2US)

Post 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
Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Re: Time conversions (ST2US)

Post 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...
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Time conversions (ST2US)

Post 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
Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Re: Time conversions (ST2US)

Post by Michail »

We can change only ST2US(n) ;)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Time conversions (ST2US)

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Time conversions (ST2US)

Post 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
Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Re: Time conversions (ST2US)

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Time conversions (ST2US)

Post 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
Michail
Posts: 28
Joined: Mon Aug 04, 2014 6:55 am

Re: Time conversions (ST2US)

Post by Michail »

Ok, thank you.

Tomorrow I'll try to update to trunk. Hope it will not break my apps too much ;)
Post Reply