ATMega328 / Arduino

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Vik
Posts: 23
Joined: Mon Apr 15, 2013 9:30 pm

Re: ATMega328 / Arduino

Post by Vik »

Giovanni wrote:I'll add a cast to uint32_t to both the parameter and CH_FREQUENCY in next version.

Giovanni


Hi Giovanni,

The AVR patches are in trunk now, but they don't really work because of this bug. Can we fix it in trunk?

Btw, if you are going to use uint32_t, then you might want to change 1L and 100000L to UL to prevent further casting.

Cheers,
Vik
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ATMega328 / Arduino

Post by Giovanni »

Hi, I committed a fix, please verify.

Giovanni
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: ATMega328 / Arduino

Post by utzig »

Works now, thanks!
Vik
Posts: 23
Joined: Mon Apr 15, 2013 9:30 pm

Re: ATMega328 / Arduino

Post by Vik »

Thanks for this, I'll test this evening but I expect that it will work fine.

A pedantic point about S2ST() though:

Code: Select all

 #define S2ST(sec)                                                           \
-  ((systime_t)((sec) * CH_FREQUENCY))
+  ((systime_t)(((uint32_t)(sec)) * ((uint32_t)CH_FREQUENCY)))


This is probably a waste on architectures where systime_t is uint16_t (mostly the 8 bit controllers).

If this is the case, then the 32 bit result will immediately get truncated to 16 bit, so the computation is more expensive but it doesn't accomplish anything. How about casting to systime_t in this case, like this?

Code: Select all

((systime_t)(((systime_t)(sec)) * ((systime_t)CH_FREQUENCY)))


Cheers,
Vik
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: ATMega328 / Arduino

Post by Giovanni »

I would verify first, being both operands constants (almost always) and then casted to systime_t there would be no 32bits operations involved at runtime, the same is true for other macros.

Anyway, I would rather leave those non-casted rather than casted to systime_t since in that case there is no division.

Giovanni
Post Reply