Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions OIL.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Status: `open`, `in progress`, `done`, `dropped` (with the reason in the item).
| 4 | Release 2.0.0 and the Library Manager | 3 | in progress | #7 |
| 5 | SelfCheck in simavr | 1 | open | |
| 6 | Only the AVRs whose Timer2 has OC2B on pin 3 | 3 | open | |
| 7 | A callback attached while stopped is called after Resume() | 3 | in progress | #8 |

### 1 On the template embedded-cpp-template

Expand Down Expand Up @@ -126,3 +127,11 @@ on 2026-11-11 while going through the README for 4:

Either the library refuses those chips with an `#error` that says why, or it hands each its pin;
which is to be decided. The README then names the boards it runs on.

### 7 A callback attached while stopped is called after Resume()

`AttachInterrupt()` enabled the overflow interrupt only while the timer ran, and `Resume()` does
not touch it: a callback attached while the timer was stopped was never called. Found on
2026-11-11 in AtmegaTimer, which took the code of 3 (its roadmap, A-10), and shown here by a
test first. The interrupt is enabled while the timer is stopped as well, where the counter stands
and comes to no bottom before `Resume()`.
8 changes: 6 additions & 2 deletions src/timer_two/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class Timer {

/*!
* \brief Sets the function the overflow interrupt calls, and enables the
* interrupt if the timer runs.
* interrupt if the timer runs or is stopped, so that Resume() goes on
* with it.
* \param callback the function
* \return Error::kNoCallback for nullptr
*/
Expand All @@ -267,7 +268,10 @@ class Timer {
// through half of the new one.
ClearBit(Registers::Timsk(), TOIE2);
callback_ = callback;
if (state_ == State::kRunning) {
// Stopped, the counter stands and comes to no bottom before Resume().
bool const has_counted{(state_ == State::kRunning) ||
(state_ == State::kStopped)};
if (has_counted) {
EnableOverflowInterrupt();
}
return Error::kOk;
Expand Down
12 changes: 12 additions & 0 deletions test/TimerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ TEST_F(TimerTest, AttachInterruptBeforeStartLeavesTheInterruptOff) {
EXPECT_EQ(timer().GetCallback(), &CountOverflow);
}

// OIL 7: the interrupt stayed off, and the callback was never called.
TEST_F(TimerTest, AttachInterruptWhileStoppedCallsTheCallbackAfterResume) {
ASSERT_EQ(timer().Begin(1000U), Error::kOk);
ASSERT_EQ(timer().Start(), Error::kOk);
ASSERT_EQ(timer().Stop(), Error::kOk);

ASSERT_EQ(timer().AttachInterrupt(CountOverflow), Error::kOk);
ASSERT_EQ(timer().Resume(), Error::kOk);

EXPECT_TRUE(BitIsSet(TIMSK2, TOIE2));
}

TEST_F(TimerTest, DetachInterruptDisablesTheInterruptAndForgetsTheCallback) {
ASSERT_EQ(timer().Begin(1000U, CountOverflow), Error::kOk);
ASSERT_EQ(timer().Start(), Error::kOk);
Expand Down
Loading