Skip to content

Commit

Permalink
Resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elsaigh committed Oct 31, 2024
1 parent cc50f5b commit f18f8a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
39 changes: 19 additions & 20 deletions lm75bd/lm75bd.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,27 @@ error_code_t lm75bdInit(lm75bd_config_t *config) {
error_code_t readTempLM75BD(uint8_t devAddr, float *temp) {
/* Implement this driver function */

uint8_t temp_pointer_byte = 0x00U; // selects temp register at 00 LSB
uint8_t *send_buf = &temp_pointer_byte;

i2cSendTo(devAddr, send_buf, 1);

uint8_t receive_buf[2];
i2cReceiveFrom(devAddr, receive_buf, 2);
uint16_t MSByte = (uint16_t) receive_buf[0];
uint16_t LSByte = (uint16_t) receive_buf[1];

int16_t temp_reg_val = (MSByte << 3) + (LSByte >> 5);
// if 11 bit is set, sign is -ve, so convert the 11 bit val to its equivalent 16 bit val
// otherwise, sign is +ve, and the 11 bit val will be the same in 16 bits
if (temp_reg_val & 0x0400U) {
// by simply setting everything above the 11 bits to 1 (top 5 bits here)
temp_reg_val |= 0xF800U;
error_code_t errCode;

if (temp == NULL) {
return ERR_CODE_INVALID_ARG;
}
printf("Temperature Register Value: %i\n", temp_reg_val);

uint8_t tempPointerByte = 0x00U; // selects temp register at 00 LSB

uint8_t *sendBuf = &tempPointerByte;
RETURN_IF_ERROR_CODE(i2cSendTo(devAddr, sendBuf, sizeof(sendBuf)));

uint8_t receiveBuf[2] = {0};
RETURN_IF_ERROR_CODE(i2cReceiveFrom(devAddr, receiveBuf, sizeof(receiveBuf)));

uint16_t MSByte = (uint16_t) receiveBuf[0];
uint16_t LSByte = (uint16_t) receiveBuf[1];

int16_t tempRegVal = ((MSByte << 8) | LSByte) >> 5;

float final_temp = ((float) temp_reg_val) * 0.125f;
printf("Final Temperature: %f\n", final_temp);
*temp = final_temp;
float finalTemp = ((float) tempRegVal) * 0.125f;
*temp = finalTemp;

return ERR_CODE_SUCCESS;
}
Expand Down
30 changes: 22 additions & 8 deletions services/thermal_mgr/thermal_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ void initThermalSystemManager(lm75bd_config_t *config) {

error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) {
/* Send an event to the thermal manager queue */
xQueueSend(thermalMgrQueueHandle, event, (TickType_t) 10);

if (thermalMgrQueueHandle == NULL || event == NULL) {
return ERR_CODE_INVALID_ARG;
}

xQueueSend(thermalMgrQueueHandle, event, (TickType_t) 0);

return ERR_CODE_SUCCESS;
}
Expand All @@ -59,26 +64,35 @@ void osHandlerLM75BD(void) {

static void thermalMgr(void *pvParameters) { // what is pvParameters used for?
/* Implement this task */

error_code_t errCode;

thermal_mgr_event_t queueEventItemBuffer = {0};

while (1) {

if (xQueueReceive(thermalMgrQueueHandle, &queueEventItemBuffer, (TickType_t) 10) == pdPASS) {

float tempC;
readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempC);
if (xQueueReceive(thermalMgrQueueHandle, &queueEventItemBuffer, (TickType_t) portMAX_DELAY) == pdPASS) {

if (queueEventItemBuffer.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD) {

float tempC;
LOG_IF_ERROR_CODE(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempC));

addTemperatureTelemetry(tempC);

} else if (queueEventItemBuffer.type == THERMAL_MGR_EVENT_OS_CMD) {
if (tempC > 75.0) {

float tempC;
LOG_IF_ERROR_CODE(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempC));

if (tempC > LM75BD_DEFAULT_HYST_THRESH) {
overTemperatureDetected();
} else {
safeOperatingConditions();
}
// To reset the OS interrupt
readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempC);

} else {
LOG_ERROR_CODE(ERR_CODE_INVALID_QUEUE_MSG);
}
}
}
Expand Down

0 comments on commit f18f8a0

Please sign in to comment.