linux下led灯驱动程序

1.驱动程序

led.c

***************************************************************************************************************************8 

#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 led1 0x01
#define led2 0x02
#define led3 0x04
#define led4 0x08
#define ledall 0x0f

#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 led_on_all ~1<<5) & ~1<<6) & ~1<<7) & ~1<<8))

#define led_off_1 1<<5)
#define led_off_2 1<<6)
#define led_off_3 1<<7)
#define led_off_4 1<<8)
#define led_off_all 1<<5) | 1<<6) | 1<<7) | 1<<8))

#define DEVICE_NAME “linux_led”

MODULE_AUTHOR”xiaoheng”);

inline void light1_onvoid)
{
writelled_on_1 & readlgpbdat), gpbdat);
}
inline void light1_offvoid)
{
writelled_off_1 | readlgpbdat), gpbdat);
}

inline void light2_onvoid)
{
writelled_on_2 & readlgpbdat), gpbdat);
}
inline void light2_offvoid)
{
writelled_off_2 | readlgpbdat), gpbdat);
}

inline void light3_onvoid)
{
writelled_on_3 & readlgpbdat), gpbdat);
}
inline void light3_offvoid)
{
writelled_off_3 | readlgpbdat), gpbdat);
}

inline void light4_onvoid)
{
writelled_on_4 & readlgpbdat), gpbdat);
}
inline void light4_offvoid)
{
writelled_off_4 | readlgpbdat), gpbdat);
}

inline void lightall_onvoid)
{
writelled_on_all & readlgpbdat), gpbdat);
}

inline void lightall_offvoid)
{
writelled_off_all | readlgpbdat),gpbdat);
}

int light_openstruct inode* inode, struct file* filp)
{
printk”myled open NOW!”);
writel1<<10) | 1<<12) | 1<<14) | 1<<16)) | readlgpbcon), gpbcon);
lightall_on);
return 0;
}

int light_releasestruct inode* inode, struct file* filp)
{
lightall_off);
printk”myled close NOW!”);
return 0;
}

int light_ioctlstruct inode* inode, struct file* filp, unsigned int cmd,unsigned long t)
{
switchcmd)
{
case LIGHT_ON:
if t == ledall)
lightall_on);
else
{
if t & led1) {light1_on); printk”led1_on
“);}
if t & led2) {light2_on); printk”led2_on
“);}
if t & led3) {light3_on); printk”led3_on
“);}
if t & led4) {light4_on); printk”led4_on
“);}
}
break;

case LIGHT_OFF:
if t == ledall)
lightall_off);
else
{
if t & led1) {light1_off); printk”led1_off
“);}
if t & led2) {light2_off); printk”led2_off
“);}
if t & led3) {light3_off); printk”led3_off
“);}
if t & led4) {light4_off); printk”led4_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_initvoid)
{
int ret;
printk”my led init NOW
“);
ret = misc_register&misc);
printkDEVICE_NAME”initialized
“);

return ret;
}

void light_cleanupvoid)
{
printk”myled OVER
“);
misc_deregister&misc);
}

module_initlight_init);
module_exitlight_cleanup);
MODULE_LICENSE”Dual BSD/GPL”);

*******************************************************************************************************************

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 0x02

void delay)
{
unsigned int i, j;
for i = 0; i < 0xf; i++)
for j = 0; j < 0xff; j++)
;
}

int mainint argc, char **argv)
{
int fd;
int i = 2;
unsigned long led = 0;

if argc < 3)
{
printf”usage: led [on/off] [led1|led2|led3|led4]
“);
return 0;
}

fd = open”/dev/linux_led”, 0);

whilei < argc)
{
if !strcmpargv[i], “led1”))
led += 1;
else if !strcmpargv[i], “led2”))
led += 2;
else if !strcmpargv[i], “led3”))
led += 4;
else if !strcmpargv[i], “led4”))
led += 8;

i++;
}

printf”The led is %d
“, led);

if!strcmpargv[1], “on”))
{
while1)
{
//delay);
ioctlfd, LED_ON, led);
//printf”The option is on!
“);
}
}
else if !strcmpargv[1], “off”))
{
while1)
{
//delay);
ioctlfd, LED_OFF, led);
//printf”The option is off!
“);
}
}
else
printf”the led option is on or off !”);

closefd);
return 0;

}

 **************************************************************************************************************8

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

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注