A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eeprom.c 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /***************************************************************************
  2. * Copyright (C) 02/2008 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/eeprom.h>
  20. #include <util/crc16.h>
  21. #include "eeprom.h"
  22. static const struct ee_param defaults = DEFAULT_PARAMETERS;
  23. struct ee_param params;
  24. struct ee_param params_in_eeprom EEMEM = DEFAULT_PARAMETERS;
  25. void write_parameters(void)
  26. {
  27. uint8_t i;
  28. uint16_t crc = 0x0000;
  29. uint8_t *tmp = (uint8_t *)&params;
  30. for (i = 0; i < sizeof(struct ee_param) -2; i++)
  31. crc = _crc_ccitt_update(crc, *tmp++);
  32. params.crc16 = crc;
  33. eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
  34. }
  35. uint8_t read_parameters(void)
  36. {
  37. eeprom_read_block(&params, &params_in_eeprom, sizeof(struct ee_param));
  38. uint8_t i;
  39. uint16_t crc = 0x0000;
  40. uint8_t *tmp = (uint8_t *)&params;
  41. for (i = 0; i < sizeof(struct ee_param); i++)
  42. crc = _crc_ccitt_update(crc, *tmp++);
  43. if (crc != 0x0000) {
  44. i = sizeof(struct ee_param);
  45. uint8_t *src = (uint8_t *)&defaults;
  46. uint8_t *dst = (uint8_t *)&params;
  47. while (i--)
  48. *dst++ = *src++;
  49. write_parameters();
  50. return 1;
  51. }
  52. return 0;
  53. }