Verified Commit 52fc4af3 authored by Rahix's avatar Rahix
Browse files

feat(epicardium): Add hardware/peripheral-lock module


Signed-off-by: Rahix's avatarRahix <rahix@rahix.de>
parent 315bf783
......@@ -150,6 +150,11 @@ int hardware_early_init(void)
*/
stream_init();
/*
* Hardware/Peripheral Locks
*/
hwlock_init();
return 0;
}
......
#include "modules/log.h"
#include "modules/modules.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include <errno.h>
static StaticSemaphore_t hwlock_mutex_data[_HWLOCK_MAX];
static SemaphoreHandle_t hwlock_mutex[_HWLOCK_MAX];
/* Which task is holding the lock */
static TaskHandle_t hwlock_tasks[_HWLOCK_MAX];
void hwlock_init(void)
{
for (int i = 0; i < _HWLOCK_MAX; i++) {
hwlock_mutex[i] =
xSemaphoreCreateMutexStatic(&hwlock_mutex_data[i]);
}
}
int hwlock_acquire(enum hwlock_periph p, TickType_t wait)
{
if (p >= _HWLOCK_MAX) {
return -EINVAL;
}
if (xSemaphoreTake(hwlock_mutex[p], wait) != pdTRUE) {
LOG_WARN("hwlock", "Lock %u is busy.", p);
return -EBUSY;
}
hwlock_tasks[p] = xTaskGetCurrentTaskHandle();
return 0;
}
int hwlock_release(enum hwlock_periph p)
{
int ret = 0;
if (p >= _HWLOCK_MAX) {
return -EINVAL;
}
if (hwlock_tasks[p] != xTaskGetCurrentTaskHandle()) {
LOG_ERR("hwlock",
"Lock %u is released by task \"%s\" while it was acquired by \"%s\"",
p,
pcTaskGetName(NULL),
pcTaskGetName(hwlock_tasks[p]));
ret = -EACCES;
}
if (xSemaphoreGive(hwlock_mutex[p]) != pdTRUE) {
LOG_ERR("hwlock", "Lock %u not released correctly.", p);
ret = -EINVAL;
}
return ret;
}
......@@ -5,6 +5,7 @@ module_sources = files(
'fileops.c',
'gpio.c',
'hardware.c',
'hw-lock.c',
'leds.c',
'lifecycle.c',
'light_sensor.c',
......
......@@ -37,6 +37,17 @@ void vBleTask(void *pvParameters);
bool ble_shall_start(void);
void ble_uart_write(uint8_t *pValue, uint8_t len);
/* ---------- Hardware (Peripheral) Locks ---------------------------------- */
void hwlock_init(void);
enum hwlock_periph {
HWLOCK_I2C = 0,
_HWLOCK_MAX,
};
int hwlock_acquire(enum hwlock_periph p, TickType_t wait);
int hwlock_release(enum hwlock_periph p);
/* ---------- Display ------------------------------------------------------ */
/* Forces an unlock of the display. Only to be used in Epicardium */
void disp_forcelock();
......
......@@ -45,7 +45,13 @@ void vPmicTask(void *pvParameters)
MAX77650_setSFT_RST(0x2);
}
while (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(500)) < 0) {
LOG_WARN("pmic", "Failed to acquire I2C. Retrying ...");
vTaskDelay(pdMS_TO_TICKS(500));
}
uint8_t int_flag = MAX77650_getINT_GLBL();
hwlock_release(HWLOCK_I2C);
if (int_flag & MAX77650_INT_nEN_F) {
/* Button was pressed */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment