]> git.tdb.fi Git - model-railway-devices.git/blob - firmware/eeprom.c
b9010f0f275d7dd5b1ccf0d67d12a000d3dbd426
[model-railway-devices.git] / firmware / eeprom.c
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <avr/io.h>
9 #include "eeprom.h"
10
11 #define BIT(n) (1<<(n))
12
13 static void eeprom_wait(void)
14 {
15         while(EECR&BIT(EEPE)) ;
16 }
17
18 void eeprom_write(uint16_t addr, uint8_t data)
19 {
20         eeprom_wait();
21         EEARH = addr>>8;
22         EEARL = addr;
23         EEDR = data;
24         EECR = BIT(EEMPE);
25         EECR |= BIT(EEPE);
26         eeprom_wait();
27 }
28
29 uint8_t eeprom_read(uint16_t addr)
30 {
31         eeprom_wait();
32         EEARH = addr>>8;
33         EEARL = addr;
34         EECR = BIT(EERE);
35         return EEDR;
36 }