Add attach/detachInterrupt support

Fixes #21
parent 784217b0
......@@ -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();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment