X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=common%2Feeprom.c;fp=common%2Feeprom.c;h=9027ed02fbf2809f35136b5bbd5dfc278dd8371e;hb=9c37d18b9c70fdb70dfec453398c4649e9e57586;hp=0000000000000000000000000000000000000000;hpb=49b6b6ad84ec47b4f9eb9ef131975cc5b72372a2;p=model-railway-devices.git diff --git a/common/eeprom.c b/common/eeprom.c new file mode 100644 index 0000000..9027ed0 --- /dev/null +++ b/common/eeprom.c @@ -0,0 +1,29 @@ +#include +#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; +}