HAL_ESP32: implement BinarySemaphore

This commit is contained in:
Andrew Tridgell 2023-12-30 08:31:21 +11:00
parent 8f8048e4cd
commit 5d1eb145cd
3 changed files with 63 additions and 0 deletions

View File

@ -15,6 +15,7 @@ class RCInput;
class Util;
class Semaphore;
class Semaphore_Recursive;
class BinarySemaphore;
class GPIO;
class DigitalSource;
class Storage;

View File

@ -73,3 +73,46 @@ bool Semaphore::check_owner()
{
return xSemaphoreGetMutexHolder((QueueHandle_t)handle) == xTaskGetCurrentTaskHandle();
}
/*
BinarySemaphore implementation
*/
BinarySemaphore::BinarySemaphore(bool initial_state)
{
_sem = xSemaphoreCreateBinary();
if (initial_state) {
xSemaphoreGive(_sem);
}
}
bool BinarySemaphore::wait(uint32_t timeout_us)
{
TickType_t ticks = pdMS_TO_TICKS(timeout_us / 1000U);
return xSemaphoreTake(_sem, ticks) == pdTRUE;
}
bool BinarySemaphore::wait_blocking()
{
return xSemaphoreTake(_sem, portMAX_DELAY) == pdTRUE;
}
void BinarySemaphore::signal()
{
xSemaphoreGive(_sem);
}
void BinarySemaphore::signal_ISR()
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_sem, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
BinarySemaphore::~BinarySemaphore(void)
{
if (_sem != nullptr) {
vSemaphoreDelete(_sem);
}
_sem = nullptr;
}

View File

@ -20,6 +20,8 @@
#include <AP_HAL/AP_HAL_Macros.h>
#include <AP_HAL/Semaphores.h>
#include "HAL_ESP32_Namespace.h"
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
class ESP32::Semaphore : public AP_HAL::Semaphore
{
@ -34,3 +36,20 @@ public:
protected:
void* handle;
};
class ESP32::BinarySemaphore : public AP_HAL::BinarySemaphore {
public:
BinarySemaphore(bool initial_state=false);
~BinarySemaphore(void);
CLASS_NO_COPY(BinarySemaphore);
bool wait(uint32_t timeout_us) override;
bool wait_blocking(void) override;
void signal(void) override;
void signal_ISR(void) override;
protected:
SemaphoreHandle_t _sem;
};