millis interrupt

pull/77/head
Alex Voinea 2021-08-04 11:06:00 +03:00 committed by DRracer
parent 2062a604d8
commit 9232e55563
6 changed files with 58 additions and 4 deletions

View File

@ -4,6 +4,7 @@ target_sources(
avr/usart.cpp avr/usart.cpp
avr/shr16.cpp avr/shr16.cpp
avr/eeprom.cpp avr/eeprom.cpp
avr/timers.cpp
avr/tmc2130.cpp avr/tmc2130.cpp
adc.cpp adc.cpp
avr/spi.cpp avr/spi.cpp

17
src/hal/avr/timers.cpp Normal file
View File

@ -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
}
}
}

View File

@ -1,13 +1,33 @@
#pragma once #pragma once
#include <avr/io.h>
#define TIMER0 ((hal::timers::Tim8bit_TypeDef *)&TCCR0A)
namespace hal { namespace hal {
/// Hardware Abstraction Layer for the CPU's internal timers /// Hardware Abstraction Layer for the CPU's internal timers
namespace timers { namespace timers {
void ConfigureTimer(uint8_t timer /* some config struct */); // void ConfigureTimer(uint8_t timer /* some config struct */);
void StartTimer(uint8_t timer); // void StartTimer(uint8_t timer);
void StopTimer(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 cpu
} // namespace hal } // namespace hal

View File

@ -18,6 +18,7 @@
#include "modules/protocol.h" #include "modules/protocol.h"
#include "modules/selector.h" #include "modules/selector.h"
#include "modules/user_input.h" #include "modules/user_input.h"
#include "modules/timebase.h"
#include "logic/command_base.h" #include "logic/command_base.h"
#include "logic/cut_filament.h" #include "logic/cut_filament.h"
@ -38,6 +39,7 @@ namespace ml = modules::leds;
namespace ms = modules::selector; namespace ms = modules::selector;
namespace mg = modules::globals; namespace mg = modules::globals;
namespace mu = modules::user_input; namespace mu = modules::user_input;
namespace mt = modules::time;
namespace hu = hal::usart; namespace hu = hal::usart;
@ -101,6 +103,8 @@ void setup() {
cpu::Init(); cpu::Init();
mt::timebase.Init();
mg::globals.Init(); mg::globals.Init();
// watchdog init // watchdog init

View File

@ -1,5 +1,6 @@
#include "timebase.h" #include "timebase.h"
#include "../hal/timers.h" #include "../hal/timers.h"
#include <avr/interrupt.h>
namespace modules { namespace modules {
namespace time { namespace time {
@ -7,9 +8,15 @@ namespace time {
Timebase timebase; Timebase timebase;
void Timebase::Init() { 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() { void Timebase::Isr() {
ms++;
} }
uint16_t Timebase::Millis() const { uint16_t Timebase::Millis() const {
@ -18,3 +25,7 @@ uint16_t Timebase::Millis() const {
} // namespace time } // namespace time
} // namespace modules } // namespace modules
ISR(TIMER0_COMPA_vect) {
modules::time::timebase.Isr();
}

View File

@ -21,9 +21,10 @@ public:
/// (usually the start of the firmware) /// (usually the start of the firmware)
uint16_t Millis() const; uint16_t Millis() const;
void Isr();
private: private:
uint16_t ms; uint16_t ms;
static void Isr();
}; };
/// The one and only instance of Selector in the FW /// The one and only instance of Selector in the FW