Changeset - 860001bb7681
[Not reviewed]
cortex-f0
0 8 1
matthewreed - 9 years ago 2015-12-19 18:28:02

Added initial RTD support
9 files changed with 132 insertions and 72 deletions:
0 comments (0 inline, 0 general)
MAX31865.pdf
Show inline comments
 
new file 100644
 
binary diff not shown
config.h
Show inline comments
 
@@ -2,8 +2,8 @@
 
#define CONFIG_H
 

	
 
// Temperature sensor type
 
#define MAX31855_TC_SENSOR
 
//#define MAX31865_RTD_SENSOR
 
//#define MAX31855_TC_SENSOR
 
#define MAX31865_RTD_SENSOR
 

	
 

	
 
// Virtual serial port transmit rate
display.c
Show inline comments
 
@@ -7,6 +7,9 @@
 
#include "syslib.h"
 
#include "flash.h"
 
#include "gpio.h"
 
#ifdef MAX31865_RTD_SENSOR
 
#include "max31865.h"
 
#endif
 

	
 
// Private function prototypes
 
static void draw_setpoint(therm_status_t* status);
 
@@ -31,7 +34,7 @@ static uint8_t sw_right_last = 0;
 
// States
 
static uint8_t trigger_drawsetpoint = 1;
 
static int16_t last_temp = 21245;
 
static uint8_t goto_mode = 2;
 
static uint8_t goto_mode = MODE_HEAT;
 

	
 

	
 

	
 
