Files @ 860001bb7681
Branch filter:

Location: therm/max31865.c - annotation

matthewreed
Added initial RTD support
860001bb7681
a76c60e8cc43
b77b97fa501d
b77b97fa501d
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
b77b97fa501d
860001bb7681
b77b97fa501d
b77b97fa501d
b77b97fa501d
860001bb7681
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
b77b97fa501d
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
b77b97fa501d
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
b77b97fa501d
a76c60e8cc43
a76c60e8cc43
860001bb7681
860001bb7681
860001bb7681
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
860001bb7681
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
860001bb7681
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
860001bb7681
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
860001bb7681
860001bb7681
860001bb7681
a76c60e8cc43
a76c60e8cc43
235f584ead39
a76c60e8cc43
860001bb7681
860001bb7681
235f584ead39
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
860001bb7681
860001bb7681
860001bb7681
235f584ead39
a76c60e8cc43
a76c60e8cc43
860001bb7681
860001bb7681
860001bb7681
860001bb7681
a76c60e8cc43
a76c60e8cc43
a76c60e8cc43
#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, &reg, 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)
{
    ///////////////////////////////////
    // This is duplicated from MAX31855, update for MAX31865 registers/etc
    /////////////////////////////////// 

    // TODO: Set configuration register based on params in config.h (2-wire, 4-wire, etc RTD). This is register 0x00.
        // 2-wire RTC or 2-wire (duh) NTC thermistor will be the only options
        // Need option for resistance of RTD
        // These options should be stored in the set structure and should be menu-selectable
    // TODO: Read RTD msbs (0x01)
    // TODO: Read RTD LSBs (0x02)

    // 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, &regh, 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 temp_pre = (rxdatah[0]<<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] = {0x11};

        HAL_SPI_Transmit(hspi1, &reg, 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 decimal
        //temp_pre = temp_pre >> 1;
        uint16_t adc_count = temp_pre & 0x7FFF; //do some scaling?

        // Convert to Fahrenheit
        if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
        {
            status->temp = adc_count;
            //convert to fahrenheit
            status->temp += set->val.temp_offset;
        }

        // Use Celsius values
        else
        {
            //convert to celsius
            int32_t temp = (((int32_t) adc_count) / 32) - 256;
            status->temp = temp;
            status->temp += set->val.temp_offset;
        }
    }




}

// vim:softtabstop=4 shiftwidth=4 expandtab