3---linux字符设备之点亮led

概要:上一篇我们编写了一个非常简单的字符设备框架和一个我们自己写的fops,似乎还是感觉有点索然无味,因此这篇我们搞一个能在现实世界上出现的东西,就是led亮起来

上一篇我们写了一个字符设备的框架,因此我们可以拿过来用:2—linux字符设备进阶篇

1.查看2440原理图,随便找一个led,并找出它的gpio口接在哪里

LED

2.打开s3c2440的芯片手册找出这几个gpio口的配置寄存器

在这里插入图片描述

我们根据这几个寄存器,来对io口进行操作

3.在入口函数进行地址映射
static unsigned long gpfcon; //定义全局变量
static unsigned long gpfdat; //定义全局变量

gpfcon= ioremap(0x56000000, 0x100000);
gpfdat = gpfcon + 1;

在linux中,我们不能直接访问物理地址,而是间接把物理地址映射出来再操作
ioremap(cookie,size)
cookie :映射的物理地址
size :内存大小

老规矩,有映射就有取消映射:

iounmap(gpfcon);

在出口函数,取消映射

4.接下来是fops编写:
static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,   
    .open   =   led_open,
    .write  =    led_write,
};

在open函数里面我们对io口进行配置,在write进行对led的开关led操作

.open()函数

static int led_open(struct inode *inode, struct file *file)
{
		
        *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
        *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
        return 0;
}

初始化gpf 4 5 6,配置为输出模式。

.write()函数

扫描二维码关注公众号,回复: 4804107 查看本文章
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
        int val;

        copy_from_user(&val, buf, count); 

        if (val == 1)
        {
                *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
        }
        else
        {
                *gpfdat |= (1<<4) | (1<<5) | (1<<6);
        }

        return 0;
}

当用户空间write的数据val为1时,灯灭。val为0时,灯亮
我们不能直接使用buf,需要用copy_from_user()函数,从用户空间拷贝
copy_from_user(to,from,count);
to : 目的
from:源
count :大小

有从用户空间拷贝,自然就有拷贝到用户空间
copy_to_user(to, from, count)
to:目的
from:源
count:大小

5.到这一步我们驱动程序就写好了

老规矩,makefile,make,拷贝到开发板,insmod led.ko

6.编写测试程序
#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;
}


编译,拷贝到开发板

./led_test on

灯全亮

谈一谈注册字符设备:
 major = register_chrdev(0, "led", &led_fops);
//int register_chrdev(unsigned int major, const char *name,const struct file_operations *fops)

这个函数没有指定次设备号,所以主设备号下的所有次设备号(0~255)都是对应这个led_fops,非常的霸道,但是我喜欢
major:主设备号,如果写0,则让内核自动分配主设备号
name:名字
fops:自己编写的fops

******ledc.c*******

#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 *led_class;
static struct class_device      *led_class_dev;

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


static int led_open(struct inode *inode, struct file *file)
{

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

static struct file_operations led_fops = {
    .owner  =   THIS_MODULE, 
    .open   =   led_open,
    .write  =   led_write,
};
int major;
           
static int led_init(void)
{
        major = register_chrdev(0, "led_drv", &led_fops); 

        firstdrv_class = class_create(THIS_MODULE, "leddrv");

        firstdrv_class_dev = class_device_create(led_class,NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

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

        return 0;
}

static void first_drv_exit(void)
{
        unregister_chrdev(major, "led_drv"); 
        class_device_unregister(led_class_dev);
        class_destroy(led_class);
        iounmap(gpfcon);
}

module_init(led_init);
module_exit(led_exit);


MODULE_LICENSE("GPL");

猜你喜欢

转载自blog.csdn.net/cnqiuge123/article/details/85861475