Linux驱动分析之RTC框架

简介: 当Linux内核启动时,它会从RTC中读取时间与日期,作为基准值。然后通过软件来维护系统时间和日期。Linux系统中提供了RTC核心层,对于驱动开发者而言,操作起来就变得很简单了。我们来看看整体框架。

前言

当Linux内核启动时,它会从RTC中读取时间与日期,作为基准值。然后通过软件来维护系统时间和日期。Linux系统中提供了RTC核心层,对于驱动开发者而言,操作起来就变得很简单了。我们来看看整体框架。


驱动框架

下面是整体框架图

640.png

与RTC核心有关的文件有:

文件

描述

/drivers/rtc/class.c

这个文件向linux设备模型核心注册了一个类RTC,然后向驱动程序提供了注册/注销接口

/drivers/rtc/rtc-dev.c

这个文件定义了基本的设备文件操作函数,如:open,read等

/drivers/rtc/interface.c

这个文件主要提供了用户程序与RTC驱动的接口函数,用户程序一般通过ioctl与RTC驱动交互,这里定义了每个ioctl命令需要调用的函数

/drivers/rtc/rtc-sysfs.c

与sysfs有关

/drivers/rtc/rtc-proc.c

与proc文件系统有关

/include/linux/rtc.h

定义了与RTC有关的数据结构



重要结构体

  • rtc_device
//RTC设备structrtc_device {
structdevicedev;
structmodule*owner;
intid;
conststructrtc_class_ops*ops; //rtc操作函数structmutexops_lock;
structcdevchar_dev;
unsignedlongflags;
unsignedlongirq_data;
spinlock_tirq_lock;
wait_queue_head_tirq_queue;
structfasync_struct*async_queue;
intirq_freq;
intmax_user_freq;
structtimerqueue_headtimerqueue;
structrtc_timeraie_timer; 
structrtc_timeruie_rtctimer;
structhrtimerpie_timer; /* sub second exp, so needs hrtimer */intpie_enabled;
structwork_structirqwork;
/* Some hardware can't support UIE mode */intuie_unsupported;
longset_offset_nsec;
boolregistered;
structnvmem_device*nvmem;
/* Old ABI support */boolnvram_old_abi;
structbin_attribute*nvram;
time64_trange_min;
timeu64_trange_max;
time64_tstart_secs;
time64_toffset_secs;
boolset_start_time;
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMULstructwork_structuie_task;
structtimer_listuie_timer;
/* Those fields are protected by rtc->irq_lock */unsignedintoldsecs;
unsignedintuie_irq_active:1;
unsignedintstop_uie_polling:1;
unsignedintuie_task_active:1;
unsignedintuie_timer_active:1;
#endif};

上面的结构体表示一个RTC设备,比较简单,主要就是中断信息,字符设备对象,操作函数等。


  • rtc_class_ops
//RTC操作函数structrtc_class_ops {
int (*ioctl)(structdevice*, unsignedint, unsignedlong);
int (*read_time)(structdevice*, structrtc_time*);
int (*set_time)(structdevice*, structrtc_time*);
int (*read_alarm)(structdevice*, structrtc_wkalrm*);
int (*set_alarm)(structdevice*, structrtc_wkalrm*);
int (*proc)(structdevice*, structseq_file*);
int (*set_mmss64)(structdevice*, time64_tsecs);
int (*set_mmss)(structdevice*, unsignedlongsecs);
int (*read_callback)(structdevice*, intdata);
int (*alarm_irq_enable)(structdevice*, unsignedintenabled);
int (*read_offset)(structdevice*, long*offset);
int (*set_offset)(structdevice*, longoffset);
};

就是一些设置时间和读取时间,以及闹钟等接口函数。


  • rtc_time
//时间结构体structrtc_time {
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
};


API函数

// 注册RTC classstaticstructrtc_device*rtc_device_register(constchar*name,
structdevice*dev,
conststructrtc_class_ops*ops,
structmodule*owner)
structrtc_device*devm_rtc_device_register(structdevice*dev,
constchar*name,
conststructrtc_class_ops*ops,
structmodule*owner)
//注销RTC      staticvoidrtc_device_unregister(structrtc_device*rtc)     
voiddevm_rtc_device_unregister(structdevice*dev, structrtc_device*rtc)


总结

RTC也是字符设备驱动,只是进行了封装,封装完之后我们调用起来其实就很简单了。只要实现好接口函数,填充好结构体,然后进行注册即可。


