-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathWInterrupts.c
173 lines (136 loc) · 4.12 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
/*
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] = { NULL };
static int8_t channelMap[NUMBER_OF_GPIO_TE] = { -1 };
static int enabled = 0;
/* Configure I/O interrupt sources */
static void __initialize()
{
NVIC_DisableIRQ(GPIOTE_IRQn);
NVIC_ClearPendingIRQ(GPIOTE_IRQn);
NVIC_SetPriority(GPIOTE_IRQn, 3); // Same priority as Uart
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.
*/
int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
uint32_t polarity;
uint8_t ch;
if (pin >= PINS_COUNT)
{
return -1;
}
pin = g_ADigitalPinMap[pin];
switch (mode)
{
//gpiote channel does not support LOW and HIGH mode. These are mantained for compatibility
case LOW:
polarity = GPIOTE_CONFIG_POLARITY_HiToLo; //same as FALLING
break;
case HIGH:
polarity = GPIOTE_CONFIG_POLARITY_LoToHi; //same as RISING
break;
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 -1;
}
for (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;
}
}
// enable the interrupt after the first one is configured
if (!enabled)
{
__initialize();
enabled = 1;
}
// return the interrupt mask
return (1 << ch);
}
/*
* \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;
}
}
}
void GPIOTE_IRQHandler()
{
uint32_t event;
uint8_t ch;
for (ch = 0; ch < NUMBER_OF_GPIO_TE; ch++)
{
event = offsetof(NRF_GPIOTE_Type, EVENTS_IN[ch]);
if ((*(uint32_t *)((uint32_t)NRF_GPIOTE + event) == 0x1UL) && (NRF_GPIOTE->INTENSET & (1 << ch)))
{
break;
}
}
// clear event
*(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
// disable the interrupt
NRF_GPIOTE->INTENCLR = (1 << ch);
// initiate the callback
callbacksInt[ch]();
// enable the interrupt
NRF_GPIOTE->INTENSET = (1 << ch);
}