/*
Diplomova prace : Palubni pocitac

pcf8563.h

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/

#ifndef PCF8563_H
#define PCF8563_H


#include <avr/io.h>
#include <avr/eeprom.h>
#include "i2csw.h"

#define	PCF8563	0xA2 	// adresa PCF8563

#define PCF_CLK_ADDR    0x0d
#define PCF_CLK_NONE    0
#define PCF_CLK_32768   0|1<<7
#define PCF_CLK_1024    1|1<<7
#define PCF_CLK_32      2|1<<7
#define PCF_CLK_1       3|1<<7

#define PCF_TIME_ADDR       0x02
#define PCF_TIME_VL_BIT    (1<<7)

#define PCF_DEF_RECALIB_SEC -30
#define PCF_DEF_RECALIB_PERID_MONTH 1

typedef struct
{

    char time;
    char bat;

} err_t;

typedef struct
{

    char isSet;
//    char isChng;

    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;


} pcf_t;

typedef struct
{

    char isSet;
    int8_t  sec;
    uint8_t month;


    uint8_t last_hour;
    uint8_t last_day;
    uint8_t last_month;
    uint16_t last_year;

} recalib_t;

void initPCF(void); /* inicializace RTC*/

void getTimePCF(pcf_t * str);   /* vraci posledni zaznamenany cas */

void setTimePCF(pcf_t * str);   /* nastavuje cas */

void setFrekOutPCF(uint8_t reg);    /* nastavi vystup generatoru frekvence */

void setRecalibPCF(int8_t sec,uint8_t perid_month); /* funkce pro rekalibraci casu */

void getRecalibPCF(int8_t * sec,uint8_t *perid_month);  /* funkce vraci rekalibracni data casu */

void timePCFPoll(void);     /* polling funkce, ziskani aktualniho casu  z RTC obvodu */
void timeRecalibPoll(void); /* polling rekalibrace */

void getFromPCF(pcf_t * str);   /* prime cteni do PCF RTC */
void setToPCF(pcf_t * str);     /* primy zapis do RTC */

void getErrors(err_t *err);     /* zaznam chyb */

void testRec(recalib_t * rec);

#endif
