diff --git a/libraries/AP_HAL/AP_HAL.h b/libraries/AP_HAL/AP_HAL.h index 90bf55a020..90adf1f28a 100644 --- a/libraries/AP_HAL/AP_HAL.h +++ b/libraries/AP_HAL/AP_HAL.h @@ -16,6 +16,7 @@ #include "RCOutput.h" #include "Scheduler.h" #include "Semaphores.h" +#include "EventHandle.h" #include "Util.h" #include "OpticalFlow.h" #include "Flash.h" diff --git a/libraries/AP_HAL/AP_HAL_Namespace.h b/libraries/AP_HAL/AP_HAL_Namespace.h index 5770d84634..2b03dc7f1d 100644 --- a/libraries/AP_HAL/AP_HAL_Namespace.h +++ b/libraries/AP_HAL/AP_HAL_Namespace.h @@ -26,6 +26,8 @@ namespace AP_HAL { class RCInput; class RCOutput; class Scheduler; + class EventHandle; + class EventSource; class Semaphore; class OpticalFlow; class DSP; diff --git a/libraries/AP_HAL/EventHandle.cpp b/libraries/AP_HAL/EventHandle.cpp new file mode 100644 index 0000000000..001d3386c2 --- /dev/null +++ b/libraries/AP_HAL/EventHandle.cpp @@ -0,0 +1,33 @@ +#include "EventHandle.h" +#include + + +bool AP_HAL::EventHandle::register_event(uint32_t evt_mask) +{ + WITH_SEMAPHORE(sem); + evt_mask_ |= evt_mask; + return true; +} + +bool AP_HAL::EventHandle::unregister_event(uint32_t evt_mask) +{ + WITH_SEMAPHORE(sem); + evt_mask_ &= ~evt_mask; + return true; +} + +bool AP_HAL::EventHandle::wait(uint64_t duration) +{ + if (evt_src_ == nullptr) { + return false; + } + return evt_src_->wait(duration, this); +} + +bool AP_HAL::EventHandle::set_source(AP_HAL::EventSource* src) +{ + WITH_SEMAPHORE(sem); + evt_src_ = src; + evt_mask_ = 0; + return true; +} diff --git a/libraries/AP_HAL/EventHandle.h b/libraries/AP_HAL/EventHandle.h new file mode 100644 index 0000000000..3c27d58cb0 --- /dev/null +++ b/libraries/AP_HAL/EventHandle.h @@ -0,0 +1,44 @@ +#pragma once + +#include "AP_HAL_Namespace.h" +#include +#include "AP_HAL_Boards.h" + +class AP_HAL::EventSource { +public: + // generate event from thread context + virtual void signal(uint32_t evt_mask) = 0; + + // generate event from interrupt context + virtual void signalI(uint32_t evt_mask) { signal(evt_mask); } + + + // Wait on an Event handle, method for internal use by EventHandle + virtual bool wait(uint64_t duration, AP_HAL::EventHandle* evt_handle) = 0; +}; + +class AP_HAL::EventHandle { +public: + //Set event source + virtual bool set_source(AP_HAL::EventSource* src); + + AP_HAL::EventSource* get_source() { return evt_src_; } + + // return true if event type was successfully registered + virtual bool register_event(uint32_t evt_mask); + + // return true if event type was successfully unregistered + virtual bool unregister_event(uint32_t evt_mask); + + // return true if event was triggered within the duration + virtual bool wait(uint64_t duration); + + virtual uint32_t get_evt_mask() const { return evt_mask_; } + +private: + // Mask of events to be handeled, + // Max 32 events can be handled per event handle + uint32_t evt_mask_; + AP_HAL::EventSource *evt_src_; + HAL_Semaphore sem; +}; diff --git a/libraries/AP_HAL/board/chibios.h b/libraries/AP_HAL/board/chibios.h index f34d4a2c6c..aed3e263d7 100644 --- a/libraries/AP_HAL/board/chibios.h +++ b/libraries/AP_HAL/board/chibios.h @@ -60,6 +60,9 @@ #include #define HAL_Semaphore ChibiOS::Semaphore +#include +#define HAL_EventHandle AP_HAL::EventHandle + /* string names for well known SPI devices */ #define HAL_BARO_MS5611_NAME "ms5611" #ifndef HAL_BARO_MS5611_SPI_INT_NAME diff --git a/libraries/AP_HAL/board/linux.h b/libraries/AP_HAL/board/linux.h index b191bad970..2af296af15 100644 --- a/libraries/AP_HAL/board/linux.h +++ b/libraries/AP_HAL/board/linux.h @@ -347,4 +347,5 @@ #include #define HAL_Semaphore Linux::Semaphore - +#include +#define HAL_EventHandle AP_HAL::EventHandle diff --git a/libraries/AP_HAL/board/sitl.h b/libraries/AP_HAL/board/sitl.h index 179c4a9693..1ca5a2fe1e 100644 --- a/libraries/AP_HAL/board/sitl.h +++ b/libraries/AP_HAL/board/sitl.h @@ -58,6 +58,11 @@ #include #define HAL_Semaphore HALSITL::Semaphore +#include +#define HAL_EventHandle AP_HAL::EventHandle + +#define HAL_NUM_CAN_IFACES 2 + #ifndef HAL_BOARD_STORAGE_DIRECTORY #define HAL_BOARD_STORAGE_DIRECTORY "." #endif diff --git a/libraries/AP_HAL_ChibiOS/AP_HAL_ChibiOS_Namespace.h b/libraries/AP_HAL_ChibiOS/AP_HAL_ChibiOS_Namespace.h index 6cc757974a..f5c4baf130 100644 --- a/libraries/AP_HAL_ChibiOS/AP_HAL_ChibiOS_Namespace.h +++ b/libraries/AP_HAL_ChibiOS/AP_HAL_ChibiOS_Namespace.h @@ -14,6 +14,7 @@ namespace ChibiOS { class RCOutput; class Scheduler; class Semaphore; + class EventSource; class SPIBus; class SPIDesc; class SPIDevice;