[En-Nut-Discussion] NutMicroDelay tends to integer overflows

Ole Reinhardt ole.reinhardt at embedded-it.de
Mon Aug 27 15:25:32 CEST 2012


Hi Uwe,

> The wording should be:
> tends to overflow on systems with fast non-Cortex CPU
> 
> With the systick approach on a STM32F4 running at 168 MHz with 1000 Hz
> systick, overflow will happen after 2^31/167999 = 12.887 seconds, that is
> more than 3 hours. Anything calling a blocking delay for that long seems
> insane...

That's not the problem. The problem is the calculation of how much
cycles are needed:

end_ticks = (us * (SysTick->LOAD +1))/NUT_TICK_FREQ;


For example:

us = 50.000   (50ms)

At my 120 Mhz CPU Clock 

SysTick->LOAD = 119.999

==> 

50.000 * (119.999 + 1) = 6.000.000.000

That's an overflow :(

Even 25ms would result in an signed integer overflow.


So, rewrite the calculation like this:

end_ticks = ((SysTick->LOAD +1)/NUT_TICK_FREQ) * us;

Now the overflow is fixed, but you lost precision.

By limiting NutMicroDelay to a maximum of 10ms we are still save until
we get even higher CPU frequencies.

So all in all this solution is not very nice.

Using 64 bit integers would do the trick but currently the mentioned
build problems prevents me to spend too much time in it.


> I even think that the use if NutDelay should be considered harmfull beside
> some every special cases. Is there e.g. a reson you use NutDelay in your
> lpc17xx_emac.c driver and not NutSleep?

Sorry and thanks for the hint :-) These two are left-overs and should be
replaced by NutSleep();

But NutDelay is still widely referenced in Nut/OS.

Here is a file list:

arch/h8300h/dev/nicrtl.c
arch/h8300h/dev/scih8.c
arch/h8300h/dev/ostimer.c
arch/avr/dev/usartavr.c
arch/avr/dev/ahdlcavr.c
arch/avr/dev/hd44780.c
arch/avr/dev/cs8900.c
arch/avr/dev/spiflash.c
arch/avr/dev/lanc111.c
arch/avr/dev/tlc16c550.c
arch/avr/dev/nicrtl.c
arch/avr/dev/uartavr.c
arch/avr/dev/ostimer.c
arch/avr/dev/hd44780_bus.c
arch/avr/dev/vs1001k.c
arch/avr32/dev/usart.c
arch/avr32/dev/macb.c
arch/m68k/dev/ostimer.c
arch/cm3/board/lisa.c
arch/cm3/dev/nxp/lpc17xx_emac.c
arch/cm3/dev/nxp/lpc177x_8x_mci.c
arch/arm/board/ethernut5.c
arch/arm/dev/atmel/st7036_at91.c
arch/arm/dev/atmel/hd44780_at91.c
arch/arm/dev/atmel/ostimer_at91.c
arch/arm/dev/atmel/at91_emac.c
arch/arm/dev/atmel/at91sam7x_emac.c
arch/arm/dev/atmel/at91_ahdlc.c
arch/arm/dev/ax88796.c
arch/arm/dev/dm9000e.c
arch/arm/dev/gba/ostimer_gba.c
arch/arm/dev/gba/uartgba.c
arch/arm/dev/cs8900a.c
arch/arm/dev/ostimer_s3c4510b.c
conf/os/os.nut
dev/vscodec.c
dev/chat.c
dev/usartsc16is752.c
dev/vs10xx.c
dev/phy.c
dev/lan91.c
dev/dm9000.c
hwtest/avr/isp2/isp2.c
include/arch/timer.h
include/sys/timer.h
include/dev/hd44780.h
mod/dev/vs1001k.c
net/tcpsm.c

Best regards,

Ole

-- 

Thermotemp GmbH, Embedded-IT

Embedded Hard-/ Software and Open Source Development, 
Integration and Consulting

http://www.embedded-it.de

Geschäftsstelle Siegen - Steinstraße 67 - D-57072 Siegen - 
tel +49 (0)271 5513597, +49 (0)271-73681 - fax +49 (0)271 736 97

Hauptsitz - Hademarscher Weg 7 - 13503 Berlin
Tel +49 (0)30 4315205 - Fax +49 (0)30 43665002
Geschäftsführer: Jörg Friedrichs, Ole Reinhardt
Handelsregister Berlin Charlottenburg HRB 45978 UstID DE 156329280 




More information about the En-Nut-Discussion mailing list