void clock_set_time(timestamp_t new_sync_timestamp) { /* The clock was synced */ if (sync_timestamp) { delta = new_sync_timestamp - sync_timestamp; #if defined(CLOCK_NTP_ADJUST_SUPPORT) && !defined(CLOCK_CPU_SUPPORT) NTPADJDEBUG("sync timestamp delta is %d\n", delta); if (delta < -300 || delta > 300) NTPADJDEBUG("eeek, delta too large. " "not adjusting.\n"); else if (sync_timestamp != clock_timestamp) NTPADJDEBUG("our clock is not up with ntp clock.\n"); else if (NTP_RESYNC_PERIOD == -delta) NTPADJDEBUG("-delta equals resync period, eeek!? " "clock isn't running at all.\n"); else { uint32_t new_value = TC1_COUNTER_COMPARE; new_value *= NTP_RESYNC_PERIOD; new_value /= NTP_RESYNC_PERIOD + delta; NTPADJDEBUG("new OCR1A value %d\n", new_value); TC1_COUNTER_COMPARE = new_value; } #endif /* CLOCK_NTP_ADJUST_SUPPORT */ } sync_timestamp = new_sync_timestamp; n_sync_timestamp = new_sync_timestamp; n_sync_tick = TIMER_8_AS_1_COUNTER_CURRENT; #if defined(CLOCK_DATETIME_SUPPORT) || defined(DCF77_SUPPORT) || defined(CLOCK_DATE_SUPPORT) || defined(CLOCK_TIME_SUPPORT) clock_reset_dst_change(); #endif /* Allow the clock to jump forward, but not to go backward * except the time difference is greater than 5 minutes */ if (sync_timestamp > clock_timestamp || (clock_timestamp - sync_timestamp) > 300) clock_timestamp = sync_timestamp; #ifdef I2C_DS13X7_SUPPORT i2c_ds13x7_sync(sync_timestamp); #endif #ifdef I2C_PCF8583_SUPPORT i2c_pcf8583_set(sync_timestamp); #endif #ifdef NTP_SUPPORT ntp_timer = NTP_RESYNC_PERIOD; #endif }
void clock_set_time(uint32_t new_sync_timestamp) { #ifdef CLOCK_NTP_ADJUST_SUPPORT /* The clock was synced */ if (sync_timestamp) { delta = new_sync_timestamp - sync_timestamp; NTPADJDEBUG ("sync timestamp delta is %d\n", delta); if (delta < -300 || delta > 300) NTPADJDEBUG ("eeek, delta too large. " "not adjusting.\n"); else if (sync_timestamp != timestamp) NTPADJDEBUG ("our clock is not up with ntp clock.\n"); else if (NTP_RESYNC_PERIOD == -delta) NTPADJDEBUG ("-delta equals resync period, eeek!? " "clock isn't running at all.\n"); else { uint32_t new_value = OCR1A; new_value *= NTP_RESYNC_PERIOD; new_value /= NTP_RESYNC_PERIOD + delta; NTPADJDEBUG ("new OCR1A value %d\n", new_value); OCR1A = new_value; } } #endif /* CLOCK_NTP_ADJUST_SUPPORT */ sync_timestamp = new_sync_timestamp; n_sync_timestamp = new_sync_timestamp; n_sync_tick = TCNT2; /* Allow the clock to jump forward, but not to go backward * except the time difference is greater than 5 minutes */ if (sync_timestamp > timestamp || (timestamp - sync_timestamp) > 300) timestamp = sync_timestamp; #ifdef WHM_SUPPORT if (startup_timestamp == 0) startup_timestamp = sync_timestamp; #endif #ifdef I2C_DS1337_SUPPORT i2c_ds1337_sync(sync_timestamp); #endif #ifdef NTP_SUPPORT ntp_timer = NTP_RESYNC_PERIOD; #endif }
void periodic_init (void) { CLOCK_SET_PRESCALER; #ifdef CLOCK_CPU_SUPPORT /* init timer1 to expire after ~20ms, with Normal */ TC1_MODE_OFF; TC1_COUNTER_CURRENT = 65536 - CLOCK_SECONDS; TC1_COUNTER_COMPARE = 65536 - CLOCK_SECONDS + CLOCK_TICKS; TC1_INT_COMPARE_ON; TC1_INT_OVERFLOW_ON; #else #ifdef FREQCOUNT_SUPPORT /* init timer1 to run with full cpu frequency, normal mode, compare and overflow int active */ TC1_PRESCALER_1; freqcount_init(); TC1_INT_COMPARE_ON; TC1_INT_OVERFLOW_ON; #else /* init timer1 to expire after ~20ms, with CTC enabled */ TC1_MODE_CTC; TC1_COUNTER_COMPARE = (F_CPU / CLOCK_PRESCALER / HZ) - 1; TC1_INT_COMPARE_ON; NTPADJDEBUG ("configured OCR1A to %d\n", TC1_COUNTER_COMPARE); #endif #endif }
void periodic_init(void) { /* init timer1 to expire after ~20ms, with CTC enabled */ TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10); OCR1A = (F_CPU/1024/50); NTPADJDEBUG ("configured OCR1A to %d\n", OCR1A); }