1012 ioctl函数的使用

头文件:

#ifndef __MYLED_H__
#define __MYLED_H__

typedef struct
{
    volatile unsigned int MODER;
    volatile unsigned int OTYPER;
    volatile unsigned int OSPEEDR;
    volatile unsigned int PUPDR;
    volatile unsigned int IDR;
    volatile unsigned int ODR;
    volatile unsigned int BSRR;
} gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_BUZZER_ADDR 0x50003000
#define PHY_FAN_ADDR 0x50006000

#define PHY_RCC_ADDR 0X50000A28
enum LED
{
    LED1 = 1,
    LED2,
    LED3,
    BUZZER,
    FAN,
};

#define LedON _IOW('a', 1, int)
#define LedOFF _IOW('a', 0, int)
#endif

内核文件:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include "mydev.h"
#define CNAME "mycdev"

int major;           //定义变量接收主设备号
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_buzzer;
gpio_t *vir_fan;
unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
    // printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    // size参数是用户期待读到的字节长度
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }

    return size;
}
// write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    if (kbuf[0] == '1') //开灯
        vir_led1->ODR |= (1 << 10);
    else if (kbuf[0] == '2')
        vir_led2->ODR |= (1 << 10);
    else if (kbuf[0] == '3')
        vir_led3->ODR |= (1 << 8);
    else //关灯
    {
        vir_led1->ODR &= ~(1 << 10);
        vir_led2->ODR &= ~(1 << 10);
        vir_led3->ODR &= ~(1 << 8);
    }
    return size;
}

