XilinxProcessorIPLib/drivers/rtcpsu/src/xrtcpsu.c
void XRtcPsu_CalculateCalibration(XRtcPsu *InstancePtr, u32 TimeReal,
	u32 CrystalOscFreq)
{
	u32 ReadTime;
	u32 SetTime;
	u32 Cprev;
	u32 Fprev;
	u32 Cnew;
	u32 Fnew;
	u32 Calibration;

	Xil_AssertVoid(TimeReal != 0U);

	Xil_AssertVoid(CrystalOscFreq != 0U);

	ReadTime = XRtcPsu_GetCurrentTime(InstancePtr);
	SetTime = XRtcPsu_GetLastSetTime(InstancePtr);
	Calibration = XRtcPsu_GetCalibration(InstancePtr);
	/*
	 * When board gets reseted, Calibration value is zero
	 * and Last setTime will be marked as 1st  second. This implies
	 * CurrentTime to be in few seconds say something in tens. TimeReal will
	 * be huge, say something in thousands. So to prevent
	 * such reset case, Cnew
	 * and Fnew will not be calculated.
	 */
	if ((Calibration == 0U) ||
		(CrystalOscFreq != InstancePtr->OscillatorFreq)) {
		Cnew = CrystalOscFreq - (u32)1;
		Fnew = 0U;
	} else {
		float Xf;
		Cprev = Calibration & XRTC_CALIB_RD_MAX_TCK_MASK;
		Fprev = (Calibration & XRTC_CALIB_RD_FRACTN_DATA_MASK) >>
			XRTC_CALIB_RD_FRACTN_DATA_SHIFT;

		Xf = (float)((ReadTime - SetTime) / (TimeReal - SetTime));
		Xf = Xf * (float)((Cprev+1U) + ((Fprev+1U)/16U));

		Cnew = (u32)(Xf) - (u32)1;
		Fnew = XRtcPsu_RoundOff((Xf - (u32)Xf) * (float)16U) - (u32)1;
	}

	Calibration = (Fnew << XRTC_CALIB_RD_FRACTN_DATA_SHIFT) + Cnew;
	Calibration |= XRTC_CALIB_RD_FRACTN_EN_MASK;

	InstancePtr->CalibrationValue = Calibration;
	InstancePtr->OscillatorFreq = CrystalOscFreq;
}
/****************************************************************************/
/**
 *
 * This function calculates the calibration value depending on the actual
 * realworld time and also helps in deriving new calibration value if
 * the user wishes to change his oscillator frequency.TimeReal is generally the
 * internet time with EPOCH time as reference i.e.,1/1/1970 1st second.
 * But this RTC driver assumes start time from 1/1/2000 1st second. Hence,if
 * the user maps the internet time InternetTimeInSecs, then he has to use
 * XRtcPsu_SecToDateTime(InternetTimeInSecs,&InternetTime),
 * TimeReal = XRtcPsu_DateTimeToSec(InternetTime)
 * consecutively to arrive at TimeReal value.
 *
 * @param	InstancePtr is a pointer to the XRtcPsu instance.
 * @param	TimeReal is the actual realworld time generally an
 *		network time / Internet time in seconds.
 *
 * @param	CrystalOscFreq is the Oscillator new frequency.
 *		Say, If the user is going with the typical
 *		32768Hz, then he inputs the same
 *		frequency value.
 *
 * @return	None.
 *
 * @note		After Calculating the calibration register,
 *			user / application has to
 *			call again CfgInitialize API to bring the
 *			new calibration into effect.
 *
 *****************************************************************************/
This function calculates the calibration value depending on the actual
realworld time and also helps in deriving new calibration value if
the user wishes to change his oscillator frequency.TimeReal is generally the
internet time with EPOCH time as reference i.e.,1/1/1970 1st second.
But this RTC driver assumes start time from 1/1/2000 1st second.

@param       InstancePtr is a pointer to the XRtcPsu instance.
@param       TimeReal is the actual realworld time generally an
network time / Internet time in seconds.

@param       CrystalOscFreq is the Oscillator new frequency.
Say, If the user is going with the typical
32768Hz, then he inputs the same
frequency value.

@return      None.

@note                After Calculating the calibration register,
user / application has to
call again CfgInitialize API to bring the
new calibration into effect.
This function calculates the calibration value depending on the actual realworld time and also helps in deriving new calibration value if the user wishes to change his oscillator frequency . TimeReal is generally the internet time with EPOCH time as reference i . e . 1 / 1 / 1970 1st second . But this RTC driver assumes start time from 1 / 1 / 2000 1st second . @param InstancePtr is a pointer to the XRtcPsu instance . @param TimeReal is the actual realworld time generally an network time / Internet time in seconds . @param CrystalOscFreq is the Oscillator new frequency . Say If the user is going with the typical 32768Hz then he inputs the same frequency value . @return None . @note After Calculating the calibration register user / application has to call again CfgInitialize API to bring the new calibration into effect .
void XRtcPsu_CalculateCalibration(XRtcPsu *InstancePtr, u32 TimeReal,
	u32 CrystalOscFreq)
{
	u32 ReadTime;
	u32 SetTime;
	u32 Cprev;
	u32 Fprev;
	u32 Cnew;
	u32 Fnew;
	u32 Calibration;
	Xil_AssertVoid(TimeReal != 0U);
	Xil_AssertVoid(CrystalOscFreq != 0U);
	ReadTime = XRtcPsu_GetCurrentTime(InstancePtr);
	SetTime = XRtcPsu_GetLastSetTime(InstancePtr);
	Calibration = XRtcPsu_GetCalibration(InstancePtr);
	if ((Calibration == 0U) ||
		(CrystalOscFreq != InstancePtr->OscillatorFreq)) {
		Cnew = CrystalOscFreq - (u32)1;
		Fnew = 0U;
	} else {
		float Xf;
		Cprev = Calibration & XRTC_CALIB_RD_MAX_TCK_MASK;
		Fprev = (Calibration & XRTC_CALIB_RD_FRACTN_DATA_MASK) >>
			XRTC_CALIB_RD_FRACTN_DATA_SHIFT;
		Xf = (float)((ReadTime - SetTime) / (TimeReal - SetTime));
		Xf = Xf * (float)((Cprev+1U) + ((Fprev+1U)/16U));
		Cnew = (u32)(Xf) - (u32)1;
		Fnew = XRtcPsu_RoundOff((Xf - (u32)Xf) * (float)16U) - (u32)1;
	}
	Calibration = (Fnew << XRTC_CALIB_RD_FRACTN_DATA_SHIFT) + Cnew;
	Calibration |= XRTC_CALIB_RD_FRACTN_EN_MASK;
	InstancePtr->CalibrationValue = Calibration;
	InstancePtr->OscillatorFreq = CrystalOscFreq;
}
void XRtcPsu_CalculateCalibration ( XRtcPsu * InstancePtr , u32 TimeReal , u32 CrystalOscFreq ) { u32 ReadTime ; u32 SetTime ; u32 Cprev ; u32 Fprev ; u32 Cnew ; u32 Fnew ; u32 Calibration ; Xil_AssertVoid ( TimeReal != 0U ) ; Xil_AssertVoid ( CrystalOscFreq != 0U ) ; ReadTime = XRtcPsu_GetCurrentTime ( InstancePtr ) ; SetTime = XRtcPsu_GetLastSetTime ( InstancePtr ) ; Calibration = XRtcPsu_GetCalibration ( InstancePtr ) ; if ( ( Calibration == 0U ) || ( CrystalOscFreq != InstancePtr -> OscillatorFreq ) ) { Cnew = CrystalOscFreq - ( u32 ) 1 ; Fnew = 0U ; } else { float Xf ; Cprev = Calibration & XRTC_CALIB_RD_MAX_TCK_MASK ; Fprev = ( Calibration & XRTC_CALIB_RD_FRACTN_DATA_MASK ) >> XRTC_CALIB_RD_FRACTN_DATA_SHIFT ; Xf = ( float ) ( ( ReadTime - SetTime ) / ( TimeReal - SetTime ) ) ; Xf = Xf * ( float ) ( ( Cprev + 1U ) + ( ( Fprev + 1U ) / 16U ) ) ; Cnew = ( u32 ) ( Xf ) - ( u32 ) 1 ; Fnew = XRtcPsu_RoundOff ( ( Xf - ( u32 ) Xf ) * ( float ) 16U ) - ( u32 ) 1 ; } Calibration = ( Fnew << XRTC_CALIB_RD_FRACTN_DATA_SHIFT ) + Cnew ; Calibration |= XRTC_CALIB_RD_FRACTN_EN_MASK ; InstancePtr -> CalibrationValue = Calibration ; InstancePtr -> OscillatorFreq = CrystalOscFreq ; }
This function calculates the calibration value depending on the actual
 realworld time and also helps in deriving new calibration value if
 the user wishes to change his oscillator frequency.TimeReal is generally the
 internet time with EPOCH time as reference i.e.,1/1/1970 1st second.
This function calculates the calibration value depending on the actual realworld time and also helps in deriving new calibration value if the user wishes to change his oscillator frequency . TimeReal is generally the internet time with EPOCH time as reference i . e . 1 / 1 / 1970 1st second .
/*
	 * When board gets reseted, Calibration value is zero
	 * and Last setTime will be marked as 1st  second. This implies
	 * CurrentTime to be in few seconds say something in tens. TimeReal will
	 * be huge, say something in thousands. So to prevent
	 * such reset case, Cnew
	 * and Fnew will not be calculated.
	 */