/** * \file persistentCounter.c * \author Daniel Otte * \brief a persistent 24-bit counter in EEPROM for ATmega µC * */ #include #include "../compat/interrupt.h" /* cli() & sei() */ #include "../compat/eeprom.h" #include "../config.h" #ifdef ERROR_HANDLING #include "error-handling.h" #define PERSISTENT_COUNTER_OVERFLOW (void*)0, 2,4,1 #define PERSISTENT_COUNTER_WRITER_ERROR (void*)0, 2,4,2 #endif #define RING_SIZE 168 uint8_t ring_idx = 0xff; uint16_t EEMEM B08_23; uint8_t EEMEM B0_7[RING_SIZE]; #ifdef INIT_EEPROM void init_buffer(void){ uint8_t i; eeprom_busy_wait(); eeprom_write_word(&B08_23, 0x0000); for(i=0; i>16)&0xff); } eeprom_busy_wait(); eeprom_write_byte(&(((uint8_t*)&B08_23)[0]),((u+1)>>8)&0xff); } /* set least significant byte (in ringbuffer) */ ring_idx = (ring_idx+1)%RING_SIZE; eeprom_busy_wait(); eeprom_write_byte(&(B0_7[ring_idx]),(u+1)&0xff); eeprom_busy_wait(); if(u+1 != percnt_get()){ #ifdef ERROR_HANDLING error(PERSISTENT_COUNTER_WRITER_ERROR); #endif } sei(); }