Linux 下的任何外设驱动,最终都是要配置相应的硬件寄存器。所以 LED 灯驱动最 终也是对 I.MX6ULL 的 IO 口进行配置。I.MX6U-ALPHA 开发板上的 LED 连接到 I.MX6ULL 的 GPIO1_IO03 这个引脚上。
1.1.1 地址映射
#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));
}
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
static void __iomem* SW_MUX_GPIO1_IO03;
SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
void iounmap (volatile void __iomem *addr)
iounmap(SW_MUX_GPIO1_IO03);
u8 readb(const volatile void __iomem *addr)
u16 readw(const volatile void __iomem *addr)
u32 readl(const volatile void __iomem *addr)
void writeb(u8 value, volatile void __iomem *addr)
void writew(u16 value, volatile void __iomem *addr)
void writel(u32 value, volatile void __iomem *addr)
1.3.1 LED 灯驱动程序编写
在VSCode中 创建“led”工程。新建 led.c 文件,在led.c中输入如下内容
#include
#include
#include
#include
#include
#include
#include
#define LED_MAJOR 200
#define LED_NAME "led"
/*寄存器物理地址*/
#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_GDIR_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;
#define LEDOFF 0 /*关闭*/
#define LEDON 1 /*打开*/
/*LED 灯打开/关闭 */
static void led_switch(u8 sta)
{
u32 val = 0;
if (sta == LEDON)
{
val = readl(GPIO1_DR);
val &= ~(1 << 3); /*bit3清零,打开LED*/
writel(val, GPIO1_DR);
}
else if (sta == LEDOFF)
{
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,关闭LED*/
writel(val, GPIO1_DR);
}
}
static int led_open(struct inode *inode, struct file *filp)
{
printk("led_open\r\n");
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
printk("led_release\r\n");
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
int retvalue;
unsigned char databuf[1];
retvalue = copy_from_user(databuf, buf, count);
if (retvalue < 0)
{
printk("kernel write failed! \r\n");
return -EFAULT;
}
/*判断是开灯还是关灯*/
led_switch(databuf[0]);
return 0;
}
/*字符设备操作集*/
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.初始化LED,地址映射*/
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_GDIR_BASE, 4);
/*2.初始化*/
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26); /*先清除以前的配置bit26,27*/
val |= 3 << 26; /*bit26,27置1*/
writel(val, IMX6U_CCM_CCGR1);
writel(0x5, SW_MUX_GPIO1_IO03); /*设置复用*/
writel(0x10B0, SW_PAD_GPIO1_IO03); /*设置电气属性*/
val = readl(GPIO1_GDIR);
val |= 1 << 3; /*bit3置1,设置为输出*/
writel(val, GPIO1_GDIR);
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,关闭LED*/
writel(val, GPIO1_DR);
/*2.注册字符设备*/
ret = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
if (ret < 0)
{
printk("register chrdev failed!\r\n");
return -EIO;
}
printk("led_init\r\n");
return 0;
}
/*出口*/
static void __exit led_exit(void)
{
unsigned int val = 0;
val = readl(GPIO1_DR);
val |= 1 << 3; /*bit3置1,打开LED*/
writel(val, GPIO1_DR);
/*1.取消地址映射*/
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);
printk("led_exit\r\n");
}
/*注册驱动加载和卸载函数*/
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("supersmart");
1.3.2 编写测试 APP
新建 ledApp.c 文件,在里面输入如下内容:
#include
#include
#include
#include
#include
#include
#include
/*
*argc:应用程序参数个数
* argv[]:具体的参数内容,字符串形式
* ./ledAPP <0:1> 0 关灯,1 开灯
* ./ledAPP /dev/led 0 关灯
* ./ledAPP /dev/led 1 开灯
*/
#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!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("file %s open failed!\r\n", filename);
return -1;
}
databuf[0] = atoi(argv[2]); /*将字符转化为数字*/
retvalue = write(fd, databuf, sizeof(databuf));
if (retvalue < 0)
{
printf("LED Control Failed ! \r\n");
close(fd);
return -1;
}
close(fd);
return 0;
}
1.4.1 编译驱动程序和测试 APP
编写 Makefile 文件
KERNELDIR := /home/znn/linux/IMX6ULL/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga
CURRENT_PAHT := $(shell pwd)
obj-m := led.o
build :kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) clean
输入如下命令编译出名为“led.ko”的驱动模块文件
将 led.ko 和 ledAPP 复制到 rootfs/lib/modules/4.1.15 目录中,命令如下:
1.4.2 运行测试
加载 led.ko 驱动模块:
创建“/dev/led”设备节点:
打开 LED 灯
./ledAPP /dev/led 1
关闭 LED 灯
./ledAPP /dev/led 0
rmmod led.ko
总结:通过以上内容学习了Linux下LED灯驱动的编写,MMU、内存映射、ioremap、iounmap、I/O 内存访问函数等内容。