1. ioremap 函数
ioremap 函数用于获取指定物理地址空间对应的虚拟地址空间。
2. iounmap
I/O内存访问函数
1.读操作函数
u8 readb(const volatile void __iomem *addr)
u16 readw(const volatile void __iomem *addr)
u32 readl(const volatile void __iomem *addr)
readb、readw 和 readl 这三个函数分别对应 8bit、16bit 和 32bit 读操作,参数 addr 就是要读取写内存地址,返回值就是读取到的数据。
2. 写操作函数
void writeb(u8 value, volatile void __iomem *addr)
void writew(u16 value, volatile void __iomem *addr)
void writel(u32 value, volatile void __iomem *addr)
writeb、writew 和 writel 这三个函数分别对应 8bit、16bit 和 32bit 写操作,参数 value 是要写入的数值,addr 是要写入的地址
led.c
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define LED_MAJOR 200
#define LED_NAME "led"
#define LEDOFF 0
#define LEDON 1
/* 寄存器物理地址 */
#define CCM_CCGR1_BASE (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIO_BASE (0X0209C004)
// 映射后的寄存器虚拟地址指针
static void __iomem * IMX6U_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;
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_read(struct file *filp, char __user *buf,size_t cnt, loff_t *offt)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf,
size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char ledstat;
retvalue = __copy_from_user(databuf, buf, cnt);
if (retvalue < 0){
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = databuf[0];
if (ledstat == LEDON){
led_switch(LEDON);
}else if (ledstat == LEDOFF){
led_switch(LEDOFF);
}
return 0;
}
static int led_release (struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};
static int __init led_init(void)
{
int retvalue = 0;
u32 val = 0;
/* 初始化LED */
// 1 寄存器地址映射
IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 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_DR_BASE, 4);
//2. 使能gpio1时钟
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26); /* 清除以前的设置 */
val |= (3 << 26); /* 设置新值 */
writel(val, IMX6U_CCM_CCGR1);
// 3 设置gpio1_io03 复用功能。
writel(5, SW_MUX_GPIO1_IO03);
writel(0x10b0, SW_PAD_GPIO1_IO03);
// 4. 设置GPIO1_IO03 为输出功能
val = readl(GPIO1_GDIR);
val &= ~(1<<3);
val |= (1<<3);
writel(val, GPIO1_GDIR);
//5 默认关闭
val = readl(GPIO1_DR);
val |= (1<<3);
writel(val, GPIO1_DR);
// 6 注册字符设备驱动
retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
if (retvalue < 0){
printk("registser chrdev faild\n");
return -EIO;
}
return 0;
}
static void __exit led_exit(void)
{
iounmap(IMX6U_CCM_CCGR1);
iounmap(SW_MUX_GPIO1_IO03);
iounmap(SW_PAD_GPIO1_IO03);
iounmap(GPIO1_DR);
iounmap(GPIO1_GDIR);
unregister_chrdev(LED_MAJOR, LED_NAME);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zzk");
ledApp.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#define LEDOFF 0
#define LEDON 1
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if (argc != 3){
printf("Error Usage!\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd < 0 ){
printf("file %s open failed!\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]);
retvalue = write(fd, databuf, sizeof(databuf));
if (retvalue < 0){
printf("Led control failed!\n");
close(fd);
return -1;
}
retvalue = close(fd);
if (retvalue < 0){
printf("file %s close failed \r\n", argv[1]);
return -1;
}
return 0;
}
Makefile
KERNELDIR := /home/zzk/linux/IMX6ULL/linux/temp/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
CURRENT_PATH := $(shell pwd)
NFS_PATH := /home/zzk/linux/nfs/rootfs/lib/modules/4.1.15
obj-m := led.o
obj-ko := led.ko
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
arm-linux-gnueabihf-gcc ledApp.c -o ledApp
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
install:
cp $(obj-ko) ledApp $(NFS_PATH)
验证
- depmod
- modprode led.ko
- mknod /dev/led c 200 0
- ./ledApp /dev/led 1 //开 ; ./ledApp /dev/led 0 //关
- rmmod
版权声明:本文为u012335044原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。