编写三盏灯控制的驱动,应用程序编写三盏灯亮灭的逻辑

text.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

    // 为了查看是否能调用驱动中的mycdev_open
    int fd = open("/dev/mycdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }

    while (1)
    {
        printf("输入“11-LED1灯亮” 输入“10-LED1灯灭”\n输入“21-LED1灯亮” 输入“20-LED1灯灭”\n输入“31-LED1灯亮” 输入“30-LED1灯灭”");
        fgets(buf, sizeof(buf), stdin);
        buf[sizeof(buf) - 1] = '\0';
        // read(fd, buf, sizeof(buf))是为了调用驱动中的mycdev_write
        write(fd, buf, sizeof(buf));
    }

    close(fd);

    return 0;
}

mycdev.c

#include 
#include 
#include 
#include 
#include 
#include "led.h"
#include "rcc.h"

unsigned int major;
char kbuf[128] = {0};

// 定义三个指针指向映射后的虚拟地址
unsigned int *vir_moder_1;
unsigned int *vir_moder_2;
unsigned int *vir_moder_3;
unsigned int *vir_odr_1;
unsigned int *vir_odr_2;
unsigned int *vir_odr_3;
unsigned int *vir_rcc;

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    // 向用户拷贝
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy to user filed\n");
        return -EIO;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    // 从用户拷贝
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy from user filed\n");
        return -EIO;
    }

    // LED1根据用户空间发送来的指令实现灯的亮灭控制
    if (kbuf[0] == '1')
    {
        if (kbuf[1] == '1') // 亮灯
            (*vir_odr_1) |= (0x1 << 10);
        else if (kbuf[1] == '0')
            (*vir_odr_1) &= (~(0x1 << 10));
    }

    // LED2根据用户空间发送来的指令实现灯的亮灭控制
    if (kbuf[0] == '2')
    {
        if (kbuf[1] == '1') // 亮灯
            (*vir_odr_2) |= (0x1 << 10);
        else if (kbuf[1] == '0')
            (*vir_odr_2) &= (~(0x1 << 10));
    }

    // LED3根据用户空间发送来的指令实现灯的亮灭控制
    if (kbuf[0] == '3')
    {
        if (kbuf[1] == '1') // 亮灯
            (*vir_odr_3) |= (0x1 << 8);
        else if (kbuf[1] == '0')
            (*vir_odr_3) &= (~(0x1 << 8));
    }

    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,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    // 字符设备驱动的注册
    major = register_chrdev(0, "mycdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }

    printk("字符设备驱动注册成功majoe=%d\n", major);

    //==========进行LED1相关寄存器地址映射==========
    vir_moder_1 = ioremap(&(GPIOE->MODER), 4);
    if (vir_moder_1 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_odr_1 = ioremap(&(GPIOE->ODR), 4);
    if (vir_odr_1 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(&(RCC->MP_AHB4ENSETR), 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    printk("LED1物理内存映射为虚拟化成功\n");
    // 相关寄存器初始化
    // GPIOE使能
    (*vir_rcc) |= (0x1 << 4);

    // 设置为输出
    (*vir_moder_1) &= (~(0x3 << 20));
    (*vir_moder_1) |= (0x1 << 20);

    // 灭灯
    (*vir_odr_1) &= (~(0x1 << 10));

    //==========进行LED2相关寄存器地址映射==========
    vir_moder_2 = ioremap(&(GPIOF->MODER), 4);
    if (vir_moder_2 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_odr_2 = ioremap(&(GPIOF->ODR), 4);
    if (vir_odr_2 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(&(RCC->MP_AHB4ENSETR), 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    printk("LED2物理内存映射为虚拟化成功\n");
    // 相关寄存器初始化
    // GPIOE使能
    (*vir_rcc) |= (0x1 << 5);

    // 设置为输出
    (*vir_moder_2) &= (~(0x3 << 20));
    (*vir_moder_2) |= (0x1 << 20);

    // 灭灯
    (*vir_odr_2) &= (~(0x1 << 10));

    //==========进行LED3相关寄存器地址映射==========
    vir_moder_3 = ioremap(&(GPIOE->MODER), 4);
    if (vir_moder_3 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_odr_3 = ioremap(&(GPIOE->ODR), 4);
    if (vir_odr_3 == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    vir_rcc = ioremap(&(RCC->MP_AHB4ENSETR), 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }

    printk("LED3物理内存映射为虚拟化成功\n");
    // 相关寄存器初始化
    // GPIOE使能
    (*vir_rcc) |= (0x1 << 4);

    // 设置为输出
    (*vir_moder_3) &= (~(0x3 << 16));
    (*vir_moder_3) |= (0x1 << 16);

    // 灭灯
    (*vir_odr_3) &= (~(0x1 << 8));

    return 0;
}
static void __exit mycdev_exit(void)
{
    // 内存取消映射
    iounmap(vir_rcc);
    iounmap(vir_odr_1);
    iounmap(vir_odr_2);
    iounmap(vir_odr_3);
    iounmap(vir_moder_1);
    iounmap(vir_moder_2);
    iounmap(vir_moder_3);

    // 字符设备驱动注销
    unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(数学建模)