Files @ 235f584ead39
Branch filter:

Location: therm/flash.c

Ethan Zonca
More testing of flash stuff that doesn't exactly work
#include "stm32f0xx_hal.h"
#include "ssd1306.h"
#include "stm32f0xx_hal_flash.h"
#include "flash.h"

__attribute__((__section__(".eeprom"))) uint16_t eeprom[512];


static void __flash_write(therm_settings_t* tosave);


#define EEPROM_MAGIC_INDEX 16 
#define EEPROM_MAGIC_VALUE 0xbeef


void flash_save(therm_settings_t* tosave)
{
    ssd1306_clearscreen();
    ssd1306_DrawString("Erase...", 0, 0);
    HAL_Delay(1500);
    ssd1306_DrawString("Save...", 1, 0);
    __flash_write(tosave);
    HAL_Delay(1500);
    ssd1306_DrawString("Done!", 2, 0);
    HAL_Delay(1500);
}


void flash_restore(therm_settings_t *torestore)
{
    ssd1306_clearscreen();
    ssd1306_DrawString("READING SAVE", 1, 0);

    if(eeprom[EEPROM_MAGIC_INDEX] != EEPROM_MAGIC_VALUE)
    {
        ssd1306_DrawString("No data to read!", 2, 0);
        
        return;
    }

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

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

}


static void __flash_write(therm_settings_t* tosave)
{

    // Erase mem    
    HAL_FLASH_Unlock();

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

    CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

    uint16_t i;
//    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();
}



// vim:softtabstop=4 shiftwidth=4 expandtab