Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "Zero/dev/RTC module ds3231"

< Zero‎ | dev
(Created page with "{{Zero_header}} {{Languages|zero/dev/adb}} Radxa Zero > Development > RTC module ds3231 ==== enable i2c overlay...")
 
 
Line 4: Line 4:
  
 
  [[Zero | Radxa Zero]] > [[zero/dev | Development]] > [[zero/dev/RTC module ds3231| RTC module ds3231 ]]
 
  [[Zero | Radxa Zero]] > [[zero/dev | Development]] > [[zero/dev/RTC module ds3231| RTC module ds3231 ]]
 +
  
 
==== enable i2c overlay ====
 
==== enable i2c overlay ====
 
use rsetup to enable I2C_EE-M3
 
use rsetup to enable I2C_EE-M3
 +
if you don't know how to use rsetup, you can refer  [[https://wiki.radxa.com/Rockpi4/dev/use-rsetup-to-enable-common-interface-with-kernel-5.10 here]]
 +
 +
==== pin ====
 +
 +
<pre>
 +
zero                ds3231
 +
pin 3                sda
 +
pin 5                scl
 +
gnd                  gnd
 +
vcc                  vcc
 +
</pre>
 +
 +
then reboot your Radxa Zero
 +
==== checkout device ====
 +
 +
* make sure that i2c_ee-m3 was enabled
 +
<pre>
 +
root@radxa-zero:/home/radxa# ls /dev/i2c-*
 +
/dev/i2c-3  /dev/i2c-5
 +
</pre>
 +
 +
* check the i2c address of the RTC module
 +
 +
<pre>
 +
root@radxa-zero:/home/radxa# i2cdetect -r -y 3
 +
    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
 +
00:                        -- -- -- -- -- -- -- --
 +
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 +
20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
 +
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 +
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 +
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 +
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
 +
70: -- -- -- -- -- -- -- --
 +
</pre>
 +
 +
==== add a new rtc device ====
 +
 +
* add a new rtc device
 +
<pre>
 +
root@radxa-zero:/home/radxa# echo ds3231 0x68 | sudo tee  /sys/class/i2c-adapter/i2c-3/new_device
 +
</pre>
 +
 +
* checkout the new rtc device
 +
<pre>
 +
root@radxa-zero:/home/radxa# ls /dev/rtc*
 +
/dev/rtc  /dev/rtc0 /dev/rtc1
 +
</pre>
 +
 +
==== read time from rtc module ====
 +
<pre>
 +
root@radxa-zero:/home/radxa# hwclock -r -f /dev/rtc1
 +
2000-01-01 00:01:40.083622+08:00
 +
</pre>
 +
 +
==== set time ====
 +
 +
<pre>
 +
root@radxa-zero:/home/radxa# apt-get install ntp -y
 +
root@radxa-zero:/home/radxa# sudo service ntp start
 +
 +
or
 +
date -s "2023-05-09 15:16:35"
 +
 +
</pre>
 +
 +
 +
==== write time to rtc module ====
 +
* write time to rtc module after you make sure the time of linux is right
 +
<pre>
 +
root@radxa-zero:/home/radxa# hwclock -w -f /dev/rtc1
 +
</pre>
 +
 +
* then read the hardware RTC at the time to see if it is correct
 +
<pre>
 +
root@radxa-zero:/home/radxa# hwclock -r -f /dev/rtc1
 +
2023-05-09 15:18:45.390726+08:00
 +
</pre>
 +
 +
* to set the Linux system time to the hardware RTC time
 +
<pre>
 +
hwclock -s -f /dev/rtc1
 +
</pre>
 +
 +
* use timedatectl to view all the time information
 +
<pre>
 +
root@radxa-zero:/home/radxa# timedatectl
 +
              Local time: Tue 2023-05-09 15:21:49 CST
 +
          Universal time: Tue 2023-05-09 07:21:49 UTC
 +
                RTC time: Thu 1970-01-01 00:04:27
 +
                Time zone: Asia/Shanghai (CST, +0800)
 +
System clock synchronized: no
 +
              NTP service: n/a
 +
          RTC in local TZ: yes
 +
 +
Warning: The system is configured to read the RTC time in the local time zone.
 +
        This mode cannot be fully supported. It will create various problems
 +
        with time zone changes and daylight saving time adjustments. The RTC
 +
        time is never updated, it relies on external facilities to maintain it.
 +
        If at all possible, use RTC in UTC by calling
 +
        'timedatectl set-local-rtc 0'.
 +
 +
</pre>
 +
 +
==== set automatic startup ====
 +
 +
<pre>
 +
root@radxa-zero:/home/radxa# touch /etc/rc.local
 +
root@radxa-zero:/home/radxa# chmod 777 /etc/rc.local
 +
root@radxa-zero:/home/radxa# cat /etc/rc.local
 +
#! /bin/bash
 +
 +
echo ds3231 0x68 | sudo tee  /sys/class/i2c-adapter/i2c-3/new_device
 +
sudo hwclock -s -f /dev/rtc1
 +
 +
</pre>

Latest revision as of 07:26, 9 May 2023

 Radxa Zero >  Development >  RTC module ds3231 


enable i2c overlay

use rsetup to enable I2C_EE-M3 if you don't know how to use rsetup, you can refer [here]

pin

zero                 ds3231
pin 3                sda
pin 5                scl
gnd                  gnd
vcc                   vcc

then reboot your Radxa Zero

checkout device

  • make sure that i2c_ee-m3 was enabled
root@radxa-zero:/home/radxa# ls /dev/i2c-*
/dev/i2c-3  /dev/i2c-5
  • check the i2c address of the RTC module
root@radxa-zero:/home/radxa# i2cdetect -r -y 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

add a new rtc device

  • add a new rtc device
root@radxa-zero:/home/radxa# echo ds3231 0x68 | sudo tee  /sys/class/i2c-adapter/i2c-3/new_device
  • checkout the new rtc device
root@radxa-zero:/home/radxa# ls /dev/rtc*
/dev/rtc  /dev/rtc0 /dev/rtc1

read time from rtc module

root@radxa-zero:/home/radxa# hwclock -r -f /dev/rtc1
2000-01-01 00:01:40.083622+08:00

set time

root@radxa-zero:/home/radxa# apt-get install ntp -y
root@radxa-zero:/home/radxa# sudo service ntp start

or
date -s "2023-05-09 15:16:35"


write time to rtc module

  • write time to rtc module after you make sure the time of linux is right
root@radxa-zero:/home/radxa# hwclock -w -f /dev/rtc1
  • then read the hardware RTC at the time to see if it is correct
root@radxa-zero:/home/radxa# hwclock -r -f /dev/rtc1
2023-05-09 15:18:45.390726+08:00
  • to set the Linux system time to the hardware RTC time
 hwclock -s -f /dev/rtc1
  • use timedatectl to view all the time information
root@radxa-zero:/home/radxa# timedatectl
               Local time: Tue 2023-05-09 15:21:49 CST
           Universal time: Tue 2023-05-09 07:21:49 UTC
                 RTC time: Thu 1970-01-01 00:04:27
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: n/a
          RTC in local TZ: yes

Warning: The system is configured to read the RTC time in the local time zone.
         This mode cannot be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

set automatic startup

root@radxa-zero:/home/radxa# touch /etc/rc.local
root@radxa-zero:/home/radxa# chmod 777 /etc/rc.local
root@radxa-zero:/home/radxa# cat /etc/rc.local
#! /bin/bash

echo ds3231 0x68 | sudo tee  /sys/class/i2c-adapter/i2c-3/new_device
sudo hwclock -s -f /dev/rtc1