Files
@ 9294a623e8e5
Branch filter:
Location: therm/max31865.c
9294a623e8e5
4.0 KiB
text/plain
Added support for both heaters and coolers as well as thermostatic control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include "max31855.h"
// Registers
#define MAX31865_REG_CONFIG 0x00
#define MAX31865_REG_RTD_MSB 0x01
#define MAX31865_REG_RTD_LSB 0x02
#define MAX31865_REG_HFAULT_THRESH_MSB 0x03
#define MAX31865_REG_HFAULT_THRESH_LSB 0x04
#define MAX31865_REG_LFAULT_THRESH_MSB 0x05
#define MAX31865_REG_LFAULT_THRESH_LSB 0x06
#define MAX31865_REG_FAULTSTATUS 0x07
#define MAX31865_REG_WRITE_MODIFIER 0x80
// Fields
#define MAX31865_CONF_VBIAS (1<<7)
#define MAX31865_CONF_AUTO_CONVERT (1<<6)
#define MAX31865_CONF_1SHOT (1<<5)
#define MAX31865_CONF_3WIRE (1<<4)
#define MAX31865_CONF_FAULT_NOACTION 0b0000
#define MAX31865_CONF_FAULT_AUTODELAY 0b0100
#define MAX31865_CONF_FAULT_MANUALELAY1 0b1000
#define MAX31865_CONF_FAULT_MANUALELAY2 0b1100
#define MAX31865_CONF_FAULT_CLEAR (1<<1)
#define MAX31865_CONF_50_60HZ_FILTER (1<<0)
void max31865_config(SPI_HandleTypeDef* hspi1)
{
uint8_t config = 0x00;
config |= MAX31865_CONF_VBIAS;
config |= MAX31865_CONF_AUTO_CONVERT;
config |= MAX31865_CONF_FAULT_CLEAR;
uint8_t reg = (MAX31865_REG_WRITE_MODIFIER | MAX31865_REG_CONFIG);
// Assert CS
HAL_GPIO_WritePin(MAX_CS, 0);
HAL_SPI_Transmit(hspi1, ®, 1, 100);
HAL_SPI_Transmit(hspi1, &config, 1, 100);
// Release CS
HAL_GPIO_WritePin(MAX_CS, 1);
}
void max31865_clear_errors(SPI_HandleTypeDef* hspi1) {
max31865_config(hspi1);
}
// Grab temperature reading from MAX31865
void max31865_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status)
{
// TODO: Set RTD ref resistance in set struct
// Assert CS
HAL_GPIO_WritePin(MAX_CS, 0);
uint8_t regh = MAX31865_REG_RTD_MSB;
uint8_t rxdatah[1] = {0x00};
uint8_t rxdatal[1] = {0x00};
HAL_SPI_Transmit(hspi1, ®h, 1, 100);
HAL_SPI_Receive(hspi1, rxdatah, 1, 100);
HAL_SPI_Receive(hspi1, rxdatal, 1, 100);
// Release CS
HAL_GPIO_WritePin(MAX_CS, 1);
// Assemble data array into one var
uint16_t adc_count = ((rxdatah[0] & 0x7F) << 8) | rxdatal[0];
if((rxdatah[0] & 0x80) && !set->val.ignore_error) {
// Assert CS
HAL_GPIO_WritePin(MAX_CS, 0);
uint8_t reg = MAX31865_REG_FAULTSTATUS;
uint8_t data[1] = {0x00};
HAL_SPI_Transmit(hspi1, ®, 1, 100);
HAL_SPI_Receive(hspi1, data, 1, 100);
// Release CS
HAL_GPIO_WritePin(MAX_CS, 1);
status->error_code = data[0];
HAL_Delay(400); // FIXME: remove?
status->state_resume = status->state;
status->state = STATE_TC_ERROR;
status->temp = 0;
status->temp_frac = 0;
}
else
{
// Convert to Fahrenheit
if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
{
//use all fixed point math!
//convert adc to resistance
//Rrtd = adc / range * Rref
status->temp = (int32_t) adc_count * 48600L;
status->temp /= 32768L;
//resistance to temp
//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
status->temp = ((status->temp) - 10000L) * (39200L - 3200L) / (17586L - 10000L) + 3200L;
//grab the fraction
status->temp_frac = (status->temp / 10) % 10L;
//scale back to degrees
status->temp = status->temp / 100L;
//add in the offset
status->temp += set->val.temp_offset;
}
// Convert to Celsius
else
{
//use all fixed point math!
//convert adc to resistance
//Rrtd = adc / range * Rref
status->temp = (int32_t) adc_count * 48600L;
status->temp /= 32768L;
//resistance to temp
//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
status->temp = (((status->temp) - 10000L) * (20000L- 0L)) / (17586L - 10000L) + 0L;
//grab the fraction
status->temp_frac = (status->temp / 10) % 100L;
//scale back to degrees
status->temp = status->temp / 100L;
//add in the offset
status->temp += set->val.temp_offset;
}
}
}
// vim:softtabstop=4 shiftwidth=4 expandtab
|