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
 
#ifndef CONFIG_H
 
#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
 
#define VCP_TX_FREQ 1000
 

	
 
// Solid-state relay maximum on-time
 
#define SSR_PERIOD 200
 

	
 
// Interval of PID calculations
 
#define PID_PERIOD 120
 

	
 

	
 

	
 
// Pin settings
 
#define LED_POWER GPIOF,GPIO_PIN_0
 
#define MAX_CS GPIOA,GPIO_PIN_15
 

	
 
#define SW_BTN  GPIOB, GPIO_PIN_4
 
#define SW_UP   GPIOB, GPIO_PIN_7
 
#define SW_DOWN GPIOB, GPIO_PIN_3
 
#define SW_LEFT GPIOB, GPIO_PIN_5
 
#define SW_RIGHT GPIOB, GPIO_PIN_6
 

	
 
#define SSR_PIN GPIOA, GPIO_PIN_1
 

	
 
// Visual niceness
 
#define hal_init() HAL_Init()
 

	
 
#endif
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
display.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "ssd1306.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "config.h"
 
#include "states.h"
 
#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);
 

	
 

	
 
// Button transition variables
 
static uint8_t sw_btn_last = 0;
 
static uint8_t sw_up_last = 0;
 
static uint8_t sw_down_last = 0;
 
static uint8_t sw_left_last = 0;
 
static uint8_t sw_right_last = 0;
 

	
 

	
 
// Buttonpress macros
 
#define SW_BTN_PRESSED (sw_btn_last == 0 && sw_btn == 1) // rising edge on buttonpress
 
#define SW_UP_PRESSED (sw_up_last == 0 && sw_up == 1)
 
#define SW_DOWN_PRESSED (sw_down_last == 0 && sw_down == 1)
 
#define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1)
 
#define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1)
 

	
 

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

	
 

	
 

	
 
// Display state machine
 
void display_process(therm_settings_t* set, therm_status_t* status)
 
