From e232bfd1f77ebc28bd357895757330a1ae3a3a84 Mon Sep 17 00:00:00 2001 From: Megan Lai Date: Sun, 19 Nov 2023 12:36:26 -0500 Subject: [PATCH] locked and unlocked mutex when taking measurements --- Core/Src/main.c | 14 ++++++++------ Core/Src/monitor.c | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 9f03eb7e..c7c061d9 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -56,7 +56,7 @@ osMutexId_t mutex_id; osMutexAttr_t mutex; struct { - osMutexAttr_t mut; + osMutexId_t mut; I2C_HandleTypeDef i2c; } mutI2Cbus; @@ -94,8 +94,9 @@ void StartDefaultTask(void *argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void CreateMutex() { +osMutexId_t CreateMutex() { mutex_id = osMutexNew(&mutex); + return *mutex_id; } /* USER CODE END 0 */ @@ -115,8 +116,9 @@ int main(void) HAL_Init(); /* USER CODE BEGIN Init */ - - mutI2Cbus.mut = mutex; + + CreateMutex(); + mutI2Cbus.mut = mutex_id; mutI2Cbus.i2c = hi2c1; /* USER CODE END Init */ @@ -168,9 +170,9 @@ int main(void) defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); /* USER CODE BEGIN RTOS_THREADS */ - temp_monitor_handle = osThreadNew(vTempMonitor, &hi2c1, &temp_monitor_attributes); + temp_monitor_handle = osThreadNew(vTempMonitor, &mutI2Cbus, &temp_monitor_attributes); // replace with structs watchdog_monitor_handle = osThreadNew(vWatchdogMonitor, GPIOB, &watchdog_monitor_attributes); - imu_monitor_handle = osThreadNew(vIMUMonitor, &hi2c1, &imu_monitor_attributes); + imu_monitor_handle = osThreadNew(vIMUMonitor, &mutI2Cbus, &imu_monitor_attributes); //TODO: Get correct ADC/GPIO value pedals_monitor_handle = osThreadNew(vPedalsMonitor, &hadc1, &pedals_monitor_attributes); diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 0e88603c..83eefcd7 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -24,24 +24,31 @@ void vTempMonitor(void* pv_params) static onboard_temp_t sensor_data; fault_data_t fault_data = { .id = ONBOARD_TEMP_FAULT, .severity = DEFCON4 }; sht30_t temp_sensor; - I2C_HandleTypeDef* hi2c1; + osMutexId_t* i2c_mut; can_msg_t temp_msg = { .id = CANID_TEMP_SENSOR, .len = can_msg_len, .line = CAN_LINE_1, .data = { 0 } }; - hi2c1 = (I2C_HandleTypeDef*)pv_params; - temp_sensor.i2c_handle = hi2c1; - + //hi2c1 = (I2C_HandleTypeDef*)pv_params; + //temp_sensor.i2c_handle = hi2c1; + + osMutexAcquire(pv_params.mutex_id, NULL); // lock + if (sht30_init(&temp_sensor)) { fault_data.diag = "Init Failed"; osMessageQueuePut(fault_handle_queue, &fault_data, 0U, 0U); } + + osMutexRelease(pv_params.mutex_id); // unlock for (;;) { + osMutexAcquire(pv_params.mutex_id, NULL); // lock + /* Take measurement */ if (sht30_get_temp_humid(&temp_sensor)) { fault_data.diag = "Failed to get temp"; osMessageQueuePut(fault_handle_queue, &fault_data, 0U, 0U); } + osMutexRelease(pv_params.mutex_id); // unlock /* Run values through LPF of sample size */ sensor_data.temperature = (sensor_data.temperature + temp_sensor.temp) / num_samples; @@ -194,6 +201,7 @@ void vIMUMonitor(void *pv_params) } for(;;) { + osMutexAcquire(pv_params.mutex_id, NULL); // lock /* Take measurement */ if (lsm6dso_read_accel(&imu)) { fault_data.diag = "Failed to get IMU acceleration"; @@ -204,6 +212,7 @@ void vIMUMonitor(void *pv_params) fault_data.diag = "Failed to get IMU gyroscope"; osMessageQueuePut(fault_handle_queue, &fault_data , 0U, 0U); } + osMutexRelease(pv_params.mutex_id); // unlock /* Run values through LPF of sample size */ sensor_data.accel_x = (sensor_data.accel_x + imu.accel_data[0])