



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
TQ2440 time0定时器控制led0闪烁驱动mytimer.c/*基于tq2440开发板,内核2.6.30功能能实现定时器0 的中断函数。定时器的中断让led 闪烁2012年7月15日13:14:02*/#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include volatile unsigned long *gpbcon = NULL;volatile unsigned long *gpbdat = NULL;volatile unsigned long *tcfg0 = NULL;/用来设置预分频volatile unsigned long *tcfg1 = NULL;/用来设置分频volatile unsigned long *tcon = NULL;/定时器控制器volatile unsigned long *tcntb0 = NULL;/计数缓冲寄存器volatile unsigned long *tcmpb0 = NULL;/比较缓冲寄存器volatile unsigned long *tcnto0 = NULL;/计数观察寄存器volatile unsigned long *gpfcon = NULL;volatile unsigned long *gpfdat = NULL;volatile unsigned long *srcpnd = NULL;volatile unsigned long *intmask = NULL;volatile unsigned long *intpnd = NULL;volatile unsigned long *intoffset = NULL;static irqreturn_t irq_interrupt(int irq, int dev_id)/*gpbcon &= (3 0 * 2);/*gpbcon |= (2 0 * 2);/设置b0 为 tout0static unsigned int irq_count;irq_count+;printk(in irq_interrupt %dn, irq_count);if(irq_count % 2)*gpbdat &= (1 5);else*gpbdat |= (1 5);return IRQ_RETVAL(IRQ_HANDLED);ssize_t drive_open (struct inode *inode, struct file *file)*gpbcon &= (0x3 (5 * 2) | (0x3 (6 * 2) | (0x3 (7 * 2) | (0x3 (8 * 2);/清零*gpbcon |= (0x1 (5 * 2) | (0x1 (6 * 2) | (0x1 (7 * 2) | (0x1 (8 * 2);/设置成输出request_irq(IRQ_TIMER0, irq_interrupt, IRQ_TYPE_EDGE_FALLING, TIME0, NULL);printk(-drive open ok-n);return 0;ssize_t drive_release (struct inode *inode, struct file *file)free_irq(IRQ_TIMER0, NULL);printk(-drive close ok-n);return 0;ssize_t drive_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)struct clk *clk_p; unsigned int pclk;unsigned int tcnt;printk(-drive ioctl ok-n);/定时器时钟频率=pclk /( 预分频器的值+1) / 分频器/62.5k*tcfg0 &= 0xff; /定时器0预分配清零*tcfg0 |= (50 - 1); /预分频 50*tcfg1 &= 0xf; /定时器0 mux 输入分频清零*tcfg1 |= 3; /mux 分频 16clk_p = clk_get(NULL, pclk);pclk = clk_get_rate(clk_p);tcnt = (pclk/50/16)/10;printk(pclk = %dn,pclk);printk(tcnt = %dn,tcnt);*tcntb0 &= 0x0000; /16位寄存器*tcmpb0 &= 0x0000;*tcntb0 |= 1000;*tcmpb0 |= 500;printk(tcntb0 = %dn, *tcntb0);printk(tcmpb0 = %dn, *tcmpb0); *tcon &= 0x1f; /清零/printk(tcon=%dn, *tcon);msleep(10);/第一次必须手动更新*tcon |= 0xb; /关闭死区、自动重载、关反相器、手动更新TCNTB0&TCMPB0、启动定时器0/1011/printk(tcon=%dn,*tcon);msleep(10);*tcon &= 2; /清除定时器0的手动更新位printk(tcfg0 = %dn, *tcfg0);printk(tcfg1 = %dn, *tcfg1);printk(tcntb0 = %dn, *tcntb0);printk(tcmpb0 = %dn, *tcmpb0);printk(tcon = %dn, *tcon); return 0;static struct file_operations drive_ops =.owner = THIS_MODULE,.open = drive_open,.ioctl = drive_ioctl,.release = drive_release,;static struct miscdevice misc = .minor = MISC_DYNAMIC_MINOR,.name = mytimer,.fops = &drive_ops,;static int _init init_drive(void) int ret; ret = misc_register(&misc);gpbcon = (volatile unsigned long *)ioremap(0x56000010,32);gpbdat = (volatile unsigned long *)ioremap(0x56000014, 32);tcfg0 = (volatile unsigned long *)ioremap(0x51000000, 4);tcfg1 = (volatile unsigned long *)ioremap(0x51000004, 4);tcon = (volatile unsigned long *)ioremap(0x51000008, 4);tcntb0 = (volatile unsigned long *)ioremap(0x5100000c, 4);tcmpb0 = (volatile unsigned long *)ioremap(0x51000010, 4);tcnto0 = (volatile unsigned long *)ioremap(0x51000014, 4);gpfcon = (volatile unsigned long *)ioremap(0x56000050, 4); gpfdat = (volatile unsigned long *)ioremap(0x56000054, 4); srcpnd = (volatile unsigned long *)ioremap(0x4a000000, 4); intmask = (volatile unsigned long *)ioremap(0x4a000008, 4); intpnd = (volatile unsigned long *)ioremap(0x4a000010, 4); intoffset = (volatile unsigned long *)ioremap(0x4a000014, 4);printk(-drive button init ok-n); return 0;static void _exit exit_drive(void)*tcon &= 1;misc_deregister(&misc);printk(-drive button exit ok-n);module_init(init_drive);module_exit(exit_drive);MODULE_LICENSE(GPL);Makefileifneq ($(KERNELRELEASE),)obj-m :=mytimer.o elseKERNELDIR ?= /home/book/opt/EmbedSky/linux-/PWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean:rm -rf *.o * core .depend .*.cmd *.ko *.mod.c endiftest.c#include #include #include #include #include int main(int argc, char *argv)int fd,ret;fd = open(/dev/mytimer, O_RDWR);if(fd = -1)printf(cant open device mknod %s c zhu ci n,/dev/mytimer);return 0;ioctl(fd, 1);while(1);close(fd);return 0;实验现象:rootEmbedSky /# insmod mytimer.ko -drive button init ok-rootEmbedSky /# ls -l /dev/mytimer crw-rw- 1 root root 10, 55 Feb 24 12:09 /dev/mytimerrootEmbedSky /# cat /proc/devices Character devices: 7 vcs 10 misc 13 inputrootEmbedSky /# ./test in i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 云南省中甸县2025年上半年事业单位公开遴选试题含答案分析
- 毛织品行业透视
- 河北省新河县2025年上半年事业单位公开遴选试题含答案分析
- 河北省无极县2025年上半年事业单位公开遴选试题含答案分析
- 2025版商场电工系统调试与验收服务合同
- 2025年度水泥产品质量保证与售后服务合同
- 2025年充电桩安装与电力系统接入合同模板
- 2025版影视动画设计费合作协议书下载
- 2025年度企业员工劳动合同绩效评估与薪酬调整合同
- 2025版石膏制品批发采购合同范本
- 插画风浙江大学浙大介绍大学介绍
- 2024年秋季学期人教版七年级上册历史全册教学课件(新版教材)
- 化学-安徽省1号卷A10联盟2025届高三上学期8月开学摸底考试试题和答案
- 创业大赛承办服务投标方案(技术方案)
- JGJ/T235-2011建筑外墙防水工程技术规程
- 2024版《供电营业规则》考试复习题库大全-上(选择、判断题)
- 如果历史是一群喵
- 《园艺学专业英语》全套教学课件
- 2024年白酒酿造职业技能等级认定(初级工)理论考试复习题库(含答案)
- 自助餐食品安全培训
- 循证护理实践案例比赛课件
评论
0/150
提交评论