新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于AT91RM9200的I/O驅動程序(linux)

        基于AT91RM9200的I/O驅動程序(linux)

        作者: 時間:2016-11-13 來源:網絡 收藏
        #include <linux/config.h>

        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include
        #include /* get ioctl stuff for gpio */
        //#include "at91_gpio.h"

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

        #defineDRIVER_NAME"key_led" //device name
        #defineDRIVER_VERSION"1.00" //driver version
        #defineDRIVER_MAJOR33

        static devfs_handle_t devfs_handle, devfs_keyled_dir;
        static ssize_t io_read(struct file*, char*, size_t, loff_t* );
        static ssize_t io_write(struct file*, const char*, size_t, loff_t* );
        //static int can0_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
        static int io_open(struct inode*,struct file*);
        static int io_release(struct inode*,struct file*);
        struct file_operations io_fops = {
        //owner: THIS_MODULE,
        //ioctl: gpio_ioctl,
        write:io_write,
        read: io_read,
        open: io_open,
        release: io_release,
        };

        void delay(int count)
        {
        int temp,i;
        for(i=0;ifor(temp=0;temp<10000;temp++)
        {
        ;
        }
        }

        void io_int(void)
        {
        unsigned char temp;
        AT91_SYS->PIOA_PER =AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;//enable the peripheral control
        AT91_SYS->PIOA_OER=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;//output enable
        AT91_SYS->PIOA_IFDR=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;
        AT91_SYS->PIOA_CODR=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;
        AT91_SYS->PIOA_IDR=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;//disable the interrupt
        AT91_SYS->PIOA_MDER=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;
        AT91_SYS->PIOA_PPUER=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;
        AT91_SYS->PIOA_OWER=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;//enable write the PIO_ODSR for PA22
        for(temp=0;temp<5;temp++)
        {
        AT91_SYS->PIOA_ODSR=0;//PA22 should goto low now
        delay(1000);
        AT91_SYS->PIOA_ODSR=AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22;//PA22 should be high now
        delay(1000);
        }
        printk("Init the LED over,and begine to init the keyn");
        //////////////////////////init the led over,begine to init the KEY
        AT91_SYS->PMC_PCER=0xffffffff;
        AT91_SYS->PIOB_PER=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//enable the peripheral control
        AT91_SYS->PIOB_ODR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//disable the output
        AT91_SYS->PIOB_IFER=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//enable the filter
        AT91_SYS->PIOB_CODR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//clean the output reg
        AT91_SYS->PIOB_IDR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//disable the interrupt
        AT91_SYS->PIOB_MDDR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//enable the multidriver
        AT91_SYS->PIOB_PPUDR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//disable the pullup
        AT91_SYS->PIOB_OWDR=0xfffe|AT91C_PIO_PB16|AT91C_PIO_PB17|AT91C_PIO_PB18|AT91C_PIO_PB28|AT91C_PIO_PB29;//disable the output write


        }
        void keyled_init(void)
        {
        if(register_chrdev(DRIVER_MAJOR,DRIVER_NAME,&io_fops))
        {printk("Can not register keyled driver as major device 33n");
        return;
        }
        else
        printk("nRegisterd the keyled driver as major device 33 successn");
        //devfs_keyled_dir = devfs_mk_dir(NULL, DRIVER_NAME, NULL);
        devfs_handle = devfs_register(devfs_keyled_dir, DRIVER_NAME,
        DEVFS_FL_DEFAULT,
        DRIVER_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR,
        &io_fops, NULL);

        io_int();
        printk("nKeyled driver V0.1 write by Simon Liun");
        return;
        }

        static int io_open(struct inode* inode,struct file* file)
        {
        MOD_INC_USE_COUNT;
        return 0;
        }

        static int io_release(struct inode* inode,struct file* file)
        {
        MOD_DEC_USE_COUNT;
        return 0;
        }

        static ssize_t io_read(struct file * file ,char * buff,size_t count,loff_t * offp)
        {
        int i;
        unsigned long temp,key_temp;
        unsigned char key_data[4];
        temp=AT91_SYS->PIOB_PDSR;
        //printk("%dn",temp);
        key_temp=temp;
        //temp=temp >>13;
        key_data[0]=(unsigned char)(key_temp&0xfe);
        temp=temp>>8;
        key_temp=temp;
        key_data[1]=(unsigned char)(key_temp& 0xff);
        temp=temp>>8;
        key_temp=temp;
        key_data[2]=(unsigned char)(key_temp& 0x7);
        temp=temp>>8;
        key_temp=temp;
        key_data[3]=(unsigned char)(key_temp& 0x30);
        copy_to_user(buff,&key_data[0],4);
        //printk("nthe key data is n");
        //for(i=0;i<4;i++)
        //printk("%4d",key_data[i]);
        return 4;
        //return 0;
        }

        ssize_t io_write(struct file* file, const char* ch, size_t count, loff_t* offp )
        {
        unsigned char led_data;
        unsigned long temp;
        copy_from_user(&led_data,ch,1);
        temp=led_data;
        temp=(temp<<17);
        temp=(temp &(AT91C_PIO_PA17|AT91C_PIO_PA18|AT91C_PIO_PA19|AT91C_PIO_PA20|AT91C_PIO_PA21|AT91C_PIO_PA22));
        AT91_SYS->PIOA_ODSR= temp;
        return 1;
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 潮州市| 桐乡市| 天祝| 昌江| 五原县| 永昌县| 甘洛县| 习水县| 上杭县| 萍乡市| 清涧县| 济宁市| 马龙县| 辽源市| 中方县| 宜兰县| 新安县| 察雅县| 扬州市| 崇文区| 建瓯市| 山丹县| 泽州县| 丰县| 鸡西市| 新郑市| 马山县| 湖北省| 西畴县| 雷山县| 正阳县| 安乡县| 景德镇市| 淅川县| 兖州市| 龙南县| 永济市| 清水县| 博兴县| 郎溪县| 甘孜|