在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驱动:rtc0和rtc1:
在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  fifiif [ -d /etc/profile.d ]; then  for i in /etc/profile.d/*.sh; do    if [ -r $i ]; then      . $i    fi  done  unset ifi#用于将外部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-14t0256-06:00  -r, --rfc-email            output date and time in rfc 5322 format.                               example: mon, 14 aug 2006 0256 -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 0256-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 1432 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)


JAVA中static、final、static final如何区分
华为宣布6月2日发布鸿蒙手机操作系统 高通联电签订六年长约应对芯片需求调整
一加9系列搭载骁龙888成为第二款2K挖孔屏旗舰
米尔基于瑞萨RZ/G2L开发板在工控领域的应用-基于SOEM的EtherCAT主站
iOS11首个公测版发布 iOS11首个公测版有什么改进?
在RTC驱动程序正常工作的前提下用户空间中如何配置时间参数
超声波换能器的类型
友达光电工厂将实现满负荷生产,正提高8B工厂产能
基于精准聚焦目标空间区域的转录组和蛋白质组一站式解决方案
南韩对日本不取消存储器芯片征税的做法不满
嵌入式汽车开发潜力巨大
新一代跨座式单轨列车在试验线上成功试跑 时速可达 100km/h
疫情当下 即使戴口罩也能通过人脸识别认证
微信推出专属对话AI机器人
“皖若初见”丨8月20日,纳特通信邀您相约第七届电磁兼容研讨会
汽车级FRAM+虚拟仪表+汽车级代工,富士通迎接新能源与自动驾驶汽车市场风口
苹果发布适用于HomePod的14.2.1软件更新
高端电子化学品行业发展规划
哪些指标会影响到频谱仪的底噪(DANL)
比特币​震荡下跌意味着什么