AP_HAL: implement BinarySemaphore

and removed event handles
This commit is contained in:
Andrew Tridgell 2023-12-30 08:29:46 +11:00
parent d1fcb76c9f
commit a52c71f380
11 changed files with 33 additions and 90 deletions

View File

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

View File

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

View File

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

View File

@ -1,33 +0,0 @@
#include "EventHandle.h"
#include <AP_HAL/AP_HAL.h>
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;
}

View File

@ -1,44 +0,0 @@
#pragma once
#include "AP_HAL_Namespace.h"
#include <stdint.h>
#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;
};

View File

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

View File

@ -63,9 +63,7 @@
// allow for static semaphores
#include <AP_HAL_ChibiOS/Semaphores.h>
#define HAL_Semaphore ChibiOS::Semaphore
#include <AP_HAL/EventHandle.h>
#define HAL_EventHandle AP_HAL::EventHandle
#define HAL_BinarySemaphore ChibiOS::BinarySemaphore
#endif
/* string names for well known SPI devices */

View File

@ -16,3 +16,4 @@
#define HAL_HAVE_SAFETY_SWITCH 1
#define HAL_Semaphore Empty::Semaphore
#define HAL_BinarySemaphore Empty::BinarySemaphore

View File

@ -39,6 +39,7 @@
// allow for static semaphores
#include <AP_HAL_ESP32/Semaphores.h>
#define HAL_Semaphore ESP32::Semaphore
#define HAL_BinarySemaphore ESP32::BinarySemaphore
#endif
#define HAL_NUM_CAN_IFACES 0

View File

@ -398,8 +398,7 @@
#ifdef __cplusplus
#include <AP_HAL_Linux/Semaphores.h>
#define HAL_Semaphore Linux::Semaphore
#include <AP_HAL/EventHandle.h>
#define HAL_EventHandle AP_HAL::EventHandle
#define HAL_BinarySemaphore Linux::BinarySemaphore
#endif
#ifndef HAL_HAVE_HARDWARE_DOUBLE

View File

@ -54,9 +54,7 @@
// allow for static semaphores
#include <AP_HAL_SITL/Semaphores.h>
#define HAL_Semaphore HALSITL::Semaphore
#include <AP_HAL/EventHandle.h>
#define HAL_EventHandle AP_HAL::EventHandle
#define HAL_BinarySemaphore HALSITL::BinarySemaphore
#endif
#ifndef HAL_NUM_CAN_IFACES