//----------------------------------------------------
/**
	\file	pccomm.c

	\brief	Knihovna seriove komunikaci
			<<< ALL >>>
			
	\author	ConTeK s.r.o., Jakub Kastner
	\version	01.01
	\date	26.07.2007
*/
//----------------------------------------------------

#include "pccomm.h"
#include "gvariables.h"
#include "datacom.h"

#include "modbusL2sl.h"
#include "modbusA.h"

#ifdef DBGUART_ENABLE
 #include <dbguart.h>
#endif // DBGUART_ENABLE

#ifdef VIRTUART
  VIRTDESC		urt; 			//!< virtualni uart
//  VIRTDESC		modbus;
  UARTDESC		modbus;
#else
  UARTDESC		urt;		//!< fyzicky uart
  USARTDESC		modbus;
#endif// VIRTUART

MODBUS_CTX	modbusL2;
MODBUS_LA_CTX modbusA;

CLP1ADESC	clp1;			//!< linkova vrstva a
CLP2ADESC	clp2;			//!< linkova vrstva b
CTPDESC		ctp;			//!< aplikacni vrstva

//-----------------------------------------------------
/**
	\brief	Fast tick ladicich vypisu
*/
//-----------------------------------------------------

void PcCommTick(void)
{
	if (ctp.flRunning)
	{
		ctp.Tick(&ctp);

	}

	modbusL2.Tick( &modbusL2 );

}


bool_t PcCommInitialize(void)
{
	bool_t status;

#ifndef VIRTUART
	status = UartInitialize( &urt, 2, UARTSPD_115200, CMM_BITS_8, CMM_PARITY_NONE, CMM_STOPBIT_ONE, CMM_MODE_RS232, 100 );
	status &= UsartInitialize( &modbus, 2, USARTSPD_38400, CMM_BITS_8, CMM_PARITY_EVEN, CMM_STOPBIT_ONE, 2 );

#else
	status = VirtInitialize( &urt, _T("SensAmteoA"), _T("SensAmteoB"), 10 );
	status = UartInitialize( &modbus, 7, UARTSPD_38400, CMM_BITS_8, CMM_PARITY_EVEN, CMM_STOPBIT_ONE, CMM_MODE_RS485, 5 );
//	status &=  VirtInitialize( &modbus, _T("MBB"), _T("MBA"), INTER_FRAME_DELAY );
#endif

	Clp1AInitialize( &clp1, (COMMDESC *)&urt.Parent );
	Clp2AInitialize( &clp2, (COMMDESC *)&urt, CRC16_CCITT, FALSE, 0xFFFF );
	CtpInitialize( &ctp, (COMMDESC *)&urt);

	MBInitialize( &modbusL2, (COMMDESC *)&modbus.Parent );
	MBLAInitialize( &modbusA, &modbusL2 ); 

	return (status);
}

void PcCommFinalize(void)
{
	if (ctp.flRunning)
	{
#ifdef VIRTUART
		VirtFinalize(&urt);
#else
		UartFinalize(&urt);
#endif // DBG_VIRT

		Clp1AFinalize(&clp1);
		Clp2AFinalize(&clp2);
		CtpFinalize(&ctp);
	}
}
