-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.c
133 lines (111 loc) · 3.08 KB
/
timer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Timer functions
*
* Copyright 2013 Tuomas Kulve, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "timer.h"
#include "utils.h"
static volatile uint16_t timer_repeats = 0;
volatile uint8_t timer_occurred = 0;
/*
* Timeout, repeat timer_repeats times, then wake up from sleep
*/
__attribute__((interrupt(TIMER1_A0_VECTOR)))
void TIMER1_A0_ISR(void)
{
if (timer_repeats > 0) {
--timer_repeats;
return;
}
timer_clear();
#if SC_USE_SLEEP == 1
// Exit from lower power mode
// Clearing all LPM4 bits should be fine even if we are only in LPM0
__bic_status_register_on_exit(LPM4_bits);
#endif
timer_occurred = 1;
}
/*
* Set timer to raise an interrupt after ms milliseconds
* NOTE: this assumes timer_repeats has already been set!
*/
void timer_set(int ms)
{
if (ms > 16000) {
ms = 16000;
}
timer_occurred = 0;
TA1CCR0 = ms << 0; // ms milliseconds
TA1CTL = TASSEL_1 + MC_1 + ID_3; // ACLK/8, upmode
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
}
/*
* Set timer to raise an interrupt after ms milliseconds
*/
void timer_clear(void)
{
// Clear timer register, disable timer interrupts
timer_occurred = 0;
timer_repeats = 0;
TA1CCTL0 = 0; // CCR0 interrupt disabled
TA1CTL = TACLR;
}
/*
* Block in low power mode for ms milliseconds
*/
void timer_sleep_ms(uint16_t ms, uint32_t mode)
{
#if SC_USE_SLEEP == 1
timer_repeats = 0;
timer_set(ms);
__bis_status_register(mode + GIE);
#else
busysleep_ms(ms);
#endif
}
/*
* Block in low poewr mode for min minutes
*/
void timer_sleep_min(uint16_t min, uint32_t mode)
{
#if SC_USE_SLEEP == 1
// Repeat 10sec sleep 6*min times.
timer_repeats = 6 * min;
timer_set(10000); // sleep 10 sec timer_repeats times
__bis_status_register(mode + GIE);
#else
int i;
for (i = 0; i < 6 * min; ++i) {
busysleep_ms(10000);
}
#endif
}
/* Emacs indentatation information
Local Variables:
indent-tabs-mode:nil
tab-width:2
c-basic-offset:2
End:
*/