#define F_CPU 1000000 // 1MHz q

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <string.h>
#include <stdlib.h>
#include "definice.h"
#include "systime_drv.h"
#include "_usart.h"
#include "_eeprom_wear.h"

int main(void)
{
	PRR0 = 0xBC;

	systime_init();
	SW_5V_init();
	SW_GSM_init();
	SW_VOLT_init();
	USART_Init(C1MHzBaud9600);
	    	
	dc_service_period = eeprom_safe_read_32bit(eeprom_dc_adress);
	
	while (1) 
    {
		while(sleep == 1)
		{
			SMCR |= ((1<<SM0)|(1<<SM1)); 
			cli();
			sleep_enable();
			sei();
			sleep_cpu();
			
			cli();
			if(((uint32_t)(get_systime32()))>=dc_service_period)
				{
					sleep = 0;
					sleep_disable();
					SW_5V_on();
					break;
				}
		}

		if (((uint8_t)(get_systime()-usart_last_service))>=usart_service_period)	// pravidelna kontrola usart
		{
			usart_last_service=get_systime();
			
			znak16=USART_get_char();
			if (znak16<0x0100)	// pokud je znak platny
				zpracuj_znak((char)znak16);
		}

    }
}

