Ken Van Hoeylandt c1f55429b6
SPI HAL implemented and more (#207)
- Cleanup unused code and move ISR/IRQ checks to `Kernel.h`
- Improve clang-format
- Fix for LVGL lock transfer: ensure lock isn't activate when changing the lock
- Implement SPI HAL
- Remove `initHardware` HAL configuration entry
- Fix `I2cScanner`: don't scan when port isn't started
2025-02-08 00:21:50 +01:00

64 lines
1.2 KiB
C++

/**
* @file mutex.h
* Mutex
*/
#pragma once
#include "Thread.h"
#include "RtosCompatSemaphore.h"
#include "Check.h"
#include "Lockable.h"
#include "kernel/Kernel.h"
#include <memory>
namespace tt {
/**
* Wrapper for FreeRTOS xSemaphoreCreateMutex and xSemaphoreCreateRecursiveMutex
* Cannot be used in IRQ mode (within ISR context)
*/
class Mutex final : public Lockable {
public:
enum class Type {
Normal,
Recursive,
};
private:
struct SemaphoreHandleDeleter {
void operator()(QueueHandle_t handleToDelete) {
assert(!kernel::isIsr());
vSemaphoreDelete(handleToDelete);
}
};
std::unique_ptr<std::remove_pointer_t<QueueHandle_t>, SemaphoreHandleDeleter> handle;
Type type;
public:
using Lockable::lock;
explicit Mutex(Type type = Type::Normal);
~Mutex() final = default;
/** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired.
* @param[in] timeout
* @return success result
*/
bool lock(TickType_t timeout) const final;
/** Attempt to unlock the mutex.
* @return success result
*/
bool unlock() const final;
/** @return the owner of the thread */
ThreadId getOwner() const;
};
} // namespace