Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finn-Cullen-Onboarding-Challenge-Submission #194

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lm75bd/lm75bd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ 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;
}
FATCullen marked this conversation as resolved.
Show resolved Hide resolved
*/
int16_t convertedTemp = (readBuffer[0]<<8) | readBuffer[1];
*temp = (float)(convertedTemp >> 5) * 0.125;

return ERR_CODE_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion lm75bd/lm75bd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdint.h>

/* LM75BD I2C Device Address */
#define LM75BD_OBC_I2C_ADDR /* Define the address here */
#define LM75BD_OBC_I2C_ADDR 0x4FU

/* LM75BD Configuration Values */
#define LM75BD_DEV_OP_MODE_NORMAL 0x00U
Expand Down
40 changes: 37 additions & 3 deletions services/thermal_mgr/thermal_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "errors.h"
#include "lm75bd.h"
#include "console.h"
#include "logging.h"

#include <FreeRTOS.h>
#include <os_task.h>
Expand Down Expand Up @@ -42,19 +43,52 @@ 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(thermalMgrQueueHandle == NULL) return ERR_CODE_INVALID_QUEUE_HANDLE;

if(xQueueSend(thermalMgrQueueHandle, event, 0) != pdPASS){
return ERR_CODE_QUEUE_FULL;
}

return ERR_CODE_SUCCESS;
}

void osHandlerLM75BD(void) {
/* Implement this function */
thermal_mgr_event_t event;
event.type = THERMAL_MGR_EVENT_INTERRUPT;
thermalMgrSendEvent(&event);
}

static void thermalMgr(void *pvParameters) {
/* Implement this task */
thermal_mgr_event_t currentEvent;
while (1) {
if(xQueueReceive(thermalMgrQueueHandle, &currentEvent, portMAX_DELAY) == pdPASS){
if(currentEvent.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD){
float 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);
Yarik-Popov marked this conversation as resolved.
Show resolved Hide resolved
}

else LOG_ERROR_CODE(ERR_CODE_INVALID_QUEUE_MSG);
}

else LOG_ERROR_CODE(ERR_CODE_QUEUE_RECIEVE_FAIL);
}
}

Expand Down
1 change: 1 addition & 0 deletions services/thermal_mgr/thermal_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

typedef enum {
THERMAL_MGR_EVENT_MEASURE_TEMP_CMD,
THERMAL_MGR_EVENT_INTERRUPT

} thermal_mgr_event_type_t;

Expand Down
3 changes: 3 additions & 0 deletions sys/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ typedef enum {
ERR_CODE_MUTEX_TIMEOUT = 100,
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,
ERR_CODE_INVALID_INTERUPT = 201,

/* Logging errors */
ERR_CODE_BUFF_TOO_SMALL = 300,
Expand Down
Loading