{
 
    uint8_t last_state = status->state;
 
    
 
    uint8_t temp_changed = status->temp != last_temp;
 
    last_temp = status->temp;
 

	
 
    uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN);
 
    uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP);
 
    uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN);
 
    uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
 
    uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
 

	
 
    switch(status->state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
            ssd1306_drawstring("therm :: idle ", 0, 40);
 
            status->pid_enabled = 0;
 

	
 
            if(temp_changed) {
 
                char tempstr[6];
 
                itoa_fp(status->temp, status->temp_frac, tempstr);
 
                ssd1306_drawstring("Temp: ", 3, 40);
 
                ssd1306_drawstring("    ", 3, 72);
 
                ssd1306_drawstring(tempstr, 3, 72);
 
            }
 

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

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                switch(goto_mode) {
 
                    case 3:
 
                    case MODE_BOOTLOADER:
 
                    {
 
                        ssd1306_clearscreen();
 
                        ssd1306_drawstring("Bootloader Entered", 0, 0);
 
                        ssd1306_drawstring("Device won't boot", 2, 0);
 
                        ssd1306_drawstring("until reflashed!", 3, 0);
 
                        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(); 
 
                        break;
 

	
 
                    default:
 
                        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--;
 
            }
 

	
 

	
 
            // Event Handler
 
            // N/A
 

	
 
        } break;
 

	
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_drawstring("Proportional", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_p, tempstr, 10);
 
            ssd1306_drawstring("P=", 1, 45);
 
            ssd1306_drawstring("    ", 1, 57);
 
            ssd1306_drawstring(tempstr, 1, 57);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETI;
 
            }
 
            else {
 
                user_input(&set->val.k_p);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_drawstring("Integral", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_i, tempstr, 10);
 
            ssd1306_drawstring("I=", 1, 45);
 
            ssd1306_drawstring("    ", 1, 57);
 
            ssd1306_drawstring(tempstr, 1, 57);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 
            
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETD;
 
            }
 
            else {
 
                user_input(&set->val.k_i);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETD:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
            ssd1306_drawstring("Derivative", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.k_d, tempstr, 10);
 
            ssd1306_drawstring("D=", 1, 45);
 
            ssd1306_drawstring("    ", 1, 57);
 
            ssd1306_drawstring(tempstr, 1, 57);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETWINDUP;
 
            }
 
            else {
 
                user_input(&set->val.k_d);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETWINDUP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Windup Guard", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.windup_guard, tempstr, 10);
 
            ssd1306_drawstring("G=", 1, 45);
 
            ssd1306_drawstring("    ", 1, 57);
 
            ssd1306_drawstring(tempstr, 1, 57);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETBOOTTOBREW;
 
            }
 
            else {
 
                user_input(&set->val.windup_guard);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETBOOTTOBREW:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Start on Boot", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            ssd1306_drawstring("sob=", 1, 45);
 
            
 
            if(set->val.boottobrew)
 
                ssd1306_drawstring("Enabled ", 1, 70);
 
            else
 
                ssd1306_drawstring("Disabled", 1, 70);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETUNITS;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_UP)) {
 
                set->val.boottobrew = 1;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->val.boottobrew = 0;
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        case STATE_SETUNITS:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Units: ", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
                ssd1306_drawstring("Fahrenheit", 1, 60);
 
            else
 
                ssd1306_drawstring("Celsius   ", 1, 60);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_SETTEMPOFFSET;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_UP)) {
 
                set->val.temp_units = TEMP_UNITS_FAHRENHEIT;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->val.temp_units = TEMP_UNITS_CELSIUS;
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 

	
 
        case STATE_SETTEMPOFFSET:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set temp offset ]
 
            // [ g = 12         ]
 
            ssd1306_drawstring("Temp Cal Offset", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->val.temp_offset, tempstr, 10);
 
            ssd1306_drawstring("O=", 1, 45);
 
            ssd1306_drawstring("    ", 1, 57);
 
            ssd1306_drawstring(tempstr, 1, 57);
 

	
 
            ssd1306_drawstring("Press to accept", 3, 40);
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                flash_save(&set);
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input_signed(&set->val.temp_offset);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 

	
 
        case STATE_PREHEAT:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
            ssd1306_drawstring("Preheating...", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 

	
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN;
 
            }
 
 
 
        } break;
 

	
 
        case STATE_MAINTAIN:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to brew ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_drawstring("Preheated!", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->val.setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->val.setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 

	
 
        // Thermocouple error
 
        case STATE_TC_ERROR:
 
        {
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            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);
 
            ssd1306_drawstring("                    ", 2, 0);
 

	
 
            ssd1306_drawstring("-> to ignore all or", 2, 0);
 
            ssd1306_drawstring("press to continue", 3, 0);
 

	
 
            // 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
 
            // Maybe handle if TC is plugged in
 
            // N/A
 
 
 
        } break;
 

	
 
        // Something is terribly wrong
 
        default:
 
        {
 
            status->state = STATE_IDLE;
 
            status->pid_enabled = 0;
 

	
 
        } break;
 
            
 
    }
 

	
 
    if(last_state != status->state) {
 
        // Clear screen on state change
 
        goto_mode = 2;
 
        goto_mode = MODE_HEAT;
 
        trigger_drawsetpoint = 1;
 
        ssd1306_clearscreen();
 
    }
 

	
 
    // Last buttonpress
 
    sw_btn_last = sw_btn;
 
    sw_up_last = sw_up;
 
    sw_down_last = sw_down;
 
    sw_left_last = sw_left;
 
    sw_right_last = sw_right;
 
}
 

	
 

	
 
static int32_t temp_last = 43002;
 
static int32_t setpoint_last = 10023;
 

	
 
// Draw current setpoint on display
 
static void draw_setpoint(therm_status_t* status) {
 
    // FIXME: need to do this when switching modes too
 
    if(status->temp != temp_last || trigger_drawsetpoint) { 
 
        char tempstr[3];
 
        itoa_fp(status->temp, status->temp_frac, tempstr);
 
        ssd1306_drawstringbig("      ", 3, 0);
 
        ssd1306_drawstringbig(tempstr, 3, 0);
 
    }
 

	
 
    if(trigger_drawsetpoint) 
 
        ssd1306_drawstringbig(">", 3, 74);
 

	
 
    if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
 
        char tempstr[3];
 
        itoa(status->setpoint, tempstr, 10);
 
        ssd1306_drawstringbig("   ", 3, 90);
 
        ssd1306_drawstringbig(tempstr, 3, 90);
 
    }
 

	
 
    trigger_drawsetpoint = 0;
 
    setpoint_last = status->setpoint;
 
    temp_last = status->temp;
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
main.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
 
