[En-Nut-Discussion] NutSleep Overflow Fix

Bob Wirka bobwirka at yahoo.com
Fri Aug 16 17:32:01 CEST 2013

PS: I'm using ethernut-4.10.3 with an AT91SAM7X512.


Bob Wirka
Realtime Control Works
Janesville, WI

 From: Bob Wirka <bobwirka at yahoo.com>
To: "en-nut-discussion at egnite.de" <en-nut-discussion at egnite.de> 
Sent: Friday, August 16, 2013 10:25 AM
Subject: [En-Nut-Discussion] NutSleep Overflow Fix


NutSleep(uint32_t ms) will not work properly if  'ms' is greater than 4,294,967 (0xffffffff / 10000).

This is because the calculation in NutTimerMillisToTicks() will overflow.

Below is my fix:

 * \brief Calculate system ticks for a given number of milliseconds.
uint32_t NutTimerMillisToTicks(uint32_t ms)
    unsigned long long        product;

    product = (unsigned long long)ms * (unsigned long long)NutGetTickClock();
    return product / 1000;

     * _RCW_: 16-Aug-2013 : This would overflow for ms values > 4,294,967, as
     * the product of ms * NutGetTickClock() had to be <= 0xffff_ffff.
//    return (ms * NutGetTickClock()) / 1000;

Best regards,

Bob Wirka
Realtime Control Works
Janesville, WI

More information about the En-Nut-Discussion mailing list