Skip to content
Snippets Groups Projects
Commit 6da4644e authored by rahix's avatar rahix
Browse files

chore(max30001): Port to new mutex and hw-lock APIs


Using a FreeRTOS mutex directly is deprecated.  Replace it with a
`struct mutex`.  Similarly, the deprecated `hwlock_acquire_timeout()`
is replaced with `hwlock_acquire()`.

Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 605d5e56
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h"
#include "queue.h" #include "queue.h"
#include "api/interrupt-sender.h" #include "api/interrupt-sender.h"
...@@ -17,9 +16,7 @@ ...@@ -17,9 +16,7 @@
#include "modules/log.h" #include "modules/log.h"
#include "modules/modules.h" #include "modules/modules.h"
#include "modules/stream.h" #include "modules/stream.h"
#include "modules/mutex.h"
/* Ticks to wait when trying to acquire lock */
#define LOCK_WAIT pdMS_TO_TICKS(MAX30001_MUTEX_WAIT_MS)
/* Interrupt Pin */ /* Interrupt Pin */
static const gpio_cfg_t max30001_interrupt_pin = { static const gpio_cfg_t max30001_interrupt_pin = {
...@@ -36,8 +33,7 @@ static const gpio_cfg_t analog_switch = { ...@@ -36,8 +33,7 @@ static const gpio_cfg_t analog_switch = {
static TaskHandle_t max30001_task_id = NULL; static TaskHandle_t max30001_task_id = NULL;
/* MAX30001 Mutex */ /* MAX30001 Mutex */
static StaticSemaphore_t max30001_mutex_data; static struct mutex max30001_mutex = { 0 };
static SemaphoreHandle_t max30001_mutex = NULL;
/* Stream */ /* Stream */
static struct stream_info max30001_stream; static struct stream_info max30001_stream;
...@@ -54,15 +50,8 @@ int epic_max30001_enable_sensor(struct max30001_sensor_config *config) ...@@ -54,15 +50,8 @@ int epic_max30001_enable_sensor(struct max30001_sensor_config *config)
{ {
int result = 0; int result = 0;
result = hwlock_acquire_timeout(HWLOCK_SPI_ECG, portMAX_DELAY); mutex_lock(&max30001_mutex);
if (result < 0) { hwlock_acquire(HWLOCK_SPI_ECG);
return result;
}
if (xSemaphoreTake(max30001_mutex, LOCK_WAIT) != pdTRUE) {
result = -EBUSY;
goto out_free_spi;
}
struct stream_info *stream = &max30001_stream; struct stream_info *stream = &max30001_stream;
; ;
...@@ -97,9 +86,8 @@ int epic_max30001_enable_sensor(struct max30001_sensor_config *config) ...@@ -97,9 +86,8 @@ int epic_max30001_enable_sensor(struct max30001_sensor_config *config)
result = SD_MAX30001_ECG; result = SD_MAX30001_ECG;
out_free_both: out_free_both:
xSemaphoreGive(max30001_mutex);
out_free_spi:
hwlock_release(HWLOCK_SPI_ECG); hwlock_release(HWLOCK_SPI_ECG);
mutex_unlock(&max30001_mutex);
return result; return result;
} }
...@@ -107,15 +95,8 @@ int epic_max30001_disable_sensor(void) ...@@ -107,15 +95,8 @@ int epic_max30001_disable_sensor(void)
{ {
int result = 0; int result = 0;
result = hwlock_acquire_timeout(HWLOCK_SPI_ECG, portMAX_DELAY); mutex_lock(&max30001_mutex);
if (result < 0) { hwlock_acquire(HWLOCK_SPI_ECG);
return result;
}
if (xSemaphoreTake(max30001_mutex, LOCK_WAIT) != pdTRUE) {
result = -EBUSY;
goto out_free_spi;
}
struct stream_info *stream = &max30001_stream; struct stream_info *stream = &max30001_stream;
result = stream_deregister(SD_MAX30001_ECG, stream); result = stream_deregister(SD_MAX30001_ECG, stream);
...@@ -134,9 +115,8 @@ int epic_max30001_disable_sensor(void) ...@@ -134,9 +115,8 @@ int epic_max30001_disable_sensor(void)
result = 0; result = 0;
out_free_both: out_free_both:
xSemaphoreGive(max30001_mutex);
out_free_spi:
hwlock_release(HWLOCK_SPI_ECG); hwlock_release(HWLOCK_SPI_ECG);
mutex_unlock(&max30001_mutex);
return result; return result;
} }
...@@ -298,15 +278,8 @@ static int max30001_fetch_fifo(void) ...@@ -298,15 +278,8 @@ static int max30001_fetch_fifo(void)
{ {
int result = 0; int result = 0;
result = hwlock_acquire_timeout(HWLOCK_SPI_ECG, portMAX_DELAY); mutex_lock(&max30001_mutex);
if (result < 0) { hwlock_acquire(HWLOCK_SPI_ECG);
return result;
}
if (xSemaphoreTake(max30001_mutex, LOCK_WAIT) != pdTRUE) {
result = -EBUSY;
goto out_free_spi;
}
uint32_t ecgFIFO, readECGSamples, ETAG[32], status; uint32_t ecgFIFO, readECGSamples, ETAG[32], status;
int16_t ecgSample[32]; int16_t ecgSample[32];
...@@ -344,9 +317,8 @@ static int max30001_fetch_fifo(void) ...@@ -344,9 +317,8 @@ static int max30001_fetch_fifo(void)
max30001_handle_samples(ecgSample, readECGSamples); max30001_handle_samples(ecgSample, readECGSamples);
} }
xSemaphoreGive(max30001_mutex);
out_free_spi:
hwlock_release(HWLOCK_SPI_ECG); hwlock_release(HWLOCK_SPI_ECG);
mutex_unlock(&max30001_mutex);
return result; return result;
} }
...@@ -369,24 +341,15 @@ static void max300001_interrupt_callback(void *_) ...@@ -369,24 +341,15 @@ static void max300001_interrupt_callback(void *_)
void max30001_mutex_init(void) void max30001_mutex_init(void)
{ {
max30001_mutex = xSemaphoreCreateMutexStatic(&max30001_mutex_data); mutex_create(&max30001_mutex);
} }
void vMAX30001Task(void *pvParameters) void vMAX30001Task(void *pvParameters)
{ {
max30001_task_id = xTaskGetCurrentTaskHandle(); max30001_task_id = xTaskGetCurrentTaskHandle();
int lockret = hwlock_acquire_timeout(HWLOCK_SPI_ECG, portMAX_DELAY); mutex_lock(&max30001_mutex);
if (lockret < 0) { hwlock_acquire(HWLOCK_SPI_ECG);
LOG_CRIT("max30001", "Failed to acquire SPI lock!");
vTaskDelay(portMAX_DELAY);
}
/* Take Mutex during initialization, just in case */
if (xSemaphoreTake(max30001_mutex, 0) != pdTRUE) {
LOG_CRIT("max30001", "Failed to acquire MAX30001 mutex!");
vTaskDelay(portMAX_DELAY);
}
/* Install interrupt callback */ /* Install interrupt callback */
GPIO_Config(&max30001_interrupt_pin); GPIO_Config(&max30001_interrupt_pin);
...@@ -407,20 +370,15 @@ void vMAX30001Task(void *pvParameters) ...@@ -407,20 +370,15 @@ void vMAX30001Task(void *pvParameters)
GPIO_Config(&analog_switch); GPIO_Config(&analog_switch);
GPIO_OutClr(&analog_switch); // Wrist GPIO_OutClr(&analog_switch); // Wrist
xSemaphoreGive(max30001_mutex);
hwlock_release(HWLOCK_SPI_ECG); hwlock_release(HWLOCK_SPI_ECG);
mutex_unlock(&max30001_mutex);
/* ----------------------------------------- */ /* ----------------------------------------- */
while (1) { while (1) {
if (max30001_sensor_active) { if (max30001_sensor_active) {
int ret = max30001_fetch_fifo(); int ret = max30001_fetch_fifo();
if (ret == -EBUSY) { if (ret < 0) {
LOG_WARN(
"max30001", "Could not acquire mutex?"
);
continue;
} else if (ret < 0) {
LOG_ERR("max30001", "Unknown error: %d", -ret); LOG_ERR("max30001", "Unknown error: %d", -ret);
} }
} }
......
...@@ -112,12 +112,10 @@ void disp_forcelock(); ...@@ -112,12 +112,10 @@ void disp_forcelock();
void vBhi160Task(void *pvParameters); void vBhi160Task(void *pvParameters);
/* ---------- MAX30001 ----------------------------------------------------- */ /* ---------- MAX30001 ----------------------------------------------------- */
#define MAX30001_MUTEX_WAIT_MS 50
void vMAX30001Task(void *pvParameters); void vMAX30001Task(void *pvParameters);
void max30001_mutex_init(void); void max30001_mutex_init(void);
/* ---------- GPIO --------------------------------------------------------- */ /* ---------- GPIO --------------------------------------------------------- */
#define MAX30001_MUTEX_WAIT_MS 50
extern gpio_cfg_t gpio_configs[]; extern gpio_cfg_t gpio_configs[];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment