要读写arm开发板上的寄存器GPF4~7(控制LED的寄存器)!
#include <linux/module.h>
#include <linux/moduleparam.h>/*模块参数*/
#include <linux/init.h>
#include <linux/types.h> /* size_t */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/input.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/arch/hardware.h>
#include <asm/arch/hardware.h>
struct light_dev
{
struct cdev cdev;
unsigned char value;
};
int light_major=0;
int light_minor=0;
struct light_dev *light_devices;
/*灯光初始化*/
void light_gpio_init()
{
s3c2410_gpio_cfgpin(S3C2410_GPF7, S3C2410_GPF7_OUTP);
}
/*点亮灯*/
void light_on()
{
/*点亮灯的函数*/
// s3c2410_gpio_setpin(unsigned int pin, unsigned int to);
s3c2410_gpio_setpin(S3C2410_GPF7,1);
}
/*熄灭灯*/
void light_off()
{
s3c2410_gpio_setpin(S3C2410_GPF7,0);
}
/*打开函数*/
int light_open(struct inode *inode,struct file *filp)
{
struct light_dev *dev;
dev=container_of(inode->i_cdev,struct light_dev,cdev);
filp->private_data=dev;
return 0;
}
/*关闭函数*/
int light_release(struct inode *inode,struct file *filp)
{
return 0;
}
/*读设备函数*/
ssize_t light_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
struct light_dev *dev=filp->private_data;
if(copy_to_user(buf,&(dev->value),1))
{
return -EFAULT;
}
return 1;
}
/*写设备函数*/
ssize_t light_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_pos)
{
struct light_dev *dev=filp->private_data;
if(copy_from_user(&(dev->value),buf,1))
{
return -EFAULT;
}
if(dev->value==1)
light_on();
else
light_off();
return 1;
}
struct file_operations light_fops=
{
.owner=THIS_MODULE,
.read=light_read,
.write=light_write,
.open=light_open,
.release=light_release,
};
/*模块卸载函数*/
void light_cleanup(void)
{
cdev_del(&light_devices->cdev);
kfree(light_devices);
unregister_chrdev_region(MKDEV(light_major,0),1);
}
/*初始化字符设备的结构体dev*/
static void light_setup_cdev(struct light_dev *dev,int index)
{
int err,devno=MKDEV(light_major,index);
cdev_init(&dev->cdev,&light_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&light_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
{
printk(KERN_NOTICE "Error %d adding scull %d",err,index);
}
}
/*模块加载函数*/
int light_init(void)
{
int result;
dev_t dev=0;
/*申请字符设备号*/
if(light_major)
{
dev=MKDEV(light_major,light_minor);
result=register_chrdev_region(dev,1,"led");
}
else
{
result=alloc_chrdev_region(&dev,light_minor,1,"led");
light_major=MAJOR(dev);
}
if(result<0)
{
printk(KERN_WARNING "led can't get major %d/n",light_major);
return result;
}
/*分配设备结构体内存*/
light_devices=kmalloc(sizeof(struct light_dev),GFP_KERNEL);
if(!light_devices)
{
result=-ENOMEM;
goto fail;
}
memset(light_devices,0,sizeof(struct light_dev));
light_setup_cdev(light_devices,0);
light_gpio_init();
return 0;
fail:
light_cleanup();
return result;
}
module_init(light_init);
module_exit(light_cleanup);
MODULE_AUTHOR("freeubuntu@hotmail.com");
MODULE_LICENSE("Dual BSD/GPL");