diff --git a/libraries/AP_HAL/AP_HAL.h b/libraries/AP_HAL/AP_HAL.h index 64198b63d0..49aab05996 100644 --- a/libraries/AP_HAL/AP_HAL.h +++ b/libraries/AP_HAL/AP_HAL.h @@ -16,7 +16,6 @@ #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 aca8e02293..9782b9fb2c 100644 --- a/libraries/AP_HAL/AP_HAL_Namespace.h +++ b/libraries/AP_HAL/AP_HAL_Namespace.h @@ -27,9 +27,8 @@ namespace AP_HAL { class RCInput; class RCOutput; class Scheduler; - class EventHandle; - class EventSource; class Semaphore; + class BinarySemaphore; class OpticalFlow; class DSP; diff --git a/libraries/AP_HAL/CANIface.h b/libraries/AP_HAL/CANIface.h index 06b4f7dffb..1b2e88fb33 100644 --- a/libraries/AP_HAL/CANIface.h +++ b/libraries/AP_HAL/CANIface.h @@ -187,7 +187,7 @@ public: return false; } - virtual bool set_event_handle(EventHandle* evt_handle) + virtual bool set_event_handle(AP_HAL::BinarySemaphore *sem_handle) { return true; } diff --git a/libraries/AP_HAL/EventHandle.cpp b/libraries/AP_HAL/EventHandle.cpp deleted file mode 100644 index d0dab77d92..0000000000 --- a/libraries/AP_HAL/EventHandle.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#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(uint16_t duration_us) -{ - if (evt_src_ == nullptr) { - return false; - } - return evt_src_->wait(duration_us, 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 deleted file mode 100644 index c5c676c389..0000000000 --- a/libraries/AP_HAL/EventHandle.h +++ /dev/null @@ -1,44 +0,0 @@ -#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(uint16_t duration_us, 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(uint16_t duration_us); - - 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/Semaphores.h b/libraries/AP_HAL/Semaphores.h index 3c5bca94b1..34e8ff93fb 100644 --- a/libraries/AP_HAL/Semaphores.h +++ b/libraries/AP_HAL/Semaphores.h @@ -55,3 +55,28 @@ private: #define JOIN( sem, line, counter ) _DO_JOIN( sem, line, counter ) #define _DO_JOIN( sem, line, counter ) WithSemaphore _getsem ## counter(sem, line) + +/* + a binary semaphore + */ +class AP_HAL::BinarySemaphore { +public: + /* + create a binary semaphore. initial_state determines if a wait() + immediately after creation would block. If initial_state is true + then it won't block, if initial_state is false it will block + */ + BinarySemaphore(bool initial_state=false) {} + + // do not allow copying + CLASS_NO_COPY(BinarySemaphore); + + virtual bool wait(uint32_t timeout_us) WARN_IF_UNUSED = 0 ; + virtual bool wait_blocking() = 0; + virtual bool wait_nonblocking() { return wait(0); } + + virtual void signal() = 0; + virtual void signal_ISR() { signal(); } + + virtual ~BinarySemaphore(void) {} +}; diff --git a/libraries/AP_HAL/board/chibios.h b/libraries/AP_HAL/board/chibios.h index b06642742d..9d1b940f31 100644 --- a/libraries/AP_HAL/board/chibios.h +++ b/libraries/AP_HAL/board/chibios.h @@ -63,9 +63,7 @@ // allow for static semaphores #include #define HAL_Semaphore ChibiOS::Semaphore - -#include -#define HAL_EventHandle AP_HAL::EventHandle +#define HAL_BinarySemaphore ChibiOS::BinarySemaphore #endif /* string names for well known SPI devices */ diff --git a/libraries/AP_HAL/board/empty.h b/libraries/AP_HAL/board/empty.h index 6e33c9a81b..0dc26bdf23 100644 --- a/libraries/AP_HAL/board/empty.h +++ b/libraries/AP_HAL/board/empty.h @@ -16,3 +16,4 @@ #define HAL_HAVE_SAFETY_SWITCH 1 #define HAL_Semaphore Empty::Semaphore +#define HAL_BinarySemaphore Empty::BinarySemaphore diff --git a/libraries/AP_HAL/board/esp32.h b/libraries/AP_HAL/board/esp32.h index 5aa515e614..1fc82f841b 100644 --- a/libraries/AP_HAL/board/esp32.h +++ b/libraries/AP_HAL/board/esp32.h @@ -39,6 +39,7 @@ // allow for static semaphores #include #define HAL_Semaphore ESP32::Semaphore +#define HAL_BinarySemaphore ESP32::BinarySemaphore #endif #define HAL_NUM_CAN_IFACES 0 @@ -114,4 +115,4 @@ // other big things.. #define HAL_QUADPLANE_ENABLED 0 -#define HAL_GYROFFT_ENABLED 0 \ No newline at end of file +#define HAL_GYROFFT_ENABLED 0 diff --git a/libraries/AP_HAL/board/linux.h b/libraries/AP_HAL/board/linux.h index 76cea1a112..65ce66d2a1 100644 --- a/libraries/AP_HAL/board/linux.h +++ b/libraries/AP_HAL/board/linux.h @@ -398,8 +398,7 @@ #ifdef __cplusplus #include #define HAL_Semaphore Linux::Semaphore -#include -#define HAL_EventHandle AP_HAL::EventHandle +#define HAL_BinarySemaphore Linux::BinarySemaphore #endif #ifndef HAL_HAVE_HARDWARE_DOUBLE diff --git a/libraries/AP_HAL/board/sitl.h b/libraries/AP_HAL/board/sitl.h index aa2799f09a..a4b3bba71d 100644 --- a/libraries/AP_HAL/board/sitl.h +++ b/libraries/AP_HAL/board/sitl.h @@ -54,9 +54,7 @@ // allow for static semaphores #include #define HAL_Semaphore HALSITL::Semaphore - -#include -#define HAL_EventHandle AP_HAL::EventHandle +#define HAL_BinarySemaphore HALSITL::BinarySemaphore #endif #ifndef HAL_NUM_CAN_IFACES