Arduino_core_stm32: Porting marlin timers to new HardwareTimers code

Created on 5 Sep 2019  Â·  5Comments  Â·  Source: stm32duino/Arduino_Core_STM32

Hello Frederic,

I was working on porting the existing Timer code in Marlin to the new HardwareTimer code in 1.6. I encountered the following code in Marlin:

void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
__HAL_TIM_SET_AUTORELOAD(&TimerHandle[timer_num].handle, compare);
if (HAL_timer_get_count(timer_num) >= compare)
TimerHandle[timer_num].handle.Instance->EGR |= TIM_EGR_UG; // Generate an immediate update interrupt
}

I can replace the __HAL_TIM_SET_AUTORELOAD call with HardwareTimers[num]->setOverflow(compare, TICK_FORMAT).

I was wondering what to do with the next two lines of code? It seems the goal they were trying to achieve is if one sets the overflow to a value greater than the current counter, generate an update interrupt.
(does this even work? I can see several race conditions)

Should we add something similar to the setOverflow method in the HardwareTimers class?

Answered Question

All 5 comments

Hi @ghent360,
that's nice you start this porting.
@ABOSTM is our Timer expert. Alexandre could you give your feedback about this? Thanks in advance.

Hi @ghent360,
First, yes it works: hardware doesn't prevent someone to set Autoreload lower than current value. Counter will continue to increment (in case of upcounting) until it reach its maximum value 0xFFFF (for 16bit timer) and then restart from 0. but generally it is not the expected behavior, because it is more or less like if you have an intermediate period set to max.
Now you can replace ' TimerHandle[timer_num].handle.Instance->EGR |= TIM_EGR_UG;' by a call to refresh() method of HardwareTimer. And replace the HAL_timer_get_count() condition with getCount() method.

You may also consider removing the condition and call systematically refresh() . It will reset counter (restart from 0) with directly the good frequency.

Thank you for the advice. I'll test that.

On Thu, Sep 5, 2019 at 1:58 AM Alexandre Bourdiol notifications@github.com
wrote:

Hi @ghent360 https://github.com/ghent360,
First, yes it works: hardware doesn't prevent someone to set Autoreload
lower than current value. Counter will continue to increment (in case of
upcounting) until it reach its maximum value 0xFFFF (for 16bit timer) and
then restart from 0. but generally it is not the expected behavior, because
it is more or less like if you have an intermediate period set to max.
Now you can replace ' TimerHandle[timer_num].handle.Instance->EGR |=
TIM_EGR_UG;' by a call to refresh() method of HardwareTimer. And replace
the HAL_timer_get_count() condition with getCount() method.

You may also consider removing the condition and call systematically
refresh() . It will reset counter (restart from 0) with directly the good
frequency.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/stm32duino/Arduino_Core_STM32/issues/636?email_source=notifications&email_token=AB6FRVTPIML4HO4D6NYJWLLQIDC45A5CNFSM4ITZREMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD56MF6Q#issuecomment-528270074,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB6FRVRUFJJI7XTOL75ZXCTQIDC45ANCNFSM4ITZREMA
.

Hi Frederic & Alexandre,

I found a small bug in the HT implementation. I sent PR #641 with a
proposed fix.

~V

On Sat, Sep 7, 2019 at 10:34 AM Venelin Efremov veffremov.ve@gmail.com
wrote:

Thank you for the advice. I'll test that.

On Thu, Sep 5, 2019 at 1:58 AM Alexandre Bourdiol <
[email protected]> wrote:

Hi @ghent360 https://github.com/ghent360,
First, yes it works: hardware doesn't prevent someone to set Autoreload
lower than current value. Counter will continue to increment (in case of
upcounting) until it reach its maximum value 0xFFFF (for 16bit timer) and
then restart from 0. but generally it is not the expected behavior, because
it is more or less like if you have an intermediate period set to max.
Now you can replace ' TimerHandle[timer_num].handle.Instance->EGR |=
TIM_EGR_UG;' by a call to refresh() method of HardwareTimer. And replace
the HAL_timer_get_count() condition with getCount() method.

You may also consider removing the condition and call systematically
refresh() . It will reset counter (restart from 0) with directly the good
frequency.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/stm32duino/Arduino_Core_STM32/issues/636?email_source=notifications&email_token=AB6FRVTPIML4HO4D6NYJWLLQIDC45A5CNFSM4ITZREMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD56MF6Q#issuecomment-528270074,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AB6FRVRUFJJI7XTOL75ZXCTQIDC45ANCNFSM4ITZREMA
.

Issues have been resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidanger picture davidanger  Â·  6Comments

pasindu-sandima picture pasindu-sandima  Â·  5Comments

kwastek picture kwastek  Â·  4Comments

pawelsky picture pawelsky  Â·  4Comments

mcer12 picture mcer12  Â·  3Comments