Repository navigation
Commit 8a81c63
fix(HardwareTimer): guard HERTZ_FORMAT get* against register==0
getCount(HERTZ_FORMAT) and getCaptureCompare(..., HERTZ_COMPARE_FORMAT)
both compute getTimerClkFreq() / (register * Prescalerfactor), dividing
directly by the live CNT (counter) or CCR (capture/compare) hardware
register with no zero-guard.
Both registers legitimately read 0 with no caller error involved:
- CNT is 0 right at/after every timer reset or overflow, simply from
the timer running (getOverflow()/setOverflow() already special-case
this exact situation via the ARR+1 "period" offset; getCount() and
getCaptureCompare() do not).
- CCR is 0 on reset before the first setCaptureCompare() call, and in
Input Capture mode holds whatever was last captured, which is 0
before any edge has been captured or if an edge lands exactly at
CNT == 0.
Hitting either case triggers an integer divide by zero: undefined
behavior in C/C++, verified to crash (SIGFPE-equivalent) on a desktop
toolchain. On Cortex-M without DIV_0_TRP configured (the default),
UDIV silently returns 0, so on-target this instead silently reports
"0 Hz" with no indication anything went wrong.
Add an explicit zero-check to both HERTZ_FORMAT branches, returning 0
(no meaningful frequency available) instead of dividing, matching the
existing "do not underflow" zero-guard style already used by
setOverflow() in this same file. No public API/signature changes.
Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>1 parent 59e2cdd commit 8a81c63
1 file changed
Lines changed: 10 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
746 | 746 | | |
747 | 747 | | |
748 | 748 | | |
749 | | - | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
750 | 753 | | |
751 | 754 | | |
752 | 755 | | |
| |||
1203 | 1206 | | |
1204 | 1207 | | |
1205 | 1208 | | |
1206 | | - | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
1207 | 1215 | | |
1208 | 1216 | | |
1209 | 1217 | | |
| |||
0 commit comments