Changeset - 235f584ead39
[Not reviewed]
cortex-f0
0 9 0
Ethan Zonca - 9 years ago 2015-11-25 19:03:24
ez@ethanzonca.com
More testing of flash stuff that doesn't exactly work
9 files changed with 116 insertions and 210 deletions:
0 comments (0 inline, 0 general)
display.c
Show inline comments
 
@@ -100,13 +100,13 @@ void display_process(therm_settings_t* s
 
                        break;
 
                    case 1:
 
                        status->state = STATE_SETP;
 
                        break;
 
                    case 0:
 
                        status->state = STATE_IDLE;
 
                        flash_erase(set);
 
                        //flash_erase();
 
                        NVIC_SystemReset(); 
 
                        break;
 

	
 
                    default:
 
                        status->state = STATE_PREHEAT_BREW;
 
                }
 
@@ -130,25 +130,25 @@ void display_process(therm_settings_t* s
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_DrawString("Proportional", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->k_p, tempstr, 10);
 
            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->k_p);
 
                user_input(&set->val.k_p);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -159,25 +159,25 @@ void display_process(therm_settings_t* s
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_DrawString("Integral", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->k_i, tempstr, 10);
 
            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->k_i);
 
                user_input(&set->val.k_i);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -188,25 +188,25 @@ void display_process(therm_settings_t* s
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
            ssd1306_DrawString("Derivative", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->k_d, tempstr, 10);
 
            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->k_d);
 
                user_input(&set->val.k_d);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -217,25 +217,25 @@ void display_process(therm_settings_t* s
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_DrawString("Windup Guard", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->windup_guard, tempstr, 10);
 
            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->windup_guard);
 
                user_input(&set->val.windup_guard);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -247,28 +247,28 @@ void display_process(therm_settings_t* s
 
            // [ g = 12         ]
 
            ssd1306_DrawString("Start on Boot", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            ssd1306_DrawString("sob=", 1, 45);
 
            
 
            if(set->boottobrew)
 
            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->boottobrew = 1;
 
                set->val.boottobrew = 1;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->boottobrew = 0;
 
                set->val.boottobrew = 0;
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -278,28 +278,28 @@ void display_process(therm_settings_t* s
 
            // Write text to OLED
 
            // [ therm :: set windup ]
 
            // [ g = 12         ]
 
            ssd1306_DrawString("Units: ", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            if(set->temp_units == TEMP_UNITS_FAHRENHEIT)
 
            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->temp_units = TEMP_UNITS_FAHRENHEIT;
 
                set->val.temp_units = TEMP_UNITS_FAHRENHEIT;
 
            }
 
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
 
                set->temp_units = TEMP_UNITS_CELSIUS;
 
                set->val.temp_units = TEMP_UNITS_CELSIUS;
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -311,26 +311,26 @@ void display_process(therm_settings_t* s
 
            // [ therm :: set temp offset ]
 
            // [ g = 12         ]
 
            ssd1306_DrawString("Temp Cal Offset", 0, 40);
 
            ssd1306_drawlogo();
 

	
 
            char tempstr[6];
 
            itoa(set->temp_offset, tempstr, 10);
 
            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->temp_offset);
 
                user_input_signed(&set->val.temp_offset);
 
            }
 

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -343,21 +343,21 @@ void display_process(therm_settings_t* s
 
            // [ 30 => 120 C             ]
 
            ssd1306_DrawString("Preheating...", 0, 0);
 
            //ssd1306_drawlogo();
 
            draw_setpoint(status);
 

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

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

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN_BREW;
 
            }
 
@@ -370,21 +370,21 @@ void display_process(therm_settings_t* s
 
            // [ 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;
 
	    status->setpoint = set->val.setpoint_brew;
 

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

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -395,21 +395,21 @@ void display_process(therm_settings_t* s
 
            // [ 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;
 
	    status->setpoint = set->val.setpoint_steam;
 
	    
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
		save_setpoints(&set); // TODO: Check for mod
 
            }
 
            else {
 
                user_input(&set->setpoint_steam);
 
                user_input(&set->val.setpoint_steam);
 
            }
 

	
 
            // Event Handler
 
            if(status->temp >= status->setpoint) {
 
                status->state = STATE_MAINTAIN_STEAM;
 
            }
 
@@ -422,21 +422,21 @@ void display_process(therm_settings_t* s
 
            // [ 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;
 
	    status->setpoint = set->val.setpoint_steam;
 

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

	
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
@@ -467,13 +467,13 @@ void display_process(therm_settings_t* s
 

	
 
            // Button handler
 
            if(SW_BTN_PRESSED) {
 
                status->state = STATE_IDLE;
 
            }
 
            else if(SW_RIGHT_PRESSED) {
 
                set->ignore_tc_error = 1;
 
                set->val.ignore_tc_error = 1;
 
                status->state = STATE_IDLE;
 
            }
 
            // Event Handler
 
            // Maybe handle if TC is plugged in
 
            // N/A
 
 
flash.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "ssd1306.h"
 
#include "stm32f0xx_hal_flash.h"
 
#include "flash.h"
 

	
 
void flash_init(therm_settings_t* tosave)
 
{
 
    ssd1306_clearscreen();
 
    uint16_t size = sizeof(therm_settings_t)-1;
 
    uint32_t flash_adr = END_ADDR - size;
 
    flash_adr -= 2;
 
    uint8_t* flash_ptr = (uint8_t *)flash_adr;
 

	
 
    // Check if flash is blank
 
    uint16_t i = 0;
 
    uint16_t count = 0;
 
__attribute__((__section__(".eeprom"))) uint16_t eeprom[512];
 

	
 
    char tempstr[10];
 
    itoa(flash_adr, tempstr, 10);
 
    ssd1306_DrawString(tempstr, 1, 0);
 

	
 
    uint16_t test;
 
    for(i=0;i<size;i++)
 
    {
 
        test = *flash_ptr;
 
        if(test==0xFF) count++;
 
    }
 

	
 
    ssd1306_DrawString("END LOOP ", 0, 0);
 

	
 
    // If blank, do nothing and just use values from RAM
 

	
 
    count = size + 1;
 
    // If not blank, check the checksums
 
    if(count != size) 
 
    {
 
        ssd1306_DrawString("FLASH NOT BLANK", 1, 0);
 
        // Calculate Checksums
 
        uint8_t cksum0=0,cksum1=0;
 
        uint8_t rdSum0=0,rdSum1=0;
 
static void __flash_write(therm_settings_t* tosave);
 

	
 
        flash_adr = END_ADDR - size;
 
        flash_ptr = (uint8_t *)flash_adr;
 
        for(i=1; i < size; i++)
 
        {
 
            cksum0 += *flash_ptr++;
 
            cksum1 += cksum0;
 
        }
 

	
 
        // Read flash checksums
 
        flash_adr -= 2;
 
        flash_ptr = (uint8_t *)flash_adr;
 
        rdSum0 = *flash_ptr++;
 
        rdSum1 = *flash_ptr;
 

	
 
        // Compare Checksums values
 
        if((rdSum1==cksum1)&&(rdSum0==cksum0)) {
 
	    ssd1306_DrawString("CHECKSUM OK", 2, 0);
 
	    flash_read(tosave);
 
	}
 
        else {
 
	    ssd1306_DrawString("CHECKSUM BAD", 2, 0);
 
	    return; // If the checksum is bad, just use vals from RAM
 
	}
 

	
 
    }
 
    else {
 
        ssd1306_DrawString("FLASH BLANK", 1, 0);
 
    }
 
}
 
#define EEPROM_MAGIC_INDEX 16 
 
#define EEPROM_MAGIC_VALUE 0xbeef
 

	
 

	
 
void flash_save(therm_settings_t* tosave)
 
{
 

	
 
    ssd1306_clearscreen();
 
    ssd1306_DrawString("BEGIN SAVE", 0, 0);
 
    ssd1306_DrawString("Erase...", 0, 0);
 
    HAL_Delay(1500);
 
    flash_erase(tosave);
 
    flash_write(tosave);
 
    flash_checksum(tosave);
 
    ssd1306_DrawString("END SAVE", 2, 0);
 
    ssd1306_DrawString("Save...", 1, 0);
 
    __flash_write(tosave);
 
    HAL_Delay(1500);
 
    ssd1306_DrawString("Done!", 2, 0);
 
    HAL_Delay(1500);
 
}
 

	
 

	
 
void flash_read(therm_settings_t *tosave)
 
void flash_restore(therm_settings_t *torestore)
 
{
 
    ssd1306_clearscreen();
 
    ssd1306_DrawString("READING SAVE", 1, 0);
 
    char tempstr[10];
 
    itoa(sizeof(therm_settings_t), tempstr, 10);
 
    ssd1306_DrawString(tempstr, 2, 0);
 

	
 
    uint16_t size = sizeof(therm_settings_t)-1; // in Bytes
 
    uint32_t flash_adr = END_ADDR - size;
 
    uint8_t *flash_ptr = (uint8_t *)flash_adr;
 
    uint8_t *struct_ptr = (uint8_t*)tosave;
 
    if(eeprom[EEPROM_MAGIC_INDEX] != EEPROM_MAGIC_VALUE)
 
    {
 
        ssd1306_DrawString("No data to read!", 2, 0);
 
        
 
        return;
 
    }
 

	
 
    uint16_t i;
 
    for(i=0;i<size;i++)
 
        *struct_ptr++ = *flash_ptr++;
 
    for(i=0;i<128;i++)
 
        torestore->data[i] = *(eeprom+i);
 

	
 
    ssd1306_DrawString("READ COMPLETE", 3, 0);
 

	
 
}
 

	
 

	
 
void flash_write(therm_settings_t* tosave)
 
static void __flash_write(therm_settings_t* tosave)
 
{
 

	
 
    // Erase mem    
 
    HAL_FLASH_Unlock();
 

	
 
    uint16_t size = sizeof(therm_settings_t)-1; // in Bytes
 
    uint32_t start_address = END_ADDR-size; // write to end of page
 
    uint32_t struct_ptr = (uint32_t*) tosave;
 
    // Erase the FLASH pages
 
    FLASH_EraseInitTypeDef erase;
 
    erase.TypeErase = TYPEERASE_PAGES; 
 
    erase.PageAddress = eeprom;
 
    erase.NbPages = 1;
 
    uint32_t SectorError = 0;
 
    HAL_FLASHEx_Erase(&erase, &SectorError);
 

	
 
    uint16_t  length;
 
    if(size%2==0)
 
        length = size/2;
 
    else
 
        length = size/2+1;
 
    CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
 

	
 
    uint16_t i;
 
    for(i=0;i<length;i++)
 
    {
 
        HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, start_address, struct_ptr);
 
        struct_ptr++;
 
        start_address +=2;
 
    }
 
//    for(i=1;i<20;i++)
 
 //   {
 
  //      HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom+i, tosave->data[i]);
 
   // }
 
    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom + EEPROM_MAGIC_INDEX, EEPROM_MAGIC_VALUE);
 

	
 
    HAL_FLASH_Lock();
 
}
 

	
 

	
 
void flash_checksum(therm_settings_t* tosave)
 
{
 
    uint8_t cksum0=0,cksum1=0;
 
    uint16_t  i,size,checksum;
 
    uint32_t flash_adr;
 
    uint8_t  *flash_ptr;
 

	
 
    HAL_FLASH_Unlock();
 

	
 
    size = sizeof(*tosave)-1; // in Bytes
 
    flash_adr = END_ADDR-size;
 
    flash_ptr = (uint8_t *)flash_adr;
 

	
 
    for(i=1; i < size; i++)
 
    {
 
        cksum0 += *flash_ptr++;
 
        cksum1 += cksum0;
 
    }
 
    checksum = (cksum1<<8) | cksum0;
 
    flash_adr -= 2;
 
    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, flash_adr, checksum);
 

	
 
    HAL_FLASH_Lock();
 
}
 

	
 

	
 
void flash_erase(therm_settings_t* tosave)
 
{
 
    uint8_t FLASHStatus = 1; // FLASH_COMPLETE=1
 
    uint32_t end_addr = END_ADDR;
 
    uint32_t NbrOfPage = abs( (sizeof(*tosave)-1)/0x400 )+1;   // Number of pages to be erased, most definitely 1 but hey, we might as well try to calculate it.
 
    uint32_t StartAddr = (end_addr+1) - (0x400*NbrOfPage);   // Starting address to be erased
 

	
 
    HAL_FLASH_Unlock();
 

	
 
    // Clear All pending flags
 
    //FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
 

	
 
    // Erase the FLASH pages
 
    FLASH_EraseInitTypeDef erase;
 
    erase.TypeErase = TYPEERASE_PAGES; 
 
    erase.PageAddress = StartAddr;
 
    erase.NbPages = NbrOfPage;
 
    uint32_t SectorError = 0;
 
    FLASHStatus = HAL_FLASHEx_Erase(&erase, &SectorError);
 

	
 
    HAL_FLASH_Lock();
 
}
 

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
flash.h
Show inline comments
 
@@ -3,15 +3,12 @@
 

	
 
#include "states.h"
 

	
 
#define PAGE_SIZE ((uint16_t)0x400)
 
#define END_ADDR 0x08007FFF
 

	
 
void flash_init(therm_settings_t* tosave);
 
void flash_save(therm_settings_t* tosave);
 
void flash_read(therm_settings_t *tosave);
 
void flash_write(therm_settings_t* tosave);
 
void flash_checksum(therm_settings_t* tosave);
 
void flash_erase(therm_settings_t* tosave);
 
void flash_restore(therm_settings_t *tosave);
 
void flash_erase(void);
 

	
 
#endif
 

	
main.c
Show inline comments
 
@@ -46,13 +46,13 @@ int main(void)
 
 
    // Init GPIO
 
    init_gpio();
 
 
    // Init USB (TODO: Handle plugged/unplugged with external power)
 
    MX_USB_DEVICE_Init();
 
//    set.usb_plugged = 
 
//    set.val.usb_plugged = 
 
 
    // USB startup delay
 
    HAL_Delay(1000);
 
    HAL_GPIO_WritePin(LED_POWER, 1);
 
 
    // Enter into bootloader if up button pressed on boot
 
@@ -64,21 +64,21 @@ int main(void)
 
 
    // Init OLED over SPI
 
    ssd1306_Init();
 
    ssd1306_clearscreen();
 
   
 
    // Default settings 
 
    set.boottobrew = 0;
 
    set.temp_units = TEMP_UNITS_CELSIUS;
 
    set.windup_guard = 1;
 
    set.k_p = 1;
 
    set.k_i = 1;
 
    set.k_d = 1;
 
    set.ignore_tc_error = 0;
 
    set.setpoint_brew = 0;
 
    set.setpoint_steam = 0;
 
    set.val.boottobrew = 0;
 
    set.val.temp_units = TEMP_UNITS_CELSIUS;
 
    set.val.windup_guard = 1;
 
    set.val.k_p = 1;
 
    set.val.k_i = 1;
 
    set.val.k_d = 1;
 
    set.val.ignore_tc_error = 0;
 
    set.val.setpoint_brew = 0;
 
    set.val.setpoint_steam = 0;
 
 
    // Default status
 
    status.temp = 0;
 
    status.temp_frac = 0;
 
    status.state_resume = 0;
 
    status.state = STATE_IDLE;
 
@@ -86,22 +86,22 @@ int main(void)
 
    status.pid_enabled = 0;
 
 
    // Load settings (if any) from EEPROM
 
    restore_settings(&set);
 
 
    // Go to brew instead of idle if configured thusly
 
    if(set.boottobrew)
 
    if(set.val.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);
 
 
    flash_init(&set);
 
    flash_restore(&set);
 
 
    HAL_Delay(1500);
 
    ssd1306_clearscreen();
 
 
 
    // Main loop
 
    while(1)
 
@@ -135,13 +135,13 @@ int16_t update_pid(uint16_t k_p, uint16_
 
  // to prevent the iTerm getting huge from lots of 
 
  //  error, we use a "windup guard" 
 
  // (this happens when the machine is first turned on and
 
  // it cant help be cold despite its best efforts)
 
  // not necessary, but this makes windup guard values 
 
  // relative to the current iGain
 
  int32_t windup_guard_res = set.windup_guard / k_i;  
 
  int32_t windup_guard_res = set.val.windup_guard / k_i;  
 
 
  // Calculate integral term with windup guard 
 
  if (i_state > windup_guard_res) 
 
    i_state = windup_guard_res;
 
  else if (i_state < -windup_guard_res) 
 
    i_state = -windup_guard_res;
 
@@ -198,13 +198,13 @@ void process()
 
 
    HAL_GPIO_TogglePin(LED_POWER);
 
 
        if(status.pid_enabled) 
 
        {
 
            // Get ssr output for next time
 
            int16_t power_percent = update_pid(set.k_p, set.k_i, set.k_d, status.temp, status.temp_frac, status.setpoint);
 
            int16_t power_percent = update_pid(set.val.k_p, set.val.k_i, set.val.k_d, status.temp, status.temp_frac, status.setpoint);
 
            //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;
 
@@ -241,13 +241,13 @@ void process()
 
        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.usb_plugged)
 
//        if(set.val.usb_plugged)
 
//            CDC_Transmit_FS(tempstr, numlen+2);
 
       // while(CDC_Transmit_FS("\r\n", 2) == USBD_BUSY);
 
 
        last_vcp_tx = ticks;
 
    }
 
}
max31855.c
Show inline comments
 
@@ -28,13 +28,13 @@ void max31855_readtemp(SPI_HandleTypeDef
 
        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->ignore_tc_error) {
 
    if(temp_pre & 0b001 && !set->val.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;
 
@@ -69,26 +69,26 @@ void max31855_readtemp(SPI_HandleTypeDef
 
        }
 
        else {
 
            signint = 1;
 
        }
 

	
 
        // Convert to Fahrenheit
 
        if(set->temp_units == TEMP_UNITS_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->temp_offset;
 
            status->temp += set->val.temp_offset;
 
        }
 

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

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
max31865.c
Show inline comments
 
@@ -58,13 +58,13 @@ void max31865_readtemp(SPI_HandleTypeDef
 

	
 
    // 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) {
 
    if(temp_pre & 0b001 && !set->val.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;
 
@@ -84,26 +84,26 @@ void max31865_readtemp(SPI_HandleTypeDef
 
        }
 
        else {
 
            signint = 1;
 
        }
 

	
 
        // Convert to Fahrenheit
 
        if(set->temp_units == TEMP_UNITS_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->temp_offset;
 
            status->temp += set->val.temp_offset;
 
        }
 

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

	
 
// vim:softtabstop=4 shiftwidth=4 expandtab 
states.h
Show inline comments
 
@@ -8,27 +8,30 @@ typedef struct {
 
    uint8_t state;
 
    int32_t setpoint;
 
    uint8_t pid_enabled;
 
    uint8_t tc_errno;
 
} therm_status_t;
 

	
 
typedef struct {
 
    uint8_t boottobrew;
 
    uint8_t temp_units;
 
    uint16_t windup_guard;
 
    uint16_t k_p;
 
    uint16_t k_i;
 
    uint16_t k_d;
 
    int16_t temp_offset;
 
    uint8_t ignore_tc_error;
 
    int16_t setpoint_brew;
 
    int16_t setpoint_steam;
 
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;
 
        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 {
stm32f042c6_flash.ld
Show inline comments
 
@@ -8,13 +8,14 @@ ENTRY(Reset_Handler)
 
_Min_Heap_Size = 0;      /* required amount of heap  */
 
_Min_Stack_Size = 0x400; /* required amount of stack */
 

	
 
/* Specify the memory areas */
 
MEMORY
 
{
 
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 32K
 
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 30K
 
EEPROM (xrw)    : ORIGIN = 0x8007C00, LENGTH = 1K
 
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 6K
 
}
 

	
 
/* Define output sections */
 
SECTIONS
 
{
 
@@ -93,12 +94,13 @@ SECTIONS
 

	
 
    . = ALIGN(4);
 
    _edata = .;        /* define a global symbol at data end */
 
  } >RAM AT> FLASH
 

	
 

	
 

	
 
  /* Uninitialized data section */
 
  . = ALIGN(4);
 
  .bss :
 
  {
 
    /* This is used by the startup in order to initialize the .bss secion */
 
    _sbss = .;         /* define a global symbol at bss start */
 
@@ -120,13 +122,19 @@ SECTIONS
 
    PROVIDE ( _end = . );
 
    . = . + _Min_Heap_Size;
 
    . = . + _Min_Stack_Size;
 
    . = ALIGN(4);
 
  } >RAM
 

	
 

	
 
  .eeprom :
 
  {
 
    . = ALIGN(4);
 
      *(.eeprom)
 
    . = ALIGN(4);
 
  } >EEPROM
 
 
 

	
 
  /* Remove information from the standard libraries */
 
  /DISCARD/ :
 
  {
 
    libc.a ( * )
 
    libm.a ( * )
syslib.c
Show inline comments
 
@@ -66,16 +66,20 @@ void systemclock_config(void)
 
{
 

	
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 

	
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
 
  // Enable HSI48 for main system clock
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI14;
 
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
 
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
 
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
 
 

	
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
 
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
 
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
0 comments (0 inline, 0 general)