HF-TMC51x0 Driver (TMC5130 & TMC5160) 0.1.0-dev
Hardware Agnostic C++ Driver for the TMC51x0 (TMC5130 & TMC5160)
Loading...
Searching...
No Matches
esp32_tmc_mutex.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include "freertos/FreeRTOS.h"
15#include "freertos/semphr.h"
16#include "esp_log.h"
17
25public:
26 Esp32TmcMutex() noexcept : handle_(xSemaphoreCreateMutex()) {
27 if (handle_ == nullptr) {
28 ESP_LOGE("Esp32TmcMutex", "Failed to create mutex");
29 }
30 }
31
32 ~Esp32TmcMutex() noexcept {
33 if (handle_ != nullptr) {
34 vSemaphoreDelete(handle_);
35 handle_ = nullptr;
36 }
37 }
38
39 // Non-copyable
40 Esp32TmcMutex(const Esp32TmcMutex&) = delete;
42
46 SemaphoreHandle_t native_handle() const noexcept { return handle_; }
47
51 bool is_valid() const noexcept { return handle_ != nullptr; }
52
53private:
54 SemaphoreHandle_t handle_;
55};
56
64public:
68 explicit TmcMutexGuard(Esp32TmcMutex& mutex) noexcept : mutex_(mutex), locked_(false) {
69 if (mutex_.is_valid()) {
70 SemaphoreHandle_t handle = mutex_.native_handle();
71 if (xSemaphoreTake(handle, portMAX_DELAY) == pdTRUE) {
72 locked_ = true;
73 }
74 }
75 }
76
80 ~TmcMutexGuard() noexcept { unlock(); }
81
85 void unlock() noexcept {
86 if (locked_ && mutex_.is_valid()) {
87 SemaphoreHandle_t handle = mutex_.native_handle();
88 xSemaphoreGive(handle);
89 locked_ = false;
90 }
91 }
92
96 bool is_locked() const noexcept { return locked_; }
97
98private:
101};
102
ESP32 FreeRTOS mutex wrapper for TMC51x0 driver.
Definition esp32_tmc_mutex.hpp:24
~Esp32TmcMutex() noexcept
Definition esp32_tmc_mutex.hpp:32
Esp32TmcMutex(const Esp32TmcMutex &)=delete
SemaphoreHandle_t native_handle() const noexcept
Get native FreeRTOS semaphore handle.
Definition esp32_tmc_mutex.hpp:46
Esp32TmcMutex() noexcept
Definition esp32_tmc_mutex.hpp:26
bool is_valid() const noexcept
Check if mutex is valid.
Definition esp32_tmc_mutex.hpp:51
Esp32TmcMutex & operator=(const Esp32TmcMutex &)=delete
SemaphoreHandle_t handle_
Definition esp32_tmc_mutex.hpp:54
RAII mutex guard for automatic lock/unlock.
Definition esp32_tmc_mutex.hpp:63
TmcMutexGuard(Esp32TmcMutex &mutex) noexcept
Lock mutex (blocks until acquired)
Definition esp32_tmc_mutex.hpp:68
Esp32TmcMutex & mutex_
Definition esp32_tmc_mutex.hpp:99
bool locked_
Definition esp32_tmc_mutex.hpp:100
~TmcMutexGuard() noexcept
Unlock mutex (automatically called on destruction)
Definition esp32_tmc_mutex.hpp:80
bool is_locked() const noexcept
Check if mutex is currently locked by this guard.
Definition esp32_tmc_mutex.hpp:96
void unlock() noexcept
Manually unlock mutex.
Definition esp32_tmc_mutex.hpp:85