通过ioctl函数实现灯\蜂鸣器和风扇的工作选择

 test.c: 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "/home/ubuntu/hqyj/22061/qd/day3/led/led.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fmq/fmq.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fs/fs.h"

int main(int argc, char const *argv[])
{
    char buf[128] = {};

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

    int fd2 = open("/dev/myfmq", O_RDWR);
    if (fd2 < 0)
    {
        printf("蜂鸣器设备文件打开失败\n");
        exit(-1);
    }
    printf("蜂鸣器设备文件打开成功\n");

    int fd3 = open("/dev/myfs", O_RDWR);
    if (fd3 < 0)
    {
        printf("风扇设备文件打开失败\n");
        exit(-1);
    }
    printf("风扇设备文件打开成功\n");

    int which;
    //终端输入
    while (1)
    {
        printf("请输入你要控制的设备:   1:LED   2:蜂鸣器    3:风扇\n");
        scanf("%d", &which);
        getchar();
        if (which == 1)
        {
            // LED灯
            printf("请输入控制命令:   1:开LED1灯 0:关灯\n");
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf) - 1] = '\0';
            printf("请输入要操作的灯:   1-LED1  2-LED2  3-LED3\n");
            scanf("%d", &which);
            getchar();
            if (buf[0] == '1')
                ioctl(fd1, LedON, &which);
            else
                ioctl(fd1, LedOFF, &which);
        }
        else if (which == 2)
        {
            //蜂鸣器
            printf("请输入控制命令:   1:开蜂鸣器 0:关蜂鸣器\n");
            bzero(buf,sizeof(buf));
            bzero(buf,sizeof(buf));
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf) - 1] = '\0';
            if (buf[0] == '1')
                ioctl(fd2,FMQ_ON);
            else
                ioctl(fd2,FMQ_OFF);
        }
        else if (which == 3)
        {
            //风扇
            printf("请输入控制命令:   1:开风扇 0:关风扇\n");
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf) - 1] = '\0';
            if (buf[0] == '1')
                ioctl(fd3,FS_ON);
            else
                ioctl(fd3,FS_OFF);
        }
    }

    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

fs.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "/home/ubuntu/hqyj/22061/qd/day3/led/led.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fmq/fmq.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fs/fs.h"
#define CNAME "myfs"

int major;           //定义变量接受主设备号
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据

gpio_t *vir_fs;

unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }

    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    return size;
}
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;
    }
    return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd)
    {
        case FS_ON:
            vir_fs->ODR |= (1 << 9);
            break;
        case FS_OFF:
            vir_fs->ODR &= ~(1 << 9);
            break;
        default:
            printk("功能码错误\n");
            break;
    }
    return 0;
}

int fs_init(void)
{
    //进行物理地址的映射
    vir_fs = ioremap(PHY_FS_ADDR, sizeof(gpio_t));
    if (vir_fs == NULL)
    {
        printk("vir_fs 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_fs 映射成功\n");
    
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if (vir_rcc == NULL)
    {
        printk("vir_rcc 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_rcc 映射成功\n");

    //寄存器的初始化
    // led1
    vir_fs->MODER &= ~(3 << 18);
    vir_fs->MODER |= (1 << 18);
    vir_fs->ODR &= ~(1 << 9);


    (*vir_rcc) |= (1 << 4);

    return 0;
}

struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .unlocked_ioctl = ioctl,
        .release = mycdev_close,
};

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, "FS");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");

    //向上创建设备节点
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myfs");
    if (IS_ERR(dev))
    {
        printk("先上创建设备节点失败\n");
        return PTR_ERR(dev);
    }
    printk("先上创建设备节点成功\n");

    fs_init();

    return 0;
}

static void __exit demo_exit(void)
{

    //销毁节点
    device_destroy(cls, MKDEV(major, 0));
    //销毁目录
    class_destroy(cls);

    //取消物理内存映射
    iounmap(vir_fs);
    iounmap(vir_rcc);
    //注销字符设备驱动
    unregister_chrdev(major, CNAME);
    printk(KERN_ERR "kernel unload\n");
}

module_init(demo_init);
module_exit(demo_exit);

MODULE_LICENSE("GPL");

fs.h:

#ifndef __MYFS_H__
#define __MYFS_H__

#define PHY_FS_ADDR 0X50006000

#define PHY_RCC_ADDR 0X50000A28

#define FS_ON _IO('a', 1)
#define FS_OFF _IO('a', 0)

#endif

 

fmq.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "/home/ubuntu/hqyj/22061/qd/day3/led/led.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fmq/fmq.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fs/fs.h"
#define CNAME "myfmq"

