驱动 实现三个灯的亮灭

1、编写LED灯的驱动,可以控制三个灯,应用程序中编写控制灯的逻辑,要使用自动创建设备节点机制

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28

#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED2_RCC 0x50000A28

#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
#define PHY_LED3_RCC 0x50000A28

#endif

mycdev.c

#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
int major;
char kbuf[128] = {0};
// 定义指针接收映射成功的虚拟内存首地址
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led1_rcc;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led2_rcc;
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_led3_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;
    }
    if (kbuf[0] == '1' && kbuf[1]== '1') // 开灯
    {
        // 开灯逻辑
        printk("开灯\n");
        (*vir_led1_odr) |= (0X1 << 10);
    }
    if (kbuf[0] == '1' && kbuf[1]== '0') // 关灯
    {
        // 关灯逻辑
        printk("关灯\n");
        (*vir_led1_odr) &= (~(0X1 << 10));
    }
    if (kbuf[0] == '2' && kbuf[1]== '1') // 开灯
    {
        // 开灯逻辑
        printk("开灯\n");
        (*vir_led2_odr) |= (0X1 << 10);
    }
    if (kbuf[0] == '2' && kbuf[1]== '0') // 关灯
    {
        // 关灯逻辑
        printk("关灯\n");
        (*vir_led2_odr) &= (~(0X1 << 10));
    }
    if (kbuf[0] == '3' && kbuf[1]== '1') // 开灯
    {
        // 开灯逻辑
        printk("开灯\n");
        (*vir_led3_odr) |= (0X1 << 8);
    }
    if (kbuf[0] == '3' && kbuf[1]== '0') // 关灯
    {
        // 关灯逻辑
        printk("关灯\n");
        (*vir_led3_odr) &= (~(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)
{
    int i;
    // 注册字符设备驱动
    major = register_chrdev(0, "mycdev", &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_led1_moder = ioremap(PHY_LED1_MODER, 4);
    if (vir_led1_moder == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led1_odr = ioremap(PHY_LED1_ODR, 4);
    if (vir_led1_odr == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led1_rcc = ioremap(PHY_LED1_RCC, 4);
    if (vir_led1_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }

    vir_led2_moder = ioremap(PHY_LED2_MODER, 4);
    if (vir_led2_moder == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
    if (vir_led2_odr == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led2_rcc = ioremap(PHY_LED2_RCC, 4);
    if (vir_led2_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
	
    vir_led3_moder = ioremap(PHY_LED3_MODER, 4);
    if (vir_led3_moder == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led3_odr = ioremap(PHY_LED3_ODR, 4);
    if (vir_led3_odr == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    vir_led3_rcc = ioremap(PHY_LED3_RCC, 4);
    if (vir_led3_rcc == NULL)
    {
        printk("物理内存映射失败%d\n", __LINE__);
        return -EFAULT;
    }
    printk("物理内存映射成功\n");
    // 硬件寄存器的初始化
    (*vir_led1_moder) &= (~(0X3 << 20)); // 设置为输出
    (*vir_led1_moder) |= (0X1 << 20);
    (*vir_led2_moder) &= (~(0X3 << 20)); // 设置为输出
    (*vir_led2_moder) |= (0X1 << 20);
    (*vir_led3_moder) &= (~(0X3 << 16)); // 设置为输出
    (*vir_led3_moder) |= (0X1 << 16);
    // rcc使能
    (*vir_led1_rcc) |= (0X1 << 4);
    (*vir_led2_rcc) |= (0X1 << 5);
    (*vir_led3_rcc) |= (0X1 << 4);
    // 默认关灯
    (*vir_led1_odr) &= (~(0X1 << 10));
    (*vir_led2_odr) &= (~(0X1 << 10));
    (*vir_led3_odr) &= (~(0X1 << 8)); 
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 取消物理内存的映射
    iounmap(vir_led1_moder);
    iounmap(vir_led1_odr);
    iounmap(vir_led1_rcc);
    iounmap(vir_led2_moder);
    iounmap(vir_led2_odr);
    iounmap(vir_led2_rcc);
    iounmap(vir_led3_moder);
    iounmap(vir_led3_odr);
    iounmap(vir_led3_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");

test.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, const char *argv[])
{
	char buf[128] = {};
	int fd = open("/dev/mycdev0",O_RDWR);
	if(fd < 0)
	{
		printf("打开设备文件失败\n");
		exit(-1);
	}
	printf("成功打开设备文件\n");
	while(1)
	{
		printf("输入要实现的逻辑:>\n");
		fgets(buf,sizeof(buf),stdin);//从终端读取一个字符存放到buf
		buf[strlen(buf)-1]='\0';
		write(fd,buf,sizeof(buf));
	}
	close(fd);
	return 0;
}

实验现象

驱动 实现三个灯的亮灭_第1张图片

 驱动 实现三个灯的亮灭_第2张图片

 

你可能感兴趣的:(驱动,arm)