100ask_imx6ull开发板led驱动的实现

声明:本例程的所有源码参考自正点原子的左神的源码,在他的基础上,修改成适用于100ask_imx6ull的开发板的驱动,感谢左神提供的开源精神,特此声明!

我的其它文章也是在左神的代码的基础上修改成适用于韦东山老师的板子的代码,其它文章就不再一一说明,如有问题请给我留言或者私信我,谢谢!

一、开发环境

1、100ask_imx6ull开发板
2、mint19.1开发环境
3、适用windows下的NFS进行挂载测试

二、硬件原理

在这里插入图片描述

LED的管脚为GPIO5_IO03。
当GPIO5_IO03为高电平时,LED熄灭;当GPIO5_IO03为低电平时,LED被点亮。

三、驱动测试源码

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>

/***************************************************************
文件名		: led.c
版本	   	: V1.0
描述	   	: LED驱动文件。
***************************************************************/
#define LED_MAJOR		200		/* 主设备号 */
#define LED_NAME		"led" 	/* 设备名字 */

#define LEDOFF 	0				/* 关灯 */
#define LEDON 	1				/* 开灯 */
 
/* 寄存器物理地址 */
#define CCM_CCGR1_BASE				(0X020C406C)	
#define SW_MUX_GPIO5_IO03_BASE		(0X02290014)
#define SW_PAD_GPIO5_IO03_BASE		(0X020E02B4)
#define GPIO5_DR_BASE				(0X020AC000)
#define GPIO5_GDIR_BASE				(0X020AC004)

/* 映射后的寄存器虚拟地址指针 */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO5_IO03;
static void __iomem *SW_PAD_GPIO5_IO03;
static void __iomem *GPIO5_DR;
static void __iomem *GPIO5_GDIR;

/*
 * @description		: LED打开/关闭
 * @param - sta 	: LEDON(0) 打开LED,LEDOFF(1) 关闭LED
 * @return 			: 无
 */
void led_switch(u8 sta)
{
	u32 val = 0;
	if(sta == LEDON) {
		val = readl(GPIO5_DR);
		val &= ~(1 << 3);	
		writel(val, GPIO5_DR);
	}else if(sta == LEDOFF) {
		val = readl(GPIO5_DR);
		val|= (1 << 3);	
		writel(val, GPIO5_DR);
	}	

	return ;
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件,file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int led_open(struct inode *inode, struct file *filp)
{
	return 0;
}

/*
 * @description		: 从设备读取数据 
 * @param - filp 	: 要打开的设备文件(文件描述符)
 * @param - buf 	: 返回给用户空间的数据缓冲区
 * @param - cnt 	: 要读取的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 读取的字节数,如果为负值,表示读取失败
 */
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	return 0;
}

/*
 * @description		: 向设备写数据 
 * @param - filp 	: 设备文件,表示打开的文件描述符
 * @param - buf 	: 要写给设备写入的数据
 * @param - cnt 	: 要写入的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 写入的字节数,如果为负值,表示写入失败
 */
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);		/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
	}
	return 0;
}

/*
 * @description		: 关闭/释放设备
 * @param - filp 	: 要关闭的设备文件(文件描述符)
 * @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_GPIO5_IO03 = ioremap(SW_MUX_GPIO5_IO03_BASE, 4);
  	SW_PAD_GPIO5_IO03 = ioremap(SW_PAD_GPIO5_IO03_BASE, 4);
	GPIO5_DR = ioremap(GPIO5_DR_BASE, 4);
	GPIO5_GDIR = ioremap(GPIO5_GDIR_BASE, 4);

	/* 2、使能GPIO5时钟 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 30);	/* 清除以前的设置 */
	val |= (3 << 30);	/* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 3、设置GPIO5_IO03的复用功能,将其复用为
	 *    GPIO5_IO03,最后设置IO属性。
	 */
	writel(5, SW_MUX_GPIO5_IO03);
	
	/*寄存器SW_PAD_GPIO5_IO03设置IO属性
	 *bit 16:0 HYS关闭
	 *bit [15:14]: 00 默认下拉
     *bit [13]: 0 kepper功能
     *bit [12]: 1 pull/keeper使能
     *bit [11]: 0 关闭开路输出
     *bit [7:6]: 10 速度100Mhz
     *bit [5:3]: 110 R0/6驱动能力
     *bit [0]: 0 低转换率
	 */
	writel(0x10B0, SW_PAD_GPIO5_IO03);

	/* 4、设置GPIO5_IO03为输出功能 */
	val = readl(GPIO5_GDIR);
	val &= ~(1 << 3);	/* 清除以前的设置 */
	val |= (1 << 3);	/* 设置为输出 */
	writel(val, GPIO5_GDIR);

	/* 5、默认关闭LED */
	val = readl(GPIO5_DR);
	val |= (1 << 3);	
	writel(val, GPIO5_DR);

	/* 6、注册字符设备驱动 */
	retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
	if(retvalue < 0){
		printk("register chrdev failed!\r\n");
		return -EIO;
	}
	
	return 0;
}

