Linux下CPLD驅動程序
========================================================================== */
/* */
/* Filename.c */
/* (c) 2001 Author Zhang Haibo */
/* */
/* Description driver program with interrupt and poll */
/* */
/* ========================================================================== */
#include linux/device.h>
#include linux/module.h>
#include linux/kernel.h>
#include linux/fs.h>
#include linux/init.h>
#include linux/delay.h>
#include asm/uaccess.h>
#include linux/types.h>
#include linux/errno.h>
#include linux/mm.h>
#include linux/sched.h>
#include linux/cdev.h>
#include linux/kdev_t.h>
#include asm/system.h>
#include linux/poll.h>
#include linux/irq.h>
#include asm/irq.h>
#include asm/io.h>
#include linux/interrupt.h>
#include mach/regs-gpio.h>
#include mach/hardware.h>
#include linux/gpio.h>
static struct class *cpld_class; //自動創(chuàng)建設備文件時需要先創(chuàng)建類
static struct class_device *cpld_class_dev; //再創(chuàng)建驅動
static DECLARE_WAIT_QUEUE_HEAD(eint1_waitq); //將中斷放入等待隊列
static struct fasync_struct *eint1_async; //
static volatile int ev_eint1 = 0; //中斷標志,為1表示中斷發(fā)生,在中斷服務程序里置1
volatile unsigned long *cpld_data = NULL; //CPLD的物理地址映射的虛擬地址指針 0x08000000
volatile unsigned long *gpfcon = NULL; //GPF控制寄存器
volatile unsigned long *gpfdat = NULL; //GPF數(shù)據(jù)寄存器,0位為使能位
static irqreturn_t eint1_irq(int irq, void *dev_id) //中斷服務程序
{
ev_eint1 = 1; //中斷標志位置1
*gpfdat = ~(12); //進入中斷GPF2清0
wake_up_interruptible(eint1_waitq); //喚醒休眠的進程
kill_fasync (eint1_async, SIGIO, POLL_IN); //產(chǎn)生中斷后,驅動向應用程序發(fā)送信號
return IRQ_RETVAL(IRQ_HANDLED);
}
static int cpld_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT1, eint1_irq, IRQ_TYPE_EDGE_FALLING, EINT1, 1);//注冊中斷:中斷號,中斷程序入口,中斷方式,中斷名,代號
return 0;
}
static ssize_t cpld_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
*gpfdat =~(10); //使能位置0,使能CPLD產(chǎn)生PWM,計數(shù)器開始計數(shù)
copy_from_user(val, buf, count); //產(chǎn)生PWM的值從用戶空間傳入
iowrite16(val,cpld_data); //寫入CPLD
return 0;
}
評論