字符設備驅動程序--LED驅動

編寫驅動程序須要編寫那些代碼:node

一、硬件相關的驅動程序linux

二、Makefile的編譯程序框架

三、還須要編寫一個相關的測試程序函數

好比說:一個攝像頭驅動程序測試

一、驅動程序的編寫,須要編寫一些硬件相關的操做,編譯Makefilethis

二、安裝、運行、卸載驅動程序(insmod ***、。./*** 、remod *** )。spa

三、使用這個驅動程序:須要一個測試程序,如QQ(測試程序)打開攝像頭。code

 

編寫驅動程序框架:blog

APP:(測試程序)                         open         read         write         .........開發

-------------------------------------------------------------------------------------------

內核                                           sys_open    sys_read  sys_writ    sys_.......

 

-------------------------------------------------------------------------------------------

驅動程序                          入口函數:

                                                    註冊一個結構體:register("  ",&file_operation);

                                                    /* 這裏執行相關的硬件操做 */

                                                    struct   file_operation{

                                                           .open      =    open_,

                                                           .read       =    read_,

                                                           .write       =    write_,

                                                    }

                                     出口函數: 

                                                   

                                     

----------------------------------------------------------------------------------------------------------------------------------------------------

驅動程序的編寫步驟包括:

入口函數

static int first_drv_init(void)

{major = register_chrdev(0, "first_drv", &first_drv_fops); // 註冊, 告訴內核}

出口函數

static void first_drv_exit(void)

{
  unregister_chrdev(major, "first_drv"); // 卸載

}

構造一個file_operation結構體

static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動建立的__this_module變量 */
.open = first_drv_open,
.write = first_drv_write,
};

相關的修飾:讓內核知道這是個特殊的函數,做爲驅動用

module_init(first_drv_init);
module_exit(first_drv_exit);

在加上一個協議:應爲Linux爲開源的,因此要遵循一些協議

MODULE_LICENSE("GPL");

剩下的就是對結構體裏面的open、read、write函數進行硬件操做,如open對硬件引腳的定義、設置,read讀取硬件引腳寄存器的狀態,如燈是開仍是關,write就是對相關硬件寄存器的操做,好比對相關數據寄存器寫0/1來控制LED燈的亮滅。

相關硬件操做:好比對於LED燈簡單硬件操做而言

根據芯片手冊、原理圖、肯定硬件,操做相關寄存器對設置相關的引腳。而後設置相關的數據寄存器對引腳的控制。

 

上層測試程序會調用open、read、write函數最終會調用到驅動程序file_opreation結果體裏面的open、read、write函數進而對硬件如LED燈的點亮操做。

相關代碼以下

fist_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

static struct class *firstdrv_class;
static struct class_device    *firstdrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;


static int first_drv_open(struct inode *inode, struct file *file)
{
    //printk("first_drv_open\n");
    /* 配置GPF4,5,6爲輸出 */
    *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
    *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
    return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    int val;

    //printk("first_drv_write\n");

    copy_from_user(&val, buf, count); //    copy_to_user();

    if (val == 1)
    {
        // 點燈
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        // 滅燈
        *gpfdat |= (1<<4) | (1<<5) | (1<<6);
    }
    
    return 0;
}

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動建立的__this_module變量 */
    .open   =   first_drv_open,     
    .write    =    first_drv_write,       
};


int major;
static int first_drv_init(void)
{
    major = register_chrdev(0, "first_drv", &first_drv_fops); // 註冊, 告訴內核

    firstdrv_class = class_create(THIS_MODULE, "firstdrv");

    firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    gpfdat = gpfcon + 1;

    return 0;
}

static void first_drv_exit(void)
{
    unregister_chrdev(major, "first_drv"); // 卸載

    class_device_unregister(firstdrv_class_dev);
    class_destroy(firstdrv_class);
    iounmap(gpfcon);
}

module_init(first_drv_init);
module_exit(first_drv_exit);


MODULE_LICENSE("GPL");

編寫:Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m    += first_drv.o

 

編譯成功後,把程序放到開發板文件系統目錄下,執行insmod  文件名  裝載驅動,運行驅動程序爲./文件名   若是想卸載執行remod 文件名命令

 

測試程序:

firstdrvtest.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
    int fd;
    int val = 1;
    fd = open("/dev/xyz", O_RDWR);
    if (fd < 0)
    {
        printf("can't open!\n");
    }
    if (argc != 2)
    {
        printf("Usage :\n");
        printf("%s <on|off>\n", argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "on") == 0)
    {
        val  = 1;
    }
    else
    {
        val = 0;
    }
    
    write(fd, &val, 4);
    return 0;
}
相關文章
相關標籤/搜索