【day3】驱动

1.编写LED灯的驱动,可以控制三个灯,应用程序中编写控制灯的逻辑,要使用自动创建设备节点机制,发布到CSDN
main()
{

while(1)
{
    printf("请输入要实现的逻辑》\n");
    printf("11:LED1亮\n");    
    printf("10:LED1灭\n");   
    printf("21:LED2亮\n");    
    printf("20:LED2灭\n"); 
    printf("31:LED3亮\n");    
    printf("30:LED3灭\n"); 
}

}
代码:
head.h:

#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_LED3_MODER 0X50006000
#define PHY_LED1_LED3_ODR 0X50006014
#define PHY_LED_RCC 0X50000A28
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#endif

驱动:

#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
int major;
char kbuf[128] = {0};
// 定义指针接收映射成功的虚拟内存首地址
unsigned int *vir_moder13;
unsigned int *vir_odr13;
unsigned int *vir_moder2;
unsigned int *vir_odr2;
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;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    int ret;
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user filad\n");
        return ret;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return ret;
    }
    switch (kbuf[0])
    {
    case '1':
        if (kbuf[1] == '1') // 开灯
        {
            // 开灯逻辑
            printk("LED1开灯\n");
            (*vir_odr13) |= (0X1 << 10);
        }
        else if (kbuf[1] == '0') // 关灯
        {
            // 关灯逻辑
            printk("LED1关灯\n");
            (*vir_odr13) &= (~(0X1 << 10));
        }
        break;
    case '2':
        if (kbuf[1] == '1') // 开灯
        {
            // 开灯逻辑
            printk("LED2开灯\n");
            (*vir_odr2) |= (0X1 << 10);
        }
        else if (kbuf[1] == '0') // 关灯
        {
            // 关灯逻辑
            printk("LED2关灯\n");
            (*vir_odr2) &= (~(0X1 << 10));
        }
        break;
    case '3':
        if (kbuf[1] == '1') // 开灯
        {
            // 开灯逻辑
            printk("LED3开灯\n");
            (*vir_odr13) |= (0X1 << 8);
        }
        else if (kbuf[1] == '0') // 关灯
        {
            // 关灯逻辑
            printk("LED3关灯\n");
            (*vir_odr13) &= (~(0X1 << 8));
        }
        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,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

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, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录信息失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录信息成功\n");
    // 向上提交设备信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(cls);
        }
    }
    printk("向上提交设备节点信息成功\n");
    // 完成硬件寄存器物理内存的映射
    vir_moder13 = ioremap(PHY_LED1_LED3_MODER, 4);
    if (vir_moder13 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr13 = ioremap(PHY_LED1_LED3_ODR, 4);
    if (vir_odr13 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_moder2 = ioremap(PHY_LED2_MODER, 4);
    if (vir_moder2 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_odr2 = ioremap(PHY_LED2_ODR, 4);
    if (vir_odr2 == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_rcc = ioremap(PHY_LED_RCC, 4);
    if (vir_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("物理内存映射成功\n");
    // 硬件寄存器的初始化
    (*vir_moder13) &= (~(0X3 << 20)); // 设置为输出
    (*vir_moder13) |= (0X1 << 20);
    (*vir_moder2) &= (~(0X3 << 20)); // 设置为输出
    (*vir_moder2) |= (0X1 << 20);
    (*vir_moder13) &= (~(0X3 << 16)); // 设置为输出
    (*vir_moder13) |= (0X1 << 16);
    // rcc使能
    (*vir_rcc) |= (0X1 << 4);
    (*vir_rcc) |= (0X1 << 5);
    // 默认关灯
    (*vir_odr13) &= (~(0X1 << 10));
    (*vir_odr2) &= (~(0X1 << 10));
    (*vir_odr13) &= (~(0X1 << 8));
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 取消物理内存的映射
    iounmap(vir_moder13);
    iounmap(vir_odr13);
    iounmap(vir_rcc);
    // 销毁设备信息
    int i;
    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 
#include 
#include 
#include 
#include 
#include 
#include
int main(int argc, const char *argv[])
{
    char buf[128] = {};
    int fd1 = open("/dev/mycdev0", O_RDWR);
    if (fd1 < 0)
    {
        printf("打开设备文件mycdev0失败\n");
        exit(-1);
    }
    int fd2 = open("/dev/mycdev1", O_RDWR);
    if (fd2 < 0)
    {
        printf("打开设备文件mycdev1失败\n");
        exit(-1);
    }
    int fd3 = open("/dev/mycdev2", O_RDWR);
    if (fd3 < 0)
    {
        printf("打开设备文件mycdev2失败\n");
        exit(-1);
    }
    printf("成功打开设备文件\n");
    while(1)
    {
        printf("请输入要实现的逻辑>\n");
        fgets(buf,sizeof(buf),stdin);//从终端读一个字符串存放在buf
        buf[strlen(buf)-1]='\0';
        switch (buf[0])
        {
        case '1':
            write(fd1, buf, sizeof(buf));//将从终端得到的字符串传递到驱动
            if(buf[1]=='1')
                printf("11:LED1亮\n");
            else
                printf("10:LED1灭\n");
            break;
        case '2':
            write(fd2, buf, sizeof(buf));//将从终端得到的字符串传递到驱动
            if(buf[1]=='1')
                printf("21:LED2亮\n");
            else
                printf("20:LED2灭\n");
            break;
            case '3':
            write(fd2, buf, sizeof(buf));//将从终端得到的字符串传递到驱动
            if(buf[1]=='1')
                printf("31:LED3亮\n");
            else
                printf("30:LED3灭\n");
            break;
        default:
            printf("请输入正确的逻辑\n"); 
            break;
        }       
    }
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

你可能感兴趣的:(c语言,驱动开发)