- 论坛徽章:
- 0
|
5可用积分
在学写Linux驱动
对ARM9系列的S3C2440A写了个最简单的linux定时器驱动,设定每秒中断一次并打印出信息
然后写个测试程序,运行情况正常
但是按Ctrl+C就是没办法终止程序运行,不知是不是驱动有冲突?没查明是何原因
代码我贴出来,希望大家帮我看看
--------------------------------------------------------------------------------------
测试程序的代码:
int main(void) //测试程序就是一个死循环,等待定时器一秒中断打印出信息
{
int fd;
fd=open("/dev/timer",0);
if(fd<0){
printf("Open /dev/timer failed!\n");
exit(1);
}
else printf("Open device successfully!\n");
while(1);
close(fd);
return 0;
}
------------------------------------------------------------------------------------------------------------------------
驱动程序的代码:
#include "arm-linux.h" //一些需要的头文件
#define timer_irq IRQ_TIMER0 //定时器0的中断号
#define DEVICE_NAME "timer"
static int major; //存放主设备号
static int interrupt_flag=0; //中断发生标记
#define DEVICE_MAJOR major
#define DEVICE_MINOR 0
void timer_do_tasklet(void)
{
interrupt_flag=0; //中断标记清零,表示已处理中断事件
printk("Doing tasklet_schedule!\n\n");
}
DECLARE_TASKLET(timer_tasklet,timer_do_tasklet,0); //静态创建一个tasklet
static irqreturn_t timer_interrupt(void)
{
interrupt_flag++;
printk("Timer0 interrupt occured!\n");
tasklet_schedule(&timer_tasklet); //开始调度tasklet的函数
return IRQ_HANDLED;
}
static int timer_open(struct inode *inode,struct file *filp)
{
int ret;
unsigned long Ftclk,Fpclk=50000000; //s3c2440a的默认Fpclk为50MHz
outl(255,S3C2410_TCFG0); //设置预分频
outl(3,S3C2410_TCFG1); //设置分频和模式
Ftclk=Fpclk/(255+1)/16; //参考datasheet公式
outl(Ftclk,S3C2410_TCNTB(0)); //写入定时初值
outl(0,S3C2410_TCMPB(0)); //写入终点比较值
outl(S3C2410_TCON_T0MANUALUPD,S3C2410_TCON); //手动刷新一次,将数据装入TCNT和TCMP
outl(S3C2410_TCON_T0START|S3C2410_TCON_T0RELOAD,S3C2410_TCON); //设置自动装载初值,开始计数
ret=request_irq(timer_irq,&timer_interrupt,SA_INTERRUPT,DEVICE_NAME,NULL);
if(ret<0){
printk("Register IRQ_TIMER0 failed!\n");
return ret;
}
}
static int timer_close(struct inode *inode,struct file *filp)
{
free_irq(timer_irq,NULL);
return 0;
}
static struct file_operations timer_fops={
.owner=THIS_MODULE,
.open=timer_open,
.release=timer_close,
};
int timer_init(void)
{
major=register_chrdev(0,DEVICE_NAME,&timer_fops);
if(major<0){
printk("Device register failed!\n");
return major;
}
devfs_mk_cdev(MKDEV(DEVICE_MAJOR,DEVICE_MINOR),S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP,DEVICE_NAME);
printk("Device register successfully!\n");
return 0;
}
void timer_exit(void)
{
unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
devfs_remove(DEVICE_NAME);
printk("Device unregistered!\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("HJW");
module_init(timer_init);
module_exit(timer_exit); |
最佳答案
查看完整内容
ctrl+c终止了应用程序,从while(1);退出,没有执行close(fd);驱动还在运行应用应改成接受到ctrl+c信号,执行close(fd);[ 本帖最后由 mingloie 于 2009-9-11 11:26 编辑 ]
|