//----------------------------------------------------
/**
	\file		ctk/inc/eepromstor.h
	\ingroup	hardware
	\ingroup	all

	\brief		Knihovna pro zabezpeceny pristup k parametrum v EEPROM pameti
				<<< ALL >>>

	\author		ConTeK s.r.o., Ing. Petr Cernin
	\version	01.01
	\date		12.01.2007
*/
//----------------------------------------------------

#ifndef _EEPROMSTOR_H_
#define _EEPROMSTOR_H_

//----------------------------------------------------
/**
	\brief	Knihovna zajistuje nasledujici:
	\note		1) kontrolu platnosti obsahu eeprom
	\note		2) inicializaci dat v eeprom
	\note		3) opravu eeprom podle obsahu backlogu
	\note		4) nacteni dat z eeprom
	\note		5) ulozeni parametru do eeprom s vyuzitim backlogu
*/
//----------------------------------------------------

#include <ctktypes.h>
#include <framstor.h>

//! Kanal TWI rozhrani pouzity pro komunikaci s FRAM
#define EEBLOG_TWI_CHANNEL		0

//! Magicke cislo ulozene v hlavicce dat V EEPROM
#define MAGIC_NUMBER   0xFA3C

//! Adresa hlavicky dat v EEPROM
#define DATA_CONFIG_HEADER_ADDR		0

//! Adresa data DATA_CONFIG v EEPROM
#define DATA_CONFIG_ADDR			(DATA_CONFIG_HEADER_ADDR + sizeof( DATA_HEADER ))

//! Offset struktury polozky Config od zacatku struktury DATA_TREE
#define DATA_CONFIG_OFFSET			offsetof( DATA_TREE, Config )

//! Adresa BackLogu ve FRAM
#define DATA_CONFIG_BACKLOG_ADDR	0


//----------------------------------------------------
//!	\brief	Zahlavi dat ulozenych v EEPROM
//!	\note	Po resetu se kontroluje toto zahlavi ulozene
//!			na adrese DATA_CONFIG_HEADER_ADDR. Pokud neni platne, pak
//!			se provede inicializace EEPROM

typedef struct
{
	uint16_t	Magic;			//!< Cislo MAGIC_NUMBER
	uint16_t	Version;		//!< Verze dat

} DATA_HEADER;


//----------------------------------------------------
//! \name	Definice backlogu
//@{

#define BACKLOG_MAX_SIZE		4	//!< Maximalni velikost dat backlogu


//----------------------------------------------------
//!	Polozka backlogu 

typedef struct
{
	uint16_t	Address;					//!< Adresa dat
	uint16_t	Size;						//!< Delka dat (1..4)
	uint8_t		Data[ BACKLOG_MAX_SIZE ];	//!< Data
	
} BACKLOGITEM;


//----------------------------------------------------
//!	Struktura backlogu

typedef struct
{
	uint8_t		Valid;			//!< Priznak platnosti
	uint16_t	Crc;			//!< Crc polozky
	BACKLOGITEM	Item;			//!< Polozka backlogu
	
} BACKLOG;


//----------------------------------------------------
//!	Kontext automatu pro pristup k parametrum v EEPROM

typedef struct 
{
	volatile bool_t		flRunning;		//!< je inicializovano, po ininializaci na TRUE, pred ukoncenim FALSE
	FRAMSTORDESC	FramStorD;		//!< ukazatel na pristup k zabezpecene fram
	volatile uint8_t	State;			//!< Stav automatu
	uint8_t			Command;		//!< Prikaz
	uint32_t 		Code;			//!< Kod zapisovaneho parametru

	BACKLOG			Blog;			//!< Struktura backlogu
	DATA_HEADER		Header;			//!< Zahlavi dat ulozenych v EEPROM 

} EEPROMSTORDESC;
//@}


//----------------------------------------------------
/**
	\brief	Automat pro pristup k parametrum v EEPROM

	\param	eepromStorD	struktura contextu automatu
*/
//----------------------------------------------------

void EEpromStorageTick( EEPROMSTORDESC * eepromStorD );


//----------------------------------------------------
/**
	\brief	Funkce pro testovani, zda je mozne automatu
			predat prikaz (tj. zavolat napr. eepStorageWriteCode)

	\param	eepromStorD	struktura contextu automatu

	\return	TRUE	automat je schopen akceptovat dalsi prikaz
	\return	FALSE	automat neni schopen
*/
//----------------------------------------------------

bool_t EEpromStorageReady( EEPROMSTORDESC * eepromStorD );


//----------------------------------------------------
/**
	\brief	Funkce pro spusteni sekvence zapisu kopirovani
			parametru z RAM do EEPROM

	\param	eepromStorD	struktura contextu automatu
	\param	code		code parametru
*/
//----------------------------------------------------

void EEpromStorageWriteCode( EEPROMSTORDESC * eepromStorD, uint32_t code );


//----------------------------------------------------
/**
	\brief	Inicializace automatu pro pristup k parametrum v EEPROM

	\param	eepromStorD	struktura contextu automatu
	\param	copyDefault	FALSE	normalni inicializace;
						TRUE	nakopirovani default parametru

	\return	TRUE	inicializace probehla v poradku
	\return	FALSE	chyba
*/
//----------------------------------------------------

bool_t EEpromStorageInitialize( EEPROMSTORDESC * eepromStorD, bool_t copyDefault );


//----------------------------------------------------
/**
	\brief	Ukonceni automatu pro pristup k parametrum v EEPROM

	\param	eepromStorD	struktura contextu automatu
*/
//----------------------------------------------------

void EEpromStorageFianlize( EEPROMSTORDESC * eepromStorD );



#endif // _EEPROMSTOR_H_
