当前位置:网站首页>基于[海思Hi3516dv300]开发的内核定时器中断
基于[海思Hi3516dv300]开发的内核定时器中断
2022-07-21 17:26:00 【I&You】
基于[海思Hi3516dv300]开发的内核定时器中断
功能1:通过内核定时器中断打印板子时间
驱动入口函数(Hi3516dv300_Timer_Init)
static int __init Hi3516dv300_Timer_Init(void)
{
//这只是个测试代码,我就不创建设备节点了~~忽略
//初始化动态定时器
init_timer(&Hi3516dv300_timer);
//配置动态定时器
Hi3516dv300_timer.function = Hi3516dv300_timer_handler;//处理函数
Hi3516dv300_timer.expires = jiffies + HZ; //当前时间开始,HZ个jiffies后,会产生超时
//HZ就是linux内核时钟的频率,HZ是linux系统中的一个常数,当配置内核的时候,
//HZ的就设好了,如果重新修改HZ的值,重新配置、编译linux内核。
//jiffies是一个全局的变量,记录了linux内核从启动到现在经过了多少内核时钟周期。1秒钟内,jiffies增加HZ次。
/*举例说明:
expires = jiffies + HZ;(1秒触发处理函数)
expires = jiffies + HZ/2;(0.5秒触发处理函数)
expires = jiffies + HZ/1000;(1微秒触发处理函数)*/
Hi3516dv300_timer.data = NULL; //传递参数
//动态定时器加到内核
add_timer(&Hi3516dv300_timer);
}
处理函数(Hi3516dv300_timer_handler)
void Hi3516dv300_timer_handler(unsigned long data)
{
do_gettimeofday(&(txc.time));
//功能获取自1970以来的秒数
rtc_time_to_tm(txc.time.tv_sec,&tm);
/*功能: 获取当前的UTC时间
time:当前自1970以来的秒数
tm : 转换成的可识别的年月日 时分秒 ,年+1900 月+1,时+8(时区)
头文件:#include <linux/rtc.h>*/
printk("UTC time :%d-%d-%d %d:%d:%d /n",tm.tm_year+1900,tm.tm_mon, tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
//重新修改超时时间,进行周期性的超时
mod_timer(&gec6818_timer,jiffies + HZ);
}
功能2:内核定时器中断统计电平持续时间
入口函数(Hi3516dv300_Timer_Init)
//入口函数
static int __init Hi3516dv300_Timer_Init(void)
{
int ret = misc_register(&Infrare_Control_dev);
//创建杂项设备
if (ret)
{
printk(KERN_ERR "register misc dev for dooralarm fail!\n");
goto err_clk;
}
gpio_num = 86;//gpio10_6 = 10*8+6
ret= gpio_request(gpio_num,NULL);//注册gpio
if(ret < 0)
{
printk("gpio_request: fail\n");
goto gpio_request_fail;
}
if (gpio_direction_input(gpio_num)) {
//设置gpio复用为输入函数
printk("[%s %d]gpio_direction_input fail!\n",
__func__, __LINE__);
return -EIO;
}
//初始化动态定时器
init_timer(&Hi3516dv300_timer);
//配置动态定时器
Hi3516dv300_timer.function = Hi3516dv300_timer_handler;//处理函数
Hi3516dv300_timer.expires = jiffies + HZ/1000; //当前时间开始,1ms后会触发处理函数
Hi3516dv300_timer.data = NULL; //传递参数
//动态定时器加到内核
add_timer(&Hi3516dv300_timer);
return 0;
gpio_request_fail:
gpio_free(gpio_num);
err_clk:
misc_deregister(&Infrare_Control_dev);
return 0;
}
响应函数(Hi3516dv300_timer_handler)
void Hi3516dv300_timer_handler(unsigned long data)
{
//重新修改超时时间,进行周期性的超时
mod_timer(&Hi3516dv300_timer,jiffies + HZ/1000);
//读出GPIO输入值
level = gpio_get_value(gpio_num);
//设置gpio函数就为gpio_set_value()
if(!level){
time_cnt++;
}else{
if(time_cnt!=0){
printk("low_level time = %d ms\n",time_cnt);
}
time_cnt=0;
}
}
特别提醒
- 关于精确的延时,建议还是使用高精确的定时器硬件去实现微秒级别延时,因为内核只能提供毫秒级别的定时器中断。
- udelay一般适用于一个比较小的delay,如果你填的数大于2000,系统会认为你这个是一个错误的delay函数,因此如果需要2ms以上的delay需要使用mdelay函数。
- mdelay() 会占用cpu资源,导致其他功能此时也无法使用cpu资源。msleep() 则不会占住cpu资源,其他模块此时也可以使用cpu资源。
- 例:检测一个高电平的持续时间:
________________|------------------|_______________
int data;
int timer_cnt = 0;
do{
data = gpio_get_value(gpio);
}while(data == 0);
do{
data = gpio_get_value(gpio);
timer_cnt ++;
udelay(1);
}while(data == 1);
得到timer_cnt,就是us的计时值。
中断开发代码研究中待更新
内核定时器代码下载链接
边栏推荐
- Zhang Chi Consulting: How Six Sigma can help companies reduce customer complaints
- Machine learning by Li Hongyi 5 Tips for neural network design
- Sharing the ultimate breakthrough strategy used by professional traders for many years (with a full set of trading templates)
- Several applications of Martin strategy
- Activiti default model ER diagram
- Self study golang [Chapter 1: basic knowledge of go language] Why do you want to learn go language? The relationship between go language and C language? Founder of go language? What are the characteri
- Self study the definition of golang [3.5go language array, range keyword] array, and use the for loop to traverse one-dimensional array
- Redis' classic three questions and sentinel
- RunTime+compiler和RunTime only版本的区别
- 解决IDEA的插件中心连接不上网络
猜你喜欢
指针深度解刨《三》(数组的认知)
Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
Experimental support for decorators is a feature that is subject to change in a future release. Set
Quantify three types of market in trading
Self study golang [3.4go language functions and pointers] define a function that returns one or more values. For the pointer of go language, the pointer cannot operate. For the parameter transmission
序列化和反序列化
Science and technology create value | cloud expansion technology is listed in the real list · top 100 of China's scientific and technological innovation brands list
指针深度解刨《二》(指针指向自己)
指针的深度解刨《七》(函数指针相关知识)
电商大促就靠RPA,摆脱重复劳动,急速提效
随机推荐
js 对象深拷贝
Explain the usage of stack, heap and method area in memory
The only way to advance transactions: from Xiaobai to stable profits (III)
Postman configures the global variable postman sets the global token
Postman 配置全局变量 postman设置全局token
Verilog -- Serial four bit adder and leading four bit adder 74hc283
指针深度解刨《四》(指针和数组的 “亲密“ 关系)
vscode 配置代码自动格式化加修复
5G和移动边缘计算服务器如何打造智慧园区
Self study golang [3.1 defining variables] use VaR keyword to define variables, use var() to define variables collectively, omit int keyword and VaR keyword, and use colon: to replace the definition
Update the version number and static resources (the storage address of the embedded web page)
RPA是什么?推荐让电商运营10倍提效的自动化工具
量化交易中的三类行情
李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
Several applications of Martin strategy
软考中级【数据库系统工程师】第1章:计算机系统知识,自学软考笔记,备考2022年5月份软考,计算机硬件系统CPU组成指令寄存器组总线输入输出的程序控制方式计算机体系结构与存储系统加密技术流水线技术
指针的深度解刨《七》(函数指针相关知识)
Self study golang [3.6 slice practice code] slice length, upper limit, copy, delete and increase
基础不牢地动山摇之牛客刷题《二》
TPS trading strategy with a winning rate of 93.98%