-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathWInterrupts.c
210 lines (165 loc) · 5.2 KB
/
WInterrupts.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
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
Copyright (c) 2016 Sandeep Mistry All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <nrf.h>
#include "Arduino.h"
#include "wiring_private.h"
#include <string.h>
#ifdef NRF52
#define NUMBER_OF_GPIO_TE 8
#else
#define NUMBER_OF_GPIO_TE 4
#endif
static voidFuncPtr callbacksInt[NUMBER_OF_GPIO_TE];
static int8_t channelMap[NUMBER_OF_GPIO_TE];
static voidFuncPtr callbackLowAccuracyInt;
static int enabled = 0;
/* Configure I/O interrupt sources */
static void __initialize()
{
memset(callbacksInt, 0, sizeof(callbacksInt));
memset(channelMap, -1, sizeof(channelMap));
callbackLowAccuracyInt = NULL;
NVIC_DisableIRQ(GPIOTE_IRQn);
NVIC_ClearPendingIRQ(GPIOTE_IRQn);
NVIC_SetPriority(GPIOTE_IRQn, 1);
NVIC_EnableIRQ(GPIOTE_IRQn);
}
/*
* \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
* Replaces any previous function that was attached to the interrupt.
*/
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
if (!enabled) {
__initialize();
enabled = 1;
}
if (pin >= PINS_COUNT) {
return;
}
pin = g_ADigitalPinMap[pin];
uint32_t polarity;
switch (mode) {
case CHANGE:
polarity = GPIOTE_CONFIG_POLARITY_Toggle;
break;
case FALLING:
polarity = GPIOTE_CONFIG_POLARITY_HiToLo;
break;
case RISING:
polarity = GPIOTE_CONFIG_POLARITY_LoToHi;
break;
default:
return;
}
for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) {
if (channelMap[ch] == -1 || (uint32_t)channelMap[ch] == pin) {
channelMap[ch] = pin;
callbacksInt[ch] = callback;
NRF_GPIOTE->CONFIG[ch] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk);
NRF_GPIOTE->CONFIG[ch] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) |
((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk);
NRF_GPIOTE->CONFIG[ch] |= GPIOTE_CONFIG_MODE_Event;
NRF_GPIOTE->INTENSET = (1 << ch);
break;
}
}
}
/*
* \brief Turns off the given interrupt.
*/
void detachInterrupt(uint32_t pin)
{
if (pin >= PINS_COUNT) {
return;
}
pin = g_ADigitalPinMap[pin];
for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) {
if ((uint32_t)channelMap[ch] == pin) {
channelMap[ch] = -1;
callbacksInt[ch] = NULL;
NRF_GPIOTE->CONFIG[ch] &= ~GPIOTE_CONFIG_MODE_Event;
NRF_GPIOTE->INTENCLR = (1 << ch);
break;
}
}
}
/*
* \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
* Replaces any previous function that was attached to the interrupt.
* Only one callback can be assigned to the low accuracy interrupt AKA PORT event.
*/
void attachInterruptLowAccuracy(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
if (!enabled) {
__initialize();
enabled = 1;
}
if (pin >= PINS_COUNT) {
return;
}
pin = g_ADigitalPinMap[pin];
uint32_t polarity;
switch (mode) {
case FALLING:
NRF_GPIO->PIN_CNF[pin] |= ((uint32_t)GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos);
break;
case RISING:
NRF_GPIO->PIN_CNF[pin] |= ((uint32_t)GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos);
break;
default:
return;
}
callbackLowAccuracyInt = callback;
NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk;
}
/*
* \brief Turns off the given low accuracy interrupt.
*/
void detachInterruptLowAccuracy(uint32_t pin)
{
if (pin >= PINS_COUNT) {
return;
}
pin = g_ADigitalPinMap[pin];
callbackLowAccuracyInt = NULL;
NRF_GPIO->PIN_CNF[pin] &= ~((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIOTE->INTENCLR = GPIOTE_INTENCLR_PORT_Msk;
}
void GPIOTE_IRQHandler()
{
if(NRF_GPIOTE->EVENTS_PORT == 1 && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_PORT_Msk)) {
if(callbackLowAccuracyInt) {
callbackLowAccuracyInt();
}
NRF_GPIOTE->EVENTS_PORT = 0;
} else {
uint32_t event = offsetof(NRF_GPIOTE_Type, EVENTS_IN[0]);
for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) {
if ((*(uint32_t *)((uint32_t)NRF_GPIOTE + event) == 0x1UL) && (NRF_GPIOTE->INTENSET & (1 << ch))) {
if (channelMap[ch] != -1 && callbacksInt[ch]) {
callbacksInt[ch]();
}
*(uint32_t *)((uint32_t)NRF_GPIOTE + event) = 0;
#if __CORTEX_M == 0x04
volatile uint32_t dummy = *((volatile uint32_t *)((uint32_t)NRF_GPIOTE + event));
(void)dummy;
#endif
}
event = (uint32_t)((uint32_t)event + 4);
}
}
}