2023/7/5 ioctl控制led 马达 蜂鸣器 风扇

#ifndef __HEAD_H__
#define __HEAD_H__

//定义寄存器组织结构体
typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;
//定义GPIOE和GPIOF
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define GPIOB 0X50003000
#define PHY_RCC_ADDR 0X50000A28
//LED
#define LED_ON _IOW('l', 1, int)    //开灯
#define LED_OFF _IOW('l', 0, int)   //关灯
//蜂鸣器
#define BUZ_ON _IOW('f', 1, int)
#define BUZ_OFF _IOW('f', 0, int)
//马达 PF6
#define MOTOR_ON _IOW('m', 1, int)
#define MOTOR_OFF _IOW('m', 0, int)
//风扇
#define FAN_ON _IOW('s', 1, int)
#define FAN_OFF _IOW('s', 0, int)


#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"

int main(int argc, char const *argv[])
{
    int a, b, c, fd; //a:开关 b:led1,2,3
    char buf[128] = {0};
    printf("请输入要启动的机器:1:LED 2:蜂鸣器3:风扇 4:马达>");
    scanf("%d", &c);
    switch (c)
    {
        case 1:
            fd = open("/dev/mychrdev0", O_RDWR);
            break;
        case 2:
            fd = open("/dev/mybuz0", O_RDWR);
            break;
        case 3:
            fd = open("/dev/myfan0", O_RDWR);
            break;
        case 4:
            fd = open("/dev/mymotor0", O_RDWR);
            break;
    }
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        switch (c)
        {
            //LED
            case 1:
                //从终端输入
                printf("请选择要实现的功能\n");
                printf("0:关灯 1:开灯>");
                scanf("%d", &a);
                printf("请输入要控制的灯\n");
                printf("1:(LED1) 2:(LED2) 3:(LED3)>");
                scanf("%d", &b);
                if(a==1)    //开灯
                {
                    ioctl(fd, LED_ON, &b);
                }else if(a==0)  //关灯
                {
                    ioctl(fd, LED_OFF, &b);
                }
                break;
            //蜂鸣器
            case 2:
                printf("请输入 0:关 1:开>");
                scanf("%d", &b);
                if(b==1)
                {
                    ioctl(fd, BUZ_ON);
                }else if(b==0)
                {
                    ioctl(fd, BUZ_OFF);
                }
                break;
            //风扇
            case 3:
                printf("请输入 0:关 1:开>");
                scanf("%d", &b);
                if(b==1)
                {
                    ioctl(fd, FAN_ON);
                }else if(b==0)
                {
                    ioctl(fd, FAN_OFF);
                }
                break;
            case 4:
                printf("请输入 0:关 1:开>");
                scanf("%d", &b);
                if(b==1)
                {
                    ioctl(fd, MOTOR_ON);
                }else if(b==0)
                {
                    ioctl(fd, MOTOR_OFF);
                }
                break;
        }
    }
    
    close(fd);
    return 0;
}

LED 

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"

struct class *cls;
struct device *dev;


int major; //用于保留主设备号
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int which;
    //通过拷贝函数获取ioctl第三个参数对应空间的数值
    int ret = copy_from_user(&which, (void *)arg, 4);
    if(ret)
    {
        printk("拷贝用户空间数据失败\n");
        return -EIO;
    }
    switch (cmd)
    {              
        case LED_ON:
            switch (which)
            {
            case 1:
                vir_led1->ODR |= (0x1 << 10);
                break;
            case 2:
                vir_led2->ODR |= (0x1 << 10);
                break;
            case 3:
                vir_led3->ODR |= (0x1 << 8);
                break;
            }
        break;
        case LED_OFF:
            switch (which)
            {
                case 1:
                    vir_led1->ODR &= (~(0X1 << 10));
                    break;
                case 2:
                    vir_led2->ODR &= (~(0X1 << 10));
                    break;
                case 3:
                    vir_led3->ODR &= (~(0X1 << 8));
                    break;
            }
            break;
    }
    return 0;
}

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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

