[Linux]第一个驱动程序-LED驱动

第一个驱动程序-LED驱动

艾恩凝 2021/3/22

序

记录学习过程,最后一个程序调了好久,到头来是一个被自己忽略的小问题,程序编译没问题,装载驱动没问题,验证驱动程序没问题,但就是点亮不了led,这种不是语法错误的问题真是让人头疼,开始从驱动程序初始化开始,一步一步地找错误,看看主设备号是否装载成功,负设备号是否正确,led寄存器是否初始化成功,搞了好久,到头来是因为一个变量数据为char型,自己定义的为int,本身是char型数字,但在程序过程中还是出现了问题

简单驱动程序

led驱动程序

#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 *second_drv_class;
static struct class_device *second_drv_class_dev;

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

static int second_drv_open(struct inode *inode,struct file *file){
	//printk("second_drv_open\n");
    *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 second_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){
	//printk("second_drv_write\n");
    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;
}

static struct file_operations second_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   second_drv_open,   
	.write	=	second_drv_write,	   
};
int major;
int second_drv_init(void){

	major = register_chrdev(0, "second_drv", &second_drv_fops);//register tell kernel
   
    second_drv_class = class_create(THIS_MODULE, "seconddrv");
	second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

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

    return 0;

}
void second_drv_exit(void){
	unregister_chrdev(111, "second_drv");//unregister tell kernel

    class_device_unregister(second_drv_class_dev);
	class_destroy(second_drv_class);

    iounmap(gpfcon);
}

module_init(second_drv_init);
module_exit(second_drv_exit);

MODULE_LICENSE("GPL");

测试程序

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

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;
}

复杂驱动程序

驱动程序

#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>

#define DEVICE_NAME   "leds"
#define LED_MAJOR_NUMBER  200

static struct class *leds_drv_class;
static struct class_device *leds_drv_class_devs[4];

volatile unsigned long *GPFCON = NULL;
volatile unsigned long *GPFDAT = NULL;


void led_config(int led_nums){
    *GPFCON &= ~(0x3<<(led_nums*2));
    *GPFCON |= (0x1<<(led_nums*2));
    *GPFDAT &= ~(1<<led_nums);
}
static int leds_drv_open(struct inode *inode,struct file *file){
    int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
    //printk("open_minor = %d \n",minor);
    switch(minor){
        case 0:{
            led_config(4);
            led_config(5);
            led_config(6);
            break;
        }
        case 1:{
            led_config(4);
            break;
        }
        case 2:{
            led_config(5);
            break;
        }
        case 3:{
            led_config(6);
            break;
        }
    }

	return 0;
}
void led_on_off(char val,int led_nums){
    if(val == 0){ 
        *GPFDAT &= ~(1<<led_nums);     
    }
    if(val == 1){
        *GPFDAT |= (1<<led_nums) ;  
    }
}
static ssize_t leds_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){
    char val;
    int minor = MINOR(file->f_dentry->d_inode->i_rdev);
    //printk("write_minor = %d \n",minor);
    copy_from_user(&val, buf, count); //	copy_to_user();
    switch (minor){
        case 0:{
            led_on_off(val,4);
            led_on_off(val,5);
            led_on_off(val,6);
            break;
        }
        case 1:{
            led_on_off(val,4);
            break;
        }
        case 2:{
            led_on_off(val,5);
            break;
        }
        case 3:{
            led_on_off(val,6);
            break;
        }
    }

	return 0;
}

static struct file_operations leds_drv_fops = {
    .owner  =   THIS_MODULE,  
    .open   =   leds_drv_open,   
	.write	=	leds_drv_write,	   
};

static int  leds_drv_init(void){
    int major,minor=0;
    GPFCON = (volatile unsigned long *)ioremap(0x56000050, 16);
	GPFDAT = (volatile unsigned long *)ioremap(0x56000054, 16);

	major = register_chrdev(LED_MAJOR_NUMBER,DEVICE_NAME, &leds_drv_fops);//register tell kernel
   
    leds_drv_class = class_create(THIS_MODULE, DEVICE_NAME);
	leds_drv_class_devs[0] = class_device_create(leds_drv_class, NULL, MKDEV(LED_MAJOR_NUMBER, 0), NULL, DEVICE_NAME); 
    for(minor=1;minor<4;minor++){
        leds_drv_class_devs[minor] = class_device_create(leds_drv_class, NULL, MKDEV(LED_MAJOR_NUMBER, minor), NULL, "led%d", minor); 
    }

    printk(DEVICE_NAME " had initialized\n");
    return 0;

}
static void  leds_drv_exit(void){
    int minor=0;
	unregister_chrdev(LED_MAJOR_NUMBER, DEVICE_NAME);//unregister tell kernel
    for (minor = 0; minor < 4; minor++){
        class_device_unregister(leds_drv_class_devs[minor]);
    }   
	class_destroy(leds_drv_class);
    iounmap(GPFCON);
}

module_init(leds_drv_init);
module_exit(leds_drv_exit);
MODULE_AUTHOR("https://aeneag.xyz");
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("S3C2440 LED Driver");
MODULE_LICENSE("GPL");

测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
void print_menu(char *file)
{
    printf("Usage:\n");
    printf("%s <dev> <on|off>\n",file);
    printf("eg. \n");
    printf("%s /dev/leds on\n", file);
    printf("%s /dev/leds off\n", file);
    printf("%s /dev/led1 on\n", file);
    printf("%s /dev/led1 off\n", file);
}
int main(int argc,char **argv){
	int fd;
	char val;

    if (argc != 3)
	{
		print_menu(argv[0]);
		return 0;
	}
	fd = open(argv[1], O_RDWR);
	if (fd < 0)
    {
        printf("error, can't open %s\n", argv[1]);
        return 0;
    }
	if (!strcmp(argv[2], "on"))
	{
		val  = 0;
		write(fd, &val, 1);
	}
	else if(!strcmp(argv[2], "off"))
	{
		val = 1;
		write(fd,&val,1);
	}else{
		print_menu(argv[0]);
        return 0;
	}
	return 0;
}

总结

led驱动程序算是处女作了,多搞搞嵌入式,多注意细节,一个细节上的错误,搞了俩小时

    评论
    0 评论
avatar

取消