]> git.tdb.fi Git - model-railway-devices.git/blobdiff - common/eeprom.c
Reorganize the directory structure
[model-railway-devices.git] / common / eeprom.c
diff --git a/common/eeprom.c b/common/eeprom.c
new file mode 100644 (file)
index 0000000..9027ed0
--- /dev/null
@@ -0,0 +1,29 @@
+#include <avr/io.h>
+#include "eeprom.h"
+
+#define BIT(n) (1<<(n))
+
+static void eeprom_wait(void)
+{
+       while(EECR&BIT(EEPE)) ;
+}
+
+void eeprom_write(uint16_t addr, uint8_t data)
+{
+       eeprom_wait();
+       EEARH = addr>>8;
+       EEARL = addr;
+       EEDR = data;
+       EECR = BIT(EEMPE);
+       EECR |= BIT(EEPE);
+       eeprom_wait();
+}
+
+uint8_t eeprom_read(uint16_t addr)
+{
+       eeprom_wait();
+       EEARH = addr>>8;
+       EEARL = addr;
+       EECR = BIT(EERE);
+       return EEDR;
+}