当前位置:首页>Linux>Linux设备驱动 -- RTC(DS1339)驱动移植

Linux设备驱动 -- RTC(DS1339)驱动移植

  • 2026-09-04 01:51:07
Linux设备驱动 -- RTC(DS1339)驱动移植

查看原理图

RTC芯片采用的是DS1339芯片,这是达拉斯半导体公司的一款RTC芯片,使用I2C接口。

芯片接在RK3568的I2C5。

Linux内核支持DS1339

检索linux内核是否支持DS1339芯片驱动。

通过搜索可知到,Linux系统内核中已有达拉斯DS1339的驱动,DS13xx这一系列芯片都是使用同一个驱动。

内核参考文档:kernel/Documentation/rtc.txt、kernel/Documentation/devicetree/bindings/rtc/rtc-ds1307.txt

驱动源码位置:kernel/drivers/rtc/rtc-ds1307.c

驱动注册和匹配:

staticconststructof_device_idds1307_of_match[] = {// ...    { .compatible = "dallas,ds1339", .data = (void *)ds_1339 },// ...};staticstructi2c_driverds1307_driver = {    .driver = {        .name = "rtc-ds1307",        .of_match_table = of_match_ptr(ds1307_of_match),        .pm = &ds1307_pm_ops,    },    .probe = ds1307_probe,    .remove = ds1307_remove,    .id_table = ds1307_id,};module_i2c_driver(ds1307_driver);

配置内核使能配置使能

-> Device Drivers                                                -> Real Time Clock(RTC_CLASS [=y])    -> Dallas/Maxim DS1307/37/38/39/40/41, ST M41T00, EPSON RX-8025, ISL12057(RTC_DRV_DS1307 [=y])
CONFIG_RTC_DRV_DS1307=y

编写设备树节点

&i2c5 {    status = "okay";    pinctrl-0 = <&i2c5m0_xfer>;    rtc_ds1339: rtc@68 {        compatible = "dallas,ds1339";        reg = <0x68>;        trickle-resistor-ohms = <2000>;        status = "okay";};    
  • pinctrl-0属性:将i2c5对应的引脚配置为i2c功能。

  • compatible属性:其值必须是dallas,ds1339,否则无法和内核中已有的驱动匹配。

  • reg属性:ds1339 rtc芯片的i2c地址。

  • trickle-resistor-ohms 用于配置 RTC 芯片涓流充电电路的限流电阻阻值(单位:欧姆),核心作用是限制对 RTC 备用充电电池的充电电流,防止过充,仅适用于支持涓流充电的 RTC 芯片(如 DS1339),取值需匹配芯片手册要求(如 DS1339 常用 2000/4000/8000 等),无充电需求时可省略或设为 0。

编写配置好设备树节点,重新编译内核。

用户空间访问

# 读取当前时间cat /sys/class/rtc/rtc0/time# 读取日期cat /sys/class/rtc/rtc0/date# 设置时间 (格式: HH:MM:SS)echo "12:34:56" > /sys/class/rtc/rtc0/time# 设置日期 (格式: YYYY-MM-DD)echo "2024-01-01" > /sys/class/rtc/rtc0/date

最新文章

随机文章