alix-usv/eeprom.c

62 lines
2.3 KiB
C

/***************************************************************************
* Copyright (C) 02/2008 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <avr/eeprom.h>
#include <util/crc16.h>
#include "eeprom.h"
static const struct ee_param defaults = DEFAULT_PARAMETERS;
struct ee_param params;
struct ee_param params_in_eeprom EEMEM = DEFAULT_PARAMETERS;
void write_parameters(void)
{
uint8_t i;
uint16_t crc = 0x0000;
uint8_t *tmp = (uint8_t *)&params;
for (i = 0; i < sizeof(struct ee_param) -2; i++)
crc = _crc_ccitt_update(crc, *tmp++);
params.crc16 = crc;
eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
}
uint8_t read_parameters(void)
{
eeprom_read_block(&params, &params_in_eeprom, sizeof(struct ee_param));
uint8_t i;
uint16_t crc = 0x0000;
uint8_t *tmp = (uint8_t *)&params;
for (i = 0; i < sizeof(struct ee_param); i++)
crc = _crc_ccitt_update(crc, *tmp++);
if (crc != 0x0000) {
i = sizeof(struct ee_param);
uint8_t *src = (uint8_t *)&defaults;
uint8_t *dst = (uint8_t *)&params;
while (i--)
*dst++ = *src++;
write_parameters();
return 1;
}
return 0;
}