//----------------------------------------------------
/**
	\file		ctk/inc/ucrc16.h
	\ingroup	crc	
	\ingroup	all

	\brief		Funkce pro vypocet CRC16
				<<< ALL >>>
			
	\author		ConTeK s.r.o., Ing. Vladimir Kastner
	\version	01.01
	\date		12.01.2007
*/
//----------------------------------------------------


#ifndef _UCRC16_H_
#define _UCRC16_H_		//!< Definice pritomnosti knihovny

#include <ctktypes.h>

//----------------------------------------------------
/**
	\name	Polynomy:
	\brief	CRC16_IEEE		0x8005		= x16 + x15 + x2 + 1
	\brief	CRC16_MODBUS	CRC16_IEEE
	\brief	CRC16_CCITT		0x1021		= x16 + x12 + x5 + 1
	\brief	CRC16_XMODEM	0x8408		= x16 + x15 + x10 + x3
	\brief	CRC8_CRC		0x07		= x8  + x2  + x + 1

	\note	ConTeK protokol CLP pouziva polyn = CRC16_CCITT,  lBit = FALSE, initVal = 0xFFFF
	\note	SofCon protokol PRT pouziva polyn = CRC16_IEEE,   lBit = TRUE,  initVal = 0x8303
	\note	Modbus protokol CLP pouziva polyn = CRC16_MODBUS, lBit = TRUE,  initVal = 0xFFFF
*/
//----------------------------------------------------
//@{

//#define CRC32_CCITT	0x04C11DB7		

#define CRC16_IEEE			0x8005	//!< Polynom
#define CRC16_IEEE_INIT		0x8303	//!< Inicializacni hodnota

#define CRC16_MODBUS		CRC16_IEEE	//!< Polynom
#define CRC16_MODBUS_INIT	0xFFFF	//!< Inicializacni hodnota

#define CRC16_CCITT			0x1021	//!< Polynom
#define CRC16_CCITT_INIT	0xFFFF	//!< Inicializacni hodnota

#define CRC16_XMODEM		0x8408	//!< Polynom
//#define CRC16_XMODEM_INIT	0xFFFF	// Inicializacni hodnota

//#define CRC8-CRC		$07
//@}

//----------------------------------------------------
//!	\name	Makra definujici pouzivane crc u protokolu
//@{

#define CRC_CONTEK_CLP	CRC16_CCITT, FALSE, CRC16_CCITT_INIT
#define CRC_CCITT		CRC16_IEEE, TRUE, CRC16_IEEE_INIT
#define CRC_SOFCON_PRT	CRC_CCITT
//@}

//----------------------------------------------------
//!	Definice typu crc16 polynomu

typedef uint16_t	crc16_t;

//----------------------------------------------------
/**
	\brief	Funkce pro vypocteni CRC16, nejvyssi bit B15 

	\param	polyn	Generacni polynom
	\param	crc		Dosud spocteny crc
	\param	data	Data, z kterych se spocte nova crc

	\return Nove spocitane crc
*/
//----------------------------------------------------

crc16_t CalcCrc16hb(const crc16_t _polyn, crc16_t _crc, const uint8_t _data);
//----------------------------------------------------
/**
	\brief	Funkce pro vypocteni CRC16, nejvyssi bit B0 

	\param	polyn	Generacni polynom
	\param	crc		Dosud spocteny crc
	\param	data	Data, z kterych se spocte nova crc

	\return Nove spocitane crc
*/
//----------------------------------------------------

crc16_t CalcCrc16lb(const crc16_t _polyn, crc16_t _crc, const uint8_t _data);


//----------------------------------------------------
/**
	\brief	Funkce pro vypocteni CRC16 bufferu 

	\param	polyn	Generacni polynom
	\param	lBit	TRUE pokud nejvyssi bit je B0
	\param	crc		Dosud spocteny crc
	\param	p		Ukazatel na buffer

	\return Nove spocitane crc
*/
//----------------------------------------------------

crc16_t CalcBuffCrc16(crc16_t _polyn, const bool_t _lBit, crc16_t _crc, const uint8_t * _p, uint16_t _size);


#endif // _UCRC16_H_
