0722_驱动3 地址映射驱动点灯

一、为什么需要地址映射

在芯片手册上查看到的地址属于物理地址,在硬件层

在内核空间地址属于虚拟地址,在内核层

在驱动中,操作的是虚拟地址

需要将物理地址《--mmu内存管理单元--》虚拟地址映射

二、映射API接口

void *ioremap(unsigned long port, unsigned long size)#include <linux/io.h>

函数功能:将物理地址映射为虚拟地址

参数:

        port:物理地址

        size:映射大小

返回值: 成功返回映射虚拟地址首地址 失败返回NULL

三、取消映射API接口

void iounmap(volatile void *addr)

函数功能:取消地址映射

参数:

        addr:取消映射虚拟地址首地址

返回值:无

四、编写驱动程序

myled.h:

#ifndef __MYLED_H__
#define __MYLED_H__

enum led{
    LED1, //0
    LED2, //1
    LED3, //2
};

typedef struct {
    volatile unsigned int MODER;   // 0x00
    volatile unsigned int OTYPER;  // 0x04
    volatile unsigned int OSPEEDR; // 0x08
    volatile unsigned int PUPDR;   // 0x0C
    volatile unsigned int IDR;     // 0x10
    volatile unsigned int ODR;     // 0x14
    volatile unsigned int BSRR;    // 0x18
    volatile unsigned int LCKR;    // 0x1C 
    volatile unsigned int AFRL;    // 0x20 
    volatile unsigned int AFRH;    // 0x24
    volatile unsigned int BRR;     // 0x28
    volatile unsigned int res;
    volatile unsigned int SECCFGR; // 0x30
}gpio_t;

#define  GPIOE   (0x50006000) //GPIOE组基地址
#define  GPIOF   (0x50007000) //GPIOF组基地址
#define RCC_MP_AHB4ENSETR 0x50000A28//RCC_MP_AHB4ENSETR寄存器地址

#endif 

demo.c:

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

#define CNAME "myled"
unsigned int major = 0;
char kbuf[128] = {};
unsigned int* rcc_virt = NULL; //RCC虚拟地址
gpio_t* gpioe_virt = NULL; //gpioe组虚拟地址
gpio_t*gpiof_virt = NULL; //gpiof组虚拟地址

#define LED1_ON (gpioe_virt->ODR |= (0x1 << 10)) //LED1点亮
#define LED1_OFF (gpioe_virt->ODR &= (~(0x1 << 10))) //LED1熄灭

#define LED2_ON (gpiof_virt->ODR |= (0x1 << 10)) //LED2点亮
#define LED2_OFF (gpiof_virt->ODR &= (~(0x1 << 10))) //LED2熄灭

#define LED3_ON (gpioe_virt->ODR |= (0x1 << 8)) //LED3点亮
#define LED3_OFF (gpioe_virt->ODR &= (~(0x1 << 8))) //LED3熄灭

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

ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    
    //如果用户空间想写的大小256个字节,大于内核空间的大小128个字节,需要更正用户空间写的大小
    if(size > sizeof(kbuf)) size = sizeof(kbuf);
    ret = copy_from_user(kbuf,ubuf,size); //将用户空间的数据,写入到内核空间
    if(ret){
        printk("copy from user is error\n");
        return -EIO;
    }
    //kbuf[0] = 0 操作LED1  kbuf[0] = 1 操作LED2  kbuf[0] = 2 操作LED3
    //kbuf[1] = 0 LED熄灭 kbuf[1] = 1 LED点亮
    switch (kbuf[0])
    {
    case LED1: //操作LED1
        kbuf[1] == 0 ? LED1_OFF : LED1_ON;
        break;
    case LED2: //操作LED2
        kbuf[1] == 0 ? LED2_OFF : LED2_ON;
        break;  
    case LED3: //操作LED3
        kbuf[1] == 0 ? LED3_OFF : LED3_ON;
        break;   
    }
    return size; 
}
int myled_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0; 
}

//操作方法结构体
const struct file_operations fops = {
    .open = myled_open,
    .write = myled_write,
    .release = myled_close,
};

//入口函数
static int __init demo_init(void)
{  
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0){
        printk("register chrdev is error\n");
        return -EIO;
    }
    printk("major = %d\n",major);
    //RCC_MP_AHB4ENSETR寄存器地址映射
    rcc_virt = ioremap(RCC_MP_AHB4ENSETR,4);
    if(rcc_virt == NULL){
        printk("rcc ioremap is error\n");
        return -EIO;
    }
    //GPIOE组寄存器地址映射
    gpioe_virt = ioremap(GPIOE,sizeof(gpio_t));
    if(gpioe_virt == NULL){
        printk("gpioe_virtr ioremap is error\n");
        return -EIO;
    }
    //GPIOF组寄存器地址映射
    gpiof_virt = ioremap(GPIOF,sizeof(gpio_t));
    if(gpiof_virt == NULL){
        printk("gpiof_virtr ioremap is error\n");
        return -EIO;
    }
    //LED1初始化
    //使能GPIOE组控制器   rcc_virt[4] = 1
    *rcc_virt |= (0x1 << 4);
    //设置PE10引脚为输出模式 MODER[21:20] = 01
    gpioe_virt->MODER &= (~(0x3 << 20));
    gpioe_virt->MODER |= (0x1 << 20);
    //设置PE10引脚输出低电平,LED1熄灭 ODR[10] = 0
    gpioe_virt->ODR &= (~(0x1 << 10));

    //LED2初始化
    //使能GPIOE组控制器   rcc_virt[5] = 1
    *rcc_virt |= (0x1 << 5);
    //设置PF10引脚为输出模式 MODER[21:20] = 01
    gpiof_virt->MODER &= (~(0x3 << 20));
    gpiof_virt->MODER |= (0x1 << 20);
    //设置PF10引脚输出低电平,LED2熄灭 ODR[10] = 0
    gpiof_virt->ODR &= (~(0x1 << 10));   

    //LED3初始化
    //设置PE8引脚为输出模式 MODER[17:16] = 01
    gpioe_virt->MODER &= (~(0x3 << 16));
    gpioe_virt->MODER |= (0x1 << 16);
    //设置PE8引脚输出低电平,LED3熄灭 ODR[8] = 0
    gpioe_virt->ODR &= (~(0x1 << 8));

    return 0; //函数的返回值
}

//出口函数
static void __exit demo_exit(void)
{
    iounmap(gpioe_virt);
    iounmap(gpiof_virt);
    iounmap(rcc_virt);
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
}

module_init(demo_init); //指定入口地址
module_exit(demo_exit); //指定出口地址
MODULE_LICENSE("GPL"); //许可证,遵循GPL协议

应用层程序:

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

int main(int argc,const char * argv[])
{
    int fd = -1;
    char buf[128] = "";
    fd = open("/dev/myled",O_RDWR); //打开
    if(fd == -1){
        perror("open is error\n");
        exit(1);
    }
    while(1)
    {
        buf[0] = LED1; //操作LED1灯
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        buf[1] = 1; //LED1灯点亮
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
        buf[1] = 0; //LED1灯熄灭
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
    
        buf[0] = LED2; //操作LED2灯
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        buf[1] = 1; //LED2灯点亮
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
        buf[1] = 0; //LED2灯熄灭
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
    
        buf[0] = LED3; //操作LED3灯
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        buf[1] = 1; //LED3灯点亮
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
        buf[1] = 0; //LED3灯熄灭
        write(fd,buf,sizeof(buf)); //将用户空间的数据,写入内核空间
        sleep(1);
    }

    close(fd);
    return 0;
}

 五、测试步骤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值