long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret, which;
    switch (cmd)
    {
    case LedON:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("用户向内核拷贝失败\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            vir_led1->ODR |= (1 << 10);
            break;
        case LED2:
            vir_led2->ODR |= (1 << 10);
            break;
        case LED3:
            vir_led3->ODR |= (1 << 8);
            break;
        case BUZZER:
            vir_buzzer->ODR |= (1 << 6);
            break;
        case FAN:
            vir_fan->ODR |= (1 << 9);
            break;
        default:
            printk("输入的设备错误\n");
        }
        break;
    case LedOFF:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("用户向内核拷贝失败\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            vir_led1->ODR &= ~(1 << 10);
            break;
        case LED2:
            vir_led2->ODR &= ~(1 << 10);
            break;
        case LED3:
            vir_led3->ODR &= ~(1 << 8);
            break;
        case BUZZER:
            vir_buzzer->ODR &= ~(1 << 6);
            break;
        case FAN:
            vir_fan->ODR &= ~(1 << 9);
            break;
        default:
            printk("输入的设备错误\n");
        }
        break;
    default:
        printk("输入错误\n");
        break;
    }
    return 0;
}
// close()
int mycdev_close(struct inode *inode, struct file *file)
{
    // printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
//操作方法结构体的初始化
struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .release = mycdev_close,
        .unlocked_ioctl = ioctl,
};
int all_led_init(void)
{
    //进行物理地址的映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if (vir_led1 == NULL)
    {
        printk("vir_led1 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led1 映射成功\n");
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if (vir_led2 == NULL)
    {
        printk("vir_led2 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led2 映射成功\n");
    vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
    if (vir_led3 == NULL)
    {
        printk("vir_led3 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led3 映射成功\n");
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");
    //寄存器的初始化
    // led1
    vir_led1->MODER &= ~(3 << 20);
    vir_led1->MODER |= (1 << 20);
    vir_led1->ODR &= ~(1 << 10);

    // led2
    vir_led2->MODER &= ~(3 << 20);
    vir_led2->MODER |= (1 << 20);
    vir_led2->ODR &= ~(1 << 10);

    // led3
    vir_led3->MODER &= ~(3 << 16);
    vir_led3->MODER |= (1 << 16);
    vir_led3->ODR &= ~(1 << 8);

    (*vir_rcc) |= (3 << 4);
    return 0;
}

int buzzer_init(void)
{
    //物理地址映射
    vir_buzzer = ioremap(PHY_BUZZER_ADDR, sizeof(gpio_t));
    if (vir_buzzer == NULL)
    {
        printk("vir_buzzer 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_led1 映射成功\n");

    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");

    vir_buzzer->MODER &= ~(3 << 12);
    vir_buzzer->MODER |= (1 << 12);
    vir_buzzer->ODR &= ~(1 << 6);
    (*vir_rcc) |= (1 << 1);

    return 0;
}

int fan_init(void)
{
    //物理地址映射
    vir_fan = ioremap(PHY_FAN_ADDR, sizeof(gpio_t));
    if (vir_fan == NULL)
    {
        printk("vir_fan 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_fan 映射成功\n");

    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");

    vir_fan->MODER &= ~(3 << 18);
    vir_fan->MODER |= (1 << 18);
    vir_fan->ODR &= ~(1 << 9);
    (*vir_rcc) |= (1 << 4);

    return 0;
}
//入口函数,当驱动安装的时候执行
static int __init demo_init(void)
{
    //动态注册字符设备驱动
    major = register_chrdev(0, CNAME, &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    //向上提交节点目录
    cls = class_create(THIS_MODULE, "LED");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //创建设备节点
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled");
    if (IS_ERR(dev))
    {
        printk("创建节点LED失败\n");
        return PTR_ERR(dev);
    }
    printk("创建节点LED成功\n");

    dev = device_create(cls, NULL, MKDEV(major, 1), NULL, "mybuzzer");
    if (IS_ERR(dev))
    {
        printk("创建节点蜂鸣器失败\n");
    }
    printk("创建节点蜂鸣器成功\n");

    dev = device_create(cls, NULL, MKDEV(major, 2), NULL, "myfan");
    if (IS_ERR(dev))
    {
        printk("创建节点风扇失败\n");
    }
    printk("创建节点风扇成功\n");
    //寄存器的初始化
    all_led_init();
    buzzer_init();
    fan_init();

    return 0;
}

//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
    //销毁节点
    device_destroy(cls, MKDEV(major, 0));
    device_destroy(cls, MKDEV(major, 1));
    device_destroy(cls, MKDEV(major, 2));
    //销毁目录
    class_destroy(cls);
    //取消映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(vir_buzzer);
    iounmap(vir_fan);
    iounmap(vir_rcc);

    //注销字符设备驱动
    unregister_chrdev(major, CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

应用文件:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "mydev.h"
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    int witch;
    int fd_led = open("/dev/myled", O_RDWR);
    if (fd_led < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    int fd_buz = open("/dev/mybuzzer", O_RDWR);
    if (fd_buz < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    int fd_fan = open("/dev/myfan", O_RDWR);
    if (fd_fan < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("打开设备文件成功\n");
    //从终端读取
    while (1)
    {
        // printf("请输入命令 1:开启LED1\t2:开启LED2\t3:开启LED3\t0:关灯\n");
        printf("请输入命令1:开启,0关闭\n");
        fgets(buf, sizeof(buf), stdin);

        printf("请输入要操作的硬件:1->LED1 2->LED2 3->LED3 4->蜂鸣器 5->风扇\n");
        scanf("%d", &witch);
        getchar();
        //吃掉换行符
        buf[strlen(buf) - 1] = '\0';
        if (buf[0] == '1')
            switch (witch)
            {
            case 1:
            case 2:
            case 3:
                ioctl(fd_led, LedON, &witch);
                break;
            case 4:
                ioctl(fd_buz, LedON, &witch);
                break;
            case 5:
                ioctl(fd_fan, LedON, &witch);
                break;
            }
        else if (buf[0] == '0')
            switch (witch)
            {
            case 1:
            case 2:
            case 3:
                ioctl(fd_led, LedOFF, &witch);
                break;
            case 4:
                ioctl(fd_buz, LedOFF, &witch);
                break;
            case 5:
                ioctl(fd_fan, LedOFF, &witch);
                break;
            }
    }
    close(fd_led);
    close(fd_buz);
    close(fd_fan);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值