Add attachInterrupt detachInterrput

Signed-off-by: Bigbits <btx000@qq.com>
This commit is contained in:
Bigbits
2019-03-20 12:52:00 +08:00
parent f5c460d4d9
commit ae0fa40e77
5 changed files with 47 additions and 11 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -20,6 +20,7 @@
#define _WIRING_INTERRUPTS_
#include <stdint.h>
#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"

View File

@@ -13,6 +13,7 @@
int main( void )
{
pll_init();
plic_init();
setup();
do {

View File

@@ -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;