08_第一个相对完整的驱动实践编写

第一个相对完整的驱动实践编写

需求:

​ 1.使用杂项设备完成一个蜂鸣器的驱动。

​ 2.完成一个上层测试应用。

​ 应用要求:在上层应用中传入参数1为打开蜂鸣器,传入参数0为 关闭蜂鸣器。

buzzer.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>

#define GPIO5_DR 0x020AC000 //蜂鸣器物理地址

unsigned int *vir_gpio5_dr; //蜂鸣器虚拟地址

int misc_open(struct inode *inode, struct file *file)
{
    printk("hello misc_open\n");
    return 0;
}

int misc_release(struct inode *inode, struct file *file)
{
    printk("hello mise_release bye bye\n");
    return 0;
}

ssize_t misc_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff_t)
{
    char kbuf[64] = "heheh";
    if (copy_to_user(ubuf, kbuf, strlen(kbuf) + 1) != 0)
    {
        printk("copy_to_user error\n");
        return -1;
    }
    printk("hello misc_read bye bye\n");
    return 0;
}

ssize_t misc_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff_t)
{
    char kbuf[64] = {0};
    if (copy_from_user(kbuf, ubuf, size) != 0)
    {
        printk("copy_from_user error\n");
        return -1;
    }
    printk("hello misc_write bye bye\n");
    if(kbuf[0] == 1)
    {
        *vir_gpio5_dr |= 0x02; 
    }else if(kbuf[0] == 0)//关闭蜂鸣器
    {
        *vir_gpio5_dr &= ~0x02; 
    }
    return 0;
}

/* 文件操作集 */
struct file_operations misc_fops = {
    .owner = THIS_MODULE, // 当前模块
    .open = misc_open,
    .release = misc_release,
    .write = misc_write,
    .read = misc_read,
};

/* 杂项设备结构体 */
struct miscdevice misc_dev = {
    .minor = MISC_DYNAMIC_MINOR, // 动态分配次设备号
    .name = "hello_mise",        // 设备节点的名字
    .fops = &misc_fops           // 文件操作集

};

static int mise_init(void)
{
    int ret;
    ret = misc_register(&misc_dev); // 注册杂项设备
    if (ret < 0)
    {
        printk("misc register is error!\n");
        return -1;
    }
    printk("mise register is ok!\n");
    
    vir_gpio5_dr = ioremap(GPIO5_DR, 4); //将物理地址转化为虚拟地址
    
    if(vir_gpio5_dr == NULL)
    {
        printk("GPIO5_DR is error!\n");
        return -EBUSY;
    }
    printk("GPIO5_DR is ok!\n");
    return 0;
}

static void mise_exit(void)
{
    misc_deregister(&misc_dev); // 注销杂项设备
    printk("misc gooodbye!\n");
    iounmap(vir_gpio5_dr); //卸载杂项设备
}

module_init(mise_init);
module_exit(mise_exit);

MODULE_LICENSE("GPL");

app.c

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

int main(int argc, const char *argv[])
{
    int fd;
    char buf[64] = {0};
    fd = open("/dev/hello_mise", O_RDWR);
    if (fd < 0)
    {
        perror("open error\n");
        return fd;
    }
    buf[0] = atoi(argv[1]);
    write(fd, buf, strlen(buf)+1);
    close(fd);
    return 0;
}

Makefile

obj-m+=buzzer.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga 
PWD?=$(shell pwd)

all:
	make -C $(KDIR) M=$(PWD) modules
posted @ 2024-04-25 22:34  爱吃冰激凌的黄某某  阅读(9)  评论(0编辑  收藏  举报