目录
一、地址映射
二、实验程序编写
1、注册驱动入口和出口函数
2、编写驱动入口和出口函数
3、驱动完整代码
4、应用完整代码
总结
一、地址映射
1、在裸机实验的时候操作LED等外设实际上是操作相应外设的寄存器。
2、linux下驱动开发最终要操作的也是寄存器,然而在Linux环境下不能直接对寄存器的物理地址进行操作。比如LED寄存器的地址为0x01010101,在裸机开发环境下可以直接对该地址进行赋值,而Linux下行,因为使能了MMU,开启了内存映射。
3、在Linux里面操作的都是虚拟地址,因此要操作LED需要先得到LED寄存器对应的虚拟地址。获得寄存器对应虚拟地址的函数为ioremap。
#define ioremap(cookie,size) __arm_ioremap((cookie), (size),
MT_DEVICE)void __iomem * __arm_ioremap(phys_addr_t phys_addr, size_t size,
unsigned int mtype){return arch_ioremap_caller(phys_addr, size, mtype,
__builtin_return_address(0));}
二、实验程序编写
1、注册驱动入口和出口函数
2、编写驱动入口和出口函数
/*驱动入口函数*/
static int __init led_init(void){int ret = 0;unsigned int val = 0;/*1、地址映射*/IMX6ULL_CCM_CCGR1 = ioremap(CCM_CCGR1, 4);SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE,4 );/*2、初始化*//*开启时钟*/val = readl(IMX6ULL_CCM_CCGR1);val &= ~(3 << 26); //清除先前的26,27位val |= (3 << 26); //26,27位置1writel(val, IMX6ULL_CCM_CCGR1);/*配置GPIO1_IO03*/writel(0x5, SW_MUX_GPIO1_IO03); //设置复用writel(0x10B0, SW_PAD_GPIO1_IO03); //设置电气属性val = readl(GPIO1_GDIR);val |= (1 << 3);writel(val, GPIO1_GDIR);val = readl(GPIO1_DR);val &= ~(1 << 3);writel(val, GPIO1_DR);/*注册字符设备*/ret = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);if(ret < 0){printk("register chardev failed\r\n");return -EIO;}printk("led_init\r\n");return 0;
}
在驱动入口函数中进行寄存器物理地址的重映射,以及初始化,最后注册了LED驱动设备。
/*驱动出口函数*/
static void __exit led_exit(void){/*关闭LED*/unsigned int val = 0;val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);/*取消地址映射*/iounmap(IMX6ULL_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);/*注销字符设备*/unregister_chrdev(LED_MAJOR, LED_NAME);printk("led_exit\r\n");
}
在驱动出口函数取消地址重映射,同时注销了该LED驱动。
3、驱动完整代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>#define LED_MAJOR 200 /*LED主设备号*/
#define LED_NAME "led"#define LEDON 1 /*开灯*/
#define LEDOFF 0 /*关灯*//*寄存器物理地址*/
#define CCM_CCGR1 (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)/*映射后虚拟地址指针*/
static void __iomem *IMX6ULL_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;static void LED_switch(u8 sta){u32 val = 0;if(sta == LEDON){val = readl(GPIO1_DR);val &= ~(1 << 3);writel(val, GPIO1_DR);}else if(sta == LEDOFF){val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);}
}static int led_open(struct inode *inode, struct file *filp){return 0;}static ssize_t led_write(struct file *filp, const char __user *buf,size_t count, loff_t *ppos)
{int retvalue = 0;unsigned char databuf[1];retvalue = copy_from_user(databuf, buf, count);if(retvalue < 0){printk("kernel write failed");return -EFAULT;}/*开灯还是关灯*/LED_switch(databuf[0]);return 0;
}static int led_release(struct inode *inode, struct file *filp){return 0;
}/*led设备操作集*/
static const struct file_operations led_fops = {.owner = THIS_MODULE,.write = led_write,.open = led_open,.release= led_release,
};/*驱动入口函数*/
static int __init led_init(void){int ret = 0;unsigned int val = 0;/*1、地址映射*/IMX6ULL_CCM_CCGR1 = ioremap(CCM_CCGR1, 4);SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE,4 );/*2、初始化*//*开启时钟*/val = readl(IMX6ULL_CCM_CCGR1);val &= ~(3 << 26); //清除先前的26,27位val |= (3 << 26); //26,27位置1writel(val, IMX6ULL_CCM_CCGR1);/*配置GPIO1_IO03*/writel(0x5, SW_MUX_GPIO1_IO03); //设置复用writel(0x10B0, SW_PAD_GPIO1_IO03); //设置电气属性val = readl(GPIO1_GDIR);val |= (1 << 3);writel(val, GPIO1_GDIR);val = readl(GPIO1_DR);val &= ~(1 << 3);writel(val, GPIO1_DR);/*注册字符设备*/ret = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);if(ret < 0){printk("register chardev failed\r\n");return -EIO;}printk("led_init\r\n");return 0;
}/*驱动出口函数*/
static void __exit led_exit(void){/*关闭LED*/unsigned int val = 0;val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);/*取消地址映射*/iounmap(IMX6ULL_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);/*注销字符设备*/unregister_chrdev(LED_MAJOR, LED_NAME);printk("led_exit\r\n");
}/*模块入口和出口函数*/
module_init(led_init);
module_exit(led_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZYC");
4、应用完整代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>/*
*argc:应用程序参数个数
*argv[]文件参数名,字符串形式
*./chardevbaseApp <filename> <0:1> 0表示关灯,1表示开灯
*./chardevbaseApp /dev/led 0 关灯
*./chardevbaseApp /dev/led 1 开灯
*/
#define LEDOFF 0
#define LEDON 1int main(int argc, char *argv[])
{int fd, retval;char *filename;unsigned char databuf[1];if(argc != 3){printf("Error use\r\n");return -1;}/*保存设备文件名*/filename = argv[1];fd = open(filename, O_RDWR);if(fd < 0){printf("open file failed \r\n");return -1;}/*将字符转化为数字*/databuf[0] = atoi(argv[2]);retval = write(fd, databuf, sizeof(databuf));if(retval < 0){printf("led write failed\r\n");close(fd);return -1;}close(fd);return 0;
}
总结
LED驱动编写是裸机LED的升级版,需要先配置各寄存器地址的重映射,再和裸机配置一样对各寄存器赋值,最后搭配字符型驱动的框架即可。