Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

mini2440上LEDS的驱动程序(含测试程序)

来源: sg131971 分享至:

leds_drv.h

#ifndef _MEMDEV_H_
#define _MEMDEV_H_
#include <linux/ioctl.h>
/* 定义幻数 */
#define MEMDEV_IOC_MAGIC  'k'
/* 定义命令 */
#define MEMDEV_IOC_ON   _IO(MEMDEV_IOC_MAGIC, 1)
#define MEMDEV_IOC_OFF _IO(MEMDEV_IOC_MAGIC, 2)
#define MEMDEV_IOC_MAXNR 2
#define LED_ON  0
#define LED_OFF 1
#endif                          /* _MEMDEV_H_ */
leds_drv.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/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 <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>

#include "leds_drv.h"

#define DEVICE_NAME "leds"

static unsigned long led_table[] = {
    S3C2410_GPB(5),
    S3C2410_GPB(6),
    S3C2410_GPB(7),
    S3C2410_GPB(8),
};

static unsigned int led_cfg_table[] = {
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
};

static int s3c2440_leds_ioctl(struct inode *inode,
                              struct file *file, unsigned int cmd, unsigned long arg)
{

    /* 检测命令的有效性 */
    if (_IOC_TYPE(cmd) != MEMDEV_IOC_MAGIC)
        return -EINVAL;
    if (_IOC_NR(cmd) > MEMDEV_IOC_MAXNR)
        return -EINVAL;

    /* 根据命令,执行相应的操作 */
    switch (cmd)
    {
      case MEMDEV_IOC_OFF:
          s3c2410_gpio_setpin(led_table[arg], LED_OFF);
          return 0;

      case MEMDEV_IOC_ON:
          s3c2410_gpio_setpin(led_table[arg], LED_ON);
          return 0;
      default:
          return -EINVAL;
    }
}

/*文件操作结构体*/
static struct file_operations dev_fops = {
    .owner = THIS_MODULE,
    .ioctl = s3c2440_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;
    /*设置GPIO控制寄存器,GPIO设置为输出模式,默认下灯全灭 */
    for (i = 0; i < 4; i++)
    {
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
        s3c2410_gpio_setpin(led_table[i], 1);
    }
    /*注册混杂型字符设备驱动 */
    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_AUTHOR("Cried Devil");
MODULE_LICENSE("GPL");
app-leds.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "leds_drv.h"           /* 包含命令定义 */
int main(int argc, char **argv)
{
    int on;
    int fd;
    int num;
    if (argc != 3 ||
        sscanf(argv[1],
               "%d",
               &num) !=
        1 || sscanf(argv[2], "%d", &on) != 1 || on < 0 || on > 1 || num < 0 || num > 3)
    {
        fprintf(stderr, "Usage:  %s   led_num   off|on\n", argv[0]);
        fprintf(stderr, "Usage:  %s   0|1|2|3   0|1\n", argv[0]);
        fprintf(stderr, "Ex   :  %s   0         1\n", argv[0]);
        exit(1);
    }
    fd = open("/dev/leds", 0);
    if (fd < 0)
    {
        perror("open device leds");
        exit(1);
    }

    if (on)
    {
        // printf("Turn on led %d!\n", num);
        ioctl(fd, MEMDEV_IOC_ON, num);
    }
    else
    {
        // printf("Turn off led %d!\n", num);
        ioctl(fd, MEMDEV_IOC_OFF, num);
    }
    close(fd);
    return 0;
}
Makefile

ifneq ($(KERNELRELEASE),)

obj-m := leds_drv.o

else
	
KDIR := /home/youshan/linux-2.6.32.2

MYAPP := app-leds

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

endif

编译结果:

[root@ShiGuang leds]# 
[root@ShiGuang leds]# make
make -C /home/youshan/linux-2.6.32.2 M=/home/my_project/temperature/leds modules ARCH=arm CROSS_COMPILE=arm-linux-
make[1]: 进入目录“/home/youshan/linux-2.6.32.2”
  CC [M]  /home/my_project/temperature/leds/leds_drv.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/my_project/temperature/leds/leds_drv.mod.o
  LD [M]  /home/my_project/temperature/leds/leds_drv.ko
make[1]: 离开目录“/home/youshan/linux-2.6.32.2”
arm-linux-gcc app-leds.c -o app-leds
[root@ShiGuang leds]# 




Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史