• 《Android深度探索》(卷1)HAL与驱动开发 第七章心得体会


    驱动的移植。

    在drivers/char 目录下,我们建立一个驱动程序文件mini2440_leds.c,内容如下:

    <span style="font-size: 18px;">#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>
    #define DEVICE_NAME "leds" <span style="color: rgb(51, 102, 255);">//设备名(/dev/leds</span>)
    <span style="color: rgb(51, 102, 255);">//LED 对应的GPIO 端口列表
    </span>static unsigned long led_table [] = {
    S3C2410_GPB(5),
    S3C2410_GPB(6),
    S3C2410_GPB(7),
    S3C2410_GPB(8),
    };
    <span style="color: rgb(51, 102, 255);">//LED 对应端口将要输出的状态列表
    </span>static unsigned int led_cfg_table [] = {
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    };
    <span style="color: rgb(51, 102, 255);">/*ioctl 函数的实现
    * 在应用/用户层将通过ioctl 函数向内核传递参数,以控制LED 的输出状态
    */
    </span>static int sbc2440_leds_ioctl(
    struct inode *inode,
    struct file *file,
    unsigned int cmd,
    unsigned long arg)
    {
    switch(cmd) {
    case 0:
    case 1:
    if (arg > 4) {
    return -EINVAL;
    }
    <span style="color: rgb(51, 102, 255);">//根据应用/用户层传递来的参数(取反),通过s3c2410_gpio_setpin 函数设置LED 对应的端口寄存
    器,</span>
    s3c2410_gpio_setpin(led_table[arg], !cmd);
    return 0;
    default:
    return -EINVAL;
    }
    }
    <span style="color: rgb(51, 102, 255);">/*
    * 设备函数操作集,在此只有ioctl 函数,通常还有read, write, open, close 等,因为本LED 驱动在下面已经
    * 注册为misc 设备,因此也可以不用open/close
    */
    </span>static struct file_operations dev_fops = {
    .owner = THIS_MODULE,
    .ioctl = sbc2440_leds_ioctl,
    };
    <span style="color: rgb(51, 102, 255); ">/*
    * 把LED 驱动注册为MISC 设备
    */
    </span>static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR, //动态设备号
    .name = DEVICE_NAME,
    .fops = &dev_fops,
    };
    <span style="color: rgb(51, 102, 255);">/*
    * 设备初始化
    */
    </span>static int __init dev_init(void)
    {
    int ret;
    int i;
    for (i = 0; i < 4; i++) {
    <span style="color: rgb(51, 102, 255);">//设置LED 对应的端口寄存器为输出(OUTPUT)
    </span>s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
    <span style="color: rgb(51, 102, 255);">//设置LED 对应的端口寄存器为低电平输出,在模块加载结束后,四个LED 应该是全部都是发光
    状态</span>
    s3c2410_gpio_setpin(led_table[i], 0);
    }
    ret = misc_register(&misc); <span style="color: rgb(51, 102, 255);">//注册设备</span>
    printk (DEVICE_NAME" initialized "); <span style="color: rgb(51, 102, 255);">//打印初始化信息</span>
    return ret;
    }
    static void __exit dev_exit(void)
    {
    misc_deregister(&misc);
    }
    module_init(dev_init); <span style="color: rgb(51, 51, 255);">//模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用
    </span>module_exit(dev_exit<span style="">);<span style="color: rgb(51, 102, 255);">//卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将调用此函
    </span></span><span style="">数
    </span>MODULE_LICENSE("GPL"); <span style="color: rgb(51, 102, 255);">//版权信息
    </span>MODULE_AUTHOR("FriendlyARM Inc."); <span style="color: rgb(51, 102, 255); ">//开发者信息</span></span>

    接下来,我们添加LED 设备的内核配置选项,打开drivers/char/Kconfig 文件,添加如下红色部分内容:
    config DEVKMEM
    bool "/dev/kmem virtual device support"
    default y
    help
    Say Y here if you want to support the /dev/kmem device. The
    /dev/kmem device is rarely used, but can be used for certain
    kind of kernel debugging operations.
    When in doubt, say "N".

    config LEDS_MINI2440
    tristate "LED Support for Mini2440 GPIO LEDs"
    depends on MACH_MINI2440
    default y if MACH_MINI2440

    help
    This option enables support for LEDs connected to GPIO lines
    on Mini2440 boards.
    config MINI2440_ADC
    bool "ADC driver for FriendlyARM Mini2440 development boards"
    depends on MACH_MINI2440
    default y if MACH_MINI2440
    help
    this is ADC driver for FriendlyARM Mini2440 development boards
    Notes: the touch-screen-driver required this option
    接下来,再根据该驱动的配置定义,把对应的驱动目标文件加入内核中,打开linux-2.6.32.2/drivers/char/Makefile 文件,添加如下红色部分内容:
    obj-$(CONFIG_JS_RTC) += js-rtc.o
    js-rtc-y = rtc.o
    obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o

    obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o
    # Files generated that shall be removed upon make clean
    clean-files := consolemap_deftbl.c defkeymap.c
    这样,我们就在内核中添加做好了LED 驱动。

    http://www.cnblogs.com/z378560707/

  • 相关阅读:
    加关注
    UI设计
    敏捷开发
    java书箱
    怎么优化JAVA程序的执行效率和性能?
    sql访注入
    Matlab中plot函数全功能解析
    matlab分割背景与物体
    ssh免密码登录
    c++字符串详解(转)
  • 原文地址:https://www.cnblogs.com/z378560707/p/5517166.html
Copyright © 2020-2023  润新知