linux-2.6.30 下TE2410板子的LED驱动程序

TE2410的板子 点3 个LED,还有一个LED是别的端口的,这里先不用它了!

//驱动程序  //led.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    "led"
#define LED_MAJOR  200

/* 用来指定LED所用的GPIO引脚 */
static unsigned long led_table [] =
{
    S3C2410_GPF5,
    S3C2410_GPF6,
    S3C2410_GPF7,
  //  S3C2410_GPF8,
};

/* 用来指定GPIO引脚的功能:输出 */
static unsigned int led_cfg_table [] =
{
    S3C2410_GPF5_OUTP,
    S3C2410_GPF6_OUTP,
    S3C2410_GPF7_OUTP,
    //S3C2410_GPF8_OUTP,
};

MODULE_AUTHOR("ChenYong");
MODULE_LICENSE("Dual BSD/GPL");


static int Led_open(
    struct inode *inode,
    struct file  *file)
{
    int i;
    for (i=0; i<3; i++)
    {
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
    }
    return 0;
}

static int Led_ioctl(
    struct inode *inode,
    struct file *file,
    unsigned int cmd,
    unsigned long arg)
{
    if (arg > 3)
    {
        return -EINVAL;
    }

    switch(cmd)
    {
        case 0:
            // 设置指定引脚的输出电平为0
            s3c2410_gpio_setpin(led_table[arg], 0);
            return 0;

        case 1:
            // 设置指定引脚的输出电平为1
            s3c2410_gpio_setpin(led_table[arg], 1);
            return 0;

        default:
            return -EINVAL;
    }
}

static struct file_operations leds_fops = {
    .owner    =    THIS_MODULE,
    .open   =       Led_open,
    .ioctl    =    Led_ioctl,
};

struct led_dev_g
{
    struct cdev cdev;
}led_dev;

static void Led_setup_cdev(void)
{
    int err, devno = MKDEV(LED_MAJOR, 0);
    cdev_init(&led_dev.cdev, &leds_fops);
    led_dev.cdev.owner = THIS_MODULE;
    led_dev.cdev.ops   = &leds_fops;
  
    err = cdev_add(&led_dev.cdev, devno, 1);

    if (err)
        printk("Error %d/n", err);
    else
        printk("have finish add/n");
}


static int __init Led_init(void)
{
    int ret;
    dev_t devno = MKDEV(LED_MAJOR, 0);
    ret = register_chrdev_region(devno, 1, DEVICE_NAME);  
  
    if (ret < 0)
    {
        printk (DEVICE_NAME " can't register/n");  
        return ret;  
    }
  
    Led_setup_cdev();
    printk (DEVICE_NAME " Initialized /n");
    return 0;
}

static void __exit Led_exit(void)
{  
    cdev_del(&led_dev.cdev);
    unregister_chrdev_region(MKDEV(LED_MAJOR, 0), 1);
    printk(DEVICE_NAME " exit/n");
}

module_init(Led_init);
module_exit(Led_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ChenYong");
MODULE_DESCRIPTION("Led control for TE2410 Board");

 

驱动程序的Makefile (linux-2.6.30驱动程序的Makefile)

//Makefile

# Makefile 
# Please modify here or set environments.
# KSOURCE should be pointed to the build directory of your kernel.
#
DEBUG ?= n
KSOURCE ?= /home/eleday/linux/linux-2.6.30.yaffs            #此处修改为你自己的内核所在目录


%.x: %.c
    /usr/local/arm/4.3.2/bin/arm-linux-gcc -o $@ $<

KBUILD_VERBOSE:=1

obj-m := led.o


default:
    make -C $(KSOURCE) LANG=C KBUILD_VERBOSE=${KBUILD_VERBOSE}  SUBDIRS=`pwd` modules

.PHONY: cscope
cscope:
    cscope -b -k -R

.PHONY: clean
clean:
    make -C $(KSOURCE) LANG=C KBUILD_VERBOSE=${KBUILD_VERBOSE}  SUBDIRS=`pwd` clean
    rm -f *.x *~

 

 

应用程序

/***************
name : test_led
 **************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int i;
    int led_no;
    int fd;
    if (argc != 2 || sscanf(argv[1], "%d", &led_no) != 1 ||
         led_no < 0 || led_no > 1) {
        fprintf(stderr, "Usage: test_led 0|1/n");
        exit(1);
    }

    fd = open("/dev/led_for_loop", 0);

    if (fd < 0) {
        perror("open device led");
        exit(1);
    }
  
    for (i=0; i<3; i++)
    {
        ioctl(fd, 1, i);
    }
      
    printf("now let's go with the led trip, come on/n");
    sleep(1);

    if (led_no)
    { 
        printf("led_no = %d /n",led_no);

        for (i=0; i<3; i++)
        {  printf("i = %d /n",i );

            ioctl(fd, 0, i);
            sleep(1);
            ioctl(fd, 1, i);
        }      
    }
    else
    {
        printf("led_no = %d /n",led_no);

        for (i=0; i<3; i++)
        {  
            printf("i = %d /n",i );

            ioctl(fd, 0, 2-i);
            sleep(1);
            ioctl(fd, 1, 2-i);

        }   
  
    }

    printf("performan is over, thank you/n");
    close(fd);
    return 0;
}

 

应用程序的Makefile

CC = /usr/local/arm/4.3.2/bin/arm-linux-gcc
LD =/usr/local/arm/4.3.2/bin/arm-linux-ld 
INCLUDE=/ARM/ARMlinux/include 

MODCFLAGS:= -Wall -I $(INCLUDE)
mytest : mytest.c
    $(CC) -o mytest mytest.c
.PHONY: clean
clean:
    -rm -f *.o mytest

 

对驱动程序和应用程序都make之后

#insmod led.ko

 #lsmod  //查看添加的模块

 #mknod   /dev/led_for_loop   c   200  0

 ##./test_led  1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值