Changeset - 149610e94cf9
[Not reviewed]
cortex-f0
0 3 2
Ethan Zonca - 9 years ago 2015-06-01 17:48:05
ez@ethanzonca.com
Moved save/restore of settings to new file
5 files changed with 81 insertions and 74 deletions:
0 comments (0 inline, 0 general)
Makefile
Show inline comments
 
# STM32F0xx Makefile
 
# #####################################
 
#
 
# Part of the uCtools project
 
# uctools.github.com
 
#
 
#######################################
 
# user configuration:
 
#######################################
 

	
 

	
 
# SOURCES: list of sources in the user application
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c
 
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c gpio.c spi.c ssd1306.c stringhelpers.c display.c bootlib.c storage.c
 

	
 
# TARGET: name of the user application
 
TARGET = main
 

	
 
# BUILD_DIR: directory to place output files in
 
BUILD_DIR = build
 

	
 
# LD_SCRIPT: location of the linker script
 
LD_SCRIPT = stm32f042c6_flash.ld
 

	
 
# USER_DEFS user defined macros
 
USER_DEFS = -D HSI48_VALUE=48000000 -D HSE_VALUE=16000000
display.c
Show inline comments
 
@@ -365,25 +365,25 @@ void display_process(therm_settings_t* s
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->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) {
 
                save_settings();
 
                save_settings(&set);
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input_signed(&set->temp_offset);
 
            }
 

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

	
 

	
 
@@ -392,25 +392,25 @@ void display_process(therm_settings_t* s
 
            // 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->setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
		save_setpoints(); // TODO: Check for mod
 
		save_setpoints(&set); // TODO: Check for mod
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->setpoint_brew);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN_BREW;
 
            }
 
 
 
        } break;
 
@@ -419,25 +419,25 @@ void display_process(therm_settings_t* s
 
        {
 
            // 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->setpoint_brew;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
		save_setpoints(); // TODO: Check for mod
 
		save_setpoints(&set); // TODO: Check for mod
 
                status->state = STATE_IDLE;
 
            }
 
            else {
 
                user_input(&set->setpoint_brew);
 
            }
 

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

	
 
        case STATE_PREHEAT_STEAM:
 
@@ -445,25 +445,25 @@ void display_process(therm_settings_t* s
 
            // Write text to OLED
 
            // [ therm : preheating steam ]
 
            // [ 30 => 120 C           ]
 
            ssd1306_DrawString("Preheating...", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->setpoint_steam;
 
	    
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
		save_setpoints(); // TODO: Check for mod
 
		save_setpoints(&set); // TODO: Check for mod
 
            }
 
            else {
 
                user_input(&set->setpoint_steam);
 
            }
 

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

	
 
@@ -472,25 +472,25 @@ void display_process(therm_settings_t* s
 
            // Write text to OLED
 
            // [ therm : ready to steam ]
 
            // [ 30 => 120 C            ]
 
            ssd1306_DrawString("Ready to Steam!", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 
            status->pid_enabled = 1;
 
	    status->setpoint = set->setpoint_steam;
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
		save_setpoints(); // TODO: Check for mod
 
		save_setpoints(&set); // TODO: Check for mod
 
            }
 
            else {
 
                user_input(&set->setpoint_steam);
 
            }
 

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

	
 
        case STATE_TC_ERROR:
 
        {
main.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
 
#include "config.h"
 
#include "states.h"
 
#include "ssd1306.h"
 
#include "gpio.h"
 
#include "spi.h"
 
#include "stringhelpers.h"
 
#include "display.h"
 
#include "storage.h"
 
 
#include "usb_device.h"
 
#include "usbd_cdc_if.h"
 
 
 
// Prototypes
 
// Move to header file
 
void process();
 
void restore_settings();
 
void save_settings();
 
void save_setpoints();
 
void SystemClock_Config(void);
 
 
therm_settings_t set;
 
therm_status_t status;
 
 
 
// Globalish setting vars
 
SPI_HandleTypeDef hspi1;
 
static __IO uint32_t TimingDelay;
 
 
void deinit(void)
 
{
 
@@ -79,25 +77,25 @@ int main(void)
 
    set.setpoint_brew = 0;
 
    set.setpoint_steam = 0;
 
 
    // Default status
 
    status.temp = 0;
 
    status.temp_frac = 0;
 
    status.state_resume = 0;
 
    status.state = STATE_IDLE;
 
    status.setpoint = 0;
 
    status.pid_enabled = 0;
 
 
    // Load settings (if any) from EEPROM
 
    restore_settings();
 
    restore_settings(&set);
 
 
    // Go to brew instead of idle if configured thusly
 
    if(set.boottobrew)
 
      status.state = STATE_PREHEAT_BREW; 
 
 
    // Startup screen 
 
    ssd1306_DrawString("therm v0.2", 1, 40);
 
    ssd1306_DrawString("protofusion.org/therm", 3, 0);
 
 
    HAL_Delay(1500);
 
    ssd1306_clearscreen();
 
 
 
@@ -332,77 +330,13 @@ void process()
 
        itoa_fp(status.temp, status.temp_frac, tempstr);
 
        uint8_t numlen = strlen(tempstr);
 
        tempstr[numlen] = '\r';
 
        tempstr[numlen+1] = '\n';
 
 
        CDC_Transmit_FS(tempstr, numlen+2);
 
       // while(CDC_Transmit_FS("\r\n", 2) == USBD_BUSY);
 
 
        last_vcp_tx = ticks;
 
    }
 
}
 
 
void save_settings()
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*
 
   Minimal_EEPROM_Unlock();
 
    // Try programming a word at an address divisible by 4
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW, boottobrew);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD, windup_guard);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P, k_p);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I, k_i);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D, k_d);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS, temp_units);
 
    Minimal_EEPROM_Lock();
 
