7月24日作业

.h文件

#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
//GPIO定义·
#define GPIOE 4
#define GPIOF 5
//GPIO各寄存器模式定义
//LED1--GPIOE 
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR   0X50006014
#define PHY_LED1_RCC   0X50000A28
//LED2--GPIOF 
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR   0X50007014
#define PHY_LED2_RCC   0X50000A28
//LED1--GPIOE 
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR   0X50006014
#define PHY_LED3_RCC   0X50000A28

#endif

.c文件

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include"head.h"
unsigned int major1;//定义一个变量保存主设备号
unsigned int major2;//定义一个变量保存主设备号
unsigned int major3;//定义一个变量保存主设备号
char kbuf[128]={0};
char cbuf[128]={0};

//************************************************************
//封装操作方法
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__);
      long ret;
     ret=copy_to_user(ubuf,kbuf,size);
     if(ret)
     {
        printk("copy_to_user filed\n");
        return -EIO;
     }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
    int a=PHY_LED1_ODR;
    int b=PHY_LED2_ODR;
    int c=PHY_LED3_ODR;
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    long ret;
     ret=copy_from_user(kbuf,ubuf,size);
     if(ret)
     {
        printk("copy_from_user filed\n");
        return -EIO;
     }
     //判断用户输入的数据,进行不同的硬件逻辑执行
     if(kbuf[0]=='1' && kbuf[1]=='0')//关灯
     {
        //关灯逻辑
        a &= (~(0X1<<10));
     }
     else if(kbuf[0]=='1' && kbuf[1]=='0')
     {
        //开灯逻辑
        a |=(0X1<<10);
     }

     //判断用户输入的数据,进行不同的硬件逻辑执行
     if(kbuf[0]=='2' && kbuf[1]=='0')//关灯
     {
        //关灯逻辑
        b &= (~(0X1<<10));
     }
     else if(kbuf[0]=='2' && kbuf[1]=='0')
     {
        //开灯逻辑
        b |=(0X1<<10);
     }

     //判断用户输入的数据,进行不同的硬件逻辑执行
     if(kbuf[0]=='3' && kbuf[1]=='0')//关灯
     {
        //关灯逻辑
        c &= (~(0X1<<8));
     }
     else if(kbuf[0]=='3' && kbuf[1]=='0')
     {
        //开灯逻辑
        c |=(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,
    .release=mycdev_close,
    .read=mycdev_read,
    .write=mycdev_write,
};
//**********************映射封装************************************
int my_ioremap(int GPIO,int PIN,int MODER,int ODR,int RCC)
    {
        unsigned int *vir_moder;
        unsigned int *vir_odr;
        unsigned int *vir_rcc;
        vir_moder=ioremap(MODER,GPIO);
        if(vir_moder==NULL)
        {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
        }
        vir_odr=ioremap(ODR,4);
        if(vir_odr==NULL)
        {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
         }
        vir_rcc=ioremap(RCC,4);
        if(vir_rcc==NULL)
        {
        printk("映射物理内存失败%d\n",__LINE__);
        return -EFAULT;
        }
         printk("映射物理内存成功\n");
        //硬件寄存器的初始化
        (*vir_rcc)|=(0X1<<GPIO);//rcc使能
        (*vir_moder) &=(~(0X3<<(PIN*2)));//设置为输出
        (*vir_moder) |=(0X1<<(PIN*2));
        (*vir_odr) &= (~(0X1<<PIN));//输出低电平
        printk("硬件寄存器初始化成功\n");
        return 0;
    }
    //************************开关灯*************************


    //*******************************************************
static int __init mycdev_init(void)
{
    //注册LED1字符设备驱动
    major1=register_chrdev(0,"mychrdev1",&fops);
    if(major1<0)
    {
        printk("注册字符设备驱动失败\n");
        return major1;
    }
    printk("注册字符设备驱动成功major=%d\n",major1);
    //注册LED2字符设备驱动
    major2=register_chrdev(0,"mychrdev2",&fops);
    if(major2<0)
    {
        printk("注册字符设备驱动失败\n");
        return major1;
    }
    printk("注册字符设备驱动成功major=%d\n",major2);
    //注册LED3字符设备驱动
    major3=register_chrdev(0,"mychrdev3",&fops);
    if(major3<0)
    {
        printk("注册字符设备驱动失败\n");
        return major3;
    }
    printk("注册字符设备驱动成功major=%d\n",major3);
    //进行寄存器地址的映射
    my_ioremap(GPIOE,10,PHY_LED1_MODER ,PHY_LED1_ODR,PHY_LED1_RCC);
    my_ioremap(GPIOF,10,PHY_LED2_MODER ,PHY_LED2_ODR,PHY_LED2_RCC);
    my_ioremap(GPIOE,8,PHY_LED3_MODER ,PHY_LED3_ODR,PHY_LED3_RCC);
    return 0;
}


static void __exit mycdev_exit(void)
{
    //注销字符设备驱动
    unregister_chrdev(major1,"mychrdev1");
    unregister_chrdev(major2,"mychrdev2");
    unregister_chrdev(major3,"mychrdev3");

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

main

#include"head.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int led=0;

    int fd1 = open("/dev/mychrdev1", O_RDWR);
    if (fd1 < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入控制命令:0(关灯)1(开灯)>");
        fgets(buf, sizeof(buf), stdin); // 从终端输入数据到buf
        buf[strlen(buf) - 1] = '\0';    // 将buf末尾的'\n'切换为'\0'
        write(fd1, buf, sizeof(buf));
    }
    close(fd1);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值