From 314335672361e266cc83713ec9545670e57aad92 Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:13:30 -0400 Subject: [PATCH 1/6] Thermal data reading --- lm75bd/lm75bd.c | 23 +++++++++++++++++++++++ lm75bd/lm75bd.h | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lm75bd/lm75bd.c b/lm75bd/lm75bd.c index 7d63da66..346b886f 100644 --- a/lm75bd/lm75bd.c +++ b/lm75bd/lm75bd.c @@ -27,6 +27,29 @@ error_code_t lm75bdInit(lm75bd_config_t *config) { error_code_t readTempLM75BD(uint8_t devAddr, float *temp) { /* Implement this driver function */ + if(temp == NULL) return ERR_CODE_INVALID_ARG; + + uint8_t writeBuffer[1] = {0}; + uint8_t readBuffer[2] = {0}; + + error_code_t errCode; + RETURN_IF_ERROR_CODE(i2cSendTo(devAddr, writeBuffer, sizeof(writeBuffer))); + RETURN_IF_ERROR_CODE(i2cReceiveFrom(devAddr, readBuffer, sizeof(readBuffer))); + +// Unsure if conversion from twos complement is neccessary, if we're only using this sensor to detect hight temp then it probably isn't? +// The code below works if we assume we wont recieve negative numbers, the active code includes twos complement conversions to cover edge cases +// uint16_t convertedTemp = (readBuffer[0] << 3) | (readBuffer[1] >> 5); +// *temp = convertedTemp * 0.125; + +// Conversion from twos complement, and conversion to Celsius + uint16_t convertedTemp = (readBuffer[0] << 3) | (readBuffer[1] >> 5); + if(readBuffer[0] & 0x80){ + convertedTemp = (convertedTemp ^ 0x07FF) + 1; + *temp = convertedTemp * -0.125; + } + else{ + *temp = convertedTemp * 0.125; + } return ERR_CODE_SUCCESS; } diff --git a/lm75bd/lm75bd.h b/lm75bd/lm75bd.h index 9a2356af..23fbdb49 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 0b1001111 /* LM75BD Configuration Values */ #define LM75BD_DEV_OP_MODE_NORMAL 0x00U From ee8cfad459426c146e99e7835e9386bf73cf62df Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:14:12 -0400 Subject: [PATCH 2/6] Implemented thermal data management --- services/thermal_mgr/thermal_mgr.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/services/thermal_mgr/thermal_mgr.c b/services/thermal_mgr/thermal_mgr.c index b41301ba..de27afe6 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.h" #include #include @@ -42,19 +43,40 @@ void initThermalSystemManager(lm75bd_config_t *config) { } error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) { - /* Send an event to the thermal manager queue */ + if(event == NULL) return ERR_CODE_INVALID_ARG; + + if(xQueueSend(thermalMgrQueueHandle, event, 1) != pdPASS){ + return ERR_CODE_QUEUE_FULL; + } return ERR_CODE_SUCCESS; } void osHandlerLM75BD(void) { - /* Implement this function */ + float temp; + if(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp) == ERR_CODE_SUCCESS){ + // Case high temp threshold exceeded + if(temp >= LM75BD_DEFAULT_OT_THRESH) overTemperatureDetected(); + // Case Hysteresis temp reached + else if(temp <= LM75BD_DEFAULT_HYST_THRESH) safeOperatingConditions(); + // Case neither + else LOG_ERROR_CODE(ERR_CODE_INVALID_INTERUPT); + } } static void thermalMgr(void *pvParameters) { - /* Implement this task */ + thermal_mgr_event_t currentEvent; while (1) { - + if(xQueueReceive(thermalMgrQueueHandle, ¤tEvent, portMAX_DELAY) == pdPASS){ + if(currentEvent.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD){ + float temp; + if(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp) == ERR_CODE_SUCCESS) addTemperatureTelemetry(temp); + } + + else LOG_ERROR_CODE(ERR_CODE_INVALID_QUEUE_MSG); + } + + else LOG_ERROR_CODE(ERR_CODE_QUEUE_RECIEVE_FAIL); } } From 080ef9f87a2c41e123745b3d791f0888e238b9c1 Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:14:50 -0400 Subject: [PATCH 3/6] Added extra error codes --- sys/errors.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sys/errors.h b/sys/errors.h index 975f34e9..adc69967 100644 --- a/sys/errors.h +++ b/sys/errors.h @@ -13,9 +13,11 @@ typedef enum { ERR_CODE_MUTEX_TIMEOUT = 100, ERR_CODE_QUEUE_FULL = 101, ERR_CODE_INVALID_QUEUE_MSG = 102, + ERR_CODE_QUEUE_RECIEVE_FAIL = 103, /* Driver errors */ ERR_CODE_I2C_TRANSFER_TIMEOUT = 200, + ERR_CODE_INVALID_INTERUPT = 201, /* Logging errors */ ERR_CODE_BUFF_TOO_SMALL = 300, From 4463335acaccfd1a01dce1cdae823313c2cce2a0 Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:18:22 -0400 Subject: [PATCH 4/6] Updated temp reading functions, simplified conversion to float --- lm75bd/lm75bd.c | 4 ++++ lm75bd/lm75bd.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lm75bd/lm75bd.c b/lm75bd/lm75bd.c index 346b886f..1bd6c652 100644 --- a/lm75bd/lm75bd.c +++ b/lm75bd/lm75bd.c @@ -42,6 +42,7 @@ error_code_t readTempLM75BD(uint8_t devAddr, float *temp) { // *temp = convertedTemp * 0.125; // Conversion from twos complement, and conversion to Celsius +/* uint16_t convertedTemp = (readBuffer[0] << 3) | (readBuffer[1] >> 5); if(readBuffer[0] & 0x80){ convertedTemp = (convertedTemp ^ 0x07FF) + 1; @@ -50,6 +51,9 @@ error_code_t readTempLM75BD(uint8_t devAddr, float *temp) { else{ *temp = convertedTemp * 0.125; } +*/ + int16_t convertedTemp = (readBuffer[0]<<8) | readBuffer[1]; + *temp = (float)(convertedTemp >> 5) * 0.125; return ERR_CODE_SUCCESS; } diff --git a/lm75bd/lm75bd.h b/lm75bd/lm75bd.h index 23fbdb49..f5461dd6 100644 --- a/lm75bd/lm75bd.h +++ b/lm75bd/lm75bd.h @@ -5,7 +5,7 @@ #include /* LM75BD I2C Device Address */ -#define LM75BD_OBC_I2C_ADDR 0b1001111 +#define LM75BD_OBC_I2C_ADDR 0x4FU /* LM75BD Configuration Values */ #define LM75BD_DEV_OP_MODE_NORMAL 0x00U From a3be2a023651e97617a11b4dc0cb2b6a724db7cc Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:18:59 -0400 Subject: [PATCH 5/6] Add files via upload --- sys/errors.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/errors.h b/sys/errors.h index adc69967..141379f3 100644 --- a/sys/errors.h +++ b/sys/errors.h @@ -14,6 +14,7 @@ typedef enum { ERR_CODE_QUEUE_FULL = 101, ERR_CODE_INVALID_QUEUE_MSG = 102, ERR_CODE_QUEUE_RECIEVE_FAIL = 103, + ERR_CODE_INVALID_QUEUE_HANDLE = 104, /* Driver errors */ ERR_CODE_I2C_TRANSFER_TIMEOUT = 200, From a15b71ccdbe913478261799e3291f4e1f66d175c Mon Sep 17 00:00:00 2001 From: FATCullen <98052707+FATCullen@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:20:28 -0400 Subject: [PATCH 6/6] Updated thermal data management Now returns all non-success error codes, OS handler now sends event instead of handling interrupts itself --- services/thermal_mgr/thermal_mgr.c | 36 ++++++++++++++++++++---------- services/thermal_mgr/thermal_mgr.h | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/services/thermal_mgr/thermal_mgr.c b/services/thermal_mgr/thermal_mgr.c index de27afe6..9ad2ae51 100644 --- a/services/thermal_mgr/thermal_mgr.c +++ b/services/thermal_mgr/thermal_mgr.c @@ -44,8 +44,9 @@ void initThermalSystemManager(lm75bd_config_t *config) { error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) { if(event == NULL) return ERR_CODE_INVALID_ARG; + if(thermalMgrQueueHandle == NULL) return ERR_CODE_INVALID_QUEUE_HANDLE; - if(xQueueSend(thermalMgrQueueHandle, event, 1) != pdPASS){ + if(xQueueSend(thermalMgrQueueHandle, event, 0) != pdPASS){ return ERR_CODE_QUEUE_FULL; } @@ -53,15 +54,9 @@ error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) { } void osHandlerLM75BD(void) { - float temp; - if(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp) == ERR_CODE_SUCCESS){ - // Case high temp threshold exceeded - if(temp >= LM75BD_DEFAULT_OT_THRESH) overTemperatureDetected(); - // Case Hysteresis temp reached - else if(temp <= LM75BD_DEFAULT_HYST_THRESH) safeOperatingConditions(); - // Case neither - else LOG_ERROR_CODE(ERR_CODE_INVALID_INTERUPT); - } + thermal_mgr_event_t event; + event.type = THERMAL_MGR_EVENT_INTERRUPT; + thermalMgrSendEvent(&event); } static void thermalMgr(void *pvParameters) { @@ -70,12 +65,29 @@ static void thermalMgr(void *pvParameters) { if(xQueueReceive(thermalMgrQueueHandle, ¤tEvent, portMAX_DELAY) == pdPASS){ if(currentEvent.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD){ float temp; - if(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp) == ERR_CODE_SUCCESS) addTemperatureTelemetry(temp); + error_code_t errCode = readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp); + // Check if temp read is successful + if(errCode != ERR_CODE_SUCCESS) LOG_ERROR_CODE(errCode); + // Send telemetry + else addTemperatureTelemetry(temp); + } + + else if(currentEvent.type == THERMAL_MGR_EVENT_INTERRUPT){ + float temp; + error_code_t errCode = readTempLM75BD(LM75BD_OBC_I2C_ADDR, &temp); + // Check that temp read is successful + if(errCode != ERR_CODE_SUCCESS) LOG_ERROR_CODE(errCode); + // Check if over temp interrupt + else if(temp >= LM75BD_DEFAULT_OT_THRESH) overTemperatureDetected(); + // Check if hysteresis interrupt + else if(temp <= LM75BD_DEFAULT_HYST_THRESH) safeOperatingConditions(); + // Check if neither + else LOG_ERROR_CODE(ERR_CODE_INVALID_INTERUPT); } else LOG_ERROR_CODE(ERR_CODE_INVALID_QUEUE_MSG); } - + else LOG_ERROR_CODE(ERR_CODE_QUEUE_RECIEVE_FAIL); } } diff --git a/services/thermal_mgr/thermal_mgr.h b/services/thermal_mgr/thermal_mgr.h index 466b3e26..19e77d86 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_INTERRUPT } thermal_mgr_event_type_t;