//----------------------------------------------------
/**
	\file		ctk/hw/rtc_m41t.h
	\ingroup	hardware
	\ingroup	all

	\brief		??? 
				<<< ALL >>>

	\author		ConTeK s.r.o., Pavel Bazika
	\version	01.00
	\date		17.01.2008
*/
//----------------------------------------------------
#ifndef _RTC_M41T_H_
#define _RTC_M41T_H_

#include <ctktime.h>
#include <twim.h>
#include <timeticks.h>

typedef struct
{
	uint8_t	ssec;		//!< setiny sekundy BCD
	uint8_t	sec;		//!< sekundy BCD, 7.bit ST (stop)
	uint8_t	min;		//!< minuty BCD,
	uint8_t	hour;		//!< hodiny
	uint8_t	wday;		//!< den v tydnu
	uint8_t	mday;		//!< den v mesici
	uint8_t	mon;		//!< mesic
	uint8_t	year;		//!< rok
	uint8_t	control;	//!< rizeni, kalibrace
	uint8_t	watchdog;	//!< watchdog
	uint8_t	alMonth;	//!< alarm mesic
	uint8_t	alDate;		//!< alarm datum
	uint8_t	alHour;		//!< alarm hodina
	uint8_t	alMin;		//!< alarm minuta
	uint8_t	alSec;		//!< alarm sekunda
	uint8_t	flags;		//!< priznaky
	// Dalsi registry jsou nezajimave
} __attribute__((packed)) M41T81Regs;


typedef struct
{
	//! Kanal TWI automtu pro komunikaci s RTC
	//@{
	TWIDESC *		Twi;
	uint8_t			TwiChannel;

	uint8_t			TwiAddr;
	//@}

	//! Registry M41T81
	//@{
	M41T81Regs		TwiData;
	//@}

	//! Stav automatu
	//@{
	volatile uint8_t		State;

	ticks_t			NextReadTime;
	//@}

	//! Statusove bity RTC						 31							26 25			21 20					 16 15				 12 11				  8 7				  4 3				  0
	uint32_t		StatusBits;			//!<	|  ST| CEB|  CB| OUT|  FT|   S|  CALIBRATION  |BMB4|BMB3|BMB2|BMB1|BMB0| RB1| RB0| AFE|SQWE| ABE|RPT4|RPT5|RPT3|  HT|RPT2|RPT1| WDF|  AF|----|----|----|
	//! Naposledy precteny cas
	ctktime_t		Time;
	//! Priznak pozadavku zapisu casu
	uint8_t			WriteReq;
	//! Zapisovany cas
	ctktime_t		NewTime;

} RTCCTX;

//----------------------------------------------------
/**
	\brief	Inicializace RTC radice a sw automatu

	\param	twiChannel	Čislo komunikacniho kanalu TWI (viz. twim.h)

	\return	 0	Pokud OK
	\return	-1	V pripade chyby Hw
	\return	 1	V pokud OK, ale RTC	bylo nainicializovane na pocatecni hodnotu
*/
//----------------------------------------------------

bool_t InitRTC( TWIDESC * const TwiCtx, const uint8_t twiChannel );


//----------------------------------------------------
/**
	\brief	Nastaveni casu RTC

	\param	time	Aktualni cas
*/
//----------------------------------------------------

void rtcSetTime( const ctktime_t * const t );

//----------------------------------------------------
/**
	\brief	Cteni aktualniho casu RTC

	\param	time	Promenna, do ktere bude zapsan aktualni cas
*/
//----------------------------------------------------

void rtcGetTime( ctktime_t * const t );

//----------------------------------------------------
/**
	\brief	Cteni status bitu RTC

	\param	_stsBits	Promenna, do ktere budou zapsany status bity
*/
//----------------------------------------------------

void rtcGetStatus(uint32_t * const _stsBits );

//----------------------------------------------------
/**
	\brief	Tick automatu obsluhujuciho RTC.

	\noteTuto funkci je potreba volat periodicky (alespon jednou za sekundu)
*/
//----------------------------------------------------

void rtcTick( void );


#endif
