慢慢学Linux驱动开发,第九篇,tiny6410_LED驱动



     一直在看代码,今天准备真正去试试,就拿tiny6410的LED灯开刀,虽说是自己写,但实际上也是参考例程来的。不过还好基本的思路还是蛮清晰的。

 

     定义本次驱动为misc device(杂项设备驱动),包含头文件miscdevice.h,其实所谓的杂项驱动程序就是主设备号为10的字符设备驱动,其实就是用主设备号10调用了函数register_chrdev()。且misc device会自动在/dev目录下建立设备节点,而不需用mkmod手动建立。

 

开发板IO定义:

 

慢慢学Linux驱动开发,第九篇,tiny6410_LED驱动_linux

 

S3C6410 GPK口:

 

慢慢学Linux驱动开发,第九篇,tiny6410_LED驱动_#include_02

 

慢慢学Linux驱动开发,第九篇,tiny6410_LED驱动_linux_03

慢慢学Linux驱动开发,第九篇,tiny6410_LED驱动_#include_04

 






#include <linux/miscdevice.h>  
#include <linux/fs.h>  
#include <linux/pci.h>  
#include <mach/map.h>  
#include <mach/regs-gpio.h>  
#include <mach/gpio-bank-k.h>  
#include "leds.h"  
#define DEVICE_NAME "tiny6410_leds"  
MODULE_LICENSE("GPL");  
MODULE_AUTHOR("pang123hui");  
static long sbc2440_leds_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)  
{  
switch(cmd) {  
        unsigned tmp;  
/* 
    case 0: 
    case 1: 
        if(arg > 4) 
        { 
            //非法参数 
            return -EINVAL; 
        } 
        tmp = readl(S3C64XX_GPKDAT); 
        tmp &= ~(1 << (3 + arg)); 
        tmp |= ((cmd) << (3 + arg)); 
        writel(tmp, S3C64XX_GPKDAT); 
        return 0; 
*/  

case 1:  
case 2:  
case 3:  
case 4:  
        tmp = readl(S3C64XX_GPKDAT);  
        tmp = (tmp & ~(1<<(cmd - 1 + 4)))|(arg<<(cmd -1 + 4));  
        writel(tmp, S3C64XX_GPKDAT);  
return 0;  

default:  
return -EINVAL;  
    }  
}  
static struct file_operations dev_fops = {  
    .owner          = THIS_MODULE,  
    .unlocked_ioctl = sbc2440_leds_ioctl,  
};  
//misc device:杂项设备,即主设备号为10的特殊字符设备  
static struct miscdevice misc = {  
//次设备号,注意不要与/proc/misc中已有杂项设备次设备号冲突  
//MISC_DYNAMIC_MINOR来动态获取次设备号  
    .minor = MISC_DYNAMIC_MINOR,  
//设备名称  
    .name = DEVICE_NAME,  
    .fops = &dev_fops,  
};  
static int __init dev_init(void)  
{  
int ret;  
    {  
        unsigned tmp;  
        tmp = readl(S3C64XX_GPKCON);  
//GPKCON配置GPK4到GPK7配置为0001输出  
        tmp = (tmp & ~(0xffffU<<16))|(0x1111U<<16);  
        writel(tmp, S3C64XX_GPKCON);  

//GPKDAT[7:4] = 1  
//灯灭  
        tmp = readl(S3C64XX_GPKDAT);  
        tmp |= (0xF << 4);  
        writel(tmp, S3C64XX_GPKDAT);  
//禁止上拉下拉  
        tmp = readl(S3C64XX_GPKPUD);  
        tmp &= (0x00 << 8);  
        writel(tmp, S3C64XX_GPKPUD);  
    }  
//该函数会自动创建设备节点,即设备文件  
    ret = misc_register(&misc);  
    printk (DEVICE_NAME"/tinitialized/n");  
return ret;  
}  
static void __exit dev_exit(void)  
{  
    misc_deregister(&misc);  
}  
module_init(dev_init);  
module_exit(dev_exit);






 

makefile






ifneq ($(KERNELRELEASE),)    
    obj-m := leds.o    
else   
KERNELDIR := /opt/FriendlyARM/mini6410/linux/linux-2.6.38  
PWD:=$(shell pwd)    
all:    
    make -C $(KERNELDIR) M=$(PWD) modules    
clean:    
    rm -rf *.ko *.o *.mod.c *.mod.o *.symvers    
endif






 

leds_test

 






#include <stdio.h>  
//exit  
#include <stdlib.h>  
#include <sys/ioctl.h>  
int main(int argc, char **argv)  
{  
int num = 0;  
int led_state = 1;  
int fd = 0;  
//参数赋值  
    if(argc != 3 || sscanf(argv[1],"%d", &num) != 1 || sscanf(argv[2], "%d", &led_state) != 1 || num > 4 || led_state > 1)  
    {  
        fprintf(stderr, "error/n");  
        exit(1);  
    }  
    printf("num = %d, led_state = %d/n", num, led_state);  
    fd = open("/dev/tiny6410_leds", 0);  
if (fd < 0) {  
        fd = open("/dev/tiny6410_leds0", 0);  
    }  
if (fd < 0) {  
        perror("open device leds");  
        exit(1);  
    }  
    ioctl(fd, num, led_state);  
    printf("led%d/n", num);  
    close(fd);  
return 0;  
}










分类:  Linux