int major;           //定义变量接受主设备号
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据

gpio_t *vir_fmq;
unsigned int *vir_rcc;

struct class *cls; //句柄
struct device *dev;

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }

    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    return size;
}
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;
    }
    return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

    switch (cmd)
    {
    case FMQ_ON:
        vir_fmq->ODR |= (1 << 6);
        break;
    case FMQ_OFF:
        vir_fmq->ODR &= ~(1 << 6);
        break;
    default:
        printk("功能码错误\n");
        break;
    }
    return 0;
}

int fmq_init(void)
{
    //进行物理地址的映射
    vir_fmq = ioremap(PHY_FMQ_ADDR, sizeof(gpio_t));
    if (vir_fmq == NULL)
    {
        printk("vir_fmq 映射失败\n");
        return -ENOMEM;
    }
    printk("vir_fmq 映射成功\n");

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

    //寄存器的初始化
    vir_fmq->MODER &= ~(3 << 12);
    vir_fmq->MODER |= (1 << 12);
    vir_fmq->ODR &= ~(1 << 6);

    (*vir_rcc) |= (1 << 1);

    return 0;
}

struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .unlocked_ioctl = ioctl,
        .release = mycdev_close,
};

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, "FMQ");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");

    //向上创建设备节点
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myfmq");
    if (IS_ERR(dev))
    {
        printk("先上创建设备节点失败\n");
        return PTR_ERR(dev);
    }
    printk("先上创建设备节点成功\n");

    fmq_init();

    return 0;
}

static void __exit demo_exit(void)
{

    //销毁节点
    device_destroy(cls, MKDEV(major, 0));
    //销毁目录
    class_destroy(cls);

    //取消物理内存映射
    iounmap(vir_fmq);
    iounmap(vir_rcc);

    //注销字符设备驱动
    unregister_chrdev(major, CNAME);
    printk(KERN_ERR "kernel unload\n");
}

module_init(demo_init);
module_exit(demo_exit);

MODULE_LICENSE("GPL");

fmq.h:

#ifndef __MYFMQ_H__
#define __MYFMQ_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;
    volatile unsigned int LCKR;
    volatile unsigned int AFRL;
} gpio_t;
#define PHY_FMQ_ADDR 0X50003000

#define PHY_RCC_ADDR 0X50000A28

#define FMQ_ON _IO('a', 1)
#define FMQ_OFF _IO('a', 0)

#endif

 

led.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include "/home/ubuntu/hqyj/22061/qd/day3/led/led.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fmq/fmq.h"
#include "/home/ubuntu/hqyj/22061/qd/day3/fs/fs.h"
#include 

#define CNAME "myled"

int major;           //定义变量接受主设备号
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据

gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls; //句柄
struct device *dev;

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }

    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    return size;
}
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;
    }
    return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret, which;
    ret = copy_from_user(&which, (void *)arg, sizeof(int));
    if (ret)
    {
        printk("用户向内核拷贝数据失败\n");
        return -EIO;
    }
    switch (cmd)
    {
        case LedON:
            if (which == LED1)
                vir_led1->ODR |= (1 << 10);
            else if (which == LED2)
                vir_led2->ODR |= (1 << 10);
            else if (which == LED3)
                vir_led3->ODR |= (1 << 8);
            break;
        case LedOFF:
            if (which == LED1)
                vir_led1->ODR &= ~(1 << 10);
            else if (which == LED2)
                vir_led2->ODR &= ~(1 << 10);
            else if (which == LED3)
                vir_led3->ODR &= ~(1 << 8);
            break;
        default:
            printk("功能码错误\n");
            break;
    }
    return 0;
}

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

struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .unlocked_ioctl = ioctl,
        .release = mycdev_close,
};

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("先上创建设备节点失败\n");
        return PTR_ERR(dev);
    }
    printk("先上创建设备节点成功\n");

    all_led_init();

    return 0;
}

static void __exit demo_exit(void)
{

    //销毁节点
    device_destroy(cls, MKDEV(major, 0));
    //销毁目录
    class_destroy(cls);

    //取消物理内存映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_led3);
    iounmap(vir_rcc);
    //注销字符设备驱动
    unregister_chrdev(major, CNAME);
    printk(KERN_ERR "kernel unload\n");
}

module_init(demo_init);
module_exit(demo_exit);

MODULE_LICENSE("GPL");

led.h:

#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_RCC_ADDR 0X50000A28
enum LED
{
    LED1=1,
    LED2,
    LED3
};

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

 

 

 

 

你可能感兴趣的:(arm,单片机,嵌入式硬件)