//Target: attiny861 atmega168
#define AVTIME_TIMER 0

#include <avr/boot.h>
#include <avr/io.h>
#include <avr/power.h>

#include <ctype.h>
//#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "avrport.h"
#include "avrtime.h"
#include "avrutil.h"

#include "Test_common.h"

/*****************************************
 Functions
*/

void main (void) {
  uint8_t hex;

  init();
  power_all_disable();
  time_init(1,TIME_CLKPS_1);

  { // time_init() must be done before time_us2clk()
    const uint32_t to_sec  = time_us2clk(1000 * 1000UL);
    uint32_t tss = time_current;
    uint32_t sec = 0;

    while(1) {
      time_get();
      port_run();

      GETHEX;
      if (time_current - tss >= to_sec) {
	tss += to_sec;
	sec++;
      }
      switch (hex) {
      case  0:
      case  1:
      case  2:
      case  3:
      case  4:
	hex = sec;
	break;
      case  5: hex = time_syspsc; break;
      case  6: hex = time_tpsc; break;
      case  7: hex = boot_lock_fuse_bits_get( GET_LOW_FUSE_BITS ) & 0x0f;
      case  8: hex = time_union.t8[0]; break;
      case  9: hex = time_union.t8[1]; break;
      case 10: hex = time_union.t8[2]; break;
      case 11: hex = time_union.t8[3]; break;
      case 12: hex = (time_sysosc >>  0) & 0xff; break;
      case 13: hex = (time_sysosc >>  8) & 0xff; break;
      case 14: hex = (time_sysosc >> 16) & 0xff; break;
      case 15: hex = (time_sysosc >> 24) & 0xff; break;
      }
      WRLED;
    }
  }
}