#include "config.h"
 
#include "syslib.h"
 
#include "pid.h"
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "max31855.h"
 
#include "max31865.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "flash.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
 
#include "usb_device.h"
 
#include "usbd_cdc_if.h"
 
 
therm_settings_t set;
 
therm_status_t status;
 
 
int main(void)
 
{
 
    // Initialize HAL
 
    hal_init();
 
 
    // Configure the system clock
 
    systemclock_init();
 
 
    // Unset bootloader option bytes (if set)
 
    // FIXME this was never getting called. Try again sometime.
 
    //bootloader_unset();
 
 
    // Init GPIO
 
    gpio_init();
 
 
    // Init USB (TODO: Handle plugged/unplugged with external power)
 
    MX_USB_DEVICE_Init();
 
//    set.val.usb_plugged = 
 
 
    // USB startup delay
 
    HAL_Delay(500);
 
    HAL_GPIO_WritePin(LED_POWER, 1);
 
 
    // Enter into bootloader if up button pressed on boot
 
    if(!HAL_GPIO_ReadPin(SW_UP))
 
        bootloader_enter(); 
 
 
    // Init SPI busses
 
    spi_init();
 
    
 
    #ifdef MAX31865_RTD_SENSOR
 
    max31865_config(spi_get());
 
    #endif
 
 
    // Init OLED over SPI
 
    ssd1306_init();
 
    ssd1306_clearscreen();
 
   
 
    // Default settings 
 
    set.val.boottobrew = 0;
 
    set.val.temp_units = TEMP_UNITS_FAHRENHEIT;
 
    set.val.windup_guard = 10;
 
    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;
 
 
    // Default status
 
    status.temp = 0;
 
    status.temp_frac = 0;
 
    status.state_resume = 0;
 
    status.state = STATE_IDLE;
 
    status.setpoint = 70;
 
    status.pid_enabled = 0;
 
 
    // Go to brew instead of idle if configured thusly
 
    if(set.val.boottobrew)
 
      status.state = STATE_PREHEAT; 
 
 
    // Startup screen 
 
    ssd1306_drawstring("therm v0.2", 1, 40);
 
    ssd1306_drawstring("protofusion.org/therm", 3, 0);
 
 
    HAL_Delay(1000);
 
 
    // Restore settings from flash memory
 
    flash_restore(&set);
 
 
    HAL_Delay(1000);
 
    ssd1306_clearscreen();
 
 
    // Soft timers
 
    uint32_t last_ssr_on = 0;
 
    uint32_t last_vcp_tx = 0;
 
    uint32_t last_led = 0;
 
    uint32_t last_pid = 0;
 
    int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD 
 
 
    // Main loop
 
    while(1)
 
    {
 
        // Process sensor inputs
 
 
        if(HAL_GetTick() - last_led > 400) 
 
        {
 
            last_led = HAL_GetTick();
 
        }
 
 
        if((HAL_GetTick() - last_pid > PID_PERIOD))
 
        {
 
            #ifdef MAX31855_TC_SENSOR
 
            max31855_readtemp(spi_get(), &set, &status); // Read MAX31855
 
            #endif
 
 
            #ifdef MAX31865_RTD_SENSOR
 
            max31865_readtemp(&set, &status);
 
            max31865_readtemp(spi_get(), &set, &status);
 
            #endif
 
 
 
            if(status.pid_enabled) 
 
            {
 
                // Get ssr output for next time
 
                int16_t power_percent = pid_update(set.val.k_p, set.val.k_i, set.val.k_d, status.temp, status.temp_frac, status.setpoint, &set, &status);
 
                //power-percent is 0-1000?
 
                ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000;
 
            }
 
            else 
 
            {
 
                ssr_output = 0;
 
            }
 
 
            last_pid = HAL_GetTick();
 
        }
 
 
        // Kill SSR once the desired on-time has elapsed
 
        if(HAL_GetTick() - last_ssr_on > ssr_output || ssr_output <= 0)
 
        {
 
            HAL_GPIO_WritePin(SSR_PIN, 0);
 
            HAL_GPIO_WritePin(LED_POWER, 0);
 
        }
 
 
        // Every 200ms, set the SSR on unless output is 0
 
        if(HAL_GetTick() - last_ssr_on > SSR_PERIOD)
 
        {
 
            // Only support heating (ssr_output > 0) right now
 
            if(ssr_output > 0) 
 
            {
 
                char tempstr[6];
 
                itoa(ssr_output, tempstr, 10);
 
                ssd1306_drawstring(tempstr, 0, 90);
 
 
                HAL_GPIO_WritePin(SSR_PIN, 1);
 
                HAL_GPIO_WritePin(LED_POWER, 1);
 
                last_ssr_on = HAL_GetTick();
 
            }
 
            else {
 
                // Make sure everything is off
 
                HAL_GPIO_WritePin(SSR_PIN, 0);
 
                HAL_GPIO_WritePin(LED_POWER, 0);
 
            }
 
            
 
        }
 
        
 
        // Transmit temperature over USB-CDC on a regulat basis
 
        if(HAL_GetTick() - last_vcp_tx > VCP_TX_FREQ)
 
        {
 
            // Print temp to cdc
 
            char tempstr[16];
 
            itoa_fp(status.temp, status.temp_frac, tempstr);
 
            uint8_t numlen = strlen(tempstr);
 
            tempstr[numlen] = '\r';
 
            tempstr[numlen+1] = '\n';
 
 
    //        if(set.val.usb_plugged)
 
    //            CDC_Transmit_FS(tempstr, numlen+2);
 
           // while(CDC_Transmit_FS("\r\n", 2) == USBD_BUSY);
 
 
            last_vcp_tx = HAL_GetTick();
 
        }
 
 
        // Run state machine
 
        display_process(&set, &status); 
 
    }
 
}
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
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)
 
