]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/timer.c
Merge branch 'chan-fatfs' into fatfs-integration
[z180-stamp.git] / avr / timer.c
CommitLineData
d684c216 1/*
35edb766
L
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
57307890 4 * SPDX-License-Identifier: GPL-2.0
d684c216
L
5 */
6
57307890 7#include "timer.h"
d684c216
L
8#include <avr/interrupt.h>
9#include <util/atomic.h>
be5cfb4b 10#include "time.h"
d684c216
L
11
12/* timer interrupt/overflow counter */
289c3252
L
13/* counts up every ms. */
14static volatile
15uint32_t timestamp;
d684c216 16
35edb766 17/*
57307890 18 * 1000Hz timer interrupt generated by OC4A
35edb766 19 */
57307890 20ISR(TIMER4_COMPA_vect)
d684c216
L
21{
22 static int_fast8_t tick_10ms;
be5cfb4b
L
23 static int_fast8_t tick_1s;
24 int_fast8_t i, j;
d684c216 25
7f552300 26 extern void disk_timerproc(void);
d684c216 27
57307890
L
28 OCR4A += F_CPU / 1000; /* 1000Hz interval */
29
d684c216
L
30 timestamp++;
31
32 i = tick_10ms + 1;
33 if (i == 10) {
d684c216 34 Stat |= S_10MS_TO;
68e463ad 35
d684c216 36 /* Drive timer procedure of low level disk I/O module */
7f552300 37 disk_timerproc();
be5cfb4b
L
38
39 j = tick_1s - 1;
40 if (j == 0) {
41 system_tick();
42 j = 100;
43 }
44 tick_1s = j;
45 i = 0;
d684c216
L
46 }
47 tick_10ms = i;
d684c216
L
48}
49
50
289c3252
L
51/*--------------------------------------------------------------------------*/
52
d684c216
L
53uint32_t get_timer(uint32_t base)
54{
55 uint32_t ret;
d684c216
L
56 ATOMIC_BLOCK(ATOMIC_FORCEON)
57 {
58 ret = timestamp;
59 }
60 return ret - base;
61}