diff --git a/flash.c b/flash.c --- a/flash.c +++ b/flash.c @@ -3,182 +3,76 @@ #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;idata[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;idata[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