int all_led_init(void)
{
    //寄存器地址映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if(vir_led1 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if(vir_led2 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3 = vir_led1;
    if(vir_led3 == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器初始化
    //led1
    (*vir_rcc) |= (0x3 << 4); //rcc使能

    (vir_led1->MODER) &= (~(0x3 << 20)); //设置使能
    (vir_led1->MODER) |= (0x1 << 20);
    (vir_led1->ODR) &= (~(0x1 << 10)); //灭灯
    //led2
    (vir_led2->MODER) &= (~(0x3 << 20)); //设置使能
    (vir_led2->MODER) |= (0x1 << 20);
    (vir_led2->ODR) &= (~(0x1 << 10)); //灭灯
    //led3
    (vir_led3->MODER) &= (~(0x3 << 16)); //设置使能
    (vir_led3->MODER) |= (0x1 << 16);
    (vir_led3->ODR) &= (~(0x1 << 8)); //灭灯
    printk("硬件寄存器初始化成功\n");
    return 0;
}

static int __init mycdev_init(void)
{
    int i;
    major = register_chrdev(0, "mychrdev", &fops);
    if( major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);

    //向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    for(i=0; i<3; i++)
    {
        dev=device_create(cls, NULL, MKDEV(major, i), NULL, "mychrdev%d", i);
        if(IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点信息成功\n");
    //映射物理寄存器
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void)
{
    int i;
    //取消寄存器 地址映射
    iounmap(vir_led1);   
    iounmap(vir_led2);
    iounmap(vir_rcc);
    //销毁设备节点信息
    for(i=0; i<3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    //销毁目录信息
    class_destroy(cls);
    //字符设备驱动的注销
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

风扇

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"

struct class *cls;
struct device *dev;


int major; //用于保留主设备号
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_fan;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

    switch (cmd)
    {              
        case FAN_ON:
            vir_fan->ODR |= (0x1 << 9);
            break;
        case FAN_OFF:
            vir_fan->ODR &= (~(0x1 << 9));
            break;
    }
    return 0;
}

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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

int all_led_init(void)
{
    //寄存器地址映射
    vir_fan = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if(vir_fan == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器初始化
    //led1
    (*vir_rcc) |= (0x9 << 1); //rcc使能

    (vir_fan->MODER) &= (~(0x3 << 18)); //设置使能
    (vir_fan->MODER) |= (0x1 << 18);
    (vir_fan->ODR) &= (~(0x1 << 9)); //
    printk("硬件寄存器初始化成功\n");
    return 0;
}

static int __init mycdev_init(void)
{
    int i=0;
    major = register_chrdev(0, "myfan", &fops);
    if( major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);

    //向上提交目录
    cls = class_create(THIS_MODULE, "myfan");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }

    dev=device_create(cls, NULL, MKDEV(major, i), NULL, "myfan%d", i);
    if(IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点信息成功\n");
    //映射物理寄存器
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消寄存器 地址映射
    iounmap(vir_fan);
    iounmap(vir_rcc);
    //销毁设备节点信息

    device_destroy(cls, MKDEV(major, 0));
    //销毁目录信息
    class_destroy(cls);
    //字符设备驱动的注销
    unregister_chrdev(major, "myfan");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

马达

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"

struct class *cls;
struct device *dev;


int major; //用于保留主设备号
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_motor;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

    switch (cmd)
    {              
        case MOTOR_ON:
            vir_motor->ODR |= (0x1 << 6);
            break;
        case MOTOR_OFF:
            vir_motor->ODR &= (~(0x1 << 6));
            break;
    }
    return 0;
}

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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

int all_led_init(void)
{
    //寄存器地址映射
    vir_motor = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if(vir_motor == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器初始化
    //led1
    (*vir_rcc) |= (0x9 << 1); //rcc使能

    (vir_motor->MODER) &= (~(0x3 << 12)); //设置使能
    (vir_motor->MODER) |= (0x1 << 12);
    (vir_motor->ODR) &= (~(0x1 << 6)); //
    printk("硬件寄存器初始化成功\n");
    return 0;
}

static int __init mycdev_init(void)
{
    int i=0;
    major = register_chrdev(0, "mymotor", &fops);
    if( major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);

    //向上提交目录
    cls = class_create(THIS_MODULE, "mymotor");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }

    dev=device_create(cls, NULL, MKDEV(major, i), NULL, "mymotor%d", i);
    if(IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点信息成功\n");
    //映射物理寄存器
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消寄存器 地址映射
    iounmap(vir_motor);
    iounmap(vir_rcc);
    //销毁设备节点信息

    device_destroy(cls, MKDEV(major, 0));
    //销毁目录信息
    class_destroy(cls);
    //字符设备驱动的注销
    unregister_chrdev(major, "mymotor");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

蜂鸣器

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"

struct class *cls;
struct device *dev;


int major; //用于保留主设备号
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_buz;
unsigned int *vir_rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

    switch (cmd)
    {              
        case BUZ_ON:
            vir_buz->ODR |= (0x1 << 6);
            break;
        case BUZ_OFF:
            vir_buz->ODR &= (~(0x1 << 6));
            break;
    }
    return 0;
}

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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

int all_led_init(void)
{
    //寄存器地址映射
    vir_buz = ioremap(GPIOB, sizeof(gpio_t));
    if(vir_buz == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }

    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL)
    {
        printk("ioremap failed:%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器初始化
    //led1
    (*vir_rcc) |= (0x9 << 1); //rcc使能

    (vir_buz->MODER) &= (~(0x3 << 12)); //设置使能
    (vir_buz->MODER) |= (0x1 << 12);
    (vir_buz->ODR) &= (~(0x1 << 6)); //
    printk("硬件寄存器初始化成功\n");
    return 0;
}

static int __init mycdev_init(void)
{
    int i=0;
    major = register_chrdev(0, "mybuz", &fops);
    if( major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);

    //向上提交目录
    cls = class_create(THIS_MODULE, "mybuz");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }

    dev=device_create(cls, NULL, MKDEV(major, i), NULL, "mybuz%d", i);
    if(IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点信息成功\n");
    //映射物理寄存器
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消寄存器 地址映射
    iounmap(vir_buz);
    iounmap(vir_rcc);
    //销毁设备节点信息

    device_destroy(cls, MKDEV(major, 0));
    //销毁目录信息
    class_destroy(cls);
    //字符设备驱动的注销
    unregister_chrdev(major, "mybuz");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值