现在的位置: 首页 > 综合 > 正文

Mini2440LED平台设备驱动

2018年03月16日 ⁄ 综合 ⁄ 共 4267字 ⁄ 字号 评论关闭

平台设备驱动包括两部分:平台设别驱动模块和平台设备模块,平台设备包含设备占用的资源,比如说端口、中断等,而平台设备驱动则是正真的设备驱动,通过设备名称完成匹配,需要完成设备操作接口。下面是MINI2440的设备模块和设备驱动模块以及测试模块代码:

led_device.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <mach/irqs.h>
#include <linux/io.h>
#include <mach/hardware.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

MODULE_AUTHOR("WK") ;
MODULE_LICENSE("GPL") ;

static void led_release()
{
printk("led device is released!\n") ;
}
static struct resource led_resource[]={
[0]={
.start=0x56000010,
.end=0x56000018,
.flags=IORESOURCE_MEM,
},
[1]={
.start=5 ,
.end=5 ,
.flags=IORESOURCE_IRQ,
},
};
static struct platform_device led_device={
.name="led_platform" ,
.id = -1 ,
.dev = {
.release= led_release,
},
.num_resources =ARRAY_SIZE(led_resource) ,
.resource = led_resource ,
} ;
static int __init led_init()
{
platform_device_register(&led_device) ;
return 0 ;
}
static void __exit led_exit()
{
platform_device_unregister(&led_device) ;
}
module_init(led_init) ;
module_exit(led_exit) ;

led_driver.c

#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/io.h>
MODULE_AUTHOR("WK") ;
MODULE_LICENSE("Dual BSD/GPL") ;

volatile static unsigned int *gpb_con=NULL ;
volatile static unsigned int *gpb_dat=NULL ;
static int led_ioctl(
    struct inode *inode,
    struct file * file,
    unsigned int cmd,
    unsigned int arg
    )
{
    if(cmd>1||arg>3){
        return -EINVAL ;
    }
    switch(cmd){
        case 0:
            *gpb_dat |=(0b01<<(5+arg)) ;
            break ;
        case 1:
            *gpb_dat &= (~(0b01<<(5+arg))) ;
            break ;
        default:
            return -EINVAL ;
    }
    return 0 ;
}

static struct file_operations led_fops={
    .owner = THIS_MODULE ,
    .ioctl = led_ioctl,
} ;

static struct miscdevice led_dev={
    .minor = MISC_DYNAMIC_MINOR,
    .name = "led_platform",
    .fops = &led_fops,
};
static int led_probe(struct platform_device *dev)
{

    int ret = -1 ;
    struct resource *ptr_res = NULL ;
    ptr_res = platform_get_resource(dev,IORESOURCE_MEM,0) ;
    if(ptr_res == NULL) {
        dev_err(dev, "no memory resource specified!\n") ;
        return -ENOENT ;
    }
    
    int mem_size=ptr_res->end-ptr_res->start+1 ;
    
    gpb_con = ioremap(ptr_res->start, mem_size) ;
    
    gpb_dat =gpb_con + 1 ;
    
    ret = misc_register(&led_dev) ;
//    /*
    *gpb_con &= (~(0b11111111<<5*2)) ;
    *gpb_con |= (0b01010101<<2*5);    
        
    *gpb_dat &= (~(0b1111<<5)) ;
//    */
/*
    *gpb_con &= (~((0x11<<5*2) | (0x11<<6*2) | (0x11<<7*2) | (0x11<<8*2))) ;
    *gpb_con |= (0x01<<5*2) | (0x01<<6*2) | (0x01<<7*2) | (0x01<<8*2) ;

    *gpb_dat &= (~((0x01<<5) | (0x01<<6) | (0x01<<7) | (0x01<<8) )) ;
*/
    printk("led deivce is probed!\n") ;
    return ret ;
}
static int led_remove(
    struct platform_device *dev
    )
{
    misc_deregister(&led_dev) ;
    iounmap(gpb_con) ;
    printk("led device is removed!\n") ;
}

static struct platform_driver led_driver={
    .probe = led_probe ,
    .remove = led_remove ,
    .driver = {
        .name = "led_platform" ,
        .owner = THIS_MODULE ,
    },
} ;
static int __init init_led_drv(void)
{
    platform_driver_register(&led_driver) ;
    return 0 ;
}
static void __exit exit_led_drv(void)
{
    platform_driver_unregister(&led_driver) ;
}
module_init(init_led_drv) ;
module_exit(exit_led_drv) ;

测试代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "sys/types.h"
#include "sys/ioctl.h"
#include "fcntl.h"

int main() {
    int fd=-1 ;
    int i,j ;
    
    fd=open("/dev/led_platform",O_RDWR) ;
    if(fd<0) {
        printf("open led device failed!\n") ;
        return -1 ;
    }
    i=0 ;
    while(1) {
        if(i%2) {
            ioctl(fd,0,0) ;
            ioctl(fd,1,1) ;    
            ioctl(fd,0,2) ;
            ioctl(fd,1,3) ;
        }else {
            ioctl(fd,1,0) ;
            ioctl(fd,0,1) ;    
            ioctl(fd,1,2) ;
            ioctl(fd,0,3) ;
        }
        sleep(1) ;
        i++ ;
    }
    close(fd) ;
    return 0 ;
}

Makefile:

    CC=arm-linux-gcc
    KDIR=/root/workspace/mini2440/linux-2.6.32.2
ifeq ($(driver),1)
ifneq ($(KERNELRELEASE),)
    obj-m:=led_device.o led_driver.o
else
    PWD=$(shell pwd)
module:
    $(MAKE) -C $(KDIR) M=$(PWD) modules
endif
install:
    insmod led_device.ko led_driver.ko
uninstall:
    rmmod led_device led_driver
endif
ifeq ($(driver),0)
    OBJS=main.o
    
    CFLAGS=-Wall -g -O
    CFLAGS+=-I $(KDIR)
    CFLAGS+=-static
main:$(OBJS)
    $(CC) $(OBJS) -o main
$(OBJS):%o:%c
    $(CC) -c $(CFLAGS) $< -o $@
endif

clean:
    rm -fr *.o *.ko main *.order *.symvers *.mod*

通过make driver=1命令编译驱动,通过make driver=0编译测试文件

抱歉!评论已关闭.