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
Post
by Michail » Mon Aug 04, 2014 7:06 am
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)?
Giovanni
Site Admin
Posts: 14895 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Mon Aug 04, 2014 7:15 am
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
Post
by Michail » Mon Aug 04, 2014 7:22 am
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...
Giovanni
Site Admin
Posts: 14895 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Mon Aug 04, 2014 7:55 am
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
Post
by Michail » Mon Aug 04, 2014 8:08 am
We can change only ST2US(n)
Giovanni
Site Admin
Posts: 14895 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Mon Aug 04, 2014 8:10 am
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
Giovanni
Site Admin
Posts: 14895 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Mon Aug 04, 2014 2:12 pm
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
Giovanni
Site Admin
Posts: 14895 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Mon Aug 04, 2014 7:45 pm
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
Post
by Michail » Mon Aug 04, 2014 8:17 pm
Ok, thank you.
Tomorrow I'll try to update to trunk. Hope it will not break my apps too much