From ae0fa40e7739f37b17109c9c5fb6b83cf75282e2 Mon Sep 17 00:00:00 2001 From: Bigbits Date: Wed, 20 Mar 2019 12:52:00 +0800 Subject: [PATCH] Add attachInterrupt detachInterrput Signed-off-by: Bigbits --- cores/arduino/Arduino.h | 5 +++-- cores/arduino/WInterrupts.c | 35 +++++++++++++++++++++++++++++++++++ cores/arduino/WInterrupts.h | 15 +++++++-------- cores/arduino/main.cpp | 1 + cores/arduino/wiring_pulse.c | 2 +- 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index ad2d1b7..00a2c77 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -39,6 +39,7 @@ #include "gpiohs.h" #include "gpio.h" #include "gpio_common.h" +#include "plic.h" #ifdef __cplusplus extern "C"{ @@ -70,7 +71,7 @@ typedef void (*voidFuncPtr)( void ) ; //#include "WString.h" //#include "WMath.h" //#include "HardwareSerial.h" -//#include "wiring_pulse.h" +#include "wiring_pulse.h" // Tone function prototypes void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); @@ -81,6 +82,6 @@ void noTone(uint8_t _pin); #include "wiring_digital.h" #include "wiring_analog.h" #include "wiring_shift.h" -//#include "WInterrupts.h" +#include "WInterrupts.h" #endif // Arduino_h diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index e69de29..380d6b5 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -0,0 +1,35 @@ +#include "Arduino.h" +#include "WInterrupts.h" +#include "wiring_constants.h" +#include "plic.h" + +void attachInterrupt(uint8_t intnum, voidFuncPtr callback, uint8_t mode) +{ + plic_irq_callback_t _callback = (plic_irq_callback_t)callback; + switch(mode){ + case LOW : + gpiohs_set_pin_edge(intnum, GPIO_PE_LOW); + break; + case HIGH : + gpiohs_set_pin_edge(intnum, GPIO_PE_HIGH); + break; + case FALLING : + gpiohs_set_pin_edge(intnum, GPIO_PE_FALLING); + break; + case CHANGE : + gpiohs_set_pin_edge(intnum, GPIO_PE_BOTH); + break; + case RISING : + gpiohs_set_pin_edge(intnum, GPIO_PE_RISING); + break; + } + gpiohs_irq_register(intnum, 1, _callback, NULL); + sysctl_enable_irq(); + +} + +void detachInterrupt(uint8_t intnum) +{ + gpiohs_irq_unregister(intnum); + +} \ No newline at end of file diff --git a/cores/arduino/WInterrupts.h b/cores/arduino/WInterrupts.h index 5f993e8..8938c1c 100644 --- a/cores/arduino/WInterrupts.h +++ b/cores/arduino/WInterrupts.h @@ -20,6 +20,7 @@ #define _WIRING_INTERRUPTS_ #include +#include "plic.h" #ifdef __cplusplus extern "C"{ @@ -27,28 +28,26 @@ extern "C"{ // LOW 0 // HIGH 1 -#define FALLING 1 #define CHANGE 2 -#define RISING 3 +#define FALLING 3 +#define RISING 4 #define DEFAULT 1 #define EXTERNAL 0 -#define digitalPinToInterrupt(P) (PLIC_INT_GPIO_BASE + variant_pin_map[P].bit_pos) - - typedef void (*voidFuncPtr)(void); - +#define digitalPinToInterrupt(P) +typedef void (*voidFuncPtr)(void); /* * \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Use digitalPinToInterrupt(pin) to get the correct intnum. * Other interrupt numbers are available, see platform.h. * Replaces any previous function that was attached to the interrupt. */ -void attachInterrupt(uint32_t intnum, voidFuncPtr callback, uint32_t mode); +void attachInterrupt(uint8_t intnum, voidFuncPtr callback, uint8_t mode); /* * \brief Turns off the given interrupt. */ -void detachInterrupt(uint32_t intnum); +void detachInterrupt(uint8_t intnum); #ifdef __cplusplus } // extern "C" diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 51eec88..8d7189c 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -13,6 +13,7 @@ int main( void ) { pll_init(); + plic_init(); setup(); do { diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c index 474d497..7126dcd 100644 --- a/cores/arduino/wiring_pulse.c +++ b/cores/arduino/wiring_pulse.c @@ -27,7 +27,7 @@ * before the start of the pulse. */ extern uint32_t pulseIn(uint32_t pin, bool state, uint32_t timeout) { - uint32_t cpu_freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); + uint32_t cpu_freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); uint32_t numloops = 0; uint32_t maxloops = timeout * (cpu_freq / 1000000L) / 16; // microsecondsToClockCycles(timeout) / 16; ??? uint32_t start, end;