Skip to content

Instantly share code, notes, and snippets.

@grammy-jiang
Last active January 19, 2023 09:24
Show Gist options
  • Save grammy-jiang/30c67ca00b82a0dd46e3e7b52f44a4db to your computer and use it in GitHub Desktop.
Save grammy-jiang/30c67ca00b82a0dd46e3e7b52f44a4db to your computer and use it in GitHub Desktop.
time configuration on Raspberry Pi with Ubuntu

Time Service

NTP

Server

foo@bar:~$ sudo apt install --yes ntp

Client

foo@bar:~$ sudo apt install --yes i2c-tools
foo@bar:~$ sudo i2cdetect -V
i2cdetect version 4.1
foo@bar:~$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- 36 -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
foo@bar:~$ man i2cdetect
I2CDETECT(8)                            System Manager's Manual                           I2CDETECT(8)

NAME
       i2cdetect - detect I2C chips
...
INTERPRETING THE OUTPUT
       Each cell in the output table will contain one of the following symbols:

       • "--". The address was probed but no chip answered.

       • "UU".  Probing  was  skipped,  because  this  address  is  currently in use by a driver. This
         strongly suggests that there is a chip at this address.

       • An address number in hexadecimal, e.g. "2d" or "4e". A chip was found at this address.
...
OPTIONS
       -y     Disable interactive mode. By default, i2cdetect will wait for a  confirmation  from  the
              user before messing with the I2C bus. When this flag is used, it will perform the opera‐
              tion directly. This is mainly meant to be used in scripts.
...
foo@bar:~$ sudo -- sh -c "echo ds1307 0x68 >> /sys/class/i2c-adapter/i2c-1/new_device"
foo@bar:~$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- 36 -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

Add modules into kernel

  • i2c-bcm2708
  • rtc-ds1307
foo@bar:~$ sudo tee -a /etc/modules > /dev/null <<EOF
i2c-bcm2708
rtc-ds1307
EOF

List all of the modules:

foo@bar:~$ find /lib/modules/$(uname -r) -type f -name '*.ko' | grep -i "i2c-bcm2708\|rtc-ds1307"
/lib/modules/5.4.0-1019-raspi/kernel/drivers/i2c/busses/i2c-bcm2708.ko
/lib/modules/5.4.0-1019-raspi/kernel/drivers/rtc/rtc-ds1307.ko

Save the date and time into RTC

foo@bar:~$ sudo hwclock --systohc

Read the date and time from RTC

foo@bar:~$ sudo hwclock --show
2020-10-03 09:37:19.247334+10:00

Set the data and time from RTC

foo@bar:~$ sudo hwclock --hctosys
foo@bar:~$ man kwclock
HWCLOCK(8)                               System Administration                              HWCLOCK(8)

NAME
       hwclock - time clocks utility
...
FUNCTIONS
...
       -r, --show
       --get
              Read  the  Hardware  Clock and print its time to standard output in the ISO 8601 format.
              The time shown is always in local time, even if you keep your  Hardware  Clock  in  UTC.
              See the --localtime option.

              Showing the Hardware Clock time is the default when no function is specified.

              The --get function also applies drift correction to the time read, based upon the infor‐
              mation in /etc/adjtime.  Do not use this function if the Hardware Clock is  being  modi‐
              fied  by  anything  other  than  the current operating system's hwclock command, such as
              '11 minute mode' or from dual-booting another OS.
...
       -s, --hctosys
              Set the System Clock from the Hardware Clock.  The time read from the Hardware Clock  is
              compensated  to  account  for  systematic drift before using it to set the System Clock.
              See the discussion below, under The Adjust Function.

              The System Clock must be kept in the UTC timescale for date-time  applications  to  work
              correctly  in  conjunction with the timezone configured for the system.  If the Hardware
              Clock is kept in local time then the time read from  it  must  be  shifted  to  the  UTC
              timescale  before  using  it  to set the System Clock.  The --hctosys function does this
              based upon the information in the  /etc/adjtime  file  or  the  command  line  arguments
              --localtime and --utc.  Note: no daylight saving adjustment is made.  See the discussion
              below, under LOCAL vs UTC.

              The kernel also keeps a timezone value, the --hctosys function sets it to  the  timezone
              configured  for  the  system.   The  system timezone is configured by the TZ environment
              variable or the /etc/localtime file, as tzset(3) would  interpret  them.   The  obsolete
              tz_dsttime  field  of  the kernel's timezone value is set to zero.  (For details on what
              this field used to mean, see settimeofday(2).)

              When used in a startup script,  making  the  --hctosys  function  the  first  caller  of
              settimeofday(2)  from  boot,  it  will  set  the  NTP '11 minute mode' timescale via the
              persistent_clock_is_local kernel variable.  If the Hardware Clock's timescale configura‐
              tion  is changed then a reboot is required to inform the kernel.  See the discussion be‐
              low, under Automatic Hardware Clock Synchronization by the Kernel.

              This is a good function to use in one of the system startup scripts before the file sys‐
              tems are mounted read/write.

              This  function  should never be used on a running system. Jumping system time will cause
              problems, such as corrupted filesystem timestamps.  Also, if something has  changed  the
              Hardware  Clock,  like  NTP's  '11 minute mode', then --hctosys will set the time incor‐
              rectly by including drift compensation.

              Drift compensation can be inhibited by setting the drift factor in /etc/adjtime to zero.
              This  setting  will  be persistent as long as the --update-drift option is not used with
              --systohc at shutdown (or anywhere else).  Another way to inhibit this is by  using  the
              --noadjfile option when calling the --hctosys function.  A third method is to delete the
              /etc/adjtime file.  Hwclock will then default to using the UTC timescale for  the  Hard‐
              ware  Clock.   If the Hardware Clock is ticking local time it will need to be defined in
              the file.  This can be done by calling hwclock --localtime --adjust; when  the  file  is
              not present this command will not actually adjust the Clock, but it will create the file
              with local time configured, and a drift factor of zero.

              A condition under which inhibiting hwclock's drift correction may  be  desired  is  when
              dual-booting  multiple  operating  systems.  If while this instance of Linux is stopped,
              another OS changes the Hardware Clock's value, then when this instance is started  again
              the drift correction applied will be incorrect.

              For  hwclock's  drift  correction to work properly it is imperative that nothing changes
              the Hardware Clock while its Linux instance is not running.
...
       -w, --systohc
              Set the Hardware Clock from the System Clock, and update the timestamps in /etc/adjtime.
              With  the --update-drift option also (re)calculate the drift factor.  Try it without the
              option if --systohc fails.  See --update-drift below.
...

Reference

ntp

timedatectl

hwclock

i2c-tools

Articles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment