香橙派OrangePi 4电脑开发板 在Linux系统下外接DS1307 RTC时钟模块

​​香橙派4开发板采用瑞芯微RK3399芯片,4G内存+16GB emmc存储,支持双频wifi和千兆网口,有mini PCIE接口,支持双路摄像头同时输入,支持四路显示,可配置任意两路同时输出,支持双频异显。具体的功能模块和硬件参数特性可参看下图:

本文将介绍香橙派4在linux系统下如何连接RTC时钟模块:

1 硬件接线

接线如下:


2 功能测试

因为内核已经有ds1307的驱动。所以直接测试就可以。

2.1 用i2c-tools测试是否识别到rtc

安装软件: i2c-tools,这样就可以用 i2cdetect 来检测设备上连接 i2c 设备

sudo apt-get install i2c-tools

这里用的是i2c2,连接好线之后,启动开发板,终端输入以下命令查看地址,

i2cdetect -y 2

已经探测到ds1307 设备,地址是0x68.

执行命令添加rtc设备

echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-2/new_device

 

可以将上述命令写到/etc/rc.local文件里边,这样可以避免每次重新开机都要设置。

 

可以看到生成了rtc1设备,rtc0是板载的rtc。

root@orangepi4:~# ls /dev/rtc*                                                  

/dev/rtc  /dev/rtc0  /dev/rtc1

 

2.2 同步时间

首先设置下时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 

设置当前时间

如果系统接入了网络,会自动同步网络时间。

也可自己设置正确的时间。

date 080719482020.00

 

将当前系统时间写入ds1307 rtc设备,因为hwclock默认指定的rtc是rtc0设备。

所以需要用-f选项指定为rtc1.

hwclock -w -f /dev/rtc1

 

看是否写入成功

hwclock -r -f /dev/rtc1

 

如果没有出错。可以拔掉电源。再次上电开机。

 

执行命令添加rtc设备

echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-2/new_device

 

将rtc的时间同步到系统

hwclock -s -f /dev/rtc1

 

使用date命令查看时间是否正确

date

 

3 开机自动同步时间

在/etc/rc.local中添加以下内容(注意在exit 0之前)

echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-2/new_device

hwclock -s -f /dev/rtc1

 

对于ubuntu18.04,请按照下面说明设置开机启动脚本

 

4 ubuntu 18.04设置开机启动脚本

对于ubuntu18.04,不能像ubuntu14.04或者ubuntu16.04一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。

4.1 建立rc-local.service文件

sudo vi /etc/systemd/system/rc-local.service

 

将下列内容复制进rc-local.service文件

[Unit]

Description=/etc/rc.local Compatibility

ConditionPathExists=/etc/rc.local

 

[Service]

Type=forking

ExecStart=/etc/rc.local start

TimeoutSec=0

StandardOutput=tty

RemainAfterExit=yes

SysVStartPriority=99

 

[Install]

WantedBy=multi-user.target

 

4.2 创建文件rc.local  

sudo vi /etc/rc.local

 

将下列内容复制进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.


exit 0

 

4.3 给rc.local加上权限

sudo chmod +x /etc/rc.local

 

4.4 启用服务

sudo systemctl enable rc-local

 

4.5 启动服务并检查状态

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service

 

猜你喜欢

转载自blog.csdn.net/weixin_45534288/article/details/107916387