相关文章
|
4天前
|
监控 Linux 应用服务中间件
探索Linux中的`ps`命令:进程监控与分析的利器
探索Linux中的`ps`命令:进程监控与分析的利器
|
4天前
|
数据管理 Linux 数据处理
探索Linux中的printf命令:数据处理与分析的利器
`printf`是Linux命令行中的文本格式化工具,类似C语言函数,用于数据处理和分析。它接受格式字符串和参数,格式化输出,支持字符串、整数、浮点数等类型。常用格式说明符有%s、%d、%f等。例如,`printf "Hello, %s!\n" "World"`会输出"Hello, World!"。配合字段宽度和对齐,可用于制作表格。在脚本中,printf常与循环和其它命令结合,实现复杂文本操作。
|
5天前
|
算法 Linux 编译器
技术笔记:LINUX2.6.32下的进程分析
技术笔记:LINUX2.6.32下的进程分析
|
5天前
|
运维 监控 网络协议
Linux 下的性能监控与分析技巧
在Linux环境中,命令行工具助力服务器管理和故障排查。通过示例展示如何监控网络、TCP连接、CPU及内存使用。例如,用`netstat`结合`awk`查TOP 20高频率IP访问80端口,识别DDoS迹象;`netstat -nat`统计TCP状态;`ps -aux`排序列出CPU和内存消耗大的进程;`find`加`tar`查找并压缩`.conf`文件。掌握这些命令提升运维效率。
11 1
|
7天前
|
JSON Linux 数据处理
探索Linux中的pinky命令:数据处理与分析的得力助手
探索Linux的pinky命令,非标准工具,专用于文本数据处理和分析。提供直观界面,擅长日志、CSV处理。特点包括:易于使用、强筛选、灵活转换和自定义输出。示例:筛选`access.log`中IP为192.168.1.1的记录并转为CSV。注意文件格式、筛选条件、输出格式和性能优化,处理前备份数据。
|
8天前
|
Linux 数据处理 开发者
深入解析Linux中的paste命令:数据处理与分析的得力助手
`paste`命令在Linux中是数据处理的利器,它按列拼接多个文件内容,支持自定义分隔符和从标准输入读取。例如,合并`file1.txt`和`file2.txt`,使用`paste file1.txt file2.txt`,默认以制表符分隔;若要使用逗号分隔,可运行`paste -d ',' file1.txt file2.txt`。当文件行数不同时,较短文件后会填充空白行。结合管道符与其他命令使用,如`cat file1.txt | paste -s`,可按行合并内容。注意文件大小可能影响性能。
|
9天前
|
Linux 编译器 测试技术
探索Linux中的objcopy命令:数据处理与分析的得力助手
`objcopy`是GNU工具集中的实用程序,用于复制和转换二进制目标文件,如ELF到S-record。它支持格式转换、内容提取和修改,如移除调试信息。命令参数包括指定输入/输出格式和复制特定段。示例用途有:`objcopy -O binary input.elf output.bin`(ELF转二进制)和`objcopy -j .text input.elf output.o`(复制.text段)。使用时注意文件格式、备份原始文件并查阅文档。对于处理和分析二进制数据,`objcopy`是不可或缺的工具。
|
存储 Unix Linux
浅入分析Linux
Linux 操作系统必须完成的两个主要目的 与硬件部分交互, 为包含在硬件平台上的所有底层可编程部件提供服务 为运行在计算机系统上的应用程序(即所谓的用户空间)提供执行环境 一些操作系统运行所有的用户程序都直接与硬件部分进行交互, 比如典型的MS-DOS。
969 0
|
1天前
|
Linux 数据处理 调度
深入探索Linux的renice命令:优化进程优先级
`renice`命令在Linux中用于调整运行进程的优先级,范围-20(最高)至19(最低)。它可以实时改变进程的优先级,无需重启,支持按PID、进程组或用户批量修改。例如,`renice -n 10 -p 1234`将PID为1234的进程优先级设为10。使用时要注意不要滥用高优先级,避免频繁修改,了解系统负载,并记录变更,通常需root权限。
|
1天前
|
Linux 数据处理 Perl
深入探索Linux的rename命令:文件重命名利器
**Linux的`rename`命令是批量重命名文件的利器,基于正则表达式或Perl,支持预览、交互式确认及多种操作模式。常用示例:用`s/ /_/g`替换空格为下划线,`s/$/.txt/`添加扩展名,`s/\..*//`删除扩展名。使用时注意备份、测试命令、权限和正则表达式知识。**