LED驱动程序:
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/init.h>
#include<linux/delay.h>
#include<asm/irq.h>
#include<mach/regs-gpio.h>
#include<mach/hardware.h>
#include<linux/device.h>
#include<linux/miscdevice.h>
#define DEVICE_NAME "myleds"
#define IOCTL_LED_ON 1
#define IOCTL_LED_OFF 0
//LEDGPIO管脚定义
static unsigned long led_table[] =
{
S3C2410_GPB5,
S3C2410_GPB6,
S3C2410_GPB7,
S3C2410_GPB8,
};
//设置管脚模式
static unsigned int led_cfg_table[] =
{
S3C2410_GPB5_OUTP,
S3C2410_GPB6_OUTP,
S3C2410_GPB7_OUTP,
S3C2410_GPB8_OUTP,
};
static int leds_open(struct inode *inode,struct file *file)
{
int i;
//引脚初始化
for(i = 0;i < 4;i++)
{
s3c2410_gpio_cfgpin(led_table[i],led_cfg_table[i]);
}
return 0;
}
static int leds_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
if(arg > 4)
{
return -EINVAL;
}
switch(cmd)
{
case IOCTL_LED_ON:
s3c2410_gpio_setpin(led_table[arg],0);
return 0;
case IOCTL_LED_OFF:
s3c2410_gpio_setpin(led_table[arg],1);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations leds_fops =
{
.owner = THIS_MODULE,
.open = leds_open,
.ioctl = leds_ioctl,
};
//混杂设备定义
static struct miscdevice misc = {
.minor =MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &leds_fops,
};
//注册混杂设备
static int __init leds_init(void)
{
int reg;
reg = misc_register(&misc);
printk(DEVICE_NAME" initialized\n");
return reg;
}
static void __exit leds_exit(void)
{
misc_deregister(&misc);
printk("deregister\n");
}
module_init(leds_init);
module_exit(leds_exit);
MODULE_AUTHOR("xh");
MODULE_DESCRIPTION("learn");
MODULE_LICENSE("GPL");
Makefile:
ifneq ($(KERNELRELEASE),)
obj-m :=leds.o
else
# KERNELDIR :=/lib/modules/2.6.32-21-generic/build
KERNELDIR :=/opt/EmbedSky/linux-2.6.30.4
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -f *.o *.ko
.PHONY:clean
测试程序:
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/ioctl.h>
#define DEV "/dev/myleds"
int main(int argc,char **argv)
{
int on;
int led_no;
int fd;
if(argc != 3 || sscanf(argv[1],"%d",&led_no) != 1 || sscanf(argv[2],"%d",&on) != 1 || on < 0 || on >1 || led_no < 0 || led_no > 3)
{
fprintf(stderr,"Usage:./led_control led_no 0|1\n");
exit(1);
}
fd = open(DEV,0);
if(fd < 0)
{
perror(DEV);
exit(1);
}
ioctl(fd,on,led_no);
close(fd);
return 0;
}