首页 > 系统相关 >linux驱动移植-RTC驱动

linux驱动移植-RTC驱动

时间:2023-03-13 22:44:23浏览次数:54  
标签:set struct rtc int RTC linux device 驱动

----------------------------------------------------------------------------------------------------------------------------

内核版本:linux 5.2.8
根文件系统:busybox 1.25.0
u-boot:2016.05
----------------------------------------------------------------------------------------------------------------------------

 一、RTC框架

1.1 RTC概述

RTC,英文全称Real Time Clock,中文就是实时时钟,是一个可以为系统提供精确的时间基准的元器件,主要是用来计时,产生闹钟等。

RTC一般有个备份电池,所以即使设备关机掉电,RTC也能在备份电池的供电下继续正常计时,这样在每次系统开机上电时就可以从RTC设备中读取到准确的时间。

RTC时间在每次系统启动的时候会使用,在需要的时候也可以由系统将要设置的时间写入到RTC设备。

在linux系统中,RTC可以使用周期性的中断来产生闹钟,也可以在系统suspend的时候作为系统的唤醒源使用。

1.2 RTC框架

Mini2440裸机开发之RTC我们介绍RTC裸机程序的编写,在linux中,内核为RTC设备设计了一套驱动模型,如果驱动工程师想增加某一种新RTC硬件的驱动,只需要去编写芯片相关的代码,然后调用内核提供函数注册到RTC核心层即可。

 

RTC框架主要由以下部分组成:

  • hardware:提供时间设置,通过一定的接口(比如通过I2C外接hym8563芯片,或者SoC片内集成了RTC)和RTC驱动进行通信;
  • driver:完成RTC硬件的访问功能,提供访问接口,以驱动的方式注册到内核;
  • class.c:这个文件向linux设备模型核心注册了一个类RTC,然后向驱动程序提供了注册/注销接口;
  • interface.c:屏蔽硬件相关的细节,提供了用户程序与RTC驱动的接口函数,用户程序一般通过ioctl与RTC驱动交互,这里定义了每个ioctl命令需要调用的函数;
  • rtc-lib.c:提供通用的时间操作函数,如rtc_time_to_tm,rtc_valid_tm;
  • rtc-dev.c:创建字符设备节点,即在/dev/目录下创建设备节点供应用层访问,如open、read、ioctl等,访问方式填充到file_operations结构体中;
  • hctosys.c:将硬件时钟写给 wall time;
  • rtc-sysfs.c:与sysfs有关;
  • rtc-proc.c:与proc文件系统有关;

1.3 目录结构

linux内核将RTC驱动相关的代码放在drivers/rtc目录下,这下面的文件还是比较多的,我们大概了解一下即可。

除了我们上面介绍的那些文件外,以rtc-xxx命名的大部分都是各个平台的RTC驱动,比如rtc-s3c.c、rtc-hym8563.c。

二、RTC核心数据结构

学习RTC驱动,首先要了解驱动框架涉及到的数据结构,知道每个数据结构以及成员的含义之后,再去看源码就容易了。

2.1 struct rtc_device

linux内核使用struct rtc_device数据结构来描述一个rtc设备,rtc_device包含了字符设备,rtc设备操作集,中断等信息,还封装了tty_driver,定义在include/linux/rtc.h:

struct rtc_device {
        struct device dev;
        struct module *owner;

        int id;

        const struct rtc_class_ops *ops;
        struct mutex ops_lock;

        struct cdev char_dev;
        unsigned long flags;

        unsigned long irq_data;
        spinlock_t irq_lock;
        wait_queue_head_t irq_queue;
        struct fasync_struct *async_queue;

        int irq_freq;
        int max_user_freq;

        struct timerqueue_head timerqueue;
        struct rtc_timer aie_timer;
        struct rtc_timer uie_rtctimer;
        struct hrtimer pie_timer; /* sub second exp, so needs hrtimer */
        int pie_enabled;
        struct work_struct irqwork;
        /* Some hardware can't support UIE mode */
        int uie_unsupported;

        /* Number of nsec it takes to set the RTC clock. This influences when
         * the set ops are called. An offset:
         *   - of 0.5 s will call RTC set for wall clock time 10.0 s at 9.5 s
         *   - of 1.5 s will call RTC set for wall clock time 10.0 s at 8.5 s
         *   - of -0.5 s will call RTC set for wall clock time 10.0 s at 10.5 s
         */
        long set_offset_nsec;

        bool registered;

        /* Old ABI support */
        bool nvram_old_abi;
        struct bin_attribute *nvram;

        time64_t range_min;
        timeu64_t range_max;
        time64_t start_secs;
        time64_t offset_secs;
        bool set_start_time;

#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
        struct work_struct uie_task;
        struct timer_list uie_timer;
        /* Those fields are protected by rtc->irq_lock */
        unsigned int oldsecs;
        unsigned int uie_irq_active:1;
        unsigned int stop_uie_polling:1;
        unsigned int uie_task_active:1;
        unsigned int uie_timer_active:1;
#endif
};