/*
 * @description	: 驱动出口函数
 * @param 		: 无
 * @return 		: 无
 */
static void __exit led_exit(void)
{
	/* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO5_IO03);
	iounmap(SW_PAD_GPIO5_IO03);
	iounmap(GPIO5_DR);
	iounmap(GPIO5_GDIR);

	/* 注销字符设备驱动 */
	unregister_chrdev(LED_MAJOR, LED_NAME);

	return ;
}

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("seedling");


四、应用程序测试源码

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>
/***************************************************************
文件名		: ledApp.c
版本	   	: V1.0
描述	   	: 测试APP。
其他	   	: 无
使用方法	 :./ledtest /dev/led  0 关闭LED
		     ./ledtest /dev/led  1 打开LED		
***************************************************************/

#define LEDOFF 	0
#define LEDON 	1

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
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];

	/* 打开led驱动 */
	fd = open(filename, O_RDWR);
	if(fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	databuf[0] = atoi(argv[2]);	/* 要执行的操作:打开或关闭 */

	/* 向/dev/led文件写入数据 */
	retvalue = write(fd, databuf, sizeof(databuf));
	if(retvalue < 0){
		printf("LED Control Failed!\r\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/mint/imx6/100ask_imx6ull-sdk/Linux-4.9.88/
CURRENT_PATH := $(shell pwd)
obj-m := led.o
APP_NAME := ledApp

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules

clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
	rm -rf $(APP_NAME)
install:
	arm-linux-gnueabihf-gcc $(APP_NAME).c -o  $(APP_NAME)
	cp $(APP_NAME)  *.ko  ~/nfs

六、编译

make
make install

七、测试

1、现将系统使用的心跳灯关掉,关掉命令请参考我的相关文章。

2、加载驱动

	dmesg -c
	insmod led.ko
	dmesg -c

在这里插入图片描述

3、手动创建设备节点

mknod /dev/led c 200 0

在这里插入图片描述

4、运行应用程序

# 关灯
./ledApp /dev/led 0
# 开灯
./ledApp /dev/led 1


八、 增加led灯闪烁的测试

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
/***************************************************************
文件名		: ledApp.c
版本	   	: V1.0
描述	   	: 测试APP。
其他	   	: 无
	
***************************************************************/
#define FILE_APTH 	"/dev/led"

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd, retvalue;
	unsigned char led_on = 0;
	unsigned char led_off = 1;	

	/* 打开led驱动 */
	fd = open(FILE_APTH, O_RDWR);
	if(fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

while(1)
{
			/* 向/dev/led文件写入数据 */
			retvalue = write(fd, &led_on, sizeof(led_on));
			if(retvalue < 0){
				printf("LED Control Failed!\r\n");
				close(fd);
				return -1;
			}
			
			usleep(1000*30);
			
			retvalue = write(fd, &led_off, sizeof(led_off));
			if(retvalue < 0){
				printf("LED Control Failed!\r\n");
				close(fd);
				return -1;
			}
			
			usleep(1000*30);

}

	retvalue = close(fd); /* 关闭文件 */
	if(retvalue < 0){
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}


运行次=此应用程序,led会以30ms的频率闪烁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值