[PATCH] updated HAL_GetTick function

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Apr 19 17:53:41 UTC 2021


On 19/04/2021 18:42, Gedare Bloom wrote:

> On Mon, Apr 19, 2021 at 10:41 AM Gedare Bloom<gedare at rtems.org>  wrote:
>> On Mon, Apr 19, 2021 at 2:51 AM Robin Müller<robin.mueller.m at gmail.com>  wrote:
>>>  From 88ac5780f5597a8bb3bf68c6b37fe6694956faaf Mon Sep 17 00:00:00 2001
>>> From: Robin Mueller<robin.mueller.m at gmail.com>
>>> Date: Mon, 19 Apr 2021 10:39:55 +0200
>>> Subject: [PATCH] updated HAL_GetTick function
>>>
>>> Patch text went missing, here is the full version of the patch.
>>> Provides implementation for HAL_GetTick but uses RTEMS.
>>> Accounts for the tick base not being 1ms now
>>> ---
>>>   bsps/arm/stm32h7/start/bspstart.c | 3 ++-
>>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/bsps/arm/stm32h7/start/bspstart.c b/bsps/arm/stm32h7/start/bspstart.c
>>> index 2fc8133cca..e6e995a50c 100644
>>> --- a/bsps/arm/stm32h7/start/bspstart.c
>>> +++ b/bsps/arm/stm32h7/start/bspstart.c
>>> @@ -32,9 +32,10 @@
>>>
>>>   #include <stm32h7xx_hal.h>
>>>
>>> +/* Get number of milliseconds elapsed since startup */
>>>   uint32_t HAL_GetTick(void)
>>>   {
>>> -  return 0;
>>> +  return (rtems_clock_get_ticks_since_boot() * 1000) / rtems_clock_get_ticks_per_second();
>> This is reasonable, but note that:
>> 1. usually it is better to divide before multiplying, to reduce the
>> chance of arithmetic overflows.
>> 2. the ticks_since_boot is 32-bits, with 1000 Hz ticking it overflows
>> in ~2 months
>>
>> I suggest you consider
>>    return rtems_clock_get_uptime_seconds()*1000;
>>
>> Or you can use rtems_clock_get_uptime_nanoseconds() / (10000000) if ms
>> precision is needed.
>>
> I just saw that the HAL function also returns a u32, so it will
> overflow anyway and it doesn't really matter what you do.
>
The HAL_GetTick() should still be correct, otherwise you have an 
infrequent bug which may or may not trouble you seriously. Maybe just do 
the computation with 64-bit floating point numbers or just 
rtems_clock_get_uptime_nanoseconds() / 1000.

-- 
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/



More information about the devel mailing list