-
Notifications
You must be signed in to change notification settings - Fork 1
/
extint.c
181 lines (163 loc) · 4.3 KB
/
extint.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
/*! \file extint.c \brief External-Interrupt function library. */
//*****************************************************************************
//
// File Name : 'extint.c'
// Title : External-Interrupt function library
// Author : Pascal Stang - Copyright (C) 2002-2004
// Created : 5/10/2002
// Revised : 11/16/2004
// Version : 1.0
// Target MCU : Atmel AVR Series
// Editor Tabs : 4
//
// Notes: This library provides convenient standardized configuration and
// access to external interrupts. The library is designed to make
// it possible to write code that uses external interrupts without
// digging into the processor datasheets to find register names and
// bit-defines. The library also strives to allow code which uses
// external interrupts to more easily cross-compile between different
// microcontrollers.
//
// NOTE: Using this library has certain advantages, but also adds
// overhead and latency to interrupt servicing. If the smallest
// code size or fastest possible latency is needed, do NOT use this
// library; link your interrupts directly.
//
//*****************************************************************************
#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "extint.h"
// Global variables
typedef void (*voidFuncPtr)(void);
volatile static voidFuncPtr ExtIntFunc[EXTINT_NUM_INTERRUPTS];
// functions
//! initializes extint library
void extintInit(void)
{
u08 intNum;
// detach all user functions from interrupts
for(intNum=0; intNum<EXTINT_NUM_INTERRUPTS; intNum++)
extintDetach(intNum);
}
//! Configure external interrupt trigger
// NOTE: this function is not complete!!!
void extintConfigure(u08 interruptNum, u08 configuration)
{
if(interruptNum == EXTINT0)
{
MCUCR &= ~((1<<ISC01) | (1<<ISC00));
MCUCR |= configuration;
}
#ifdef INT1_vect
else if(interruptNum == EXTINT1)
{
MCUCR &= ~((1<<ISC11) | (1<<ISC10));
MCUCR |= configuration<<2;
}
#endif
#ifdef INT2_vect
else if(interruptNum == EXTINT2)
{
if(configuration == EXTINT_EDGE_RISING)
sbi(MCUCSR, ISC2);
else
cbi(MCUCSR, ISC2);
}
#endif
// need to handle a lot more cases
// and differences between processors.
// looking for clean way to do it...
}
//! Attach a user function to an external interrupt
void extintAttach(u08 interruptNum, void (*userHandler)(void) )
{
// make sure the interrupt number is within bounds
if(interruptNum < EXTINT_NUM_INTERRUPTS)
{
// set the interrupt function to run
// the supplied user's function
ExtIntFunc[interruptNum] = userHandler;
}
}
//! Detach a user function from an external interrupt
void extintDetach(u08 interruptNum)
{
// make sure the interrupt number is within bounds
if(interruptNum < EXTINT_NUM_INTERRUPTS)
{
// set the interrupt function to run
// the supplied user's function
ExtIntFunc[interruptNum] = 0;
}
}
//! Interrupt handler for INT0
EXTINT_INTERRUPT_HANDLER(INT0_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT0])
ExtIntFunc[EXTINT0]();
}
#ifdef INT1_vect
//! Interrupt handler for INT1
EXTINT_INTERRUPT_HANDLER(INT1_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT1])
ExtIntFunc[EXTINT1]();
}
#endif
#ifdef INT2_vect
//! Interrupt handler for INT2
EXTINT_INTERRUPT_HANDLER(INT2_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT2])
ExtIntFunc[EXTINT2]();
}
#endif
#ifdef INT3_vect
//! Interrupt handler for INT3
EXTINT_INTERRUPT_HANDLER(INT3_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT3])
ExtIntFunc[EXTINT3]();
}
#endif
#ifdef INT4_vect
//! Interrupt handler for INT4
EXTINT_INTERRUPT_HANDLER(INT4_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT4])
ExtIntFunc[EXTINT4]();
}
#endif
#ifdef INT5_vect
//! Interrupt handler for INT5
EXTINT_INTERRUPT_HANDLER(INT5_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT5])
ExtIntFunc[EXTINT5]();
}
#endif
#ifdef INT6_vect
//! Interrupt handler for INT6
EXTINT_INTERRUPT_HANDLER(INT6_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT6])
ExtIntFunc[EXTINT6]();
}
#endif
#ifdef INT7_vect
//! Interrupt handler for INT7
EXTINT_INTERRUPT_HANDLER(INT7_vect)
{
// if a user function is defined, execute it
if(ExtIntFunc[EXTINT7])
ExtIntFunc[EXTINT7]();
}
#endif