您的位置 首页 知识

根据AT91RM9200的I/O驱动程序(linux)

#includelinux/config.h>#includelinux/module.h>#includelinux/sched.h>#includelinux/slab.h&gt

#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”

#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 key\n”);
//////////////////////////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 33\n”);
return;
}
else
printk(“\nRegisterd the keyled driver as major device 33 success\n”);
//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 Liu\n”);
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(“%d\n”,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;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/273048.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部