linux驱动编程实践之LED驱动

开发板:TQ2440

内核:2.6.30.4-EmbedSky

实现目标:控制led开关,并当前读取状态

-------驱动实现  mini2440_leds.c-------

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>


#define DEVICE_NAME "leds"   //要操作的设备名称,加载成功后在/dev/下出现
//操作的4个led
static unsigned long led_table [] = {
 S3C2410_GPB5,
 S3C2410_GPB6,
 S3C2410_GPB7,
 S3C2410_GPB8,
};
//输出
static unsigned int led_cfg_table [] = {
 S3C2410_GPB5_OUTP,
 S3C2410_GPB6_OUTP,
 S3C2410_GPB7_OUTP,
 S3C2410_GPB8_OUTP,
};

/*static unsigned int led_cfg_table1 [] = {
 S3C2410_GPB5_INP,
 S3C2410_GPB6_INP,
 S3C2410_GPB7_INP,
 S3C2410_GPB8_INP,
};*/

static int sbc2440_leds_ioctl(
 struct inode *inode, 
 struct file *file, 
 unsigned int cmd, 
 unsigned long arg)
{
        unsigned char i,tmp;         

 switch(cmd) {
 case 0:
 case 1:
  if (arg > 4) {
   return -EINVAL;
  }
  s3c2410_gpio_cfgpin(led_table[arg], led_cfg_table[arg]);
  s3c2410_gpio_setpin(led_table[arg], !cmd); //关或开
  return 0;
 case 2:  
  tmp = s3c2410_gpio_getpin(led_table[arg]); //读取状态
                tmp = tmp >>(arg+5);

  return (unsigned int)tmp;
 default:
  return -EINVAL;
 }
}

static struct file_operations dev_fops = {
 .owner = THIS_MODULE,
 .ioctl = sbc2440_leds_ioctl,
};
//混杂结构
static struct miscdevice misc = {
 .minor = MISC_DYNAMIC_MINOR,
 .name = DEVICE_NAME,
 .fops = &dev_fops,
};

static int __init dev_init(void)
{
 int ret;

 int i;
 //初始化四个led,并打开
 for (i = 0; i < 4; i++) {
  s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  s3c2410_gpio_setpin(led_table[i], 0);
 }

 ret = misc_register(&misc);

 printk (DEVICE_NAME"\tinitialized\n");

 return ret;
}

static void __exit dev_exit(void)
{
 misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");


======应用程序app-led.c======
 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int on;
    int led_no;
    int fd;
    if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on) != 1 ||
            on < 0 || on > 2 || led_no < 0 || led_no > 3) {
        fprintf(stderr, "Usage: leds led_no 0|1|2\n");
        exit(1);
    }
    fd = open("/dev/leds", 0);
    if (fd < 0) {
        perror("open device leds");
        exit(1);
    }
    if(on==2) //读取led状态
    {
        printf("state:=%d\n",ioctl(fd, on, led_no));
    }
    else      //控制led开或关,即0,1
    {
        ioctl(fd, on, led_no);
    }  
    close(fd);
    return 0;
}


=========Makefile==========

ifneq ($(KERNELRELEASE),)

obj-m := mini2440_leds.oelse 
KDIR := /opt/guoqian/opt/EmbedSky/linux-2.6.30.4

all:
 make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
 rm -f *.ko *.o *.mod.o *.mod.c *.symvers  modul*
endif


=======操作结果======
[root@EmbedSky /tmp]# insmod mini2440_leds.ko
[root@EmbedSky /tmp]# ./app-led 1 2
state:=0
[root@EmbedSky /tmp]# ./app-led 1 0
[root@EmbedSky /tmp]# ./app-led 1 2
state:=1
[root@EmbedSky /tmp]#

你可能感兴趣的:(linux驱动编程实践之LED驱动)