码迷,mamicode.com
首页 > 系统相关 > 详细

linux下led灯驱动程序

时间:2014-05-15 10:16:13      阅读:571      评论:0      收藏:0      [点我收藏+]

标签:des   c   int   a   string   linux   

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

 

linux下led灯驱动程序,布布扣,bubuko.com

linux下led灯驱动程序

标签:des   c   int   a   string   linux   

原文地址:http://www.cnblogs.com/xiaoheng008/p/3728983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!