驱动开发2 CoetexA7核 字符设备驱动(LED亮灯)(单独映射寄存器实现+封装结构体映射实现)

一、单独映射寄存器实现

可参考arm点灯C语言 cortex-A7核 点LED灯 (附 汇编实现、使用C语言 循环实现、使用C语言 封装函数实现【重要、常用】)-CSDN博客

1 应用程序 test.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int fd = open("/dev/mychrdev",O_RDWR);
    if(fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    printf("打开设备文件成功\n");   
    while(1)
    {
        printf("请输入要进行的操作:0(关灯) 1(开灯) >>> ");
        fgets(buf,sizeof(buf),stdin);//在终端读一个字符串
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));//将数据传递给内核
    }
    close(fd);
    return 0;
}

2 头文件head.h

#ifndef __HEAD_H__
#define __HEAD_H__
//GPIOE
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0x50006014
//GPIOF
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0x50007014
//RCC
#define PHY_RCC 0x50000A28

#endif

3 驱动程序 demo.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
unsigned int major;
char kbuf[128] = {};
//定义三个指针指向映射后的虚拟内存
unsigned int *vir_moder_E;
unsigned int *vir_odr_E;
unsigned int *vir_moder_F;
unsigned int *vir_odr_F;
unsigned int *vir_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 *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int 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)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret;
    ret = copy_from_user(kbuf,ubuf,size);
    if (ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    if(kbuf[0] == '0') //关灯
    {
        //关灯逻辑
        (*vir_odr_E) &= (~(0x1 << 10)); //LED1默认关灯
        (*vir_odr_F) &= (~(0x1 << 10)); //LED2默认关灯
        (*vir_odr_E) &= (~(0x1 << 8)); //LED3默认关灯
    }
    else if(kbuf[0] == '1') //开灯
    {
        //开灯逻辑
        (*vir_odr_E) |= (0x1 << 10); //LED1
        (*vir_odr_F) |= (0x1 << 10); //LED2
        (*vir_odr_E) |= (0x1 << 8); //LED3
    }
    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,"mychrdev",&fops);
    if(major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n",major);
    //进行寄存器的地址映射
    //GPIOE
    vir_moder_E = ioremap(PHY_LED1_MODER,4);
    if(vir_moder_E == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_odr_E = ioremap(PHY_LED1_ODR,4);
    if(vir_odr_E == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    //GPIOF
    vir_moder_F = ioremap(PHY_LED2_MODER,4);
    if(vir_moder_F == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    vir_odr_F = ioremap(PHY_LED2_ODR,4);
    if(vir_odr_F == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    //RCC
    vir_rcc = ioremap(PHY_RCC,4);
    if(vir_rcc == NULL)
    {
        printk("物理内存地址映射失败%d\n",__LINE__);
        return -EFAULT;
    }
    printk("寄存器内存映射成功\n");
    //LED1寄存器初始化
    (*vir_rcc) |= (0x1 << 4); //GPIOE控制器时钟使能
    (*vir_moder_E) &= (~(0x3 << 20)); //MODER[21:20]->00
    (*vir_moder_E) |= (0x1 << 20); //MODER[21:20]->01
    (*vir_odr_E) &= (~(0x1 << 10)); //默认关灯
    //LED2寄存器初始化
    (*vir_rcc) |= (0x1 << 5); //GPIOF控制器时钟使能
    (*vir_moder_F) &= (~(0x3 << 20)); //MODER[21:20]->00
    (*vir_moder_F) |= (0x1 << 20); //MODER[21:20]->01
    (*vir_odr_F) &= (~(0x1 << 10)); //默认关灯
    //LED3寄存器初始化
    (*vir_rcc) |= (0x1 << 4); //GPIOE控制器时钟使能
    (*vir_moder_E) &= (~(0x3 << 16)); //MODER[17:16]->00
    (*vir_moder_E) |= (0x1 << 16); //MODER[17:16]->01
    (*vir_odr_E) &= (~(0x1 << 8)); //默认关灯
    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消内存映射
    iounmap(vir_moder_E);
    iounmap(vir_odr_E);
    iounmap(vir_moder_F);
    iounmap(vir_odr_F);
    iounmap(vir_rcc);
    //注册字符设备驱动
    unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

编写好代码后,make成arm架构

make arch=arm modname=demo

使用交叉编译工具链

arm-linux-gnueabihf-gcc test.c 

将文件通过tftp传输到开发板中

cp demo.ko ~/nfs/rootfs/
cp a.out ~/nfs/rootfs/

4 效果呈现

二、封装结构体映射实现

kbuf[0] 控制灯

kbuf[1] 控制状态

1 头文件head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 
typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR    0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR    0X50000A28
#endif 

2 驱动程序 demo.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include"head.h"

int major;
char kbuf[128]={0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_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 *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     unsigned long ret;
    //向用户空间读取拷贝
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
    unsigned long ret;
    //从用户空间读取数据
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    switch(kbuf[0]){
        case '1'://LED1
            if(kbuf[1]=='0')//关灯
                vir_led1->ODR &= (~(1<<10));
            else//开灯
                vir_led1->ODR |= 1<<10;
            break;
        case '2'://LED2
            if(kbuf[1]=='0')//关灯
                vir_led2->ODR &= (~(1<<10));
            else//开灯
                vir_led2->ODR |= 1<<10;
            break;
        case '3'://LED3
            if(kbuf[1]=='0')//关灯
                vir_led3->ODR &= (~(1<<8));
            else//开灯
                vir_led3->ODR |= 1<<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,
};

int all_led_init(void)
{
    //寄存器地址的映射
    vir_led1=ioremap(PHY_LED1_ADDR,sizeof(gpio_t));
    if(vir_led1==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
     vir_led2=ioremap(PHY_LED2_ADDR,sizeof(gpio_t));
    if(vir_led2==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
     vir_led3=vir_led1;
    vir_rcc=ioremap(PHY_RCC_ADDR,4);
    if(vir_rcc==NULL)
    {
        printk("ioremap filed:%d\n",__LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //寄存器的初始化
    //rcc
    (*vir_rcc) |= (3<<4);
    //led1
    vir_led1->MODER &= (~(3<<20));
    vir_led1->MODER |= (1<<20);
    vir_led1->ODR &= (~(1<<10));
    //led2
    vir_led2->MODER &= (~(3<<20));
    vir_led2->MODER |= (1<<20);
    vir_led2->ODR &= (~(1<<10));
    //led3
    vir_led3->MODER &= (~(3<<16));
    vir_led1->MODER |= (1<<16);
    vir_led1->ODR &= (~(1<<8));
    printk("寄存器初始化成功\n");

    return 0;
}

static int __init mycdev_init(void)
{
    //字符设备驱动注册
    major=register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n",major);

    //寄存器映射以及初始化
    all_led_init();

    return 0;
}
static void __exit mycdev_exit(void)
{
    //取消地址映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
    //注销字符设备驱动
    unregister_chrdev(major,"mychrdev");


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

3 应用程序 test.c

#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>


int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int fd=open("/dev/mychrdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        //从终端读取
        printf("请输入两个字符\n");
        printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");
        printf("第二个字符:0(关灯) 1(开灯)\n");
        printf("请输入>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        //向设备文件中写
        write(fd,buf,sizeof(buf));
    }

    
    close(fd);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值