其中部分参数含义如下:

2.2 struct rtc_class_ops

考虑到RTC物理设备可能采用不同的接线方式,比如I2C、平台、SPI等,linux内核使用struct rtc_class_ops数据结构来描述如何对rtc设备进行操作,定义在include/linux/rtc.h:

/*
 * For these RTC methods the device parameter is the physical device
 * on whatever bus holds the hardware (I2C, Platform, SPI, etc), which
 * was passed to rtc_device_register().  Its driver_data normally holds
 * device state, including the rtc_device pointer for the RTC.
 *
 * Most of these methods are called with rtc_device.ops_lock held,
 * through the rtc_*(struct rtc_device *, ...) calls.
 *
 * The (current) exceptions are mostly filesystem hooks:
 *   - the proc() hook for procfs
 *   - non-ioctl() chardev hooks:  open(), release()
 *
 * REVISIT those periodic irq calls *do* have ops_lock when they're
 * issued through ioctl() ...
 */
struct rtc_class_ops {
        int (*ioctl)(struct device *, unsigned int, unsigned long);
        int (*read_time)(struct device *, struct rtc_time *);
        int (*set_time)(struct device *, struct rtc_time *);
        int (*read_alarm)(struct device *, struct rtc_wkalrm *);
        int (*set_alarm)(struct device *, struct rtc_wkalrm *);
        int (*proc)(struct device *, struct seq_file *);
        int (*alarm_irq_enable)(struct device *, unsigned int enabled);
        int (*read_offset)(struct device *, long *offset);
        int (*set_offset)(struct device *, long offset);
};

其中部分参数含义如下:

  • ioctl:io控制函数,用来实现各种控制命令;
  • read_time:读取时间;
  • set_time:设置时间;
  • read_alarm:读取闹钟;
  • set_alarm:设置闹钟;
  • proc:procfs接口;
  • alarm_irq_enable:闹钟中断使能;
  • read_offset:
  • set_offset:

 

参考文章

[1]30.Linux-RTC驱动分析及使用

[2]Linux驱动| Linux内核 RTC时间架构

[3]Linux驱动修炼之道-RTC子系统框架与源码分析

[4]i.MX 6ULL 驱动开发 二十:RTC

[5]Linux RTC驱动分析及应用

 

标签:set,struct,rtc,int,RTC,linux,device,驱动
From: https://www.cnblogs.com/zyly/p/17208082.html

相关文章

  • Linux下的find的使用方法
    (Linux下的find的使用方法)一、通过文件修改时间查询1.查找5分钟前的文件查找5分钟之前的文件,可使用以下命令:[root@tianyi~]#find/etc-mmin+5|head|nl1 ......
  • Linux多线程中互斥锁、读写锁、自旋锁、条件变量、信号量详解
    Hello、Hello大家好,我是ST,今天我们继续来聊一聊Linux中多线程编程中的重要知识点,详细谈谈多线程中同步和互斥机制。1、同步和互斥互斥:多线程中互斥是指多个线程访问同一资源......
  • Linux系统之时间同步方法
    (Linux系统之时间同步方法)一、使用NTP服务时间同步1.安装ntp[root@node~]#yum-yinstallntp2.启动ntp服务[root@node~]#systemctlstartntpd[root@node~]#......
  • Linux进程与线程的基本概念及区别
    前言假设你正在玩一款在线多人游戏,在游戏中,有多个角色需要进行不同的操作,例如攻击、移动、释放技能等等。接下来,我们用玩游戏的例子,来解释进程和和线程的概念,以及进程和......
  • linux系统常用目录操作命令整理
    目录1、切换工作目录、显示工作目录、显示当前工作目录路径1.1cd命令1.2pwd命令1.3ls命令2、创建和删除目录命令2.1mkdir命令2.2rmdir命令3、改变文件、目录权限3.1c......
  • linux系统常见文件操作命令整理
    目录1显示文件命令1.1cat命令1.2more命令1.3less命令1.4head命令1.5tail命令2.搜索、排序及去掉重复行命令2.1grep命令2.2sort命令2.3uniq命令3、比较文件内容命......
  • Linux statvfs()获取系统磁盘信息
    目录获取磁盘信息方式statvfs,fstatvfs函数说明示例:求磁盘剩余空间获取磁盘信息方式Linux中,可以在终端用df-h,fdisk-l等命令,查看磁盘信息,但C/C++程序中,如何查看呢?可......
  • linux部署jenkins
    linux部署jenkins参考:https://blog.csdn.net/liu_chen_yang/article/details/127202910参考:https://blog.csdn.net/zy10151/article/details/126875108相关依赖......
  • 在linux下使用sqlite3
    前言SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎(来源百度百科)。它是一款轻量级数据库,所占资源低,消耗总量小,被用于嵌入式开......
  • Linux系统编程与网络编程
    Linux系统编程Chapter1系统编程入门GCC区别GCC与G++gcc既能编译c,也能编译c++。只不过gcc在链接的时候,不能自动链接C++的库。在编译阶段,g++会调用gcc,二者是等价的......