驱动day2点灯

#include 
#include 
#include 
#include 
#include 
#include "led.h"
unsigned int major;
char kbuf[128]={0};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
unsigned int *vir1_moder;
unsigned int *vir1_odr;
unsigned int *vir1_rcc;
unsigned int *vir2_moder;
unsigned int *vir2_odr;
unsigned int *vir2_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 *iof)
{
    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 *iof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    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;
    }
    //根据用户空间发送来的指令
    if(kbuf[0]=='1')//亮灯
    {
        (*vir_odr) |= (0x1<<10);
        (*vir1_odr) |= (0x1<<10);
        (*vir2_odr) |= (0x1<<8);
    }
    else if(kbuf[0]=='0')//灭灯
    {
        (*vir_odr) &= (~(0x1<<10));
        (*vir1_odr) &= (~(0x1<<10));
        (*vir2_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)
{
    //字符设备驱动的注册
    major=register_chrdev(0,"mycdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n",major);
    //进行LED1相关寄存器地址映射
    vir_moder=ioremap(EPHY_MODER,4);
    if(vir_moder==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir_odr=ioremap(EPHY_ODR,4);
    if(vir_odr==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir_rcc=ioremap(PHY_RCC,4);
    if(vir_rcc==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    printk("物理内存映射为虚拟内存成功");
    //相关寄存器初始化
    (*vir_rcc) |= (0x1<<4);
    (*vir_moder) &= (~(0x3<<20));
    (*vir_moder) |=(0x1<<20);
    (*vir_odr) &= (~(0x1<<10));
    printk("相关寄存器初始化完成\n");

    //进行LED2相关寄存器地址映射
    vir1_moder=ioremap(FPHY_MODER,4);
    if(vir1_moder==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir1_odr=ioremap(FPHY_ODR,4);
    if(vir1_odr==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir1_rcc=ioremap(PHY_RCC,4);
    if(vir1_rcc==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    printk("物理内存映射为虚拟内存成功");
    //相关寄存器初始化
    (*vir1_rcc) |= (0x1<<5);
    (*vir1_moder) &= (~(0x3<<20));
    (*vir1_moder) |=(0x1<<20);
    (*vir1_odr) &= (~(0x1<<10));
    printk("相关寄存器初始化完成\n");
    //进行LED3相关寄存器地址映射
    vir2_moder=ioremap(EPHY_MODER,4);
    if(vir2_moder==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir2_odr=ioremap(EPHY_ODR,4);
    if(vir2_odr==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    vir2_rcc=ioremap(PHY_RCC,4);
    if(vir2_rcc==NULL)
    {
        printk("物理内存映射失败\n");
        return -EFAULT;
    }
    printk("物理内存映射为虚拟内存成功");
    //相关寄存器初始化
    (*vir2_rcc) |= (0x1<<4);
    (*vir2_moder) &= (~(0x3<<16));
    (*vir2_moder) |=(0x1<<16);
    (*vir2_odr) &= (~(0x1<<8));
    printk("相关寄存器初始化完成\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    //内存取消映射
    iounmap(vir_rcc);
    iounmap(vir_odr);
    iounmap(vir_moder);
    iounmap(vir1_rcc);
    iounmap(vir1_odr);
    iounmap(vir1_moder);
    iounmap(vir2_rcc);
    iounmap(vir2_odr);
    iounmap(vir2_moder);
    unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(驱动开发)