驱动开发——day3

test.c

#include "head.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const* argv[])
{
    int fd, beepfd, fanfd, motorfd;
    int a;
    int n;
    int f;
    char buf[128] = { 0 };
    // 打开LED
    fd = open("/dev/mychrdev0", O_RDWR);
    if (fd < 0) {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    beepfd = open("/dev/mychrdev3", O_RDWR);
    if (beepfd < 0) {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    fanfd = open("/dev/mychrdev4", O_RDWR);
    if (fanfd < 0) {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    motorfd = open("/dev/mychrdev4", O_RDWR);
    if (motorfd < 0) {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1) {
        printf("请选择要控制的硬件\n");
        printf("0(LED灯) 1(蜂鸣器) 2(马达) 3(风扇)");
        scanf("%d", &a);
        switch (a) {
        case 0: // LED灯
            printf("请选择要实现的功能\n");
            printf("0(关灯) 1(开灯)");
            scanf("%d", &f);
            printf("请选择要控制的LED灯\n");
            printf("1(LED1) 2(LED2) 3(LED3)");
            scanf("%d", &n);
            if (f == 1) {
                ioctl(fd, LED_ON, &n);
            } else if (f == 0) {
                ioctl(fd, LED_OFF, &n);
            }
            break;
        case 1: // 蜂鸣器

            printf("请输入要实现的功能\n");
            printf("0(关闭) 1(启动)");
            scanf("%d", &f);
            if (f == 1) {
                ioctl(beepfd, BUZZER_ON);
            } else if (f == 0) {
                ioctl(beepfd, BUZZER_OFF);
            }
            break;
        case 2: // 马达

            printf("请输入要实现的功能\n");
            printf("0(关闭) 1(启动)");
            scanf("%d", &f);
            if (f == 1) {
                ioctl(motorfd, MOTOR_ON);
            } else if (f == 0) {
                ioctl(motorfd, MOTOR_OFF);
            }
            break;
        case 3: // 风扇

            printf("请输入要实现的功能\n");
            printf("0(关闭) 1(启动)");
            scanf("%d", &f);
            if (f == 1) {
                ioctl(fanfd, FAN_ON);
            } else if (f == 0) {
                ioctl(fanfd, FAN_OFF);
            }
            break;
        default:
            break;
        }
    }
    close(fd);
    return 0;
}

mychrdev_led.c:

#include "head.h"
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>
static int __init mycdev_init(void);
int major; // 用于保存主设备号
char kbuf[128] = { 0 };
gpio_t* led1;
gpio_t* led2;
gpio_t* led3;
gpio_t* buzzer;
unsigned int* 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;
}
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:
            led1->ODR |= (0x1 << 10);
            break;
        case 2:
            led2->ODR |= (0x1 << 10);
            break;
        case 3:
            led3->ODR |= (0x1 << 8);
            break;
        default:
            break;
        }
        break;
    case LED_OFF: // 关灯
        switch (which) {
        case 1:
            led1->ODR &= (~(0x1 << 10));
            break;
        case 2:
            led2->ODR &= (~(0x1 << 10));
            break;
        case 3:
            led3->ODR &= (~(0x1 << 8));
            break;
        default:
            break;
        }
        break;

    default:
        break;
    }
    return 0;
}
// 定义操作方法结构体变量并完成初始化
struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
int all_led_init(void)
{
    // 映射结构体物理寄存器
    led1 = ioremap(GPIOE, sizeof(gpio_t));
    if (led1 == NULL) {
        printk("led1结构体映射表失败\n");
        return -EFAULT;
    }
    led2 = ioremap(GPIOF, sizeof(gpio_t));
    if (led2 == NULL) {
        printk("led2结构体映射表失败\n");
        return -EFAULT;
    }
    led3 = ioremap(GPIOE, sizeof(gpio_t));
    if (led3 == NULL) {
        printk("led3结构体映射表失败\n");
        return -EFAULT;
    }

    rcc = ioremap(RCC, 4);
    if (rcc == NULL) {
        printk("rcc寄存器地址映射表失败\n");
        return -EFAULT;
    }

    // led1寄存器初始化
    (*rcc) |= (0x1 << 4);
    led1->MODER &= (~(0x3 << 20));
    led1->MODER |= (0x1 << 20);
    led1->ODR &= (~(0x1 << 10));
    // led2初始化
    led2->MODER &= (~(0x3 << 20));
    led2->MODER |= (0x1 << 20);
    led2->ODR &= (~(0x1 << 10));
    // led3初始化
    led3->MODER &= (~(0x3 << 16));
    led3->MODER |= (0x1 << 16);
    led3->ODR &= (~(0x1 << 8));

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

    // 寄存器映射初始化
    all_led_init();
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    int i;
    // 取消结构体映射
    iounmap(led1);
    iounmap(led2);
    iounmap(led3);
    iounmap(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");

mychrdev_fan.c

#include "head.h"
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>
static int __init mycdev_init(void);
int major; // 用于保存主设备号
char kbuf[128] = { 0 };
gpio_t* led1;
gpio_t* led2;
gpio_t* led3;
gpio_t* buzzer;
gpio_t* fan;
unsigned int* 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;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{

    switch (cmd) {

    case FAN_ON:
        fan->ODR |= (0x1 << 9);
        break;
    case FAN_OFF:
        fan->ODR &= (~(0x1 << 9));

    default:
        break;
    }
    return 0;
}
// 定义操作方法结构体变量并完成初始化
struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
int all_led_init(void)
{
    // 映射结构体物理寄存器

    rcc = ioremap(RCC, 4);
    if (rcc == NULL) {
        printk("rcc寄存器地址映射表失败\n");
        return -EFAULT;
    }

    // 风扇初始化
    (*rcc) |= (0x1 << 4);
    fan = ioremap(GPIOE, sizeof(gpio_t));
    if (fan == NULL) {
        printk("fan结构体映射表失败\n");
        return -EFAULT;
    }
    fan->MODER &= (~(0x3 << 18));
    fan->MODER |= (0x1 << 18);

    return 0;
}
static int __init mycdev_init(void)
{

    major = register_chrdev(0, "mychrdev_fan", &fops);
    if (major < 0) {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev4");
    if (IS_ERR(cls)) {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    // 向上提交设备结点信息
    // 为蜂鸣器创建设备文件

    dev = device_create(cls, NULL, MKDEV(major, 4), NULL, "mychrdev4");
    if (IS_ERR(dev)) {
        printk("向上提交设备结点信息失败\n");
        return -PTR_ERR(dev);
    }

    // 寄存器映射初始化
    all_led_init();
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{

    // 取消结构体映射
    iounmap(fan);

    iounmap(rcc);
    // 销毁设备结点信息

    device_destroy(cls, MKDEV(major, 4));

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

mychrdev_buzzer.c

#include "head.h"
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>
static int __init mycdev_init(void);
int major; // 用于保存主设备号
char kbuf[128] = { 0 };
gpio_t* led1;
gpio_t* led2;
gpio_t* led3;
gpio_t* buzzer;
unsigned int* 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;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{

    switch (cmd) {

    case BUZZER_ON:
        buzzer->ODR |= (0x1 << 6);
        break;
    case BUZZER_OFF:
        buzzer->ODR &= (~(0x1 << 6));
    default:
        break;
    }
    return 0;
}
// 定义操作方法结构体变量并完成初始化
struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
int all_led_init(void)
{
    // 映射结构体物理寄存器

    rcc = ioremap(RCC, 4);
    if (rcc == NULL) {
        printk("rcc寄存器地址映射表失败\n");
        return -EFAULT;
    }

    // 蜂鸣器初始化
    (*rcc) |= (0x1 << 1);
    buzzer = ioremap(GPIOB, sizeof(gpio_t));
    if (buzzer == NULL) {
        printk("buzzer结构体映射表失败\n");
        return -EFAULT;
    }
    buzzer->MODER &= (~(0x3 << 12));
    buzzer->MODER |= (0x1 << 12);

    return 0;
}
static int __init mycdev_init(void)
{

    major = register_chrdev(0, "mychrdev_buzzer", &fops);
    if (major < 0) {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev3");
    if (IS_ERR(cls)) {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    // 向上提交设备结点信息
    // 为蜂鸣器创建设备文件

    dev = device_create(cls, NULL, MKDEV(major, 3), NULL, "mychrdev3");
    if (IS_ERR(dev)) {
        printk("向上提交设备结点信息失败\n");
        return -PTR_ERR(dev);
    }

    // 寄存器映射初始化
    all_led_init();
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{

    // 取消结构体映射
    iounmap(buzzer);

    iounmap(rcc);
    // 销毁设备结点信息

    device_destroy(cls, MKDEV(major, 3));

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

mychrdev_motor.c:

#include "head.h"
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/uaccess.h>
static int __init mycdev_init(void);
int major; // 用于保存主设备号
char kbuf[128] = { 0 };
gpio_t* led1;
gpio_t* led2;
gpio_t* led3;
gpio_t* buzzer;
gpio_t* fan;
gpio_t* motor;
unsigned int* 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;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{

    switch (cmd) {

    case MOTOR_ON:
        motor->ODR |= (0x1 << 6);
        break;
    case MOTOR_OFF:
        motor->ODR &= (~(0x1 << 6));
        break;
    default:
        break;
    }
    return 0;
}
// 定义操作方法结构体变量并完成初始化
struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};
int all_led_init(void)
{
    // 映射结构体物理寄存器

    rcc = ioremap(RCC, 4);
    if (rcc == NULL) {
        printk("rcc寄存器地址映射表失败\n");
        return -EFAULT;
    }

    // 马达初始化
    (*rcc) |= (0x1 << 5);
    motor = ioremap(GPIOF, sizeof(gpio_t));
    if (motor == NULL) {
        printk("motor结构体映射表失败\n");
        return -EFAULT;
    }
    motor->MODER &= (~(0x3 << 12));
    motor->MODER |= (0x1 << 12);

    return 0;
}
static int __init mycdev_init(void)
{

    major = register_chrdev(0, "mychrdev_motor", &fops);
    if (major < 0) {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev5");
    if (IS_ERR(cls)) {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    // 向上提交设备结点信息
    // 为蜂鸣器创建设备文件

    dev = device_create(cls, NULL, MKDEV(major, 5), NULL, "mychrdev5");
    if (IS_ERR(dev)) {
        printk("向上提交设备结点信息失败\n");
        return -PTR_ERR(dev);
    }

    // 寄存器映射初始化
    all_led_init();
    printk("硬件寄存器初始化成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{

    // 取消结构体映射
    iounmap(motor);

    iounmap(rcc);
    // 销毁设备结点信息

    device_destroy(cls, MKDEV(major, 5));

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

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct {
    volatile unsigned int MODER; // 0x00
    volatile unsigned int OTYPER; // 0x04
    volatile unsigned int OSPEEDR; // 0x08
    volatile unsigned int PUPDR; // 0x0C
    volatile unsigned int IDR; // 0x10
    volatile unsigned int ODR; // 0x14
    volatile unsigned int BSRR; // 0x18
    volatile unsigned int LCKR; // 0x1C
    volatile unsigned int AFRL; // 0x20
    volatile unsigned int AFRH; // 0x24
    volatile unsigned int BRR; // 0x28
    volatile unsigned int res;
    volatile unsigned int SECCFGR; // 0x30

} gpio_t;
#define RCC 0x50000A28 // AHB4

#define GPIOA 0x50002000
#define GPIOB 0x50003000
#define GPIOC 0x50004000
#define GPIOD 0x50005000
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define GPIOG 0x50008000
#define GPIOH 0x50009000
#define GPIOI 0x5000A000
#define GPIOJ 0x5000B000
#define GPIOK 0x5000C000
#define GPIOZ 0x54004000
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)
#define BUZZER_ON _IO('b', 1)
#define BUZZER_OFF _IO('b', 0)
#define FAN_ON _IO('f', 1)
#define FAN_OFF _IO('f', 0)
#define MOTOR_ON _IO('m', 1)
#define MOTOR_OFF _IO('m', 0)
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值