diff --git a/fantemp/clock.cpp b/fantemp/clock.cpp new file mode 100644 index 0000000..390a0d0 --- /dev/null +++ b/fantemp/clock.cpp @@ -0,0 +1,37 @@ +#include "clock.hpp" + +#include +#include + +namespace clk { + +namespace detail { + +volatile uint64_t sm_millisCounter = 0; + +ISR(TIMER2_COMPA_vect) +{ + ++sm_millisCounter; +} + +} // namespace detail + +void init() +{ + TCCR2A |= (1 << WGM21); + TCCR2B |= (1 << CS22) | (1 << CS20); + OCR2A = 124; + TIMSK2 |= (1 << OCIE2A); +} + +uint64_t millis() +{ + const auto oldSreg = SREG; + cli(); + const auto millisCounter = detail::sm_millisCounter; + SREG = oldSreg; + + return millisCounter; +} + +} // namespace clk diff --git a/fantemp/clock.hpp b/fantemp/clock.hpp index 94845e9..fe832be 100644 --- a/fantemp/clock.hpp +++ b/fantemp/clock.hpp @@ -2,3 +2,12 @@ #define F_CPU 16000000 #include + +#include + +namespace clk { + +void init(); +uint64_t millis(); + +} // namespace clk diff --git a/fantemp/fantemp.cppproj b/fantemp/fantemp.cppproj index 7124ef1..e4e1237 100644 --- a/fantemp/fantemp.cppproj +++ b/fantemp/fantemp.cppproj @@ -68,6 +68,19 @@ J41800099437 Atmel-ICE + + + + 125000 + + + + + custom + + + Custom Programming Tool + @@ -206,6 +219,9 @@ compile + + compile + compile diff --git a/fantemp/main.cpp b/fantemp/main.cpp index 43749c8..64149fa 100644 --- a/fantemp/main.cpp +++ b/fantemp/main.cpp @@ -20,6 +20,8 @@ int main() Controller controller; controller.init(); + clk::init(); + while (true) { controller.callback(); terminal.callback(); diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index 2b6c796..e60bedd 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -8,6 +8,7 @@ #include "flash/flash.hpp" +#include "clock.hpp" #include "controller.hpp" namespace detail { @@ -97,9 +98,9 @@ class Terminal { parseInput(); } - if (m_state == State::MONITOR && --m_monitorDelayCounter == 0) { + if (m_state == State::MONITOR && clk::millis() >= m_monitorDelayLastUpdate + MONITOR_DELAY) { showState(); - m_monitorDelayCounter = MONITOR_DELAY; + m_monitorDelayLastUpdate = clk::millis(); } } @@ -107,7 +108,7 @@ class Terminal { static constexpr auto INPUT_BUFFER_SIZE = 128; static constexpr auto BACKSPACE = uint8_t{0x7f}; static constexpr auto CTRL_C = uint8_t{0x03}; - static constexpr auto MONITOR_DELAY = 60000; + static constexpr auto MONITOR_DELAY = 500; enum class State { NONE, @@ -118,7 +119,7 @@ class Terminal { static char m_inputBuffer[INPUT_BUFFER_SIZE]; static uint16_t m_inputSize; static State m_state; - static uint16_t m_monitorDelayCounter; + static uint64_t m_monitorDelayLastUpdate; static void parseInput() { @@ -134,7 +135,6 @@ class Terminal { printCurve(); } else if (substringEquals(m_inputBuffer, detail::MONITOR_CMD, m_inputSize)) { m_state = State::MONITOR; - m_monitorDelayCounter = MONITOR_DELAY; } else if (substringEquals(m_inputBuffer, detail::BOOTLOADER_CMD, m_inputSize)) { handleBootloader(); } else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) { @@ -227,4 +227,4 @@ template typename Terminal::State Terminal::m_state = State::NONE; template -uint16_t Terminal::m_monitorDelayCounter; +uint64_t Terminal::m_monitorDelayLastUpdate = 0;