aboutsummaryrefslogtreecommitdiff
path: root/kernel/time/mcu/atmega2560/timer.c
blob: 663df0261bd5c12177ce1357afdc6e4e3bc374fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <avr/interrupt.h>
#include "sched/sched.h"
#include "time/timer.h"

void timer_init() {
  cli();
  TCCR3A = 0;
  TCCR3B = 0;
  TCCR3C = 0;

  TCCR3B = (1 << WGM32); // turn on CTC mode:
  TCCR3B |= (1 << CS32) | (0 << CS31) | (1 << CS30); // set to 1024 prescaler
  OCR3A = 770;
  sei();
}

void timer_start() {
  TIMSK3 |= (1 << OCIE3A);
}

void timer_stop() {
  TIMSK3 &= ~(1 << OCIE3A);
}

ISR(TIMER3_COMPA_vect, ISR_NAKED) {
  SAVE_CONTEXT(); 
  sched_tick();
  sei();
  RESTORE_CONTEXT();
  asm volatile ("reti");
}