EXTRA_CFLAGS += $(DEBFLAGS) -Wall
ifneq ($(KERNELRELEASE),)
obj-m := led.o
else
KDIR ?= /opt/270-s-2.6.9/linux-2.6.9_270-s #之前的内核位置
PWD := $(shell pwd)
all:
make $(EXTRA_CFLAGS) -C $(KDIR) M=$(PWD) modules
endif
clean:
rm *.o *.ko led.mod.c
test.c:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEV_NAME "/dev/test"
#define FLAGS (O_RDWR | O_CREAT)
int main(void)
{
int fd = -1;
int i = -1;
int cnt = -1;
unsigned char led = 0xff;
//open
fd = open(DEV_NAME, FLAGS);
if(0 > fd)
{
perror("open err:");
goto _out;
}
//流水灯
while(1)
{
for(i = 0; i < 8; i++)
{
led &= ~(1 << i);
cnt = write(fd, &led, 1);
if(0 > cnt)
{
perror("write err:");
goto _out;
}
led = 0xff;
sleep(1);
}
}
//close
close(fd);
return 0;
_out:
return -1;
}
由于代码比较多,2个目录,一个驱动driver目录,一个应用程序目录test,上面程序的下载代码地址:
具体下载目录在 /2013年资料/4月/15日/Linux设备驱动的Hello World—LED驱动