linux下led灯驱动程序
1.驱动程序
led.c
#include <linux/module.h>
#include <linux/init.h>
#include
<linux/fs.h>
#include <linux/cdev.h>
#include
<linux/types.h>
#include <asm/io.h>
#include
<asm/irq.h>
#include <mach/regs-gpio.h>
#include
<asm/uaccess.h>
#include <linux/delay.h>
#include
<linux/kernel.h>
#include <linux/device.h>
#include
<mach/hardware.h>
#include <linux/miscdevice.h>
#define LIGHT_ON 0X01
#define LIGHT_OFF 0X02
#define gpbcon S3C2410_GPBCON
#define gpbdat S3C2410_GPBDAT
#define
gpbup S3C2410_GPBUP
#define led_on_1 ~(1<<5)
#define led_on_2 ~(1<<6)
#define
led_on_3 ~(1<<7)
#define led_on_4 ~(1<<8)
#define DEVICE_NAME "linux_led"
MODULE_AUTHOR("xiaoheng");
void light_on(void)
{
writel(led_on_1, gpbdat);
mdelay(300);
printk("one");
writel(led_on_2, gpbdat);
mdelay(300);
printk("two");
writel(led_on_3, gpbdat);
mdelay(300);
printk("three");
writel(led_on_4, gpbdat);
mdelay(300);
printk("four");
writel(0, gpbdat);
mdelay(300);
printk("all
on\n");
}
void light_off(void)
{
writel(0xffff,gpbdat);
}
int light_open(struct inode* inode, struct file* filp)
{
printk("myled open NOW!");
writel((1<<10) | (1<<12) |
(1<<14) | (1<<16), gpbcon);
writel(0, gpbdat);
return
0;
}
int light_release(struct inode* inode, struct file* filp)
{
printk("myled close NOW!");
return 0;
}
int light_ioctl(struct inode* inode, struct file* filp, unsigned int
cmd,unsigned long t)
{
switch(cmd)
{
case
LIGHT_ON:
light_on();
break;
case
LIGHT_OFF:
light_off();
break;
default:
return -ENOTTY;
}
return 0;
}
struct file_operations light_fops = {
.owner = THIS_MODULE,
.ioctl = light_ioctl,
.open = light_open,
.release =
light_release,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &light_fops,
};
int light_init(void)
{
int ret;
printk("my led init
NOW\n");
ret = misc_register(&misc);
printk(DEVICE_NAME"initialized\n");
return ret;
}
void light_cleanup(void)
{
printk("myled OVER\n");
misc_deregister(&misc);
}
module_init(light_init);
module_exit(light_cleanup);
2.测试程序
ledtest.c
#include <stdio.h>
#include <string.h>
#include
<stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define LED_ON 0x01
#define LED_OFF 0x01
void delay()
{
unsigned int i, j;
for (i = 0; i < 0xf;
i++)
for (j = 0; j < 0xff; j++)
;
}
int main(int argc, char **argv)
{
int fd;
fd = open("/dev/linux_led", 0);
if(strcmp(argv[1], "0"))
{
while(1)
{
delay();
ioctl(fd, LED_ON, 0);
printf("1\n");
}
}
else if (strcmp(argv[1], "1"))
{
printf("0");
ioctl(fd, LED_OFF, 0);
}
return 0;
}
3.Makefile
obj-m:=led.o
KDIR:=/home/xiaoheng/Desktop/2.6.30.4/opt/EmbedSky/linux-2.6.30.4
all:
make
-C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd)
clean
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。