From 91111d9110a908bb8efceae7533500ac1a3bf1fc Mon Sep 17 00:00:00 2001 From: Seth Winton Date: Sat, 2 Nov 2024 19:17:27 -0400 Subject: [PATCH] Seth Onboarding --- lm75bd/lm75bd.c | 20 ++++++++++++++- lm75bd/lm75bd.h | 2 +- services/thermal_mgr/thermal_mgr.c | 41 +++++++++++++++++++++++++----- services/thermal_mgr/thermal_mgr.h | 1 + 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/lm75bd/lm75bd.c b/lm75bd/lm75bd.c index 7d63da66..7c3cca24 100644 --- a/lm75bd/lm75bd.c +++ b/lm75bd/lm75bd.c @@ -9,6 +9,7 @@ /* LM75BD Registers (p.8) */ #define LM75BD_REG_CONF 0x01U /* Configuration Register (R/W) */ +#define LM75BD_REG_TEMP 0x00 /* Temperature register*/ error_code_t lm75bdInit(lm75bd_config_t *config) { error_code_t errCode; @@ -26,7 +27,24 @@ error_code_t lm75bdInit(lm75bd_config_t *config) { } error_code_t readTempLM75BD(uint8_t devAddr, float *temp) { - /* Implement this driver function */ + error_code_t errCode; + uint8_t tempReg = LM75BD_REG_TEMP; + errCode = i2cSendTo(devAddr, &tempReg, 1); /* Select temperature register*/ + if (errCode != ERR_CODE_SUCCESS) return errCode; + + uint8_t buff[2] = {0}; + errCode = i2cReceiveFrom(devAddr, buff, 2); /* Read temp data */ + if (errCode != ERR_CODE_SUCCESS) return errCode; + + uint16_t val = ((buff[0] << 3) + (buff[1] >> 5)); /* Combine MSB and LSB */ + if (val >> 10) { + /* Negative */ + *temp = ((~val + 1) & 0x7FF) * -0.125; + } + else { + /* Positive */ + *temp = val * 0.125; + } return ERR_CODE_SUCCESS; } diff --git a/lm75bd/lm75bd.h b/lm75bd/lm75bd.h index 9a2356af..92b15bc6 100644 --- a/lm75bd/lm75bd.h +++ b/lm75bd/lm75bd.h @@ -5,7 +5,7 @@ #include /* LM75BD I2C Device Address */ -#define LM75BD_OBC_I2C_ADDR /* Define the address here */ +#define LM75BD_OBC_I2C_ADDR 0x4F /* LM75BD Configuration Values */ #define LM75BD_DEV_OP_MODE_NORMAL 0x00U diff --git a/services/thermal_mgr/thermal_mgr.c b/services/thermal_mgr/thermal_mgr.c index b41301ba..60b446dc 100644 --- a/services/thermal_mgr/thermal_mgr.c +++ b/services/thermal_mgr/thermal_mgr.c @@ -2,6 +2,7 @@ #include "errors.h" #include "lm75bd.h" #include "console.h" +#include "logging/logging.h" #include #include @@ -42,20 +43,46 @@ void initThermalSystemManager(lm75bd_config_t *config) { } error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) { - /* Send an event to the thermal manager queue */ - + // Send an event to the thermal manager queue + if (xQueueSend(thermalMgrQueueHandle, event, 1000) != pdTRUE) { + return ERR_CODE_QUEUE_FULL; + } return ERR_CODE_SUCCESS; } void osHandlerLM75BD(void) { - /* Implement this function */ + // Send event to handle OS interrupt + thermal_mgr_event_t event; + event.type = THERMAL_MGR_EVENT_HANDLE_OS_CMD; + if (xQueueSendFromISR(thermalMgrQueueHandle, &event, NULL) != pdTRUE) { + LOG_ERROR_CODE(ERR_CODE_QUEUE_FULL); + } } static void thermalMgr(void *pvParameters) { - /* Implement this task */ - while (1) { - - } + while (1) { + if (xQueueReceive(thermalMgrQueueHandle, pvParameters, 0) == pdTRUE) { + thermal_mgr_event_t event = *(thermal_mgr_event_t *) pvParameters; + + // Handle event received from queue + if (event.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD) { + float temp; + readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp); + addTemperatureTelemetry(temp); + } + else if (event.type == THERMAL_MGR_EVENT_HANDLE_OS_CMD) { + float temp; + readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp); + if (temp > LM75BD_DEFAULT_HYST_THRESH) { + overTemperatureDetected(); + } + else { + safeOperatingConditions(); + } + + } + } + } } void addTemperatureTelemetry(float tempC) { diff --git a/services/thermal_mgr/thermal_mgr.h b/services/thermal_mgr/thermal_mgr.h index 466b3e26..85a3517d 100644 --- a/services/thermal_mgr/thermal_mgr.h +++ b/services/thermal_mgr/thermal_mgr.h @@ -5,6 +5,7 @@ typedef enum { THERMAL_MGR_EVENT_MEASURE_TEMP_CMD, + THERMAL_MGR_EVENT_HANDLE_OS_CMD } thermal_mgr_event_type_t;