嵌入式系统与单片机|技术阅读
登录|注册

您现在的位置是:嵌入式系统与单片机 > 技术阅读 > 嵌入式linux的RTC可以这样“玩”

嵌入式linux的RTC可以这样“玩”


导读

嵌入式系统中,设备时间是一个重要的参数。一般情况下,为了让设备在断电情况下,系统能正常走时,会采用外部RTC芯片为系统提供断电后的系统走时。

在linux内核中,内核对RTC的支持分成了两层:

(1)与硬件无关的顶层字符驱动程序:用于实现内核的RTC相关的API。

(2)与硬件相关的底层驱动程序:用于和底层总线进行通信。

RTC API保证了用户空间的程序(工具)独立于底层平台和特定的RTC芯片,例如:hwclock则遵循这些API 接口对/dev/rtc进行操作。这些API也定义了sys(/sys/class/rtc)和proc(/proc/driver/rtc)文件系统中的属性。

linux内核的RTC框架支持各种各样的RTC:集成到SOC中的RTC,支持I2C、SPI或者其他总线通信的单独RTC芯片等。对于用户空间来说,RTC框架会提供三种接口:

(1)/dev/rtcN。N表示RTC在系统上的序号。

(2)/sys/class/rtc/rtcN

(3)/proc/driver/rtc

本文不去具体描述RTC的框架细节和如何设计RTC驱动程序,而是描述:在RTC驱动程序正常工作的前提下,对于多个rtc,在用户空间中如何配置时间参数。

背景描述

在一块搭载rk3568处理器的硬件板卡上,需要使用RTC为系统提供断电后的时间走时,在对应硬件板卡的Linux内核中,针对rk3568开发了两款rtc驱动:rtc0rtc1

在Linux启动过程中,内核会自动使用rtc0设置系统时间,该时间是一个默认状态时间参数,当系统断电后,发现时间不会自动走时,因此在每次系统启动后,使用date命令查看系统时间,时间都是最初的默认参数。实际上rtc1才代表外部的RTC芯片,所以此处可以在linux内核启动后,进入根文件系统服务启动过程中,在配置脚本中将rtc1的时间参数同步到系统。

解决方法

具体操作如下:

  • 在命令行使用以下命令设置rtc1的时间:
date -s "具体时间参数"

时间参数格式为:“年-月-日 时:分:秒”

在设置系统时间的时候,可以手动设置,这样在秒上可能存在误差。如果系统板卡在开发阶段可以连接网络,通过网络更新系统时间也是一种较好的方式,哈哈。

  • 接着使用以下命令将时间参数同步到rtc1:
hwclock -w -f /dev/rtc1
  • /etc/profile文件末尾添加如下代码,用于当Linux启动后自动从rtc1同步时间到系统:
hwclock -s -f /dev/rtc1

完成后如下所示:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

#用于将外部RTC时间同步到linux系统。
hwclock -s -f /dev/rtc1 &

总结

本文是一例在实际工作中所遇到的RTC问题的解决方法总结,没有其他的了。

补充:『date命令帮助』

Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING          display time described by STRING, not 'now'
      --debug                annotate the parsed date,
                              and warn about questionable usage to stderr
  -f, --file=DATEFILE        like --date; once for each line of DATEFILE
  -I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                               FMT='date' for date only (the default),
                               'hours''minutes''seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14T02:34:56-06:00
  -R, --rfc-email            output date and time in RFC 5322 format.
                               Example: Mon, 14 Aug 2006 02:34:56 -0600
      --rfc-3339=FMT         output date/time in RFC 3339 format.
                               FMT='date''seconds', or 'ns'
                               for date and time to the indicated precision.
                               Example: 2006-08-14 02:34:56-06:00
  -r, --reference=FILE       display the last modification time of FILE
  -s, --set=STRING           set time described by STRING
  -u, --utc, --universal     print or set Coordinated Universal Time (UTC)
      --help     display this help and exit
      --version  output version information and exit

补充:『hwclock命令帮助』

BusyBox v1.34.1 (2022-08-12 14:49:32 CST) multi-call binary.

Usage: hwclock [-swul] [--systz] [-f DEV]

Show or set hardware clock (RTC)

        -s      Set system time from RTC
        -w      Set RTC from system time
        --systz Set in-kernel timezone, correct system time
                if RTC is kept in local time
        -f DEV  Use specified device (e.g. /dev/rtc2)
        -u      Assume RTC is kept in UTC
        -l      Assume RTC is kept in local time
                (if neither is given, read from /var/lib/hwclock/adjtime)