• 追加された行はこの色です。
  • 削除された行はこの色です。
  • Elec/App/RasPi/RTC へ行く。

#contents

*はじめに [#o219512a]
Raspberry Piで機器を作っていると、地味に困るのが時計合わせです。
とくにネットワークにつなげられないと、NTPで時刻取得ができないので、
データのログを取るときに、嘘っぱちの時刻を使わなければならなくなってしまいます。

ここでは、外付けのリアルタイムクロックユニットを使って、簡単に時刻を保持する機能を追加します。

N番煎じ感はありますが、覚え書きとI2C通信の勉強ついでに・・・


*実験環境 [#n78b3ba7]


*RTCユニットの製作 [#j02fbb7c]

秋月電子で一番安価に購入できるIC「DS1307」を選定しました。
DS1307のデータシートを参考にしています。
|&ref("01_circuit.png",,80%,);|


-試作した基板の外観
|&ref("02_Board_a.jpg",,50%,);|&ref("02_Board_b.jpg",,50%,);|
|実装・表|実装・裏|


*Raspberry Piとの接続 [#ue74e479]

|&ref("pi3_gpio.png",,30%,);|


|Raspberry Pi ピン番号[機能]||RTC ユニット接続先|h
|Pin03 [SDA1, I2C] |→|SDA|
|Pin05 [SDA1, I2C] |→|SDA|
|Pin04 [GND] |→|GND|
|Pin06 [+5V] |→|Vcc|


*Raspbianの初期設定 [#nf7dbf66]

-1.I2Cツールのインストール
 $ sudo apt-get install i2c-tools

-2.Raspberri Pi Configの設定を変更
 $ sudo raspi-config
++'''「9 Advanced Options Configure advanced settings」'''を選択
++'''「A7 I2C             Enable/Disable automatic loading of I2C kernel module」'''を選択
++'''「Would you like the ARM I2C interface to be enabled?」'''→''「YES」を選択''
++'''「Would you like the I2C kernel module to be loaded by default?」'''→''「YES」を選択''


-3./etc/modules に、設定を追記
 $ sudo nano /etc/modules
 
 # /etc/modules: kernel modules to load at boot time.
 #
 # This file contains the names of kernel modules that should be loaded
 # at boot time, one per line. Lines beginning with "#" are ignored.
 
 snd-bcm2835
 i2c-dev
 rtc-ds1307
これで起動時に、自動的にモジュールがロードされます。

-4.起動時に、ds1307をI2Cデバイスとして有効にする記述を追記
 $ sudo nano /etc/rc.local
 
 #!/bin/sh -e
 #
 # rc.local
 #
 # This script is executed at the end of each multiuser runlevel.
 # Make sure that the script will "exit 0" on success or any other
 # value on error.
 #
 # In order to enable or disable this script just change the execution
 # bits.
 #
 # By default this script does nothing.
 
 # Print the IP address
 _IP=$(hostname -I) || true
 if [ "$_IP" ]; then
   printf "My IP address is %s\n" "$_IP"
 fi 
 
 echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device 
 
 exit 0

-5.再起動する


*RTCと通信してみる [#t70ec231]

''注意:ボタン電池をつなげていない状態では、I2Cの通信の応答がありません。必ずボタン電池をつなげておいてください''
 pi@raspberrypi:~ $ sudo hwclock
 hwclock: ioctl(RTC_RD_TIME) to /dev/rtc to read the time failed: Input/output error
↑こんなエラーメッセージが出ます。


-1.まず、DS1307のドライバが組み込まれていることを確認。
 pi@raspberrypi:~ $ dmesg | grep -i ds1307
 [   15.326130] rtc-ds1307 1-0068: rtc core: registered ds1307 as rtc0
 [   15.326196] rtc-ds1307 1-0068: 56 bytes nvram
 [   15.332385] i2c i2c-1: new_device: Instantiated device ds1307 at 0x68
DS1307のドライバを認識してます。

-2.I2C通信経由で、DS1307を認識しているか確認。
 pi@raspberrypi:~ $ 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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
 70: -- -- -- -- -- -- -- --
0x68に検出できています。


-3.ハードウェアクロックの確認
 pi@raspberrypi:~ $ sudo hwclock
 hwclock: Timed out waiting for time change.
 hwclock: The Hardware Clock registers contain values that are either invalid (e.g. 50th day of month) or beyond the range we can handle (e.g. Year 2095).
この時点では、まだRTCに時刻を書き込んでおらず、時刻のずれが異常に大きいため、警告が出ています。


-4.システムクロック設定の確認
 pi@raspberrypi:~ $ timedatectl status
       Local time: Fri 2016-09-23 22:54:24 JST
   Universal time: Fri 2016-09-23 13:54:24 UTC
         RTC time: Sat 2000-01-01 00:00:00
        Time zone: Asia/Tokyo (JST, +0900)
      NTP enabled: no
 NTP synchronized: no
  RTC in local TZ: no
       DST active: n/a
この時点では、「RTC time」が2000年のままになっていることが解ります。

-5.システムクロックを、RTCへ同期します。
 pi@raspberrypi:~ $ sudo hwclock -w
 hwclock: Timed out waiting for time change.
 
 pi@raspberrypi:~ $ sudo hwclock -w
一回では上手くいかないので、複数回、同じコマンドを実行します。

-4.システムクロック設定をもう一度確認
 pi@raspberrypi:~ $ timedatectl status
       Local time: Fri 2016-09-23 22:54:38 JST
   Universal time: Fri 2016-09-23 13:54:38 UTC
         RTC time: Fri 2016-09-23 13:54:38
        Time zone: Asia/Tokyo (JST, +0900)
      NTP enabled: no
 NTP synchronized: no
  RTC in local TZ: no
       DST active: n/a
「Universal time」と、「RTC time」が一致しています。これでRTCに時刻を設定完了です。


*その他 [#j057b21f]

**バックアップ電池の寿命 [#a10444b1]
-http://www.edaboard.com/thread34325.html
こちらの考察によると、通常のリチウムボタン電池で、ざっと10年程度持つようです。


*参考 [#n73a3270]
-https://mynotebook.h2np.net/post/1085
-http://qiita.com/key/items/06c2915de55d6155aa54


RIGHT:2016-09-24 (土) 07:54:13
----
[[Elec]]
トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS