-
Notifications
You must be signed in to change notification settings - Fork 94
/
timer.c
684 lines (582 loc) · 18.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// *************************************************************************************************
//
// Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// *************************************************************************************************
// Timer service routines.
// *************************************************************************************************
// *************************************************************************************************
// Include section
// system
#include "project.h"
// driver
#include "timer.h"
#include "ports.h"
#include "buzzer.h"
#include "vti_ps.h"
#ifdef FEATURE_PROVIDE_ACCEL
#include "vti_as.h"
#endif
#include "display.h"
// logic
#include "clock.h"
#include "battery.h"
#include "stopwatch.h"
#include "alarm.h"
#include "altitude.h"
#include "display.h"
#include "rfsimpliciti.h"
#include "simpliciti.h"
#ifdef FEATURE_PROVIDE_ACCEL
#include "acceleration.h"
#endif
//pfs
#ifndef ELIMINATE_BLUEROBIN
#include "bluerobin.h"
#endif
#include "temperature.h"
#ifdef CONFIG_EGGTIMER
#include "eggtimer.h"
#endif
#ifdef CONFIG_SIDEREAL
#include "sidereal.h"
#endif
#ifdef CONFIG_STRENGTH
#include "strength.h"
#endif
// *************************************************************************************************
// Prototypes section
void Timer0_Init(void);
void Timer0_Stop(void);
void Timer0_A1_Start(u16 ticks);
void Timer0_A1_Stop(void);
void Timer0_A3_Start(u16 ticks);
void Timer0_A3_Stop(void);
void Timer0_A4_Delay(u16 ticks);
void (*fptr_Timer0_A3_function)(void);
#ifdef CONFIG_USE_GPS
void (*fptr_Timer0_A1_function)(void);
#endif
// *************************************************************************************************
// Defines section
// *************************************************************************************************
// Global Variable section
struct timer sTimer;
// *************************************************************************************************
// Extern section
extern void BRRX_TimerTask_v(void);
extern void to_lpm(void);
// *************************************************************************************************
// @fn Timer0_Init
// @brief Set Timer0 to a period of 1 or 2 sec. IRQ TACCR0 is asserted when timer overflows.
// @param none
// @return none
// *************************************************************************************************
void Timer0_Init(void)
{
// Set interrupt frequency to 1Hz
TA0CCR0 = 32768 - 1;
// Enable timer interrupt
TA0CCTL0 |= CCIE;
// Clear and start timer now
// Continuous mode: Count to 0xFFFF and restart from 0 again - 1sec timing will be generated by ISR
TA0CTL |= TASSEL0 + MC1 + TACLR;
}
// *************************************************************************************************
// @fn Timer0_Start
// @brief Start Timer0.
// @param none
// @return none
// *************************************************************************************************
void Timer0_Start(void)
{
// Start Timer0 in continuous mode
TA0CTL |= MC_2;
}
// *************************************************************************************************
// @fn Timer0_Stop
// @brief Stop and reset Timer0.
// @param none
// @return none
// *************************************************************************************************
void Timer0_Stop(void)
{
// Stop Timer0
TA0CTL &= ~MC_2;
// Set Timer0 count register to 0x0000
TA0R = 0;
}
void Timer0_A1_Start(u16 ticks)
{
/*old version
// Set interrupt frequency to 1Hz
TA0CCR1 = TA0R + 32678 ;
// Enable timer interrupt
TA0CCTL1 |= CCIE; */
u16 value;
// Store timer ticks in global variable
sTimer.timer0_A1_ticks = ticks;
// Delay based on current counter value
value = TA0R + ticks;
// Update CCR
TA0CCR1 = value;
// Reset IRQ flag
TA0CCTL1 &= ~CCIFG;
// Enable timer interrupt
TA0CCTL1 |= CCIE;
}
void Timer0_A1_Stop(void)
{
// Clear timer interrupt
TA0CCTL1 &= ~CCIE;
}
// *************************************************************************************************
// @fn Timer0_A3_Start
// @brief Trigger IRQ every "ticks" microseconds
// @param ticks (1 tick = 1/32768 sec)
// @return none
// *************************************************************************************************
void Timer0_A3_Start(u16 ticks)
{
u16 value;
// Store timer ticks in global variable
sTimer.timer0_A3_ticks = ticks;
// Delay based on current counter value
value = TA0R + ticks;
// Update CCR
TA0CCR3 = value;
// Reset IRQ flag
TA0CCTL3 &= ~CCIFG;
// Enable timer interrupt
TA0CCTL3 |= CCIE;
}
// *************************************************************************************************
// @fn Timer0_A3_Stop
// @brief Stop Timer0_A3.
// @param none
// @return none
// *************************************************************************************************
void Timer0_A3_Stop(void)
{
// Clear timer interrupt
TA0CCTL3 &= ~CCIE;
}
// *************************************************************************************************
// @fn Timer0_A4_Delay
// @brief Wait for some microseconds
// @param ticks (1 tick = 1/32768 sec)
// @return none
// *************************************************************************************************
void Timer0_A4_Delay(u16 ticks)
{
u16 value;
// Exit immediately if Timer0 not running - otherwise we'll get stuck here
if ((TA0CTL & (BIT4 | BIT5)) == 0) return;
// Disable timer interrupt
TA0CCTL4 &= ~CCIE;
// Clear delay_over flag
sys.flag.delay_over = 0;
// Add delay to current timer value
value = TA0R + ticks;
// Update CCR
TA0CCR4 = value;
// Reset IRQ flag
TA0CCTL4 &= ~CCIFG;
// Enable timer interrupt
TA0CCTL4 |= CCIE;
// Wait for timer IRQ
while (1)
{
// Delay in LPM
to_lpm();
#ifdef USE_WATCHDOG
// Service watchdog
WDTCTL = WDTPW + WDTIS__512K + WDTSSEL__ACLK + WDTCNTCL;
#endif
#ifdef CONFIG_STOP_WATCH
// Redraw stopwatch display
if (is_stopwatch_run()) display_stopwatch(LINE2, DISPLAY_LINE_UPDATE_PARTIAL);
#endif
// Check stop condition
if (sys.flag.delay_over) break;
}
}
// *************************************************************************************************
// @fn TIMER0_A0_ISR
// @brief IRQ handler for TIMER0_A0 IRQ
// Timer0_A0 1/1sec clock tick (serviced by function TIMER0_A0_ISR)
// Timer0_A1 (serviced by function TIMER0_A1_5_ISR)
// Timer0_A2 1/100 sec Stopwatch (serviced by function TIMER0_A1_5_ISR)
// Timer0_A3 Configurable periodic IRQ (serviced by function TIMER0_A1_5_ISR)
// Timer0_A4 One-time delay (serviced by function TIMER0_A1_5_ISR)
// @param none
// @return none
// *************************************************************************************************
//pfs
#ifdef __GNUC__
#include <signal.h>
interrupt (TIMER0_A0_VECTOR) TIMER0_A0_ISR(void)
#else
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
#endif
{
static u8 button_lock_counter = 0;
static u8 button_beep_counter = 0;
// Disable IE
TA0CCTL0 &= ~CCIE;
// Reset IRQ flag
TA0CCTL0 &= ~CCIFG;
// Add 1 sec to TACCR0 register (IRQ will be asserted at 0x7FFF and 0xFFFF = 1 sec intervals)
TA0CCR0 += 32768;
// Enable IE
TA0CCTL0 |= CCIE;
// Add 1 second to global time
clock_tick();
// Set clock update flag
display.flag.update_time = 1;
// While SimpliciTI stack operates or BlueRobin searches, freeze system state
//pfs
#ifdef ELIMINATE_BLUEROBIN
if (is_rf())
#else
if (is_rf() || is_bluerobin_searching())
#endif
{
// SimpliciTI automatic timeout
if (sRFsmpl.timeout == 0)
{
simpliciti_flag |= SIMPLICITI_TRIGGER_STOP;
}
else
{
sRFsmpl.timeout--;
}
// Exit from LPM3 on RETI
_BIC_SR_IRQ(LPM3_bits);
return;
}
// -------------------------------------------------------------------
// Service modules that require 1/min processing
if (sTime.drawFlag >= 2)
{
#ifdef CONFIG_BATTERY
// Measure battery voltage to keep track of remaining battery life
request.flag.voltage_measurement = 1;
#endif
#ifdef CONFIG_ALARM
// Check if alarm needs to be turned on
check_alarm();
#endif
}
// -------------------------------------------------------------------
// Service active modules that require 1/s processing
#ifdef CONFIG_ALARM // N8VI NOTE eventually, eggtimer should use this code too
// Generate alarm signal
if (sAlarm.state == ALARM_ON)
{
// Decrement alarm duration counter
if (sAlarm.duration-- > 0)
{
request.flag.buzzer = 1;
}
else
{
sAlarm.duration = ALARM_ON_DURATION;
stop_alarm();
}
}
#endif
#ifdef CONFIG_STRENGTH
// One more second gone by.
if(is_strength())
{
strength_tick();
}
#endif
// Do a temperature measurement each second while menu item is active
if (is_temp_measurement()) request.flag.temperature_measurement = 1;
// Do a pressure measurement each second while menu item is active
#ifdef CONFIG_ALTITUDE
if (is_altitude_measurement())
{
// Countdown altitude measurement timeout while menu item is active
sAlt.timeout--;
// Stop measurement when timeout has elapsed
if (sAlt.timeout == 0)
{
stop_altitude_measurement();
// Show ---- m/ft
display_chars(LCD_SEG_L1_3_0, (u8*)"----", SEG_ON);
// Clear up/down arrow
display_symbol(LCD_SYMB_ARROW_UP, SEG_OFF);
display_symbol(LCD_SYMB_ARROW_DOWN, SEG_OFF);
}
// In case we missed the IRQ due to debouncing, get data now
if ((PS_INT_IN & PS_INT_PIN) == PS_INT_PIN) request.flag.altitude_measurement = 1;
}
#endif
#ifdef FEATURE_PROVIDE_ACCEL
// Count down timeout
if (is_acceleration_measurement())
{
// Countdown acceleration measurement timeout
sAccel.timeout--;
// Stop measurement when timeout has elapsed
if (sAccel.timeout == 0) as_stop();
// If DRDY is (still) high, request data again
if ((AS_INT_IN & AS_INT_PIN) == AS_INT_PIN) request.flag.acceleration_measurement = 1;
}
#endif
//pfs
#ifndef ELIMINATE_BLUEROBIN
// If BlueRobin transmitter is connected, get data from API
if (is_bluerobin()) get_bluerobin_data();
#endif
#ifdef CONFIG_BATTERY
// If battery is low, decrement display counter
if (sys.flag.low_battery)
{
if (sBatt.lobatt_display-- == 0)
{
message.flag.prepare = 1;
message.flag.type_lobatt = 1;
sBatt.lobatt_display = BATTERY_LOW_MESSAGE_CYCLE;
}
}
#endif
// If a message has to be displayed, set display flag
if (message.all_flags)
{
if (message.flag.prepare)
{
message.flag.prepare = 0;
message.flag.show = 1;
}
else if (message.flag.erase) // message cycle is over, so erase it
{
message.flag.erase = 0;
message.flag.block_line1 = 0;
message.flag.block_line2 = 0;
display.flag.full_update = 1;
}
}
// -------------------------------------------------------------------
// Check idle timeout, set timeout flag
if (sys.flag.idle_timeout_enabled)
{
if (sTime.system_time - sTime.last_activity > INACTIVITY_TIME) sys.flag.idle_timeout = 1; //setFlag(sysFlag_g, SYS_TIMEOUT_IDLE);
}
// -------------------------------------------------------------------
// Detect continuous button high states
if (BUTTON_STAR_IS_PRESSED && BUTTON_UP_IS_PRESSED)
{
if (button_beep_counter++ > LEFT_BUTTON_LONG_TIME)
{
// Toggle no_beep buttons flag
sys.flag.no_beep = ~sys.flag.no_beep;
// Show "beep / nobeep" message synchronously with next second tick
message.flag.prepare = 1;
if (sys.flag.no_beep) message.flag.type_no_beep_on = 1;
else message.flag.type_no_beep_off = 1;
// Reset button beep counter
button_beep_counter = 0;
}
} else if (BUTTON_NUM_IS_PRESSED && BUTTON_DOWN_IS_PRESSED) // Trying to lock/unlock buttons?
{
if (button_lock_counter++ > LEFT_BUTTON_LONG_TIME)
{
// Toggle lock / unlock buttons flag
sys.flag.lock_buttons = ~sys.flag.lock_buttons;
// Show "buttons are locked/unlocked" message synchronously with next second tick
message.flag.prepare = 1;
if (sys.flag.lock_buttons) message.flag.type_locked = 1;
else message.flag.type_unlocked = 1;
// Reset button lock counter
button_lock_counter = 0;
}
}
else // Trying to create a long button press?
{
// Reset button lock counter
button_lock_counter = 0;
if (BUTTON_STAR_IS_PRESSED)
{
sButton.star_timeout++;
// Check if button was held low for some seconds
if (sButton.star_timeout > LEFT_BUTTON_LONG_TIME)
{
button.flag.star_long = 1;
sButton.star_timeout = 0;
}
}
else
{
sButton.star_timeout = 0;
}
if (BUTTON_NUM_IS_PRESSED)
{
sButton.num_timeout++;
// Check if button was held low for some seconds
if (sButton.num_timeout > LEFT_BUTTON_LONG_TIME)
{
button.flag.num_long = 1;
sButton.num_timeout = 0;
}
}
else
{
sButton.num_timeout = 0;
}
}
// Exit from LPM3 on RETI
_BIC_SR_IRQ(LPM3_bits);
}
// *************************************************************************************************
// @fn Timer0_A1_5_ISR
// @brief IRQ handler for timer IRQ.
// Timer0_A0 1/1sec clock tick (serviced by function TIMER0_A0_ISR)
// Timer0_A1 BlueRobin timer / doorlock
// Timer0_A2 1/100 sec Stopwatch
// Timer0_A3 Configurable periodic IRQ (used by button_repeat and buzzer)
// Timer0_A4 One-time delay
// @param none
// @return none
// *************************************************************************************************
//pfs
#ifdef __GNUC__
#include <signal.h>
interrupt (TIMER0_A1_VECTOR) TIMER0_A1_5_ISR(void)
#else
#pragma vector = TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_5_ISR(void)
#endif
{
u16 value;
switch (TA0IV)
{
//pfs
#ifndef ELIMINATE_BLUEROBIN
// Timer0_A1 BlueRobin timer
case 0x02: // Timer0_A1 handler
BRRX_TimerTask_v();
break;
#endif
#ifdef CONFIG_SIDEREAL
// Timer0_A1 Used for sidereal time until CCR0 becomes free
case 0x02: // Timer0_A1 handler
// Disable IE
TA0CCTL1 &= ~CCIE;
// Reset IRQ flag
TA0CCTL1 &= ~CCIFG;
//for sidereal time we need 32768/1.00273790935=32678.529149 clock cycles for one second
//32678.5 clock cycles gives a deviation of ~0.9e-7~0.1s/day which is likely less than the ozillator deviation
if(sSidereal_time.second & 1)
{
TA0CCR1+=32678;
}
else
{
TA0CCR1+=32679;
}
// Enable IE
TA0CCTL1 |= CCIE;
// Add 1 second to global time
sidereal_clock_tick();
// Set clock update flag
display.flag.update_sidereal_time = 1;
break;
#endif
#ifdef CONFIG_USE_GPS
case 0x02: // Disable IE
TA0CCTL1 &= ~CCIE;
// Reset IRQ flag
TA0CCTL1 &= ~CCIFG;
// Store new value in CCR
value = TA0R + sTimer.timer0_A1_ticks; //timer0_A1_ticks_g;
// Load CCR register with next capture point
TA0CCR1 = value;
// Enable timer interrupt
TA0CCTL1 |= CCIE;
// Call function handler
fptr_Timer0_A1_function();
break;
#endif
// Timer0_A2 1/1 or 1/100 sec Stopwatch
case 0x04: // Timer0_A2 handler
// Disable IE
TA0CCTL2 &= ~CCIE;
// Reset IRQ flag
TA0CCTL2 &= ~CCIFG;
// Load CCR register with next capture point
#ifdef CONFIG_STOP_WATCH
update_stopwatch_timer();
#endif
#ifdef CONFIG_EGGTIMER
update_eggtimer_timer();
#endif
// Enable timer interrupt
TA0CCTL2 |= CCIE;
// Increase stopwatch counter
#ifdef CONFIG_STOP_WATCH
stopwatch_tick();
#endif
#ifdef CONFIG_EGGTIMER
eggtimer_tick();
#endif
break;
// Timer0_A3 Configurable periodic IRQ (used by button_repeat and buzzer)
case 0x06: // Disable IE
TA0CCTL3 &= ~CCIE;
// Reset IRQ flag
TA0CCTL3 &= ~CCIFG;
// Store new value in CCR
value = TA0R + sTimer.timer0_A3_ticks; //timer0_A3_ticks_g;
// Load CCR register with next capture point
TA0CCR3 = value;
// Enable timer interrupt
TA0CCTL3 |= CCIE;
// Call function handler
fptr_Timer0_A3_function();
break;
// Timer0_A4 One-time delay
case 0x08: // Disable IE
TA0CCTL4 &= ~CCIE;
// Reset IRQ flag
TA0CCTL4 &= ~CCIFG;
// Set delay over flag
sys.flag.delay_over = 1;
break;
}
// Exit from LPM3 on RETI
_BIC_SR_IRQ(LPM3_bits);
}