*/
 
}
 
 
void save_setpoints()
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*
 
 
    Minimal_EEPROM_Unlock();
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP, setpoint_brew);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP, setpoint_steam); 
 
    Minimal_EEPROM_Lock();
 
*/
 
}
 
 
 
// TODO: Make a struct that has all settings in it. Pass by ref to this func in a library.
 
void restore_settings()
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*    Minimal_EEPROM_Unlock();
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    boottobrew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    windup_guard = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_p = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P));
 
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_i = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I));
 
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_d = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    setpoint_brew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP));
 
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    setpoint_steam = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP));    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    temp_units = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS));    
 
 
    Minimal_EEPROM_Lock(); */
 
}
 
 
 
 
 
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
storage.c
Show inline comments
 
new file 100644
 
#include "stm32f0xx_hal.h"
 
#include "states.h"
 

	
 
void save_settings(therm_settings_t *tosave)
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*
 
   Minimal_EEPROM_Unlock();
 
    // Try programming a word at an address divisible by 4
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW, boottobrew);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD, windup_guard);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P, k_p);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I, k_i);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D, k_d);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS, temp_units);
 
    Minimal_EEPROM_Lock();
 
*/
 
}
 

	
 
void save_setpoints(therm_settings_t *tosave)
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*
 

	
 
    Minimal_EEPROM_Unlock();
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP, setpoint_brew);
 
    Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP, setpoint_steam); 
 
    Minimal_EEPROM_Lock();
 
*/
 
}
 

	
 

	
 
// TODO: Make a struct that has all settings in it. Pass by ref to this func in a library.
 
void restore_settings(therm_settings_t *tosave)
 
{
 
    // TODO: Rework with FLASH read/write 
 
/*    Minimal_EEPROM_Unlock();
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    boottobrew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BOOTTOBREW));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    windup_guard = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_WINDUP_GUARD));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_p = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P));
 

	
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_i = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I));
 

	
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    k_d = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D));
 
    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    setpoint_brew = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_BREWTEMP));
 

	
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    setpoint_steam = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP));    
 
    while(Minimal_FLASH_GetStatus()==FLASH_BUSY);
 
    temp_units = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS));    
 

	
 
    Minimal_EEPROM_Lock(); */
 
}
 

	
 

	
storage.h
Show inline comments
 
new file 100644
 
#ifndef STORAGE_H
 
#define STORAGE_H
 

	
 

	
 
void save_settings(therm_settings_t *tosave);
 
void save_setpoints(therm_settings_t *tosave);
 
void restore_settings(therm_settings_t *tosave);
 

	
 
#endif
0 comments (0 inline, 0 general)