天天看點

字元裝置驅動---Led

/*******************************************
使用linux3.2.81核心
********************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>

static struct class *ledsdrv_class;             //類結構體
static struct device    *ledsdrv_class_dev;  //裝置結構體

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


static int leds_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 leds_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    int val;


    //将使用者空間的資料傳送到核心空間
    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;
}

/*
當應用程式操作裝置檔案時所調用的open、read、write等函數,最終會調用這個結構體中上的對應函數
*/
static struct file_operations leds_drv_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個宏,推向編譯子產品時自動建立的__this_module變量 */
    .open   =   leds_drv_open,     
    .write  =   leds_drv_write,    
};


int major;
//指定insmod指令時會調用這個函數
static int leds_drv_init(void)
{
    //第一個參數如果等于0,則表示采用系統動态配置設定的主裝置号;不為0,則表示靜态注冊
    major = register_chrdev(0, "leds_drv", &leds_drv_fops); // 注冊, 告訴核心,傳回動态建立的主裝置号

    //以下兩條語句是為了實作自動建立裝置
    ledsdrv_class = class_create(THIS_MODULE, "ledsdrv");  //class_create為該裝置建立一個class
    ledsdrv_class_dev = device_create(ledsdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ //class_device_create建立對應的裝置

    //驅動中要使用虛拟位址,不能直接使用實體位址
    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);  //gpfcon映射為0x56000050,映射長度為16個位元組(gpfcon為虛位址,0x56000050為實體位址)
    gpfdat = gpfcon + 1;  //gpfcon + 1即為0x56000050+4

    return 0;
}

//執行rmmod時會調用這個函數
static void leds_drv_exit(void)
{
    unregister_chrdev(major, "leds_drv");               // 解除安裝

    device_unregister(ledsdrv_class_dev);  //将自動建立的裝置登出
    class_destroy(ledsdrv_class);                       //删除建立的類
    iounmap(gpfcon);                                        //取消gpfcon的映射
}

//指定驅動程式的初始化函數和解除安裝函數
module_init(leds_drv_init);
module_exit(leds_drv_exit);


MODULE_LICENSE("GPL");  //防止出現“module license”unspecified taints kernel的警告      
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

 /* led_test on
  * led_test off
  */
int main(int argc, char **argv)
{
    int fd;   //檔案句柄
    int val = 1;
    fd = open("/dev/xyz", O_RDWR);  //使用讀寫模式打開/dev/xyz裝置檔案
    if (fd < 0)
    {
        printf("can't open!\n");  //打開失敗
    }
    if (argc != 2)   //參數個數不為2,則顯示使用方法
    {
        printf("Usage :\n");
        printf("%s <on|off>\n", argv[0]);
        return 0;
    }

    if (strcmp(argv[1], "on") == 0)   //第二個參數為"on"
    {
        val  = 1;
    }
    else
    {
        val = 0;
    }

    write(fd, &val, 4);   //寫入4個位元組資料到裝置檔案,該函數最終将調用底層驅動的write函數
    return 0;
}      

繼續閱讀