1. 程式人生 > >s5pv210開發板 led驅動學習筆記

s5pv210開發板 led驅動學習筆記

相應驅動程式如下:
#include <linux/module.h>  
#include <linux/init.h>  
#include <linux/fs.h>  
#include <linux/types.h>  
#include <linux/gpio.h>  
#include <linux/ioctl.h>  
#include <linux/cdev.h>  
#include <linux/mm.h>  
#include <linux/device.h>  
#include <mach/regs-gpio.h>  
#include <mach/hardware.h>  
#include <asm/uaccess.h>  
#include <asm/atomic.h>  
#include <asm/unistd.h>
#include <mach/gpio.h>
#include <plat/gpio-cfg.h>
#include <asm/uaccess.h>
#include <asm/io.h>

//**************  
static struct device *dev;  
//*************  


 volatile unsigned long *gpio_con;
 volatile unsigned long *gpio_dat;

#define DEVICE_NAME "leds"  
//定義一個cdev結構體,並初始化
static struct cdev *cdevp=NULL; 
//定義一個裝置號 
static dev_t devno;

#define LED_OFF 0
#define LED_ON 1


static int leds_open(struct inode *inode, struct file *file)
{
    
    printk("use open\n");
	
    *gpio_con = 0x11111111;   //設定暫存器為輸出
    printk("gpio_con == %0x\n",*gpio_con);
    return 0;
}


static long leds_ioctl(struct file *filp,unsigned int cmd,unsigned long arg) 
{  
	printk("use ioctl\n");
	
	
	if(cmd == 1){
		*gpio_dat =0x1f;     //設定相應引腳位高電平
		printk("gpio_dat == %0x\n",*gpio_dat);
	}
	else
   		*gpio_dat &= 0x00;
} 

//應用程式與驅動對映  
static struct file_operations led_fops = {  
    .owner   = THIS_MODULE,
    .open    = leds_open,
   .unlocked_ioctl = leds_ioctl,
};  

static char __initdata banner[] =
	"CES-V210 LEDS , (c) 2012 Shenzhen Haitianxiong Electronic\n";

//**************  
static struct class *led_class;  
//**************  
static int __init led_init_module(void)  
{  
    int ret;  
    int err;  
    
    printk(banner);
    
    gpio_con = (volatile unsigned long *)ioremap(0xE0200C40, 16);
	
    gpio_dat = gpio_con + 1;

    ret = alloc_chrdev_region(&devno,0,1,DEVICE_NAME);//註冊裝置,函式向系統申請裝置號放在devno
    if(ret < 0)  
    {  
        printk(DEVICE_NAME "can't get the major number\n");  
        return ret;  
    }  

//****************************  
   led_class= class_create(THIS_MODULE,DEVICE_NAME);  
   if(IS_ERR(led_class)) {  
	printk("Err: failed in leds-class.\n");  
	return -1;  
   }  
   dev=device_create(led_class,NULL,devno,NULL,DEVICE_NAME);  
//************************** 
 
    cdevp = cdev_alloc();//動態申請一個cdev記憶體  
    cdev_init(cdevp,&led_fops);//初始化cdev  
    cdevp->owner = THIS_MODULE;  
    err=cdev_add(cdevp,devno,1);  
    if(err){  
         printk(KERN_NOTICE "Error %d adding cdev",err);  
         unregister_chrdev_region(devno,1);  
         return -EFAULT;  
    } 
 
    return 0;  
}  

static void __exit led_exit_module(void)  
{  
    cdev_del(cdevp);  
    unregister_chrdev_region(devno,1);  
    
//****************  
    device_destroy(led_class,devno);  
    class_destroy(led_class);  
    iounmap(gpio_con);
//*****************  
  
}  
  
module_init(led_init_module);  
module_exit(led_exit_module);  
MODULE_LICENSE("GPL");
MODULE_AUTHOR("w_fly");
MODULE_DESCRIPTION("CES-V210 led driver");





Makefile檔案如下: