millis interrupt
parent
2062a604d8
commit
9232e55563
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,33 @@
|
|||
#pragma once
|
||||
#include <avr/io.h>
|
||||
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "timebase.h"
|
||||
#include "../hal/timers.h"
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue