Skip to content

Off-by one error in HardwareTimer ticks values? #897

Closed
@matthijskooijman

Description

@matthijskooijman

The HardwareTimer class applies a -1 to the timer tick values it sets. e.g. in setOverflow():

case HERTZ_FORMAT:
period_cyc = getTimerClkFreq() / overflow;
Prescalerfactor = (period_cyc / 0x10000) + 1;
LL_TIM_SetPrescaler(_timerObj.handle.Instance, Prescalerfactor - 1);
ARR_RegisterValue = (period_cyc / Prescalerfactor) - 1;
break;
case TICK_FORMAT:
default :
ARR_RegisterValue = overflow - 1;
break;

And similarly in setCaptureCompare().

This is a bit unexpected. When using TICK_FORMAT, I would expect that the value you pass is set into the hardware directly. One might say that the tick number passed to these function identifies the number of the tick and is 1-based and must be translated to 0-based when writing to the register, but I did not expect this. I passed 0 to this function only to find that the capture never triggered, apparently since -1 was written to the register.

Furthermore, when using TICK_MICROSECONDS, it would also make sense to pass 0. If you do that now, the -1 also causes an underflow and the compare never triggers. This also happens when you pass a non-zero but low value (depending on the value of the prescaler), so just passing a fixed low value is not always helpful either.

So: I would think that all these -1's should just be removed? Or is there any compelling argument I'm missing?

Metadata

Metadata

Assignees

Labels

question ❓Usually converted as a discussion

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions