LoginSignup
0
1

More than 1 year has passed since last update.

Ubuntu 20.04 on Raspberry pi 3 B+ でRTC(DS1307)を設定

Posted at

やり方

必要なパッケージをインストールします。

sudo apt update
sudo apt install i2c-tools libraspberrypi-bin

/boot/firmware/syscfg.txtを開いて、i2cが有効化されているか確認します。

/boot/firmware/syscfg.txt
# This file is intended to be modified by the pibootctl utility. User
# configuration changes should be placed in "usercfg.txt". Please refer to the
# README file for a description of the various configuration files on the boot
# partition.

enable_uart=1
dtparam=audio=on
dtparam=i2c_arm=on
dtparam=spi=on
cmdline=cmdline.txt

dtparam=i2c_arm=onとなっていればOKと思います。たぶん。

DS1307をI2Cのピンに配線します。

DS1307が認識されているか確認します。

sudo i2cdetect -y 1

そうすると、こんな感じに表示されると思います。

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

0x68が今回接続したDS1307のアドレスになります。問題なく接続されていそうです。

/boot/firmware/usercfg.txtに以下の一行を追加します。

/boot/firmware/usercfg.txt
dtoverlay=i2c-rtc,ds1307

再起動します。

sudo reboot

DS1307がOSの管理下に入ったか確認します。

i2cdetect -y 1

そうると、こんな感じに表示されると思います。

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

DS1307のアドレス0x68UUに変わりました。
これで、DS1307がOSの管理下に入った意味となります。

こうなると、hwclockコマンドが使えるようになります。

sudo hwclock      # RTC時刻の表示
sudo hwclock -w   # システム時刻をRTCに書き込み
sudo hwclock -s   # RTC時刻をシステム時刻に書き込み

timedatectlコマンドでもRTC時刻を確認できます。

NTPに接続しているシステムであれば、基本的にシステム時刻は正しいと思うので、cronとか使ってたまにDS1307にシステム時刻を書き込んであげれば良いと思いますが、今回特に何もしなくてもDS1307の時刻がシステム時刻と同期されていたので、timedatectlなどが何か自動的にやってくれているのかもしれません。ひとまずcronには何も設定しないでおきます。

起動時にDS1307の時刻を反映させるために、/etc/udev/rules.d/85-hwclock.rulesというファイルを新規作成して以下の内容を書き込みます。

KERNEL=="rtc0", RUN+="/sbin/hwclock --rtc=$root/$name -s"

再起動して/var/log/syslogを確認してみると

Jan 24 10:33:53 localhost kernel: [    3.066602] rtc-ds1307 1-0068: registered as rtc0
Jan 24 10:33:53 localhost systemd[1]: Starting Create Static Device Nodes in /dev...
Jan 24 10:33:53 localhost kernel: [    3.067799] rtc-ds1307 1-0068: setting system clock to 2023-01-24T01:33:27 UTC (1674524007)
・・・
Jan 24 10:36:21 localhost systemd-timesyncd[608]: Initial synchronization to time server xxx.xxx.xxx.xxx:xxx (xxx.jp).
・・・

のようになっていたので、起動直後にシステム時刻をDS1307の時刻に一旦合わせたあとに、インターネット接続が確立されてからNTP時刻を参照しているような動作になったようです。

あとは、任意にはなりますが、紛らわしい時刻に万が一にもなってほしくない場合はfake-hwclockをアンインストールしておくと良いと思います。

sudo apt -y remove fake-hwclock
sudo dpkg --purge fake-hwclock

参考にしたページ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1