XilinxProcessorIPLib/drivers/axiethernet/examples/avb/xavb_rtc_sync.c
void XAvb_Adjust8kClock (u32 BaseAddress, u32 NewOffset) {

  u32 PreviousOffset      = 0;
  u32 OffsetChange        = 0;
  u32 ChangeIn8kPeriods   = 0;
#ifdef DEBUG_XAVB_LEVEL2
  u32 Clock8kOffset       = 0;
#endif

  /** Read the previous offset */
  PreviousOffset = XAvb_ReadReg(BaseAddress, XAVB_RTC_NANOSEC_OFFSET);

  /** Calculate the change in the previous and current RTC ns offset */
  if (PreviousOffset > NewOffset) {
    OffsetChange =  PreviousOffset - NewOffset;
  } else {
    OffsetChange =  NewOffset - PreviousOffset;
  }


  /** Is the adjustment "large"? "large" is chosen here to be one 8k
   * clock period which is a somewhat arbitrary figure */
  if (OffsetChange > XAVB_PERIOD_8KHZ) {

#ifdef DEBUG_XAVB_LEVEL2
    Clock8kOffset = XAvb_ReadReg(BaseAddress, XAVB_RTC_8K_OFFSET_OFFSET);
    xil_printf("\r\nXAvb_Adjust8kClock()");
    xil_printf("\r\nold ns offset: %x"  , PreviousOffset);
    xil_printf("\r\nold Clk8kOffset: %x", Clock8kOffset);
#endif


    /** The value XAVB_PERIOD_8KHZ is one 8k clock period in ns.  We divide the
     * RTC ns offset change by this to get the offset change in a
     * multiple of 8k clock periods, the add 1 so that we always round
     * up.  Then multiply this by XAVB_PERIOD_8KHZ again so that we are always
     * phased aligned to the RTC master (only evey adjust in a multiple
     * of 8k periods.
     */
    ChangeIn8kPeriods = NewOffset / XAVB_PERIOD_8KHZ;
    OffsetChange = (ChangeIn8kPeriods + 1) * XAVB_PERIOD_8KHZ;

    /** Write the results to the 8K clock logic Offset register */
    XAvb_WriteReg(BaseAddress, XAVB_RTC_8K_OFFSET_OFFSET, OffsetChange);

#ifdef DEBUG_XAVB_LEVEL2
    xil_printf("\r\nXAvb_Adjust8kClock()");
    xil_printf("\r\nnew ns offset: %x"  , NewOffset);
    xil_printf("\r\nnew Clk8kOffset: %x", OffsetChange);
#endif

  }
}
/*****************************************************************************/
/***
*
* A function to Adjust the phase offset of the 8k clock
*
* @param  InstancePtr->BaseAddress is the base address of the device
* @param  NewOffset is the newly calculated RTC Offset value
*
* @return None. But the devices RTC Phase Adjustment Register is updated
*
* @note   None.
*
*****************************************************************************/
A function to Adjust the phase offset of the 8k clock
A function to Adjust the phase offset of the 8k clock
void XAvb_Adjust8kClock (u32 BaseAddress, u32 NewOffset) {
  u32 PreviousOffset      = 0;
  u32 OffsetChange        = 0;
  u32 ChangeIn8kPeriods   = 0;
#ifdef DEBUG_XAVB_LEVEL2
  u32 Clock8kOffset       = 0;
#endif
  PreviousOffset = XAvb_ReadReg(BaseAddress, XAVB_RTC_NANOSEC_OFFSET);
  if (PreviousOffset > NewOffset) {
    OffsetChange =  PreviousOffset - NewOffset;
  } else {
    OffsetChange =  NewOffset - PreviousOffset;
  }
  if (OffsetChange > XAVB_PERIOD_8KHZ) {
#ifdef DEBUG_XAVB_LEVEL2
    Clock8kOffset = XAvb_ReadReg(BaseAddress, XAVB_RTC_8K_OFFSET_OFFSET);
    xil_printf("\r\nXAvb_Adjust8kClock()");
    xil_printf("\r\nold ns offset: %x"  , PreviousOffset);
    xil_printf("\r\nold Clk8kOffset: %x", Clock8kOffset);
#endif
    ChangeIn8kPeriods = NewOffset / XAVB_PERIOD_8KHZ;
    OffsetChange = (ChangeIn8kPeriods + 1) * XAVB_PERIOD_8KHZ;
    XAvb_WriteReg(BaseAddress, XAVB_RTC_8K_OFFSET_OFFSET, OffsetChange);
#ifdef DEBUG_XAVB_LEVEL2
    xil_printf("\r\nXAvb_Adjust8kClock()");
    xil_printf("\r\nnew ns offset: %x"  , NewOffset);
    xil_printf("\r\nnew Clk8kOffset: %x", OffsetChange);
#endif
  }
}
void XAvb_Adjust8kClock ( u32 BaseAddress , u32 NewOffset ) { u32 PreviousOffset = 0 ; u32 OffsetChange = 0 ; u32 ChangeIn8kPeriods = 0 ; #ifdef DEBUG_XAVB_LEVEL2 u32 Clock8kOffset = 0 ; #endif PreviousOffset = XAvb_ReadReg ( BaseAddress , XAVB_RTC_NANOSEC_OFFSET ) ; if ( PreviousOffset > NewOffset ) { OffsetChange = PreviousOffset - NewOffset ; } else { OffsetChange = NewOffset - PreviousOffset ; } if ( OffsetChange > XAVB_PERIOD_8KHZ ) { #ifdef DEBUG_XAVB_LEVEL2 Clock8kOffset = XAvb_ReadReg ( BaseAddress , XAVB_RTC_8K_OFFSET_OFFSET ) ; xil_printf ( " \r \n " ) ; xil_printf ( " \r \n " , PreviousOffset ) ; xil_printf ( " \r \n " , Clock8kOffset ) ; #endif ChangeIn8kPeriods = NewOffset / XAVB_PERIOD_8KHZ ; OffsetChange = ( ChangeIn8kPeriods + 1 ) * XAVB_PERIOD_8KHZ ; XAvb_WriteReg ( BaseAddress , XAVB_RTC_8K_OFFSET_OFFSET , OffsetChange ) ; #ifdef DEBUG_XAVB_LEVEL2 xil_printf ( " \r \n " ) ; xil_printf ( " \r \n " , NewOffset ) ; xil_printf ( " \r \n " , OffsetChange ) ; #endif } }
A function to Adjust the phase offset of the 8k clock
A function to Adjust the phase offset of the 8k clock
/** Read the previous offset */ /** Calculate the change in the previous and current RTC ns offset */ /** Is the adjustment "large"? "large" is chosen here to be one 8k
   * clock period which is a somewhat arbitrary figure */ /** The value XAVB_PERIOD_8KHZ is one 8k clock period in ns.  We divide the
     * RTC ns offset change by this to get the offset change in a
     * multiple of 8k clock periods, the add 1 so that we always round
     * up.  Then multiply this by XAVB_PERIOD_8KHZ again so that we are always
     * phased aligned to the RTC master (only evey adjust in a multiple
     * of 8k periods.
     */ /** Write the results to the 8K clock logic Offset register */