X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=logging.c;h=05716bf1050cdc6d4aa907e7bc3a8d90e181dc47;hb=404cf862f8c6bb413580dce16d0a0bdab1037525;hp=5c5beeb30848f4fd4895a10474cd56c93f705d39;hpb=7d2f4ab4e669b0b0f484604016e93a25d3b9cf33;p=bike-lights.git diff --git a/logging.c b/logging.c index 5c5beeb..05716bf 100644 --- a/logging.c +++ b/logging.c @@ -10,8 +10,10 @@ static unsigned char log_buffer_ee[LOG_BUFFER] EEMEM; static unsigned char log_buffer_count; static unsigned char log_buffer[LOG_BUFFER]; static unsigned char log_state EEMEM; +/* Upper 4 bits are reset count, lower 4 bits are reset reason from MCUSR */ static unsigned char reboot_count EEMEM = 0; static unsigned char can_write_eeprom = 0; +static uint16_t flushed_end; void log_set_state(unsigned char val) { @@ -24,10 +26,13 @@ void log_init() unsigned char r_count; r_count = eeprom_read_byte(&reboot_count); + r_count >>= 4; if (r_count < 5) { r_count++; - eeprom_write_byte(&reboot_count, r_count); + eeprom_write_byte(&reboot_count, + (r_count << 4) | (MCUSR & 0xF)); + MCUSR = 0; can_write_eeprom = 1; } else { //eeprom_write_byte(&log_state, 0xFF); @@ -36,6 +41,7 @@ void log_init() log_set_state(1); log_buffer_count = 0; + flushed_end = 0; } void log_byte(unsigned char byte) { @@ -57,16 +63,18 @@ void log_word(uint16_t word) { void log_flush() { unsigned char i; - log_buffer_count = LOG_BUFFER; - if (!can_write_eeprom) return; - for (i=0; i < LOG_BUFFER; i++) { + for (i=flushed_end; i < log_buffer_count; i++) { eeprom_write_byte(&log_buffer_ee[i], log_buffer[i]); } - log_set_state(0x42); + + flushed_end = i; + + if (flushed_end == LOG_BUFFER) + log_set_state(0x42); } #endif