Merge pull request #22 from earlephilhower/attache
Add attach/detachInterrupt support
This commit is contained in:
commit
ac867d7159
2 changed files with 36 additions and 0 deletions
|
|
@ -28,6 +28,7 @@
|
|||
#include "api/ArduinoAPI.h"
|
||||
|
||||
#include <pins_arduino.h>
|
||||
#define digitalPinToInterrupt(P) (P)
|
||||
|
||||
#include "debug_internal.h"
|
||||
|
||||
|
|
@ -35,6 +36,13 @@
|
|||
extern "C"{
|
||||
#endif // __cplusplus
|
||||
|
||||
void interrupts();
|
||||
void noInterrupts();
|
||||
void attachInterrupt(pin_size_t pin, voidFuncPtr callback, PinStatus mode);
|
||||
void detachInterrupt(pin_size_t pin);
|
||||
|
||||
|
||||
|
||||
void pinMode(pin_size_t pinNumber, PinMode pinMode);
|
||||
|
||||
// SIO (GPIO)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <hardware/gpio.h>
|
||||
#include <hardware/sync.h>
|
||||
#include <stack>
|
||||
|
||||
|
|
@ -37,3 +38,30 @@ extern "C" void noInterrupts() {
|
|||
irqStack.push(save_and_disable_interrupts());
|
||||
}
|
||||
|
||||
static uint32_t _irqMap = 0;
|
||||
|
||||
extern "C" void attachInterrupt(pin_size_t pin, voidFuncPtr callback, PinStatus mode) {
|
||||
uint32_t events;
|
||||
switch (mode) {
|
||||
case LOW: events = 1; break;
|
||||
case HIGH: events = 2; break;
|
||||
case FALLING: events = 4; break;
|
||||
case RISING: events = 8; break;
|
||||
case CHANGE: events = 4 | 8; break;
|
||||
default: return; // ERROR
|
||||
}
|
||||
noInterrupts();
|
||||
detachInterrupt(pin);
|
||||
gpio_set_irq_enabled_with_callback(pin, events, true, (gpio_irq_callback_t)callback);
|
||||
_irqMap |= 1<<pin;
|
||||
interrupts();
|
||||
}
|
||||
|
||||
extern "C" void detachInterrupt(pin_size_t pin){
|
||||
noInterrupts();
|
||||
if (_irqMap & (1<<pin)) {
|
||||
gpio_set_irq_enabled(pin, 0x0f /* all */, false);
|
||||
_irqMap &= ~(1<<pin);
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue