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

Shayan's Onboarding #97

Closed
wants to merge 7 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
68 changes: 45 additions & 23 deletions lm75bd/lm75bd.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <math.h>

/* LM75BD Registers (p.8) */
#define LM75BD_REG_CONF 0x01U /* Configuration Register (R/W) */
#define LM75BD_REG_TEMP 0x00U /* Temperature register */
#define LM75BD_REG_CONF 0x01U /* Configuration Register (R/W) */

error_code_t lm75bdInit(lm75bd_config_t *config) {
error_code_t lm75bdInit(lm75bd_config_t *config)
{
error_code_t errCode;

if (config == NULL) return ERR_CODE_INVALID_ARG;
if (config == NULL)
return ERR_CODE_INVALID_ARG;

RETURN_IF_ERROR_CODE(writeConfigLM75BD(config->devAddr, config->osFaultQueueSize, config->osPolarity,
config->osOperationMode, config->devOperationMode));
Expand All @@ -25,15 +28,32 @@ error_code_t lm75bdInit(lm75bd_config_t *config) {
return ERR_CODE_SUCCESS;
}

error_code_t readTempLM75BD(uint8_t devAddr, float *temp) {
/* Implement this driver function */

error_code_t readTempLM75BD(uint8_t devAddr, float *temp)
{
if (temp == NULL)
return ERR_CODE_INVALID_ARG;

error_code_t errCode;
uint8_t tempRegister = LM75BD_REG_TEMP;
uint8_t tempBuf[2] = {0};

RETURN_IF_ERROR_CODE(i2cSendTo(devAddr, &tempRegister, 1));
RETURN_IF_ERROR_CODE(i2cReceiveFrom(devAddr, tempBuf, 2));

// Combines the 2 bytes into one 16 bit integer
// and drops the 5 least siginificant bits
int16_t tempData = ((tempBuf[0] << 8) | tempBuf[1]);
tempData >>= 5;

*temp = (float)(tempData) * 0.125f;

return ERR_CODE_SUCCESS;
}

#define CONF_WRITE_BUFF_SIZE 2U
error_code_t writeConfigLM75BD(uint8_t devAddr, uint8_t osFaultQueueSize, uint8_t osPolarity,
uint8_t osOperationMode, uint8_t devOperationMode) {
uint8_t osOperationMode, uint8_t devOperationMode)
{
error_code_t errCode;

// Stores the register address and data to be written
Expand All @@ -44,21 +64,22 @@ error_code_t writeConfigLM75BD(uint8_t devAddr, uint8_t osFaultQueueSize, uint8_
buff[0] = LM75BD_REG_CONF;

uint8_t osFaltQueueRegData = 0;
switch (osFaultQueueSize) {
case 1:
osFaltQueueRegData = 0;
break;
case 2:
osFaltQueueRegData = 1;
break;
case 4:
osFaltQueueRegData = 2;
break;
case 6:
osFaltQueueRegData = 3;
break;
default:
return ERR_CODE_INVALID_ARG;
switch (osFaultQueueSize)
{
case 1:
osFaltQueueRegData = 0;
break;
case 2:
osFaltQueueRegData = 1;
break;
case 4:
osFaltQueueRegData = 2;
break;
case 6:
osFaltQueueRegData = 3;
break;
default:
return ERR_CODE_INVALID_ARG;
}

buff[1] |= (osFaltQueueRegData << 3);
Expand All @@ -67,7 +88,8 @@ error_code_t writeConfigLM75BD(uint8_t devAddr, uint8_t osFaultQueueSize, uint8_
buff[1] |= devOperationMode;

errCode = i2cSendTo(LM75BD_OBC_I2C_ADDR, buff, CONF_WRITE_BUFF_SIZE);
if (errCode != ERR_CODE_SUCCESS) return errCode;
if (errCode != ERR_CODE_SUCCESS)
return errCode;

return ERR_CODE_SUCCESS;
}
68 changes: 35 additions & 33 deletions 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 0b1001111

/* LM75BD Configuration Values */
#define LM75BD_DEV_OP_MODE_NORMAL 0x00U
Expand All @@ -32,7 +32,8 @@
* @param overTempThreshold Overtemperature shutdown threshold, in degrees Celsius
* @param hysteresisThreshold Hysteresis threshold, in degrees Celsius
*/
typedef struct {
typedef struct
{
uint8_t devAddr;
uint8_t osFaultQueueSize;
uint8_t osPolarity;
Expand All @@ -43,42 +44,43 @@ typedef struct {
} lm75bd_config_t;

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

/**
* @brief Initialize the LM75BD
*
* @param config Configuration struct for LM75BD
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t lm75bdInit(lm75bd_config_t *config);
/**
* @brief Initialize the LM75BD
*
* @param config Configuration struct for LM75BD
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t lm75bdInit(lm75bd_config_t *config);

/**
* @brief Read the temperature from the LM75BD
*
* @param temp Pointer to float to store the temperature in degrees Celsius
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t readTempLM75BD(uint8_t devAddr, float *temp);
/**
* @brief Read the temperature from the LM75BD
*
* @param temp Pointer to float to store the temperature in degrees Celsius
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t readTempLM75BD(uint8_t devAddr, float *temp);

/**
* @brief Write to the configuration register from the LM75BD
*
* @param devAddr I2C address of the LM75BD
* @param osFaultQueueSize Number of consecutive OS faults until OS output is activated (1, 2, 4, or 6)
* @param osPolarity OS output polarity, 0 = active low, 1 = active high
* @param osOperationMode OS output operation mode, 0 = comparator, 1 = interrupt
* @param devOperationMode Device operation mode, 0 = normal, 1 = shutdown
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t writeConfigLM75BD(uint8_t devAddr, uint8_t osFaultQueueSize, uint8_t osPolarity,
uint8_t osOperationMode, uint8_t devOperationMode);
/**
* @brief Write to the configuration register from the LM75BD
*
* @param devAddr I2C address of the LM75BD
* @param osFaultQueueSize Number of consecutive OS faults until OS output is activated (1, 2, 4, or 6)
* @param osPolarity OS output polarity, 0 = active low, 1 = active high
* @param osOperationMode OS output operation mode, 0 = comparator, 1 = interrupt
* @param devOperationMode Device operation mode, 0 = normal, 1 = shutdown
* @return ERR_CODE_SUCCESS if successful, error code otherwise
*/
error_code_t writeConfigLM75BD(uint8_t devAddr, uint8_t osFaultQueueSize, uint8_t osPolarity,
uint8_t osOperationMode, uint8_t devOperationMode);

/**
* @brief Handle an OS interrupt from the LM75BD
*/
void osHandlerLM75BD(void);
/**
* @brief Handle an OS interrupt from the LM75BD
*/
void osHandlerLM75BD(void);

#ifdef __cplusplus
}
Expand Down
80 changes: 63 additions & 17 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 All @@ -24,48 +25,93 @@ static uint8_t thermalMgrQueueStorageArea[THERMAL_MGR_QUEUE_LENGTH * THERMAL_MGR

static void thermalMgr(void *pvParameters);

void initThermalSystemManager(lm75bd_config_t *config) {
void initThermalSystemManager(lm75bd_config_t *config)
{
memset(&thermalMgrTaskBuffer, 0, sizeof(thermalMgrTaskBuffer));
memset(thermalMgrTaskStack, 0, sizeof(thermalMgrTaskStack));

thermalMgrTaskHandle = xTaskCreateStatic(
thermalMgr, "thermalMgr", THERMAL_MGR_STACK_SIZE,
config, 1, thermalMgrTaskStack, &thermalMgrTaskBuffer);
thermalMgr, "thermalMgr", THERMAL_MGR_STACK_SIZE,
config, 1, thermalMgrTaskStack, &thermalMgrTaskBuffer);

memset(&thermalMgrQueueBuffer, 0, sizeof(thermalMgrQueueBuffer));
memset(thermalMgrQueueStorageArea, 0, sizeof(thermalMgrQueueStorageArea));

thermalMgrQueueHandle = xQueueCreateStatic(
THERMAL_MGR_QUEUE_LENGTH, THERMAL_MGR_QUEUE_ITEM_SIZE,
thermalMgrQueueStorageArea, &thermalMgrQueueBuffer);

THERMAL_MGR_QUEUE_LENGTH, THERMAL_MGR_QUEUE_ITEM_SIZE,
thermalMgrQueueStorageArea, &thermalMgrQueueBuffer);
}

error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event) {
/* Send an event to the thermal manager queue */
error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event)
{
if (event == NULL)
return ERR_CODE_INVALID_ARG;

if (thermalMgrQueueHandle == NULL)
return ERR_CODE_INVALID_STATE;

if (xQueueSend(thermalMgrQueueHandle, event, 0) != pdTRUE)
return ERR_CODE_QUEUE_FULL;

return ERR_CODE_SUCCESS;
}

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

static void thermalMgr(void *pvParameters) {
/* Implement this task */
while (1) {
static void thermalMgr(void *pvParameters)
{
while (1)
{
thermal_mgr_event_t event;
error_code_t errCode;
float tempCelsius;

if (xQueueReceive(thermalMgrQueueHandle, &event, 5) == pdTRUE)
{
if (event.type == THERMAL_MGR_EVENT_MEASURE_TEMP_CMD)
{
LOG_IF_ERROR_CODE(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempCelsius));
if (errCode == ERR_CODE_SUCCESS)
{
addTemperatureTelemetry(tempCelsius);
}
}

if (event.type == THERMAL_MGR_EVENT_INTERRUPT)
{
LOG_IF_ERROR_CODE(readTempLM75BD(LM75BD_OBC_I2C_ADDR, &tempCelsius));
if (errCode == ERR_CODE_SUCCESS)
{
if (tempCelsius > LM75BD_DEFAULT_HYST_THRESH)
{
overTemperatureDetected();
}
else
{
safeOperatingConditions();
}
}
}
}
}
}

void addTemperatureTelemetry(float tempC) {
void addTemperatureTelemetry(float tempC)
{
printConsole("Temperature telemetry: %f deg C\n", tempC);
}

void overTemperatureDetected(void) {
void overTemperatureDetected(void)
{
printConsole("Over temperature detected!\n");
}

void safeOperatingConditions(void) {
void safeOperatingConditions(void)
{
printConsole("Returned to safe operating conditions!\n");
}
21 changes: 12 additions & 9 deletions services/thermal_mgr/thermal_mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
#include "lm75bd.h"
#include "errors.h"

typedef enum {
typedef enum
{
THERMAL_MGR_EVENT_MEASURE_TEMP_CMD,

THERMAL_MGR_EVENT_INTERRUPT,
} thermal_mgr_event_type_t;

typedef struct {
typedef struct
{
thermal_mgr_event_type_t type;
} thermal_mgr_event_t;

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

void initThermalSystemManager(lm75bd_config_t *config);
void initThermalSystemManager(lm75bd_config_t *config);

error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event);
error_code_t thermalMgrSendEvent(thermal_mgr_event_t *event);

void addTemperatureTelemetry(float tempC);
void addTemperatureTelemetry(float tempC);

void overTemperatureDetected(void);
void overTemperatureDetected(void);

void safeOperatingConditions(void);
void safeOperatingConditions(void);

#ifdef __cplusplus
}
Expand Down