Ken Van Hoeylandt 74127a5f6c
Add kernel drivers for SPI and UART and make locking APIs more consistent (#489)
- Add kernel support for SPI driver
- Add kernel support for UART driver
- Implemented ESP32 UART kernel driver
- Update existing UART-related code in Tactility to use new kernel driver
- Remove UART from tt::hal::Configuration
- Remove tt_hal_uart functionality but keep functions for now
- Update devicetrees for UART changes
- Kernel mutex and recursive mutex: improved locking API design
- Other kernel improvements
- Added device_exists_of_type() and device_find_by_name()
2026-02-07 21:28:11 +01:00

56 lines
1.3 KiB
C

// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <assert.h>
#include <stdbool.h>
#include <tactility/freertos/semphr.h>
#include <tactility/check.h>
#ifdef __cplusplus
extern "C" {
#endif
struct Mutex {
QueueHandle_t handle;
// TODO: Debugging functionality
};
inline static void mutex_construct(struct Mutex* mutex) {
mutex->handle = xSemaphoreCreateMutex();
}
inline static void mutex_destruct(struct Mutex* mutex) {
vPortAssertIfInISR();
vSemaphoreDelete(mutex->handle);
mutex->handle = NULL;
}
inline static void mutex_lock(struct Mutex* mutex) {
check(xPortInIsrContext() != pdTRUE);
xSemaphoreTake(mutex->handle, portMAX_DELAY);
}
inline static bool mutex_try_lock(struct Mutex* mutex, TickType_t timeout) {
check(xPortInIsrContext() != pdTRUE);
return xSemaphoreTake(mutex->handle, timeout) == pdTRUE;
}
inline static bool mutex_is_locked(struct Mutex* mutex) {
if (xPortInIsrContext() == pdTRUE) {
return xSemaphoreGetMutexHolderFromISR(mutex->handle) != NULL;
} else {
return xSemaphoreGetMutexHolder(mutex->handle) != NULL;
}
}
inline static void mutex_unlock(struct Mutex* mutex) {
check(xPortInIsrContext() != pdTRUE);
xSemaphoreGive(mutex->handle);
}
#ifdef __cplusplus
}
#endif