+ uint8_t pwm_int: 1;
+ uint8_t adc_int: 1;
+ uint8_t tmr_int: 1;
+}
+intflags;
+
+static void inline led_on()
+{
+ DDRB |= _BV( PB5 );
+}
+
+static void inline led_off()
+{
+ DDRB &= ~_BV( PB5 );
+}
+
+/* ------------ Logging/Debugging ----------- */
+
+unsigned char debug_state EEMEM;
+
+static void inline debug_setstate(unsigned char val)
+{
+ eeprom_write_byte(&debug_state, val);
+}
+
+#define LOG_BUFFER 192
+uint16_t log_buffer[LOG_BUFFER] EEMEM;
+uint16_t log_buffer_count;
+
+static void inline init_log()
+{
+ debug_setstate(1);
+ log_buffer_count = 0;
+}
+
+static void log_word(uint16_t word) {
+ if (log_buffer_count == LOG_BUFFER) {
+ debug_setstate(0x42);
+ log_buffer_count++;
+ }
+ if (log_buffer_count >= LOG_BUFFER)
+ return;
+
+ eeprom_write_word(&log_buffer[log_buffer_count], word);
+ log_buffer_count++;
+}
+
+/* ------------ Timer ----------- */
+
+uint16_t clock = 0;
+
+static void inline init_tmr()
+{
+ TCCR0A = _BV(WGM00);
+ TCCR0B = _BV(CS02) | _BV(CS00); // 1 kHz
+ OCR0A = 10; // 100 Hz
+ TIMSK |= _BV(OCIE0A);
+ DDRA |= _BV( PA0 );
+
+ clock = 0;
+}
+
+static void inline tmr_handler()
+{
+ unsigned char c = clock & 0x7F;
+ ++clock;
+
+ if (c == 10 || c == 30)
+ led_on();
+
+ if (c == 20 || c == 40)
+ led_off();
+
+ log_word(adcval);
+ if (adcval != 0xFFEE) {
+ adcval = 0xFFEE;
+
+ ADCSRA |= _BV(ADIE) | _BV(ADSC);
+ }
+}
+
+ISR(TIMER0_COMPA_vect)
+{
+ tmr_handler();
+}
+
+/* ------------ PWM ----------- */
+
+static void inline init_pwm()
+{
+ /* Async clock */
+ PLLCSR = _BV(LSM) | _BV(PLLE);
+ _delay_ms(1);
+ while (PLLCSR & _BV(PLOCK) == 0)
+ ;
+ PLLCSR |= _BV(PCKE);