-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterruptIn.cpp
236 lines (190 loc) · 7.04 KB
/
InterruptIn.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
/*
* InterruptIn.cpp
*
* Created on: Ene 2018
* Author: raulMrello
*
*/
#include "InterruptIn.h"
//------------------------------------------------------------------------------------
//-- STATIC IMPLEMENTATION -----------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
/** Macro para imprimir trazas de depuración, siempre que se haya configurado un objeto
* Logger válido (ej: _debug)
*/
static const char* _MODULE_ = "[InterruptIn]...";
#define _EXPR_ (_defdbg && !IS_ISR())
#define DEBUG_CHECK(x) \
do { \
esp_err_t __err_rc = (x); \
if (__err_rc != ESP_OK) { \
DEBUG_TRACE_E(_EXPR_, _MODULE_, "Err = %d, line = %d, func = %s, expr = %s", __err_rc, __LINE__, __ASSERT_FUNC, #x); \
} \
} while(0);
//------------------------------------------------------------------------------------
static bool g_isr_svc_installed = false;
//------------------------------------------------------------------------------------
static void gpio_isr_handler(void* arg){
InterruptIn* me = (InterruptIn*)arg;
ENTER_ISR();
if(me->read()){
InterruptIn::_irq_handler((uint32_t)me, IRQ_RISE);
EXIT_ISR();
return;
}
InterruptIn::_irq_handler((uint32_t)me, IRQ_FALL);
EXIT_ISR();
}
//------------------------------------------------------------------------------------
static void defaultIsrHandler(){
}
//------------------------------------------------------------------------------------
//-- PUBLIC METHODS IMPLEMENTATION ---------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
InterruptIn::InterruptIn(PinName pin, bool defdbg) : _gpio() {
// No lock needed in the constructor
_defdbg = defdbg;
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Creando objeto en pin %d", pin);
_rise = callback(&defaultIsrHandler);
_fall = callback(&defaultIsrHandler);
_pin = pin;
//disable interrupt
_gpio.intr_type = GPIO_INTR_DISABLE;
//set as output mode
_gpio.mode = GPIO_MODE_INPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
if(pin < GPIO_NUM_32){
_gpio.pin_bit_mask = (1<<pin);
}
else{
_gpio.pin_bit_mask = ((uint64_t)(((uint64_t)1)<<pin));
}
//disable pull-down mode
_gpio.pull_down_en = GPIO_PULLDOWN_DISABLE;
//disable pull-up mode
_gpio.pull_up_en = GPIO_PULLUP_DISABLE;
//configure GPIO with the given settings
DEBUG_CHECK(gpio_config(&_gpio));
//install _gpio isr service (default flags =0 )
if(!g_isr_svc_installed){
DEBUG_CHECK(gpio_install_isr_service(0));
g_isr_svc_installed = true;
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Instalado isr-service");
}
}
//------------------------------------------------------------------------------------
InterruptIn::~InterruptIn() {
//disable interrupt
_gpio.intr_type = GPIO_INTR_DISABLE;
//set as output mode
_gpio.mode = GPIO_MODE_INPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
if(_pin < GPIO_NUM_32){
_gpio.pin_bit_mask = (1<<_pin);
}
else{
_gpio.pin_bit_mask = ((uint64_t)(((uint64_t)1)<<_pin));
}
//disable pull-down mode
_gpio.pull_down_en = GPIO_PULLDOWN_DISABLE;
//disable pull-up mode
_gpio.pull_up_en = GPIO_PULLUP_DISABLE;
//configure GPIO with the given settings
DEBUG_CHECK(gpio_config(&_gpio));
}
//------------------------------------------------------------------------------------
int InterruptIn::read() {
// Read only
return gpio_get_level(_pin);
}
//------------------------------------------------------------------------------------
void InterruptIn::mode(PinMode pull) {
switch(pull){
case PullUp:{
DEBUG_CHECK(gpio_set_pull_mode(_pin, GPIO_PULLUP_ONLY));
break;
}
case PullDown:{
DEBUG_CHECK(gpio_set_pull_mode(_pin, GPIO_PULLDOWN_ONLY));
break;
}
default:{
DEBUG_CHECK(gpio_set_pull_mode(_pin, GPIO_FLOATING));
break;
}
}
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Ajustado pull-mode = %d", pull);
}
//------------------------------------------------------------------------------------
void InterruptIn::rise(Callback<void()> func) {
if(func){
_rise = func;
_gpio.intr_type = (gpio_int_type_t)(_gpio.intr_type | GPIO_INTR_POSEDGE);
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Activando isr-rise intr_type = %d", _gpio.intr_type);
}
else{
_rise = callback(&defaultIsrHandler);
_gpio.intr_type = (gpio_int_type_t)(_gpio.intr_type & (~GPIO_INTR_POSEDGE));
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Desactivando isr-rise intr_type = %d", _gpio.intr_type);
}
DEBUG_CHECK(gpio_set_intr_type(_pin, _gpio.intr_type));
if(_gpio.intr_type != GPIO_INTR_DISABLE){
DEBUG_CHECK(gpio_isr_handler_add(_pin, gpio_isr_handler, (void*)this));
}
else{
DEBUG_CHECK(gpio_isr_handler_remove(_pin));
}
}
//------------------------------------------------------------------------------------
void InterruptIn::fall(Callback<void()> func) {
if(func){
_fall = func;
_gpio.intr_type = (gpio_int_type_t)(_gpio.intr_type | GPIO_INTR_NEGEDGE);
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Activando isr-fall intr_type = %d", _gpio.intr_type);
}
else{
_fall = callback(&defaultIsrHandler);
_gpio.intr_type = (gpio_int_type_t)(_gpio.intr_type & (~GPIO_INTR_NEGEDGE));
DEBUG_TRACE_D(_EXPR_, _MODULE_, "Desactivando isr-fall intr_type = %d", _gpio.intr_type);
}
DEBUG_CHECK(gpio_set_intr_type(_pin, _gpio.intr_type));
if(_gpio.intr_type != GPIO_INTR_DISABLE){
DEBUG_CHECK(gpio_isr_handler_add(_pin, gpio_isr_handler, (void*)this));
}
else{
DEBUG_CHECK(gpio_isr_handler_remove(_pin));
}
}
//------------------------------------------------------------------------------------
void InterruptIn::enable_irq() {
if(_gpio.intr_type != GPIO_INTR_DISABLE){
DEBUG_CHECK(gpio_isr_handler_add(_pin, gpio_isr_handler, (void*)this));
}
}
//------------------------------------------------------------------------------------
void InterruptIn::disable_irq() {
DEBUG_CHECK(gpio_isr_handler_remove(_pin));
}
//------------------------------------------------------------------------------------
InterruptIn::operator int() {
// Underlying call is atomic
return read();
}
//------------------------------------------------------------------------------------
void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) {
InterruptIn *handler = (InterruptIn*)id;
switch (event) {
case IRQ_RISE:
handler->_rise.call();
break;
case IRQ_FALL:
handler->_fall.call();
break;
case IRQ_NONE: break;
}
}
//------------------------------------------------------------------------------------
//-- PROTECTED METHODS IMPLEMENTATION ------------------------------------------------
//------------------------------------------------------------------------------------