-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupts.cpp
245 lines (180 loc) · 7.04 KB
/
interrupts.cpp
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
//
// Actually i wanted to make this 'class' as generically as possible, but being able to set each possible Pin
// for a Pin Change Interrupt would require storing 23 Configs, one for each Pin.
// This would use up a lot of ATMega's SDRAM, which i'd like to avoid.
// So currently it's only possible to configure one Pin of Register B, C and D each for a Pin Change Interrupt.
//
#include "interrupts.h"
#define DebounceDelay 50
uint8_t countEnabledInterrupts = 0;
// config for External Interrupts
void (*externalInterrupt0ISR)(void) = NULL;
void (*externalInterrupt1ISR)(void) = NULL;
unsigned long lastDebounceTimeExternalInterrupt0 = 0;
unsigned long lastDebounceTimeExternalInterrupt1 = 0;
// config for Pin Change Interrupts
PinChangeInterruptConfig unsetPinChangeInterruptConfig; // cannot set a struct to NULL
// per Register only one Pin can be configured for Pin Change Interrupt
PinChangeInterruptConfig registerB_PinChangInterruptConfig = unsetPinChangeInterruptConfig;
PinChangeInterruptConfig registerC_PinChangInterruptConfig = unsetPinChangeInterruptConfig;
PinChangeInterruptConfig registerD_PinChangInterruptConfig = unsetPinChangeInterruptConfig;
// External Interrupt ISRs
ISR(INT0_vect) {
fireExternalInterruptISRIfNoDebounce(externalInterrupt0ISR, &lastDebounceTimeExternalInterrupt0);
}
ISR(INT1_vect) {
fireExternalInterruptISRIfNoDebounce(externalInterrupt1ISR, &lastDebounceTimeExternalInterrupt1);
}
void fireExternalInterruptISRIfNoDebounce(void (*isrFunction)(void), unsigned long *lastDebounceTime) {
if(isrFunction != NULL) {
if(hasTimeElapsed(lastDebounceTime, DebounceDelay)) {
isrFunction();
}
}
}
// Pin Change Interrupt ISRs
ISR(PCINT0_vect) {
if(®isterB_PinChangInterruptConfig != &unsetPinChangeInterruptConfig) {
firePinChangeISRIfNoDebounce(®ISTER_B_PIN_REGISTER, registerB_PinChangInterruptConfig);
}
}
ISR(PCINT1_vect) {
if(®isterC_PinChangInterruptConfig != &unsetPinChangeInterruptConfig) {
firePinChangeISRIfNoDebounce(®ISTER_C_PIN_REGISTER, registerC_PinChangInterruptConfig);
}
}
ISR(PCINT2_vect) {
if(®isterD_PinChangInterruptConfig != &unsetPinChangeInterruptConfig) {
firePinChangeISRIfNoDebounce(®ISTER_D_PIN_REGISTER, registerD_PinChangInterruptConfig);
}
}
void firePinChangeISRIfNoDebounce(volatile uint8_t* pinRegister, PinChangeInterruptConfig config) {
if (hasTimeElapsed(&config.lastDebounceTime, DebounceDelay)) {
uint8_t currentPinValue = *pinRegister & (1 << config.pin);
if (doesPinChangeEqualInterruptMode(config.mode, currentPinValue, config.lastPinValue)) {
if(config.isrFunction != NULL) {
config.isrFunction();
}
}
config.lastPinValue = currentPinValue;
}
}
bool doesPinChangeEqualInterruptMode(InterruptMode mode, uint8_t currentPinValue, uint8_t previousPinValue) {
if(mode == Rising && currentPinValue > previousPinValue) {
return true;
}
else if(mode == Falling && currentPinValue < previousPinValue) {
return true;
}
else if(mode == Change && currentPinValue != previousPinValue) {
return true;
}
else if(mode == LowLevel && currentPinValue == 0) {
return true;
}
return false;
}
// Configure External Interrupts
void enableExternalInterrupt0(InterruptMode mode, void (*isrFunction)(void)) {
enableExternalInterrupt(Zero, mode, isrFunction);
}
void enableExternalInterrupt1(InterruptMode mode, void (*isrFunction)(void)) {
enableExternalInterrupt(One, mode, isrFunction);
}
void enableExternalInterrupt(ExternalInterrupt interrupt, InterruptMode mode, void (*isrFunction)(void)) {
if(interrupt == Zero) {
externalInterrupt0ISR = isrFunction;
EIMSK |= 1 << INT0; // enable External Interrupt 0
setPinToLow(&EXTERNAL_INTERRUPT_DDR_REGISTER, EXTERNAL_INTERRUPT_0_DDR_PIN); // set INT0 as Input
}
else {
externalInterrupt1ISR = isrFunction;
EIMSK |= 1 << INT1; // enable External Interrupt 1
setPinToLow(&EXTERNAL_INTERRUPT_DDR_REGISTER, EXTERNAL_INTERRUPT_1_DDR_PIN); // set INT1 as Input
}
setExternalInterruptMode(interrupt, mode);
globallyEnableInterrupts();
countEnabledInterrupts++;
}
void setExternalInterruptMode(ExternalInterrupt interrupt, InterruptMode mode) {
uint8_t bit0 = interrupt == Zero ? ISC00 : ISC10;
uint8_t bit1 = interrupt == Zero ? ISC01 : ISC11;
if(mode == LowLevel || mode == Change) {
EICRA &= ~(1 << bit0);
}
else {
EICRA |= (1 << bit0);
}
if(mode == LowLevel || mode == Falling) {
EICRA &= ~ (1 << bit1);
}
else {
EICRA |= (1 << bit1);
}
}
void disableExternalInterrupt0() {
disableExternalInterrupt(Zero);
}
void disableExternalInterrupt1() {
disableExternalInterrupt(One);
}
void disableExternalInterrupt(ExternalInterrupt interrupt) {
if(interrupt == Zero) {
EIMSK &= ~(1 << INT0);
}
else {
EIMSK &= ~(1 << INT1);
}
decreaseCountEnabledInterrupts();
}
// Configure Pin Change Interrupts
void enablePinChangeInterruptOnRegisterB(uint8_t interruptPin, InterruptMode mode, void (*isrFunction)(void)) {
enablePinChangeInterrupt(PCIE0, &PCMSK0, interruptPin, isrFunction);
registerB_PinChangInterruptConfig = {
interruptPin,
mode,
isrFunction,
1, // Pull-up -> is HIGH per default
0
};
}
void enablePinChangeInterruptOnRegisterC(uint8_t interruptPin, InterruptMode mode, void (*isrFunction)(void)) {
enablePinChangeInterrupt(PCIE1, &PCMSK1, interruptPin, isrFunction);
registerC_PinChangInterruptConfig = {
interruptPin,
mode,
isrFunction,
1, // Pull-up -> is HIGH per default
0
};
}
void enablePinChangeInterruptOnRegisterD(uint8_t interruptPin, InterruptMode mode, void (*isrFunction)(void)) {
enablePinChangeInterrupt(PCIE2, &PCMSK2, interruptPin, isrFunction);
registerD_PinChangInterruptConfig = {
interruptPin,
mode,
isrFunction,
1, // Pull-up -> is HIGH per default
0
};
}
void enablePinChangeInterrupt(uint8_t pcie, volatile uint8_t* pinChangeMaskRegister, uint8_t interruptPin, void (*isrFunction)(void)) {
PCICR |= (1 << pcie);
*pinChangeMaskRegister |= (1 << interruptPin);
globallyEnableInterrupts();
countEnabledInterrupts++;
}
// TODO: implement a disable() function for each Register which knows correct PCIE and PCMSK Registers and unsets its Config struct
void disablePinChangeInterrupt(uint8_t pcie, volatile uint8_t* pinChangeMaskRegister, uint8_t interruptPin) {
*pinChangeMaskRegister &= ~(1 << interruptPin);
if(*pinChangeMaskRegister == 0) { // all Interrupts have been disabled for this Register
PCICR &= ~(1 << pcie);
}
decreaseCountEnabledInterrupts();
}
void decreaseCountEnabledInterrupts() {
countEnabledInterrupts--;
if(countEnabledInterrupts == 0) {
globallyDisableInterrupts();
}
}