[PATCH] Updated tick implementation for HAL_GetTick

Gedare Bloom gedare at rtems.org
Thu Apr 22 15:00:01 UTC 2021

```On Thu, Apr 22, 2021 at 8:02 AM Sebastian Huber
<sebastian.huber at embedded-brains.de> wrote:
>
> On 22/04/2021 14:17, Robin Müller wrote:
>
> > It worked for me. Can you specify that? The overflow check would have
> > to be implemented by an upper layer, right?
> > I can adapt my former patch using the tick solution to do the
> > multiplication first if this is better.
> If you call HAL_GetTick() when the clock tick is 0xffffabcd and then
> 0x123 and you calculate the difference of the results, the value should
> be all right. I am not sure if this is the case if you calculate it by
> rtems_clock_get_ticks_since_boot() * 1000) /
> rtems_clock_get_ticks_per_second()
>
It will not work properly in case of an overflow in the
multiplication. A quick illustration that is (hopefully) correct:

(a*x/y)%n != (a*x % n) / y

In case of n a power of 2, the division of the overflow does not
divide out the value of 2^n, so you end up with an error of 2^(n-1)/y
in your result.

Example in 8-bits, with x = 8 and y = 2:
start = 7 ticks
stop = 9 ticks
The right answer should be (9-7)*8/2 = 8

With (a*x)/y:
7*8/2 = 28
9*8/2 = 4
4 - 56 = 232 (or so)

With a*(x/y)
7*(8/2) = 28
9*(8/2) = 36
36 - 28 = 8         (hooray)

when a*(x/y) overflows this will also work as usual for wrapped
overflow arithmetic, example:
start = 63
stop = 65
expected (65-63)*8/2 = 8

63*(8/2) = 252
65*(8/2) = 4
4 - 252 = 8    (hooray)

Do the division first please. :)

> --
> embedded brains GmbH
> Herr Sebastian HUBER
> Dornierstr. 4
> 82178 Puchheim
> Germany
> email: sebastian.huber at embedded-brains.de
> phone: +49-89-18 94 741 - 16
> fax:   +49-89-18 94 741 - 08
>
> Registergericht: Amtsgericht München
> Registernummer: HRB 157899
> Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
> Unsere Datenschutzerklärung finden Sie hier:
> https://embedded-brains.de/datenschutzerklaerung/
>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
```

More information about the devel mailing list