Open main menu

DAVE Developer's Wiki β

Difference between revisions of "DESK-MX8M-AN-0003: Add an external RTC to SBC ORCA"

(Created page with "{{InfoBoxTop}} {{AppliesTo ORCA AN}} {{AppliesTo ORCA SBC AN}} {{InfoBoxBottom}} {{ImportantMessage|text=This application note has been validated using the '''kit version'''...")
 
(Accessing the rtc device)
 
(5 intermediate revisions by the same user not shown)
Line 27: Line 27:
 
== Linux driver ==
 
== Linux driver ==
  
[[DESK-MX8M-L/General/Release_Notes#DESK-MX8M-L_4.0.0|DESK-MX8M-L 4.0.0]] kernel has already the related <code>rtc-ds1307</code> driver module built and available in the root file system image.
+
[[DESK-MX8M-L/General/Release_Notes#DESK-MX8M-L_4.0.0|DESK-MX8M-L 4.0.0]] kernel has the related <code>rtc-ds1307</code> driver module already built and available in the root file system image.
  
It is enough to connect the RTC board to an I2C and probe the kernel module.
+
It is enough to connect the RTC board to an I2C: for the purposes of this AN, it is possible to use the GROOVE connector which has the four required signals available: SCL, SDA, VDD, GND (see the SBC [[ORCA_SOM/ORCA_Evaluation_Kit/Electrical_and_Mechanical_Documents/Schematics | schematics]] page).
 
 
For the purposes of this AN, it is possible to use the GROOVE connector which has the four required signals available: SCL, SDA, VDD, GND (see the SBC [[ORCA_SOM/ORCA_Evaluation_Kit/Electrical_and_Mechanical_Documents/Schematics | schematics]] page).
 
  
 
Then, the driver has to be enabled in the device tree using the <code>i2c6</code> interface available in the GROOVE connector:
 
Then, the driver has to be enabled in the device tree using the <code>i2c6</code> interface available in the GROOVE connector:
Line 43: Line 41:
 
         status = "okay";
 
         status = "okay";
  
         rtc: ds3231@68 {
+
         ds3231: ds3231@68 {
 
                 compatible = "maxim,ds3231";
 
                 compatible = "maxim,ds3231";
 
                 reg = <0x68>;
 
                 reg = <0x68>;
Line 51: Line 49:
 
</pre>
 
</pre>
  
 +
== Accessing the rtc device ==
 +
Setting the RTC device as <code>rtc0</code> lets the system clock be synchronized. The related section can be added to the device tree as an ''alias'':
  
 +
<pre class="board-terminal">
 +
    aliases {
 +
            /* ds3231 is our default rtc dev (rtc0) */
 +
            rtc0 = &ds3231;
 +
            rtc1 = &snvs_rtc;
 +
    };
 +
</pre>
  
== Accessing the rtc device ==
+
The RTC device is available as <code>/dev/rtc0</code> device and the related time can be read using the <code>hwclock</code> command:
The RTC device is available as <code>/dev/rtc1</code> device and the related time can be read using the <code>hwclock</code> command:
 
  
 
<pre class="workstation-terminal">
 
<pre class="workstation-terminal">
root@desk-mx8mp:~# dmesg | grep rtc1
+
root@desk-mx8mp:~# dmesg | grep rtc0
[    5.794607] rtc-ds1307 5-0068: registered as rtc1
+
[    6.949783] rtc-ds1307 5-0068: registered as rtc0
root@desk-mx8mp:~# hwclock -f /dev/rtc1
+
root@desk-mx8mp:~# hwclock -f /dev/rtc0
 
2024-03-06 16:18:28.182912+01:00
 
2024-03-06 16:18:28.182912+01:00
 
root@desk-mx8mp:~#
 
root@desk-mx8mp:~#
</pre>
 
 
A proper ''systemd'' script can be used for sysnchronizing the system clock from the hardware rtc device:
 
 
<pre>
 
[Unit]
 
Description=Synchronise System clock to hardware RTC
 
DefaultDependencies=no
 
After=systemd-modules-load.service
 
Before=systemd-journald.service time-sync.target sysinit.target shutdown.target
 
Conflicts=shutdown.target
 
 
[Service]
 
Type=oneshot
 
RemainAfterExit=yes
 
ExecStart=/etc/hwrtc.sh
 
RestrictRealtime=yes
 
 
[Install]
 
WantedBy=sysinit.target
 
</pre>
 
 
where the startup script is <code>/etc/hwrtc.sh</code> used for probing the <code>rtc-ds1307</code> kernel module:
 
 
<pre>
 
#!/bin/sh
 
 
modprobe rtc-ds1307
 
sleep 1
 
hwclock -f /dev/rtc1 --hctosys
 
 
exit 0
 
 
</pre>
 
</pre>
  

Latest revision as of 14:08, 18 April 2024

Info Box


200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.

Contents

HistoryEdit

Version Date Development Kit version
4.0.0 Mar 2024 DESK-MX8M-L 4.0.0

IntroductionEdit

An external RTC device can be connected to ORCA SBC to keep date & time and synchronizing the system clock at boot time.

As an example, an easy - just ready - RTC small board can be bought from Amazon with the DS3231 RTC device.

 
DS3231 small board with CR2032 battery

Linux driverEdit

DESK-MX8M-L 4.0.0 kernel has the related rtc-ds1307 driver module already built and available in the root file system image.

It is enough to connect the RTC board to an I2C: for the purposes of this AN, it is possible to use the GROOVE connector which has the four required signals available: SCL, SDA, VDD, GND (see the SBC schematics page).

Then, the driver has to be enabled in the device tree using the i2c6 interface available in the GROOVE connector:

/* groove connector */
&i2c6 {
        clock-frequency = <100000>;
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_i2c6>;
        status = "okay";

        ds3231: ds3231@68 {
                compatible = "maxim,ds3231";
                reg = <0x68>;
                status = "okay";
        };
};

Accessing the rtc deviceEdit

Setting the RTC device as rtc0 lets the system clock be synchronized. The related section can be added to the device tree as an alias:

    aliases {
            /* ds3231 is our default rtc dev (rtc0) */
            rtc0 = &ds3231;
            rtc1 = &snvs_rtc;
    };

The RTC device is available as /dev/rtc0 device and the related time can be read using the hwclock command:

root@desk-mx8mp:~# dmesg | grep rtc0
[    6.949783] rtc-ds1307 5-0068: registered as rtc0
root@desk-mx8mp:~# hwclock -f /dev/rtc0
2024-03-06 16:18:28.182912+01:00
root@desk-mx8mp:~#

Now the date command shows the system clock synchronized at any reboot.

root@desk-mx8mp:~# date
Wed Mar  6 16:40:44 CET 2024
root@desk-mx8mp:~#