-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc.c
100 lines (89 loc) · 2.2 KB
/
rtc.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Copyright (C) 2013 Enrico Rossi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
/*!
\file rtc.c
\brief Counters handler and IRQ routine.
*/
#include <stdint.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "rtc.h"
/*!
* IRQ wakes up on the timer oveflow and increment the global
* micro seconds.
*/
ISR(TIMER0_COMPA_vect)
{
rtc_us++;
}
/*! Zeroing the global counter.
*/
void rtc_clear(void)
{
rtc_us=0;
}
/*!
* Setup timer/counter.
*
* We use the CTC (Clear Timer on Compare match) mode in which
* the counter will be compared to the OCR0A value and if they
* match an interrupt will be raised. The interrupt routine
* TIMER0_COMPA_vect will be called and the counter will
* restart from the BOTTOM.
*
* \bug The global interrupt enable should be something
* done by the main program, other routine my need to
* initialize IRQ routines before enable the global IRQ.
*/
void rtc_setup(void)
{
/* CTC counter mode */
TCCR0A = _BV(WGM01);
TCCR0B = 0;
/* Set the CTC MAX value. */
OCR0A = CTC_MAX;
/* Enable interrupt on timer compare match */
TIMSK0 = _BV(OCIE0A);
/* clear the rtc_us value. */
rtc_clear();
/* enable interrupt. */
sei();
}
/*!
* Setup prescaler and start counter.
*/
void rtc_start(void)
{
/* start counter prescaled by 8 */
TCCR0B = _BV(CS01);
}
/*!
* Stop the counter.
*
* Disable the interrupt and remove the link
* to the interrupt routine.
*
* \bug The interrupt can be required enable by
* other part of the program.
* It shouldn't be totally disabled here.
*/
void rtc_stop(void)
{
cli();
TCCR0B = 0;
}