diff --git a/OIL.md b/OIL.md index 1352b7e..3ba3b36 100644 --- a/OIL.md +++ b/OIL.md @@ -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 @@ -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()`. diff --git a/src/timer_two/timer.h b/src/timer_two/timer.h index 74ecfe3..437297a 100644 --- a/src/timer_two/timer.h +++ b/src/timer_two/timer.h @@ -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 */ @@ -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; diff --git a/test/TimerTest.cpp b/test/TimerTest.cpp index 61b93f6..2e637c8 100644 --- a/test/TimerTest.cpp +++ b/test/TimerTest.cpp @@ -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);