56 lines
1.4 KiB
C
56 lines
1.4 KiB
C
#include "TMP1075.h"
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_TEMP ((int16_t)(59 / 0.0625f))
|
|
#define MIN_TEMP 0
|
|
#define MAX_FAILED_TEMP 12 //TODO: change value for compliance with the actual number of sensors
|
|
#warning "change value for compliance with the actual number of sensors"
|
|
|
|
int16_t tmp1075_temps[N_TEMP_SENSORS] = {0};
|
|
|
|
|
|
I2C_HandleTypeDef* hi2c;
|
|
|
|
HAL_StatusTypeDef tmp1075_init(I2C_HandleTypeDef* handle) {
|
|
hi2c = handle;
|
|
for (int i = 0; i < N_TEMP_SENSORS; i++) {
|
|
HAL_StatusTypeDef status = tmp1075_sensor_init(i);
|
|
if (status != HAL_OK) {
|
|
return status;
|
|
}
|
|
}
|
|
return HAL_OK;
|
|
}
|
|
|
|
|
|
HAL_StatusTypeDef tmp1075_measure() {
|
|
for (int i = 0; i < N_TEMP_SENSORS; i++) {
|
|
if (tmp1075_sensor_read(i, &tmp1075_temps[i]) != HAL_OK ||
|
|
(tmp1075_temps[i] & 0x000F) != 0) {
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
}
|
|
|
|
HAL_StatusTypeDef tmp1075_sensor_init(int n) {
|
|
uint16_t addr = (0b1000000 | n) << 1;
|
|
uint8_t data[] = {0};
|
|
return HAL_I2C_Master_Transmit(hi2c, addr, data, sizeof(data), 100);
|
|
}
|
|
|
|
HAL_StatusTypeDef tmp1075_sensor_read(int n, int16_t* res) {
|
|
uint16_t addr = (0b1000000 | n) << 1;
|
|
addr |= 1; // Read
|
|
uint8_t result[2];
|
|
HAL_StatusTypeDef status =
|
|
HAL_I2C_Master_Receive(hi2c, addr, result, sizeof(result), 5); //5ms timeout for failure (cascading faliure max = 30 * 5 = 150ms)
|
|
if (status == HAL_OK) {
|
|
*res = (result[0] << 8) | result[1];
|
|
}
|
|
return status;
|
|
}
|