From 9232e55563c0dd7394d6691725a6fad8d4bd85b1 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Wed, 4 Aug 2021 11:06:00 +0300 Subject: [PATCH] millis interrupt --- src/hal/CMakeLists.txt | 1 + src/hal/avr/timers.cpp | 17 +++++++++++++++++ src/hal/timers.h | 26 +++++++++++++++++++++++--- src/main.cpp | 4 ++++ src/modules/timebase.cpp | 11 +++++++++++ src/modules/timebase.h | 3 ++- 6 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/hal/avr/timers.cpp diff --git a/src/hal/CMakeLists.txt b/src/hal/CMakeLists.txt index 169c0a8..04c4602 100644 --- a/src/hal/CMakeLists.txt +++ b/src/hal/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources( avr/usart.cpp avr/shr16.cpp avr/eeprom.cpp + avr/timers.cpp avr/tmc2130.cpp adc.cpp avr/spi.cpp diff --git a/src/hal/avr/timers.cpp b/src/hal/avr/timers.cpp new file mode 100644 index 0000000..495e217 --- /dev/null +++ b/src/hal/avr/timers.cpp @@ -0,0 +1,17 @@ +#include "../timers.h" + +namespace hal { +namespace timers { + +void Configure_CTC8(const uint8_t timer, Tim8bit_TypeDef *const htim8, Tim8bit_CTC_config *const conf) { + TIMSK[timer] = 0; //clear all interrupt sources + htim8->TCCRxA = (1 << 1); //WGM=2 (CTC) + htim8->TCCRxB = conf->cs; //CS = bits from conf (<<0) + htim8->OCRxA = conf->ocra; //set the TOP value from config + htim8->TCNTx = 0; //initialize timer to 0 + TIFR[timer] = 0xFF; //clear all interrupt flags + TIMSK[timer] = (1 << 1); //enable OCIExA +} + +} +} diff --git a/src/hal/timers.h b/src/hal/timers.h index 6a39a55..b9ebe5b 100644 --- a/src/hal/timers.h +++ b/src/hal/timers.h @@ -1,13 +1,33 @@ #pragma once +#include +#define TIMER0 ((hal::timers::Tim8bit_TypeDef *)&TCCR0A) namespace hal { /// Hardware Abstraction Layer for the CPU's internal timers namespace timers { -void ConfigureTimer(uint8_t timer /* some config struct */); -void StartTimer(uint8_t timer); -void StopTimer(uint8_t timer); +// void ConfigureTimer(uint8_t timer /* some config struct */); +// void StartTimer(uint8_t timer); +// void StopTimer(uint8_t timer); + +struct Tim8bit_TypeDef { + volatile uint8_t TCCRxA; + volatile uint8_t TCCRxB; + volatile uint8_t TCNTx; + volatile uint8_t OCRxA; + volatile uint8_t OCRxB; +}; + +constexpr volatile uint8_t *TIFR = &TIFR0; +constexpr volatile uint8_t *TIMSK = &TIMSK0; + +struct Tim8bit_CTC_config { + uint8_t cs : 3; ///clock source as per datasheet. It is not consistent between timer types + uint8_t ocra; ///compare value for TOP +}; + +void Configure_CTC8(const uint8_t timer, Tim8bit_TypeDef *const htim8, Tim8bit_CTC_config *const conf); } // namespace cpu } // namespace hal diff --git a/src/main.cpp b/src/main.cpp index aa8e2eb..c233c7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "modules/protocol.h" #include "modules/selector.h" #include "modules/user_input.h" +#include "modules/timebase.h" #include "logic/command_base.h" #include "logic/cut_filament.h" @@ -38,6 +39,7 @@ namespace ml = modules::leds; namespace ms = modules::selector; namespace mg = modules::globals; namespace mu = modules::user_input; +namespace mt = modules::time; namespace hu = hal::usart; @@ -101,6 +103,8 @@ void setup() { cpu::Init(); + mt::timebase.Init(); + mg::globals.Init(); // watchdog init diff --git a/src/modules/timebase.cpp b/src/modules/timebase.cpp index 3db6852..edf2a27 100644 --- a/src/modules/timebase.cpp +++ b/src/modules/timebase.cpp @@ -1,5 +1,6 @@ #include "timebase.h" #include "../hal/timers.h" +#include namespace modules { namespace time { @@ -7,9 +8,15 @@ namespace time { Timebase timebase; void Timebase::Init() { + hal::timers::Tim8bit_CTC_config tim8ctc_conf = { + .cs = 3, // ck/64 + .ocra = 250 - 1, + }; + hal::timers::Configure_CTC8(0, TIMER0, &tim8ctc_conf); } void Timebase::Isr() { + ms++; } uint16_t Timebase::Millis() const { @@ -18,3 +25,7 @@ uint16_t Timebase::Millis() const { } // namespace time } // namespace modules + +ISR(TIMER0_COMPA_vect) { + modules::time::timebase.Isr(); +} diff --git a/src/modules/timebase.h b/src/modules/timebase.h index 0241e42..61b6790 100644 --- a/src/modules/timebase.h +++ b/src/modules/timebase.h @@ -21,9 +21,10 @@ public: /// (usually the start of the firmware) uint16_t Millis() const; + void Isr(); + private: uint16_t ms; - static void Isr(); }; /// The one and only instance of Selector in the FW