/*! Time-stamp: <@(#)ATBoardModDVBS2.cpp   07/05/2007 - 15:46:04   P.Hoogervorst>
*********************************************************************
*  @file   : ATBoardModDVBS2.cpp
*
*  Author  : P.Hoogervorst                              Date: 03/04/2008
*
*  Purpose : Implementation of methods for class 
*
*********************************************************************
* Version History:
*
* V 0.10  03/04/2008  BN : First Revision
*
*********************************************************************
*/

/**********************************************************************************************************/
// includes
#include "ATBoardModDVBS2.h"

/**********************************************************************************************************/
// defines

/**********************************************************************************************************/
// functions
// constructor
CATBoardModDVBS2::CATBoardModDVBS2(void)
{
}

// destructor
CATBoardModDVBS2::~CATBoardModDVBS2(void)
{
}

/*
 * Do the one time initialize of the modulator. Is done once at startup.
 */
BOOL CATBoardModDVBS2::Init()
{
	// init modulator
	if (!m_pAtBoard->DvbSource(SRC_INIT, NULL))
		return FALSE;

	return TRUE;
}

/*
* Initialize play for derived class. If the initialization of the play must be changed or expanded by a derived class,
* The derived class should do that in this function
*/
BOOL CATBoardModDVBS2::_InitPlay   (u32			nFileBitrate,
									BOOL		bORemux,
									u32			nOutputBitrate,
									EIOSel		OSel,
									EIOMode		OMode,
									EIOSpiMode	OSpiMode,
									ETSPSize	OTsPSize,
									BOOL		bOHTP,
									BOOL		bOCTP,
									BYTE		nBurstSize)
{

	// set registers
	CAtRegisters RegAcc(m_pAtBoard);
	ATREGISTRY &Registers = RegAcc;

	// get registers from device and store them in the ATBoard object
	m_pAtBoard->GetRegisters();

	// clear loop through
	CLR_BITMASK(Registers.m_RecordConfig, AT_RCONF_LEN);

	// enable remultiplexing
	SET_BITMASK(Registers.m_PlayConfig, AT_PCONF_PCRREST);
	SET_BITMASK(Registers.m_PlayConfig, AT_PCONF_BRREMUX);
	CLR_BITMASK(Registers.m_PlayConfig, AT_PCONF_CTP);

	// set registers to device
	m_pAtBoard->UpdateRegisters();

	
	/*
	*	Reset the modulator parameters, because core is out of sync after start play
	*/
	dvb_at2900_parameters DvbS2Params;

	// set the parameters
	if (!m_pAtBoard->DvbSource(SRC_RESTORE_MOD_DVB, &DvbS2Params))
		return FALSE;

	// set the parameters
	if (!m_pAtBoard->DvbSource(SRC_SET_MOD_PARAMS, &DvbS2Params))
		return FALSE;

	return TRUE;
}

/*
* Initialize recording for derived class. If the initialization of the recording must be changed or expanded by a derived class,
* The derived class should do that in this function
*/
BOOL CATBoardModDVBS2::_InitRec	   (EIOMode		IMode,
									EIOSel		ISel,
									BOOL		bILoop,
									BOOL		bIPassThroughSer,
									BOOL		bIPassThroughSpi,
									BOOL		bITimeStamp,
									EIOSpiMode	ISpiMode)
{
	/*
	*	Reset the modulator parameters, because core is out of sync after start play
	*/
	dvb_at2900_parameters DvbS2Params;

	// set the parameters
	if (!m_pAtBoard->DvbSource(SRC_RESTORE_MOD_DVB, &DvbS2Params))
		return FALSE;

	// set the parameters
	if (!m_pAtBoard->DvbSource(SRC_SET_MOD_PARAMS, &DvbS2Params))
		return FALSE;

	return TRUE;
}


/*
* Set the modulation settings.
*/
BOOL CATBoardModDVBS2::Set(	src_modulation_t			Modulation,
							src_code_rate_t				FECCodeRate,
							src_filter_rolloff_t		FltRollOff,
							src_spectral_inversion_t	Inversion,
							u32							nSymbolRate,
							u32							nPLscrambleSeed,
							src_dvbs_mode_t				DvbModeSel,
							BOOL						bPLscrambleEn,
							BOOL						bISSYIndEn,
							BOOL						bISSYLongByteEn,
							BOOL						bNullPckDelEn,
							BOOL						bPilotInsEn,
							BOOL						bDummyFrameEn)
{
	dvb_at2900_parameters DvbS2Params;

	DvbS2Params.constellation		 = (src_modulation_t)Modulation;
	DvbS2Params.code_rate			 = (src_code_rate_t)FECCodeRate;
	DvbS2Params.alpha_rolloff		 = (src_filter_rolloff_t)FltRollOff;
	DvbS2Params.inversion			 = (src_spectral_inversion_t)Inversion;
	DvbS2Params.input_sel			 = (src_dvbs2_input_sel_t)DVB_S2_TRANSPORT_STREAM;
	DvbS2Params.symbol_rate			 = (u32)nSymbolRate;
	DvbS2Params.PLscramble_seed		 = (u32)nPLscrambleSeed;
	DvbS2Params.dvb_mod_sel			 = (src_dvbs_mode_t)DvbModeSel;
	DvbS2Params.PLscramble_en		 = (u8)bPLscrambleEn;
	DvbS2Params.ISSY_ind_en			 = (u8)bISSYIndEn;
	DvbS2Params.ISSY_long_byte_en	 = (u8)bISSYLongByteEn;
	DvbS2Params.null_pck_del_en		 = (u8)bNullPckDelEn;
	DvbS2Params.CCM_en				 = (u8)TRUE;
	DvbS2Params.SIS_en				 = (u8)FALSE;
	DvbS2Params.MA_type2			 = (u8)0;
	DvbS2Params.short_frame_en		 = (u8)FALSE;
	DvbS2Params.pilot_ins_en		 = (u8)bPilotInsEn;
	DvbS2Params.dummy_frame_en		 = (u8)bDummyFrameEn;

	// set the parameters
	if (!m_pAtBoard->DvbSource(SRC_SET_MOD_PARAMS, &DvbS2Params))
		return FALSE;
	
	// store the parameters
	return m_pAtBoard->DvbSource(SRC_STORE_MOD_DVB, &DvbS2Params);
}

/*
* Get the modulation status of the modulator
*/
BOOL CATBoardModDVBS2::Get(BOOL &bTsInSync, BOOL &bTs204PacketSize, BOOL &bTsOFlow)
{
	// set to defaults
	bTsInSync = FALSE;
	bTs204PacketSize = FALSE;
	bTsOFlow = FALSE;

	src_status_at2900_t eStatus;

	// get the modulation status
	if (!m_pAtBoard->DvbSource(SRC_GET_MOD_STAT, &eStatus))
		return FALSE;

	if (eStatus & DST_HAS_TS_INSYNC)
		bTsInSync = TRUE;
	else
		bTsInSync = FALSE;

	if (eStatus & DST_HAS_TS_204PKT)
		bTs204PacketSize = TRUE;
	else
		bTs204PacketSize = FALSE;

	if (eStatus & DST_HAS_TS_OFLOW)
		bTsOFlow = TRUE;
	else
		bTsOFlow = FALSE;

	return TRUE;
}

