px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_async_tmr.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2014 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_async_tmr.h : Asynchronous TMR connected to 32 kHz crystal
14 Author(s): Pieter Conradie
15 Creation Date: 2014-06-23
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <avr/io.h>
21#include <avr/interrupt.h>
22
23/* _____PROJECT INCLUDES_____________________________________________________ */
24#include "px_async_tmr.h"
25#include "px_board.h"
26
27/* _____LOCAL DEFINITIONS____________________________________________________ */
28// Check if TMR0 must be used
29#ifdef AS0
30#define ASYNC_TMR 0
31#define ASYNC_TMR_INT_DISABLE() PX_BIT_SET_LO(TIMSK0, OCIE0)
32#define ASYNC_TMR_INT_ENABLE() PX_BIT_SET_HI(TIMSK0, OCIE0)
33#define ASYNC_TMR_INT_IS_ENABLED() PX_BIT_IS_HI(TIMSK0, OCIE0)
34#define ASYNC_TMR_INT TIMER0_COMP_vect
35#endif
36
37// Check if TMR2 must be used
38#ifdef AS2
39#define ASYNC_TMR 2
40#define ASYNC_TMR_INT_DISABLE() PX_BIT_SET_LO(TIMSK2, OCIE2A)
41#define ASYNC_TMR_INT_ENABLE() PX_BIT_SET_HI(TIMSK2, OCIE2A)
42#define ASYNC_TMR_INT_IS_ENABLED() PX_BIT_IS_HI(TIMSK2, OCIE2A)
43#define ASYNC_TMR_INT TIMER2_COMPA_vect
44#endif
45
46#ifndef ASYNC_TMR
47#error "Unsupported AVR"
48#endif
49
50/* _____LOCAL VARIABLES______________________________________________________ */
51
52/* _____LOCAL FUNCTIONS______________________________________________________ */
53/// Interrupt handler on TMR compare match (TCNT is cleared automatically)
54ISR(ASYNC_TMR_INT)
55{
57}
58
59/* _____GLOBAL FUNCTIONS_____________________________________________________ */
61{
62#if (ASYNC_TMR == 0)
63
64 // Select asynchronous timer operation to use external 32.768 kHz crystal
65 PX_BIT_SET_HI(ASSR, AS0);
66 // Start timer with clock prescaler CLK/1024 and CTC Mode ("Clear Timer on Compare")
67 // Resolution is 32.25 ms
68 TCCR0 = (0 << WGM00) | (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
69 // Reset time
70 TCNT0 = 0;
71 // Calculate and set period
72 OCR0 = (uint16_t)(((F_RTC / 1024) * PX_RTC_PERIOD_MS) / 1000) - 1;
73 // Wait for "update busy" flags to clear
74 while(ASSR & ((1 << TCN0UB) | (1 << OCR0UB) | (1 << TCR0UB))) {;}
75
76#elif (ASYNC_TMR == 2)
77
78 // Select asynchronous timer operation to use external 32768 Hz crystal
79 PX_BIT_SET_HI(ASSR, AS2);
80 // Start timer with clock prescaler CLK/1024 and CTC Mode ("Clear Timer on Compare")
81 // Resolution is 32.25 ms
82 TCCR2A = (0 << WGM20) | (1 << WGM21);
83 TCCR2B = (0 << WGM22) | (1 << CS22) | (1 << CS21) | (1 << CS20);
84 // Reset time
85 TCNT2 = 0;
86 // Calculate and set period (F_CPU is defined in 'px_board.h')
87 OCR2A = (uint16_t)(((F_RTC / 1024) * ASYNC_TMR_CFG_PERIOD_MS) / 1000) - 1;
88 // Wait for "update busy" flags to clear
89 while(ASSR & ((1 << TCN2UB) | (1 << OCR2AUB) | (1 << TCR2AUB))) {;}
90
91#else
92#error "Unsupported AVR"
93#endif
94
95 // Enable interrupt
96 ASYNC_TMR_INT_ENABLE();
97}
98
100{
101 ASYNC_TMR_INT_ENABLE();
102}
103
105{
106 bool int_is_enabled = ASYNC_TMR_INT_IS_ENABLED();
107
108 ASYNC_TMR_INT_DISABLE();
109
110 return int_is_enabled;
111}
void px_async_tmr_int_enable(void)
Enable TMRx interrupt.
Definition: px_async_tmr.c:99
#define ASYNC_TMR_CFG_ON_TICK()
Define handler function to call when timeout interrupt occurs.
bool px_async_tmr_int_disable(void)
Disable TMRx interrupt.
Definition: px_async_tmr.c:104
#define ASYNC_TMR_CFG_PERIOD_MS
Define timeout period.
void px_async_tmr_init(void)
Initalises TMRx to use an external 32768 Hz crystal.
Definition: px_async_tmr.c:60
#define PX_BIT_SET_HI(var, bit)
Set a bit (1)
Definition: px_defs.h:178