@@ -70,22 +73,22 @@ void display_process(therm_settings_t* s
 
            ssd1306_drawlogo();
 

	
 
            switch(goto_mode) {
 
                case 3:
 
                case MODE_BOOTLOADER:
 
                {
 
                    ssd1306_drawstring("-> loader   ", 1, 40);
 
                } break;
 

	
 
                case 2:
 
                case MODE_HEAT:
 
                {
 
                    ssd1306_drawstring("-> heat     ", 1, 40);
 
                } break;
 

	
 
                case 1:
 
                case MODE_SETUP:
 
                {
 
                    ssd1306_drawstring("-> setup    ", 1, 40);
 
                } break;
 

	
 
                case 0:
 
                case MODE_RESET:
 
                {
 
                    ssd1306_drawstring("-> reset    ", 1, 40);
 
                }
 
@@ -94,7 +97,7 @@ void display_process(therm_settings_t* s
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                switch(goto_mode) {
 
                    case 3:
 
                    case MODE_BOOTLOADER:
 
                    {
 
                        ssd1306_clearscreen();
 
                        ssd1306_drawstring("Bootloader Entered", 0, 0);
 
@@ -103,13 +106,13 @@ void display_process(therm_settings_t* s
 
                        bootloader_enter(); // Resets into bootloader
 
                        status->state = STATE_IDLE; // Just in case
 
                    } break;
 
                    case 2:
 
                    case MODE_HEAT:
 
                        status->state = STATE_PREHEAT;
 
                        break;
 
                    case 1:
 
                    case MODE_SETUP:
 
                        status->state = STATE_SETP;
 
                        break;
 
                    case 0:
 
                    case MODE_RESET:
 
                        status->state = STATE_IDLE;
 
                        //flash_erase();
 
                        NVIC_SystemReset(); 
 
@@ -119,10 +122,10 @@ void display_process(therm_settings_t* s
 
                        status->state = STATE_PREHEAT;
 
                }
 
            }
 
            else if(SW_UP_PRESSED && goto_mode < 3) {
 
            else if(SW_DOWN_PRESSED && goto_mode < (MODE_SIZE - 1)) {
 
                goto_mode++;
 
            }
 
            else if(SW_DOWN_PRESSED && goto_mode > 0) {
 
            else if(SW_UP_PRESSED && goto_mode > 0) {
 
                goto_mode--;
 
            }
 

	
 
@@ -404,14 +407,16 @@ void display_process(therm_settings_t* s
 
            ssd1306_drawstring("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->tc_errno, tempstr, 10);
 
            itoa(status->error_code, tempstr, 10);
 
            ssd1306_drawstring(tempstr, 0, 57);
 

	
 
            if(status->tc_errno == 1)
 
            //TODO: add RTD error codes
 

	
 
            if(status->error_code == 1)
 
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
 
            else if(status->tc_errno == 4)
 
            else if(status->error_code == 4)
 
                ssd1306_drawstring("    TC Short to GND", 1, 0);
 
            else if(status->tc_errno == 8)
 
            else if(status->error_code == 8)
 
                ssd1306_drawstring("    TC Short to VCC", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
@@ -423,9 +428,12 @@ void display_process(therm_settings_t* s
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
				#ifdef MAX31865_RTD_SENSOR
 
				max31865_clear_errors(spi_get());
 
				#endif
 
            }
 
            else if(SW_RIGHT_PRESSED) {
 
                set->val.ignore_tc_error = 1;
 
                set->val.ignore_error = 1;
 
                status->state = STATE_IDLE;
 
            }
 
            // Event Handler
 
@@ -446,7 +454,7 @@ void display_process(therm_settings_t* s
 

	
 
    if(last_state != status->state) {
 
        // Clear screen on state change
 
        goto_mode = 2;
 
        goto_mode = MODE_HEAT;
 
        trigger_drawsetpoint = 1;
 
        ssd1306_clearscreen();
 
    }
main.c
Show inline comments
 
@@ -6,6 +6,7 @@
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "max31855.h"
 
#include "max31865.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "flash.h"
 
@@ -47,6 +48,10 @@ int main(void)
 
 
    // Init SPI busses
 
    spi_init();
 
    
 
    #ifdef MAX31865_RTD_SENSOR
 
    max31865_config(spi_get());
 
    #endif
 
 
    // Init OLED over SPI
 
    ssd1306_init();
 
@@ -59,7 +64,7 @@ int main(void)
 
    set.val.k_p = 10;
 
    set.val.k_i = 1;
 
    set.val.k_d = 1;
 
    set.val.ignore_tc_error = 0;
 
    set.val.ignore_error = 0;
 
    set.val.setpoint_brew = 70;
 
    set.val.setpoint_steam = 70;
 
 
@@ -111,7 +116,7 @@ int main(void)
 
            #endif
 
 
            #ifdef MAX31865_RTD_SENSOR
 
            max31865_readtemp(&set, &status);
 
            max31865_readtemp(spi_get(), &set, &status);
 
            #endif
 
 
max31855.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 

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

	
 
// Grab temperature reading from MAX31855
 
void max31855_readtemp(SPI_HandleTypeDef* hspi1, therm_settings_t* set, therm_status_t* status)
 
@@ -31,8 +26,8 @@ void max31855_readtemp(SPI_HandleTypeDef
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    } */
 
    if(temp_pre & 0b001 && !set->val.ignore_tc_error) {
 
        status->tc_errno = 1;
 
    if(temp_pre & 0b001 && !set->val.ignore_error) {
 
        status->error_code = 1;
 
        HAL_Delay(400); // FIXME: remove?
 
        status->state_resume = status->state;
 
        status->state = STATE_TC_ERROR;
max31855.h
Show inline comments
 
#ifndef MAX31855_H
 
#define MAX31855_H
 

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

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

	
 
#endif
max31865.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 

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

	
 

	
 
// Registers
 
#define MAX31855_REG_CONFIG 0x00
 
#define MAX31855_REG_RTD_MSB 0x01
 
#define MAX31855_REG_RTD_LSB 0x02
 
#define MAX31855_REG_HFAULT_THRESH_MSB 0x03
 
#define MAX31855_REG_HFAULT_THRESH_LSB 0x04
 
#define MAX31855_REG_LFAULT_THRESH_MSB 0x05
 
#define MAX31855_REG_LFAULT_THRESH_LSB 0x06
 
#define MAX31855_REG_FAULTSTATUS 0x07
 
#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 MAX31855_REGWRITEMODIFIER 0x80
 
#define MAX31865_REG_WRITE_MODIFIER 0x80
 

	
 
// Fields 
 
#define MAX31865_CONF_VBIAS (1<<7)
 
#define MAX31865_CONF_CONVMODE (1<<6)
 
#define MAX31865_CONF_AUTO_CONVERT (1<<6)
 
#define MAX31865_CONF_1SHOT (1<<5)
 
#define MAX31865_CONF_3WIRE (1<<4)
 

	
 
@@ -32,6 +27,28 @@
 
#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)
 
@@ -49,10 +66,13 @@ void max31865_readtemp(SPI_HandleTypeDef
 

	
 
    // 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);
 

	
 
@@ -60,50 +80,59 @@ void max31865_readtemp(SPI_HandleTypeDef
 
    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->val.ignore_tc_error) {
 
        status->tc_errno = 1;
 
    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 
 
    {
 
        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 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 = 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 = adc_count;
 
            //convert to fahrenheit
 
            status->temp += set->val.temp_offset;
 
        }
 

	
 
        // Use Celsius values
 
        else
 
        {
 
            status->temp = temp_pre * signint;
 
            //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 
max31865.h
Show inline comments
 
#ifndef MAX31865_H 
 
#define MAX31865_H 
 
#define MAX31865_H
 

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

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

	
 
#endif
states.h
Show inline comments
 
@@ -8,7 +8,7 @@ typedef struct {
 
    uint8_t state;
 
    int32_t setpoint;
 
    uint8_t pid_enabled;
 
    uint8_t tc_errno;
 
    uint8_t error_code;
 
} therm_status_t;
 

	
 
typedef union
 
@@ -21,7 +21,7 @@ typedef union
 
        uint32_t k_i;
 
        uint32_t k_d;
 
        int32_t temp_offset;
 
        uint32_t ignore_tc_error;
 
        uint32_t ignore_error;
 
        int32_t setpoint_brew;
 
        int32_t setpoint_steam;
 
    } val;
 
@@ -52,4 +52,12 @@ enum state {
 
    STATE_TC_ERROR,
 
};
 

	
 
enum GOTO_MODE {
 
	MODE_HEAT = 0,
 
	MODE_SETUP,
 
	MODE_RESET,
 
	MODE_BOOTLOADER,
 
	MODE_SIZE,
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)