新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > Linux下CPLD驅動程序

        Linux下CPLD驅動程序

        作者: 時間:2012-05-25 來源:網絡 收藏

        ========================================================================== */
        /* */
        /* Filename.c */
        /* (c) 2001 Author Zhang Haibo */
        /* */
        /* Description driver program with interrupt and poll */
        /* */
        /* ========================================================================== */

        本文引用地址:http://www.104case.com/article/148984.htm

        #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; //自動創建設備文件時需要先創建類
        static struct class_device *cpld_class_dev; //再創建驅動
        static DECLARE_WAIT_QUEUE_HEAD(eint1_waitq); //將中斷放入等待隊列
        static struct fasync_struct *eint1_async; //
        static volatile int ev_eint1 = 0; //中斷標志,為1表示中斷發生,在中斷服務程序里置1
        volatile unsigned long *cpld_data = NULL; //的物理地址映射的虛擬地址指針 0x08000000
        volatile unsigned long *gpfcon = NULL; //GPF控制寄存器
        volatile unsigned long *gpfdat = NULL; //GPF數據寄存器,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); //產生中斷后,驅動向應用程序發送信號
        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,使能產生PWM,計數器開始計數
        copy_from_user(val, buf, count); //產生PWM的值從用戶空間傳入
        iowrite16(val,cpld_data); //寫入
        return 0;
        }

        static ssize_t cpld_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
        {
        int val;
        wait_event_interruptible(eint1_waitq, ev_eint1); //若無中斷產生,即ev_eint1=0,則休眠
        val=ioread16(cpld_data); //若中斷產生,則從CPLD讀取數據,即編碼器值
        copy_to_user(buf,val,count); //將讀取值傳遞到用戶空間
        ev_eint1 = 0; //中斷標志清0
        *gpfdat |= (12); //中斷讀數后,GPF2置1
        return 0;
        }

        linux操作系統文章專題:linux操作系統詳解(linux不再難懂)

        上一頁 1 2 下一頁

        關鍵詞: 驅動程序 CPLD Linux

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 特克斯县| 易门县| 调兵山市| 贵溪市| 仙游县| 华阴市| 绥化市| 东明县| 宜黄县| 永新县| 唐海县| 太湖县| 金寨县| 海兴县| 东丽区| 睢宁县| 厦门市| 前郭尔| 东乡族自治县| 辽阳市| 手机| 浦北县| 抚松县| 蓝山县| 静乐县| 钟祥市| 苗栗市| 双城市| 老河口市| 宜兰市| 巨鹿县| 巨野县| 金昌市| 龙胜| 大化| 博乐市| 德江县| 平顶山市| 广安市| 云浮市| 荔浦县|