Files
@ 752fd27f607a
Branch filter:
Location: therm-ng/lib/max31856/max31856.c
752fd27f607a
3.2 KiB
text/plain
Basic code cleanup
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 148 149 150 151 152 153 154 155 | //
// MAX31856: Driver to configure and read temperature from the MAX31856 thermoouple-to-Digital IC
//
#include "max31856.h"
#include "states.h"
#include "error.h"
// Private variables
static float temp_latest = 0.0;
static float temp_avg = 0.0;
SPI_HandleTypeDef* spiport;
static GPIO_TypeDef* csport;
static uint32_t cspin;
// Private prototypes
static void __cs_assert(void);
static void __cs_deassert(void);
static void __write_reg(uint8_t reg, uint8_t data);
static void __read_reg(uint8_t reg, uint8_t* rxbuf, uint8_t len);
// Initialize the MAX31856 driver
void max31856_init(SPI_HandleTypeDef* spi_port, GPIO_TypeDef* cs_port, uint32_t cs_pin, uint32_t sensor_type)
{
// Set CS pin references
csport = cs_port;
cspin = cs_pin;
// Set SPI port reference
spiport = spi_port;
// Configure the CS pin for output
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = cs_pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(cs_port, &GPIO_InitStruct);
// MAX31856
// - set to continuous conversion mode
// - probably no filtering, we'll do that on this side of things
// - set up to read the typical open/short faults but not the high/low alarms
// TODO: Enable open/short detection
// Enables auto conv
__write_reg(MAX31856_CR0_REG, MAX31856_CR0_AUTOCONVERT); // MAX31856_CR0_OCFAULT0
// Averaging set to 1 sample, TC type set to K
__write_reg(MAX31856_CR1_REG, MAX31856_TCTYPE_K);
// sensor type - could we just mask bits off? maybe optimize the enum for this
}
// Pull reading from the MAX31856 IC
float max31856_process(void)
{
uint8_t tempbuf[3];
__read_reg(MAX31856_LTCBH_REG, tempbuf, 3);
volatile int32_t temp24 = tempbuf[0] << 16 | tempbuf[1] << 8 | tempbuf[2];
if (temp24 & 0x800000) {
temp24 |= 0xFF000000; // fix sign
}
temp24 >>= 5; // bottom 5 bits are unused
float tempfloat = temp24;
tempfloat *= 0.0078125;
temp_latest = tempfloat;
return tempfloat;
// Read temperature from the MAX31856 (approx 10hz optimally)
// uint8_t data[] = {0,0,0,0};
// HAL_SPI_Transmit(spiport, data, 1, 100);
}
// Return latest temperature reading (unaveraged, deg C)
float max31856_latest_temp(void)
{
return temp_latest;
}
// Return average temperature reading (deg C)
float max31856_avg_temp(void)
{
return temp_latest;
}
static void __write_reg(uint8_t reg, uint8_t data)
{
// Set write bit
reg |= MAX31856_WRITE_BIT;
uint8_t outarr[2] = {reg, data};
uint8_t dummyrx[2];
// Assert the bus
__cs_assert();
// Write data
volatile HAL_StatusTypeDef res = HAL_SPI_TransmitReceive(spiport, outarr, dummyrx, 2, 100);
// Release the bus
__cs_deassert();
}
static void __read_reg(uint8_t reg, uint8_t* rxbuf, uint8_t len)
{
// Transmit buffer only uses first item for reg addr
uint8_t regarr[1];
regarr[0] = reg;
uint8_t dummyrx[12] = {0};
uint8_t dummytx[12] = {0};
// Assert the bus
__cs_assert();
// Send address
HAL_SPI_TransmitReceive(spiport, regarr, dummyrx, 1, 100);
// Receive data
HAL_SPI_TransmitReceive(spiport, dummytx, rxbuf, len, 100);
// Release bus
__cs_deassert();
}
static void __cs_assert(void)
{
HAL_GPIO_WritePin(csport, cspin, 0);
}
static void __cs_deassert(void)
{
HAL_GPIO_WritePin(csport, cspin, 1);
}
|