Changeset - a76c60e8cc43
[Not reviewed]
cortex-f0
0 0 2
Ethan Zonca - 9 years ago 2015-08-23 19:04:19
ez@ethanzonca.com
Add MAX31865 RTD reader stub code
2 files changed with 82 insertions and 0 deletions:
0 comments (0 inline, 0 general)
max31865.c
Show inline comments
 
new file 100644
 
#include "stm32f0xx_hal.h"
 

	
 
#include "config.h"
 
#include "stringhelpers.h"
 
#include "states.h"
 
#include "gpio.h"
 

	
 
// Grab temperature reading from MAX31865
 
void max31865_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status)
 
{
 
    // ** FIXME ** FIXME ** FIXME ** //
 
    // This is duplicated from MAX31855, update for MAX31865 registers/etc
 
    /////////////////////////////////// 
 

	
 
    // Assert CS
 
    HAL_GPIO_WritePin(MAX_CS, 0);
 

	
 
    uint8_t rxdatah[1] = {0x00};
 
    uint8_t rxdatal[1] = {0x00};
 

	
 
    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 = rxdatal[0] | (rxdatah[0]<<8);
 
    if(temp_pre & 0b001 && !set->ignore_tc_error) {
 
        status->tc_errno = 1;
 
        HAL_Delay(400); // FIXME: remove?
 
        status->state_resume = status->state;
 
        status->state = STATE_TC_ERROR;
 
        status->temp = 0;
 
        status->temp_frac = 0;
 
    }
 
    else 
 
    {
 
        uint8_t sign = status->temp >> 15;// top bit is sign
 
        temp_pre = temp_pre >> 2; // Drop 2 lowest bits
 
        status->temp_frac = temp_pre & 0b11; // get fractional part
 
        status->temp_frac *= 25; // each bit is .25 a degree, up to fixed point
 
        temp_pre = temp_pre >> 2; // Drop 2 fractional bits 
 

	
 
        int8_t signint;
 

	
 
        if(sign) {
 
            signint = -1;
 
        }
 
        else {
 
            signint = 1;
 
        }
 

	
 
        // Convert to Fahrenheit
 
        if(set->temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
            status->temp = signint * ((temp_pre*100) + status->temp_frac);
 
            status->temp = status->temp * 1.8;
 
            status->temp += 3200;
 
            status->temp_frac = status->temp % 100;
 
            status->temp /= 100;
 
            status->temp += set->temp_offset;
 
        }
 

	
 
        // Use Celsius values
 
        else
 
        {
 
            status->temp = temp_pre * signint;
 
            status->temp += set->temp_offset;
 
        }
 
    }
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
max31865.h
Show inline comments
 
new file 100644
 
#ifndef MAX31865_H 
 
#define MAX31865_H 
 

	
 
void max31865_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status);
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
0 comments (0 inline, 0 general)