wr_date 1.48 KB
Newer Older
1 2
#!/bin/sh

Jean-Claude BAU's avatar
Jean-Claude BAU committed
3 4
# First of all, run 'wr_date set host tai', so to fix TAI offset in the kernel. Does not set the time
/wr/bin/wr_date set host tai > /dev/null
5

6
F=/etc/wr_date.conf
7
#CCS=/sys/bus/clocksource/devices/clocksource0/current_clocksource
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

#Each NTP attempts last around 10s
ntp_retries()
{
	S=$1
	N=$2

	for i in `seq $N` ; do # Manual retries
		if ntpd -n -q -p $S -d; then
			success=1
			echo "NTP success"
			return 0
		else
			echo "Retry $i/$N : NTP query failed, unable to contact server ($S)."
		fi
	done
	echo "ERROR: could not reach NTP server '$S' after $N retries"
	return 1
}

ntp_to_hwclk()
{
	S=$1
31
	ntp_ntries=1
32

33 34 35 36
# Get timing mode: Get it from dot-config ad wr_date is started before PPSi
	TM=`grep -e '^CONFIG_TIME_' /wr/etc/dot-config | sed 's/^CONFIG_TIME_\(.*\)[=].*/\1/'`
	
	if [ "$TM" == "GM" ] ; then  
37 38
		ntp_ntries=10
	fi
39
		
40 41 42 43 44 45
	#Request NTP server and set time to FPGA is okay
	if ntp_retries $S ${ntp_ntries}; then
		/wr/bin/wr_date -v set host
		date_set=1
	fi
}
46

47 48 49 50 51 52 53
date_set=0
# check whether there is config file
if [ -f $F ]; then
	# pick the first server, if any
	S=$(grep 'ntpserver' $F | sed 's/ntpserver//' | head -n 1)
	# check whether server found
	if [ -n "$S" ]; then
54
		ntp_to_hwclk "$S"
55 56 57
	fi
fi
# if ntp failed, try to restore date saved before last reboot
58
if [ $date_set = 0 ] && [ -f /update/saved_date ]; then
59 60
	#date is not accurate but no better idea.
	echo "Restore saved time"
61
	date -s `cat /update/saved_date`
62 63 64
	echo "Setting WR time from host time"
	/wr/bin/wr_date set host
fi