{
 
    // 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 & 0b010) {
 
        ssd1306_clearscreen();
 
        HAL_Delay(400); // FIXME: remove?
 
        status.tc_errno = 4;
 
        status.state = STATE_TC_ERROR;
 
        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;
 
        status->temp = 0;
 
        status->temp_frac = 0;
 
    }/*
 
    else if(temp_pre & 0b100 && !set.ignore_tc_error) {
 
        status.tc_errno = 8;
 
        HAL_Delay(400); // FIXME: remove?
 
        status.state_resume = status.state;
 
        status.state = STATE_TC_ERROR;
 
        status.temp = 0;
 
        status.temp_frac = 0;
 
    }*/
 
    else 
 
    {
 
        //if(status.state == STATE_TC_ERROR)
 
        //{
 
        //    status.state = status.state_resume;
 
        //    ssd1306_clearscreen();
 
        //}
 

	
 
        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->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 += set->val.temp_offset;
 
        }
 

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

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
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
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
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)
 

	
 
#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 = 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
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
states.h
Show inline comments
 
#ifndef STATES_H
 
#define STATES_H
 

	
 
typedef struct {
 
    int32_t temp;
 
    uint8_t temp_frac;
 
    uint8_t state_resume;
 
    uint8_t state;
 
    int32_t setpoint;
 
    uint8_t pid_enabled;
 
    uint8_t tc_errno;
 
    uint8_t error_code;
 
} therm_status_t;
 

	
 
typedef union
 
{
 
     struct {
 
        uint32_t boottobrew;
 
        uint32_t temp_units;
 
        uint32_t windup_guard;
 
        uint32_t k_p;
 
        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;
 

	
 
    uint16_t data[128];
 
} therm_settings_t;
 

	
 
enum tempunits {
 
    TEMP_UNITS_CELSIUS = 0,
 
    TEMP_UNITS_FAHRENHEIT,
 
};
 

	
 
enum state {
 
    STATE_IDLE = 0,
 

	
 
    STATE_SETP,
 
    STATE_SETI,
 
    STATE_SETD,
 
    STATE_SETSTEPS,
 
    STATE_SETWINDUP,
 
    STATE_SETBOOTTOBREW,
 
    STATE_SETUNITS,
 
    STATE_SETTEMPOFFSET,
 

	
 
    STATE_PREHEAT,
 
    STATE_MAINTAIN,
 

	
 
    STATE_TC_ERROR,
 
};
 

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

	
 
#endif
0 comments (0 inline, 0 general)