Linux嵌入式系统开发之Led开发——驱动篇(一)

在mini2440中LED链接线使用引脚GPB5~8外接4个LED,操作方法是:

1)引脚功能设为输出。  2)要点亮LED,令引脚输出为0.      3)要熄灭LED,令引脚输出为1.

好了下边,给出详细的源码注释:Linux嵌入式系统开发之Led开发---驱动篇

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <machhardware.h>
#include <linux/gpio.h>
 
#define DEVICE_NAME     "leds_control"  
#define LED_MAJOR       231      
#define IOCTL_LED_ON    0     
#define IOCTL_LED_OFF   1 

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_OUTP,
    S3C2410_GPIO_OUTP,
    S3C2410_GPIO_OUTP,
    S3C2410_GPIO_OUTP,
}; 

//应用程序对设备文件/dev/leds执行open(...)时,就会调用s3c2410_leds_open函数 
static int s3c2410_leds_open(struct inode *inode, struct file *file)
{
    int i;
    for (i = 0; i < 4; i++) {
       // 设置GPIO引脚的功能:本驱动中LED所涉及的GPIO引脚设为输出功能
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
    }
    return 0;
} 

//应用程序对设备文件/dev/leds执行ioclt(...)时,就会调用s3c2410_leds_ioctl函数 
static int s3c2410_leds_ioctl(
    struct inode *inode, 
    struct file *file, 
    unsigned int cmd, 
    unsigned long arg)
{
    if (arg > 4) {
        return -EINVAL;
    }
    switch(cmd) {
    case IOCTL_LED_ON:
        // 设置指定引脚的输出电平为0
        s3c2410_gpio_setpin(led_table[arg], 0);
        return 0; 

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

    default:
        return -EINVAL;
    }
} 

//字符设备驱动程序的核心,当应用程序操作设备文件时所调用的open、read、write等函数,最终会调用这个结构中指定的对应函数
static struct file_operations s3c2410_leds_fops = {
    .owner  =   THIS_MODULE,    
    .open   =   s3c2410_leds_open,     
    .ioctl  =   s3c2410_leds_ioctl,
}; 

//执行“insmod s3c2410_leds.ko”命令时就会调用这个函数
static int __init s3c2410_leds_init(void)
{
    int ret; 

    
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c2410_leds_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
    printk(DEVICE_NAME " initialized\n");
    return 0;
} 

//执行”rmmod s3c24xx_leds.ko”命令时就会调用这个函数 
static void __exit s3c2410_leds_exit(void)
{
        unregister_chrdev(LED_MAJOR, DEVICE_NAME); 
} 

module_init(s3c2410_leds_init);    
module_exit(s3c2410_leds_exit);

 

上面的代码看清楚了吗,那么怎么使用呢,使用方法有两种(假设保存为leds_control.c):

方法一:
将代码放到内和drivers/char目录下,在drivers/char/Makefile中增加一行obj-m    += leds_control.o,然后在内核根目录下执行make modules,就可以生成模块drivers/char/leds_control.ko。

方法二:

直接在当前驱动源码目录下,建立Makefile文件,内容如下:

CROSS=arm-linux-

#依赖的内核源代码目录,不一定是当前系统的,要是开发板系统源码的目录
KERNELDIR = /opt/linux-2.6.32.2
PWD := $(shell pwd)
.PHONY:

<tab>modules clean
obj-m += leds_control.o
modules:

<tab>$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
<tab>rm -rf *.o *~ core .depend .*.cmd  *.mod.c .tmp_versions

然后执行make,可以看到在当前目录下也会生成leds_control.ko.

 

通过以上两种方法,生成了leds_control.ko,下面就用ftp下载到开发板上,用:

insmod ./leds_control.ko

mknod /dev/leds_control c 150 0

就建好了驱动模块和设备节点,接下来就是应用程序调用、、、

转载于:https://www.cnblogs.com/LakeFollow/archive/2012/07/30/2614454.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值