updated to work with new gcc and avrlibc
[openmag.git] / src / avrdrivers / eeprom.c
blobf12df3269e550b124eb0054ff3e0bd91e7c462fa
1 /* Copyright (C) 2008 Sean D'Epagnier <sean@depagnier.com>
3 * This Program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 3 of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 * For more information on the GPL, please go to:
18 * http://www.gnu.org/copyleft/gpl.html
21 /* the routines in avrlibc for reading and writing to eeprom are not
22 interrupt safe
24 I disable interrupt 2 (if it was enabled)
27 #include <avr/interrupt.h>
29 #define AVRDATA_SKIP_EEPROM_DEFINE
30 #include <avrdata.h>
32 uint8_t
33 eeprom_read_byte_safe(const uint8_t *addr)
35 uint8_t val;
36 DATA_LOCK;
37 val = eeprom_read_byte(addr);
38 DATA_UNLOCK;
39 return val;
42 uint16_t
43 eeprom_read_word_safe(const uint16_t *addr)
45 DATA_LOCK;
46 uint16_t val = eeprom_read_word(addr);
47 DATA_UNLOCK;
48 return val;
51 void
52 eeprom_read_block_safe(void *pointer_ram, const void *pointer_eeprom,
53 size_t n)
55 int i;
56 for(i = 0; i<n; i++)
57 ((uint8_t*)pointer_ram)[i] =
58 eeprom_read_byte_safe((uint8_t*)pointer_eeprom + i);
61 float
62 eeprom_read_float_safe(float *var)
64 float val;
65 eeprom_read_block_safe(&val, var, sizeof val);
66 return val;
69 void
70 eeprom_write_byte_safe(uint8_t *addr, uint8_t val)
72 DATA_LOCK;
73 eeprom_write_byte(addr, val);
74 DATA_UNLOCK;
77 void
78 eeprom_write_word_safe(uint16_t *addr, uint16_t val)
80 DATA_LOCK;
81 eeprom_write_word(addr, val);
82 DATA_UNLOCK;
85 void
86 eeprom_write_block_safe(const void *pointer_ram, void *pointer_eeprom,
87 size_t n)
89 int i;
90 for(i = 0; i<n; i++)
91 eeprom_write_byte_safe((uint8_t*)pointer_eeprom + i,
92 ((uint8_t*)pointer_ram)[i]);
95 void
96 eeprom_write_float_safe(float *var, float val)
98 eeprom_write_block_safe(&val, var, sizeof val);