驱动开发day3

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

int major;
char kbuf[256] = {0};

//定义指针接收映射成功的虚拟内存首地址
unsigned int *vir_GPIOE;
unsigned int *vir_GPIOF;
unsigned int *vir_Rcc;

GPIO_TypeDef* GPIOE_Init;
GPIO_TypeDef* GPIOF_Init;

//定义创建设备结点指针
struct class* cls;
struct device* dev;

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

ssize_t mycdev_read(struct file *file,char* ubuf,size_t size,loff_t* lof)
{
    int ret;
    ret = copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filad\n"); 
    }   
    
    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 filad\n"); 
    }   
    switch(kbuf[0])
    {
        case '0' :
            if(kbuf[1] == '1')
            {
                
                GPIOE_Init->ODR |= (0x01 << 10);
            }else
            {
                GPIOE_Init->ODR &= (~(0x01 << 10));
            }
        break; 

        case '1' :
            if(kbuf[1] == '1')
            {
                
                GPIOE_Init->ODR |= (0x01 << 8);
            }else
            {
                GPIOE_Init->ODR &= (~(0x01 << 8));
            }
        break; 

        case '2' :
            if(kbuf[1] == '1')
            {
                
                GPIOF_Init->ODR |= (0x01 << 10);
            }else
            {
                GPIOF_Init->ODR &= (~(0x01 << 10));
            }
        break; 
    }
    return 0;
}

int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%d\n",__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=0;
    /*注册字符设备驱动*/
    major = register_chrdev(0,"mychrdev",&fops);
    if(major < 0)
    {
        printk("字符设备注册失败\n");
        return major;
    }
    printk("字符设备注册完成 major:%d\n",major);

    /*映射虚拟地址到物理地址*/
    vir_GPIOE = ioremap(GPIOE,4);//GPIOE
    if(vir_GPIOE == NULL)
    {
       printk("地址映射失败:GPIOE!\n");
        return -EFAULT;
    }
    GPIOE_Init = (GPIO_TypeDef*)vir_GPIOE;

    vir_GPIOF = ioremap(GPIOF,4);//GPIOF
    if(vir_GPIOF == NULL)
    {
       printk("地址映射失败:GPIOF!\n");
        return -EFAULT;
    }
    GPIOF_Init = (GPIO_TypeDef*)vir_GPIOF;

    vir_Rcc = ioremap(RCC_MP_AHB4_ENSETR,4);//RCC
    if(vir_Rcc == NULL)
    {
       printk("地址映射失败:RCC_MP_AHB4_ENSETR!\n");
        return -EFAULT;
    }

    /*自动创建字符设备*/
    //提交目录信息
    cls = class_create(THIS_MODULE,"mycdev");
    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,"mycdev%d",i);
        if(IS_ERR(dev))
        {
            printk("设备信息提交失败 devID:%d\n",i);
            return -PTR_ERR(dev);
        }
    }
    printk("设备信息提交完成 devID:%d\n",i);

    /*初始化GPIO*/
    (*vir_Rcc) |= (3 << 4);//初始化RCC时钟

    GPIOE_Init->MODER &= (~(0x03 << 20));
    GPIOE_Init->MODER |= (0x01 << 20);

    GPIOE_Init->MODER &= (~(0x03 << 16));
    GPIOE_Init->MODER |= (0x01 << 16);

    GPIOF_Init->MODER &= (~(0x03 << 20));
    GPIOF_Init->MODER |= (0x01 << 20);

    GPIOE_Init->ODR &= (~(0x01 << 10));
    GPIOE_Init->ODR |= (0x01 << 10);

    GPIOF_Init->ODR &= (~(0x01 << 10));
    GPIOF_Init->ODR |= (0x01 << 10);

    GPIOE_Init->ODR &= (~(0x01 << 8));
    GPIOE_Init->ODR |= (0x01 << 8);
    // (*GPIOF_Init) |= (3 << 4);//初始化GPIOE引脚
    // (*GPIOF_Init) |= (3 << 4);//初始化GPIOF引脚

    return 0;
}

//出口函数 内核模块卸载时执行
static void __exit mycdev_exit(void)
{
    int i=0;
    /*取消物理内存映射*/
    iounmap(vir_GPIOE);
    iounmap(vir_GPIOF);
    iounmap(vir_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);
//用于声明当前内核模块遵循GPL协议
MODULE_LICENSE("GPL");

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

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd0 = open("/dev/mycdev0", O_RDWR);

    if(fd0 < 0) {
        printf("设备0读取失败\n");
        return -1;
    }

    while(1)
    {
        printf("请输入LED指令>>\n");
        fgets(buf,sizeof(buf),stdin);//从终端读一个字符串存放在buf
        buf[strlen(buf)-1]='\0';
        write(fd0, buf, sizeof(buf));//将从终端得到的字符串传递到驱动
        
    }
    close(fd0);
    
    return 0;
}

驱动开发day3_第1张图片

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