Prusa-Firmware/Firmware/eeprom.cpp

105 lines
3.9 KiB
C++
Raw Normal View History

2019-06-20 16:00:29 +00:00
//! @file
//! @date Jun 20, 2019
//! @author Marek Běl
#include "eeprom.h"
#include "Marlin.h"
2019-06-20 16:00:29 +00:00
#include <avr/eeprom.h>
#include <stdint.h>
2019-07-03 17:37:11 +00:00
#include "language.h"
2019-06-20 16:00:29 +00:00
#if 0
template <typename T>
static T eeprom_read(T *address);
template<>
char eeprom_read<char>(char *address)
{
return eeprom_read_byte(reinterpret_cast<uint8_t*>(address));
}
#endif
template <typename T>
static void eeprom_write(T *address, T value);
template<>
void eeprom_write<char>(char *addres, char value)
{
eeprom_write_byte(reinterpret_cast<uint8_t*>(addres), static_cast<uint8_t>(value));
}
template <typename T>
static bool eeprom_is_uninitialized(T *address);
template <>
bool eeprom_is_uninitialized<char>(char *address)
{
return (0xff == eeprom_read_byte(reinterpret_cast<uint8_t*>(address)));
}
bool is_sheet_initialized(uint8_t sheet_num){
2019-07-04 11:21:27 +00:00
return (0xffff != eeprom_read_word(reinterpret_cast<uint16_t*>(&(EEPROM_Sheets_base->
s[sheet_num].z_offset))));
2019-07-04 11:21:27 +00:00
}
2019-06-20 16:00:29 +00:00
void eeprom_init()
{
if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
if (eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
if (eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
if (eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == 0xff) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0);
2019-06-20 16:00:29 +00:00
for (uint_least8_t i = 0; i < (sizeof(Sheets::s)/sizeof(Sheets::s[0])); ++i)
{
bool is_uninitialized = true;
for (uint_least8_t j = 0; j < (sizeof(Sheet::name)/sizeof(Sheet::name[0])); ++j)
{
if (!eeprom_is_uninitialized(&(EEPROM_Sheets_base->s[i].name[j]))) is_uninitialized = false;
}
if(is_uninitialized)
{
SheetName sheetName;
default_sheet_name(i,sheetName);
2019-07-23 09:34:42 +00:00
for (uint_least8_t a = 0; a < sizeof(Sheet::name); ++a){
eeprom_write(&(EEPROM_Sheets_base->s[i].name[a]), sheetName.c[a]);
}
2019-07-23 09:34:42 +00:00
// When upgrading from version older version (before multiple sheets were implemented in v3.8.0)
// Sheet 1 uses the previous Live adjust Z (@EEPROM_BABYSTEP_Z)
if(i == 0){
int last_babystep = eeprom_read_word((uint16_t *)EEPROM_BABYSTEP_Z);
eeprom_write_word(reinterpret_cast<uint16_t *>(&(EEPROM_Sheets_base->s[i].z_offset)), last_babystep);
}
2019-06-20 16:00:29 +00:00
}
}
check_babystep();
2019-06-20 16:00:29 +00:00
}
//! @brief Get default sheet name for index
//!
//! @param[in] index
//! @param[out] sheetName
void default_sheet_name(uint8_t index, SheetName &sheetName)
{
sheetName.c[0] = '1' + index;
for (uint8_t i = 1; i < (sizeof(sheetName.c)/sizeof(sheetName.c[0])); ++i)
{
sheetName.c[i] = '\0';
}
}