]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/timer.c
Merge tag 'fatfs-0.10b'
[z180-stamp.git] / avr / timer.c
CommitLineData
d684c216
L
1/*
2 */
3
4
5#include "common.h"
6
7//#include <avr/power.h>
8//#include <avr/pgmspace.h>
9#include <avr/interrupt.h>
10#include <util/atomic.h>
11
12//#include <stdio.h>
13
14
15#include "timer.h"
16
17/* timer interrupt/overflow counter */
18static volatile uint32_t timestamp;
19
20
21/*---------------------------------------------------------*/
22/* 1000Hz timer interrupt generated by OC1A */
23/*---------------------------------------------------------*/
24
25ISR(TIMER1_COMPA_vect)
26{
27 static int_fast8_t tick_10ms;
28 int_fast8_t i;
29
30
31 timestamp++;
32
33 i = tick_10ms + 1;
34 if (i == 10) {
35 i = 0;
36 Stat |= S_10MS_TO;
37
38 /* Drive timer procedure of low level disk I/O module */
39 //disk_timerproc();
40 }
41 tick_10ms = i;
42
43}
44
45
46
47/*--------------------------------------------------------------------------*/
48
49#if 0
50
51void do_10ms(void)
52{
53 if (to_counter)
54 to_counter--;
55}
56
57#endif
58
59/*--------------------------------------------------------------------------*/
60
61
62void timer_setup(void)
63{
64
65 /* Clock */
66 CLKPR = _BV(CLKPCE);
67 CLKPR = 0;
68
69 /* Timer */
70
71 OCR1A = F_CPU / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
72 TCCR1B = 0b00001001;
73 TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
74}
75
76
77uint32_t get_timer(uint32_t base)
78{
79 uint32_t ret;
80
81 ATOMIC_BLOCK(ATOMIC_FORCEON)
82 {
83 ret = timestamp;
84 }
85 return ret - base;
86}
87