LED灯比较简单,在linux系统中,LED被看做一个字符设备来使用。因此它有字符设备的操作方法。
字符设备重要的结构体
编写驱动程序
编写方法可以参考“裸机代码”。具体如下
< driver / led.c >新建目录driver且在目录下新建led.c文件
/*
*
*This is a led driver test.(2014/8/26)
*The author is Alan
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "leds"
static int led_gpios[]={
S5PV210_GPJ2(0),
S5PV210_GPJ2(1),
S5PV210_GPJ2(2),
S5PV210_GPJ2(3),
};
#define LED_NUM ARRAY_SIZE(led_gpios)
//实现系统的IO控制命令
static long gec210_leds_ioctl(struct file *flip,unsigned int cmd,
unsigned long arg)
{
switch(cmd)
{
case 0:
case 1:
if(arg>LED_NUM)
return -EINVAL;
gpio_set_value(led_gpios[arg],cmd);//(原代码有错)
break;
default:
return -EINVAL;
}
return 0;
}
//初始化file_operations结构体,实现系统调用
static struct file_operations gec210_led_dev_fops={
.owner = THIS_MODULE,
.unlocked_ioctl = gec210_leds_ioctl,
};
//初始化杂项设备结构体
static struct miscdevice gec210_led_dev={
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gec210_led_dev_fops,
};
//驱动装载函数
static int __init gec210_led_dev_init(void)
{
int ret;
int i;
for(i=0;i<LED_NUM;i++)//轮询请求IO资源
{
ret=gpio_request(led_gpios[i],"LED");
if(ret)//请求不成功
{
printk("%s: request GPIO %d for LED failed,ret = %d\n",
DEVICE_NAME,led_gpios[i],ret);
return ret;
}
s3c_gpio_cfgpin(led_gpios[i],S3C_GPIO_OUTPUT);//配置GPIO为输出
gpio_set_value(led_gpios[i],1);//设置GPIO输出1,即LED灭
}
ret = misc_register(&gec210_led_dev); //注册杂项设备
printk("\t%s initalized\n",DEVICE_NAME);//(原代码有错)
return ret;
}
//驱动卸载函数
static void __exit gec210_led_dev_exit(void)
{
int i;
for(i=0; i<LED_NUM;i++)//轮询释放IO资源
gpio_free(led_gpios[i]);
misc_deregister(&gec210_led_dev);//注销杂项设备
}
module_init(gec210_led_dev_init);
module_exit(gec210_led_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ALAN");
编写驱动程序的makefile
< driver / Makefile >
ifneq ($(KERNELRELEASE),)
obj-m :=led.o
else
module-objs :=led.o
KERNELDIR :=/home/gec/linux_kernel/linux2.6.35.7/
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
$(RM) *.ko *.mod.c *.mod.o *.o *.order *.symvers *.cmd
编写了驱动程序,接下来就要写使用驱动程序的代码,我们称为app吧。
< app / led_test.c >
#include
#include
#include
#include
#include
#include
#define IOCTL_LED_ON 0
#define IOCTL_LED_OFF 1
void usage(char *exename)//使用说明
{
printf("Usage:\n");
printf(" %s \n" ,exename);
printf(" led_no=1,2\n");
}
int main(int argc, char **argv)
{
unsigned int led_no;
int fd = -1;
if(argc != 3)
{
goto err;
}
fd = open("/dev/leds",O_RDWR);//打开文件
if(fd < 0)
{
printf("Can't open /dev/leds\n");
return -1;
}
led_no = strtoul(argv[1],0,0) - 1;//获取LED号
if (led_no > 3)
goto err;
if(!strcmp(argv[2],"on"))
{
ioctl(fd,IOCTL_LED_ON,led_no); //点亮LED灯
}
else if(!strcmp(argv[2],"off"))
{
ioctl(fd,IOCTL_LED_OFF,led_no);//熄灭LED灯
}
else
{
goto err;
}
close(fd);
return 0;
err:
if(fd > 0)
close(fd);
usage(argv[0]);
return -1;
}
< app / Makefile >
#
# General Makefile
Exec := led_test
Obj := led_test.c
CC := arm-linux-gcc #此编译器需根据自己的内核编译器来更改
$(Exec) : $(Obj)
$(CC) -o $@ $(Obj) $(LDLIBS$(LDLIBS-$(@)))
clean:
rm -vf $(Exec) *.elf *.o
将led1打开。