-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap MRAA lib's
Gpio::isr()
for IRQ support
I also removed MRAA exclusion from examples build. Previously, interruptConfigure.cpp was not built for MRAA driver.
- Loading branch information
Showing
7 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
#endif | ||
|
||
#include "MRAA/RF24_arch_config.h" | ||
#include "MRAA/interrupt.h" | ||
|
||
#endif // RF24_UTILITY_INCLUDES_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <map> | ||
#include <mraa.h> // mraa_strresult() | ||
#include <mraa.hpp> // mraa::Gpio | ||
#include "interrupt.h" | ||
#include "gpio.h" // rf24_gpio_pin_t | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
std::map<rf24_gpio_pin_t, mraa::Gpio*> irqCache; | ||
|
||
int attachInterrupt(rf24_gpio_pin_t pin, uint8_t mode, void (*function)(void)) | ||
{ | ||
// ensure pin is not already being used in a separate thread | ||
detachInterrupt(pin); | ||
GPIO::close(pin); | ||
|
||
mraa::Gpio* gpio = new mraa::Gpio(pin); | ||
mraa::Result status = gpio->dir(mraa::DIR_IN); | ||
if (status != mraa::SUCCESS) { | ||
std::string msg = "[attachInterrupt] Could not set the pin as an input; "; | ||
msg += mraa_strresult((mraa_result_t)status); | ||
throw IRQException(msg); | ||
return 0; | ||
} | ||
status = gpio->isr((mraa::Edge)mode, (void (*)(void*))function, NULL); | ||
if (status != mraa::SUCCESS) { | ||
std::string msg = "[attachInterrupt] Could not setup the ISR; "; | ||
msg += mraa_strresult((mraa_result_t)status); | ||
throw IRQException(msg); | ||
return 0; | ||
} | ||
|
||
std::pair<std::map<rf24_gpio_pin_t, mraa::Gpio*>::iterator, bool> indexPair = irqCache.insert(std::pair<rf24_gpio_pin_t, mraa::Gpio*>(pin, gpio)); | ||
if (!indexPair.second) { | ||
// this should not be reached, but indexPair.first needs to be the inserted map element | ||
gpio->close(); | ||
throw IRQException("[attachInterrupt] Could not cache the mraa::Gpio object"); | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
int detachInterrupt(rf24_gpio_pin_t pin) | ||
{ | ||
std::map<rf24_gpio_pin_t, mraa::Gpio*>::iterator cachedPin = irqCache.find(pin); | ||
if (cachedPin == irqCache.end()) { | ||
return 0; // pin not in cache; just exit | ||
} | ||
cachedPin->second->close(); | ||
irqCache.erase(cachedPin); | ||
return 1; | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // !defined(ARDUINO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef RF24_UTILITY_MRAA_INTERRUPT_H_ | ||
#define RF24_UTILITY_MRAA_INTERRUPT_H_ | ||
|
||
#include <stdexcept> // std::exception, std::string | ||
#include "mraa.hpp" // mraa:: | ||
#include "gpio.h" // rf24_gpio_pin_t | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
enum Edge | ||
{ | ||
INT_EDGE_FALLING = mraa::Edge::EDGE_FALLING, | ||
INT_EDGE_RISING = mraa::Edge::EDGE_RISING, | ||
INT_EDGE_BOTH = mraa::Edge::EDGE_BOTH, | ||
}; | ||
|
||
/** Specific exception for IRQ errors */ | ||
class IRQException : public std::runtime_error | ||
{ | ||
public: | ||
explicit IRQException(const std::string& msg) | ||
: std::runtime_error(msg) | ||
{ | ||
} | ||
}; | ||
|
||
/** | ||
* Take the details and create an interrupt handler that will | ||
* callback to the user-supplied function. | ||
*/ | ||
int attachInterrupt(rf24_gpio_pin_t pin, uint8_t mode, void (*function)(void)); | ||
|
||
/** | ||
* Will cancel the interrupt thread, close the filehandle and release the pin. | ||
*/ | ||
int detachInterrupt(rf24_gpio_pin_t pin); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // RF24_UTILITY_MRAA_INTERRUPT_H_ |