makefile为
#
# Makefile by appleboy <appleboy.tw AT gmail.com>
#
obj-m += myled.o
all:
$(MAKE) -C /opt/FriendlyARM/mini2440/linux-2.6.32.2 M=$(PWD) modules
clean:
$(MAKE) -C /opt/FriendlyARM/mini2440/linux-2.6.32.2 M=$(PWD) clean
led驱动为
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LIU SHUI QING");
MODULE_DESCRIPTION("WROD FOR LED");
int major=0;
static struct class *cls;
volatile unsigned long *gpbcon ;
volatile unsigned long *gpbdat ;
static int myled_open(struct inode *inode,struct file *file)
{*gpbcon &=~((0x3)<<(5*2)|(0x3)<<(6*2)|(0x3)<<(7*2)|(0x3)<<(8*2));
*gpbcon |=((0x1)<<(5*2)|(0x1)<<(6*2)|(0x1)<<(7*2)|(0x1)<<(8*2));
return 0;
}
static ssize_t myled_write(struct file *filp,const char __user
*buf,size_t size,loff_t *ppos)
{
int val;
copy_from_user(&val,buf,size);//copy_to_user
printk("copy ok\n");
if(val==1){
*gpbdat &= ~(1<<5) | (1<<6) | (1<<7) | (1<<8);
}
else{
*gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);
};
return 0;
}
static struct file_operations myled_fops={
.owner = THIS_MODULE,
.open = myled_open,
.write = myled_write,
};
int myled_init(void)
{ major = register_chrdev(0, "myled", &myled_fops);
cls = class_create(THIS_MODULE, "myled");//lei
device_create(cls, NULL, MKDEV(major, 0), NULL, "myled"); //jie dian
gpbcon = ioremap(0x56000010,16);
gpbdat = gpbcon+1;
return 0;
}
int myled_exit(void)
{ device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "myled");
iounmap(gpbcon);
return 0;
}
module_init(myled_init);
module_exit(myled_exit);
测试程序为
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* led_test on
* led_test off
*/
int main(int argc, char **argv)
{
int fd;
int buf = 1;
fd = open("/dev/myled", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return 0;
}
printf("open myled ok!\n");
if (argc != 2)
{
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
buf = 1;
printf("the buf is %d\n",val);
}
else
{
buf = 0;
printf("the buf is %d\n",val);
}
write(fd, &buf, 4);
printf("have write to myled\n");
return 0;
}
测试结果
和大家分享一下