Add millis timer for timekeeping
This commit is contained in:
parent
e29ee8c11f
commit
9ef4d2a737
37
fantemp/clock.cpp
Normal file
37
fantemp/clock.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include "clock.hpp"
|
||||||
|
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
|
||||||
|
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
|
@ -2,3 +2,12 @@
|
|||||||
|
|
||||||
#define F_CPU 16000000
|
#define F_CPU 16000000
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace clk {
|
||||||
|
|
||||||
|
void init();
|
||||||
|
uint64_t millis();
|
||||||
|
|
||||||
|
} // namespace clk
|
||||||
|
@ -68,6 +68,19 @@
|
|||||||
<ToolNumber>J41800099437</ToolNumber>
|
<ToolNumber>J41800099437</ToolNumber>
|
||||||
<ToolName>Atmel-ICE</ToolName>
|
<ToolName>Atmel-ICE</ToolName>
|
||||||
</com_atmel_avrdbg_tool_atmelice>
|
</com_atmel_avrdbg_tool_atmelice>
|
||||||
|
<custom>
|
||||||
|
<ToolOptions>
|
||||||
|
<InterfaceProperties>
|
||||||
|
<IspClock>125000</IspClock>
|
||||||
|
</InterfaceProperties>
|
||||||
|
<InterfaceName>
|
||||||
|
</InterfaceName>
|
||||||
|
</ToolOptions>
|
||||||
|
<ToolType>custom</ToolType>
|
||||||
|
<ToolNumber>
|
||||||
|
</ToolNumber>
|
||||||
|
<ToolName>Custom Programming Tool</ToolName>
|
||||||
|
</custom>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<ToolchainSettings>
|
<ToolchainSettings>
|
||||||
@ -206,6 +219,9 @@
|
|||||||
<Compile Include="bootloader.hpp">
|
<Compile Include="bootloader.hpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="clock.cpp">
|
||||||
|
<SubType>compile</SubType>
|
||||||
|
</Compile>
|
||||||
<Compile Include="clock.hpp">
|
<Compile Include="clock.hpp">
|
||||||
<SubType>compile</SubType>
|
<SubType>compile</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
@ -20,6 +20,8 @@ int main()
|
|||||||
Controller controller;
|
Controller controller;
|
||||||
controller.init();
|
controller.init();
|
||||||
|
|
||||||
|
clk::init();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
controller.callback();
|
controller.callback();
|
||||||
terminal.callback();
|
terminal.callback();
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "flash/flash.hpp"
|
#include "flash/flash.hpp"
|
||||||
|
|
||||||
|
#include "clock.hpp"
|
||||||
#include "controller.hpp"
|
#include "controller.hpp"
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@ -97,9 +98,9 @@ class Terminal {
|
|||||||
parseInput();
|
parseInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_state == State::MONITOR && --m_monitorDelayCounter == 0) {
|
if (m_state == State::MONITOR && clk::millis() >= m_monitorDelayLastUpdate + MONITOR_DELAY) {
|
||||||
showState();
|
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 INPUT_BUFFER_SIZE = 128;
|
||||||
static constexpr auto BACKSPACE = uint8_t{0x7f};
|
static constexpr auto BACKSPACE = uint8_t{0x7f};
|
||||||
static constexpr auto CTRL_C = uint8_t{0x03};
|
static constexpr auto CTRL_C = uint8_t{0x03};
|
||||||
static constexpr auto MONITOR_DELAY = 60000;
|
static constexpr auto MONITOR_DELAY = 500;
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
NONE,
|
NONE,
|
||||||
@ -118,7 +119,7 @@ class Terminal {
|
|||||||
static char m_inputBuffer[INPUT_BUFFER_SIZE];
|
static char m_inputBuffer[INPUT_BUFFER_SIZE];
|
||||||
static uint16_t m_inputSize;
|
static uint16_t m_inputSize;
|
||||||
static State m_state;
|
static State m_state;
|
||||||
static uint16_t m_monitorDelayCounter;
|
static uint64_t m_monitorDelayLastUpdate;
|
||||||
|
|
||||||
static void parseInput()
|
static void parseInput()
|
||||||
{
|
{
|
||||||
@ -134,7 +135,6 @@ class Terminal {
|
|||||||
printCurve();
|
printCurve();
|
||||||
} else if (substringEquals(m_inputBuffer, detail::MONITOR_CMD, m_inputSize)) {
|
} else if (substringEquals(m_inputBuffer, detail::MONITOR_CMD, m_inputSize)) {
|
||||||
m_state = State::MONITOR;
|
m_state = State::MONITOR;
|
||||||
m_monitorDelayCounter = MONITOR_DELAY;
|
|
||||||
} else if (substringEquals(m_inputBuffer, detail::BOOTLOADER_CMD, m_inputSize)) {
|
} else if (substringEquals(m_inputBuffer, detail::BOOTLOADER_CMD, m_inputSize)) {
|
||||||
handleBootloader();
|
handleBootloader();
|
||||||
} else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
|
} else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
|
||||||
@ -227,4 +227,4 @@ template <class Uart>
|
|||||||
typename Terminal<Uart>::State Terminal<Uart>::m_state = State::NONE;
|
typename Terminal<Uart>::State Terminal<Uart>::m_state = State::NONE;
|
||||||
|
|
||||||
template <class Uart>
|
template <class Uart>
|
||||||
uint16_t Terminal<Uart>::m_monitorDelayCounter;
|
uint64_t Terminal<Uart>::m_monitorDelayLastUpdate = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user