HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯 原创 精华

Mr_qzk
发布于 2021-12-13 15:18
9538浏览
7收藏

【本文正在参与优质创作者激励】
老规矩还是将最终希望跑出来的效果放出来。如下:
HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯-鸿蒙开发者社区

HDF驱动框架探路5:

前言

  • 想要深入了解HDF框架的话,应该绕不开linux驱动程序的掌握。由于是在看了韦东山老师对openharmony做的移植后,觉得linux驱动的内功还是必须要有的,所以本文章对比linux应用在imx6ull中点亮LED灯。所以先修炼修炼内功。

本文框架图

HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯-鸿蒙开发者社区

  • 本文的框架图是最近这段时间结合了对3516测试HDF框架,以及imx6ull上linux驱动程序的学习,所得出的,是基于目前社区中所用的比较多的几款板子和openharmony、linux对比所做的图,大佬们觉得这个图有任何问题,欢迎批评指出。

1.驱动程序

1.1 最简单的驱动程序逻辑

HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯-鸿蒙开发者社区

  • 1.1 如上图所示,首先有个驱动程序入口函数和出口函数分别是module_init(led_init),module_exit(led_exit);
  • 1.2 然后分别实现led_open和led_write这两个业务函数去填充file_operations结构体。
  • 1.3 最后把file_operations结构体放入register_chrdev函数进行注册,然后放入入口函数中。
  • 1.4 因为驱动程序的字符设备需要绑定IO设备去使用,所以在入口函数中调用class_create和device_create。

1.2 完成的实现代码如下:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <asm/io.h>

static int major;
static struct class *led_class;

/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;

// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;

//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;

static ssize_t led_write(struct file *filp, const char __user *buf,
			 size_t count, loff_t *ppos)
{
	char val;
	int ret;
	
	/* copy_from_user : get data from app */
	ret = copy_from_user(&val, buf, 1);

	/* to set gpio register: out 1/0 */
	if (val)
	{
		/* set gpio to let led on */
		*GPIO5_DR &= ~(1<<3);
	}
	else
	{
		/* set gpio to let led off */
		*GPIO5_DR |= (1<<3);
	}
	return 1;
}

static int led_open(struct inode *inode, struct file *filp)
{
	/* enable gpio5
	 * configure gpio5_io3 as gpio
	 * configure gpio5_io3 as output 
	 */
	*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &= ~0xf;
	*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |= 0x5;

	*GPIO5_GDIR |= (1<<3);
	
	return 0;
}

static struct file_operations led_fops = {
	.owner		= THIS_MODULE,
	.write		= led_write,
	.open		= led_open,
};

/* 入口函数 */
static int __init led_init(void)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	
	major = register_chrdev(0, "hello_led", &led_fops);

	/* ioremap */
	// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
	IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x02290000 + 0x14, 4);
	
	// GPIO5_GDIR 地址:0x020AC004
	GPIO5_GDIR = ioremap(0x020AC004, 4);
	
	//GPIO5_DR 地址:0x020AC000
	GPIO5_DR  = ioremap(0x020AC000, 4);

	led_class = class_create(THIS_MODULE, "helloled");
	device_create(led_class, NULL, MKDEV(major, 0), NULL, "helloled"); /* /dev/myled */
	
	return 0;
}

static void __exit led_exit(void)
{
	iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
	iounmap(GPIO5_GDIR);
	iounmap(GPIO5_DR);
	
	device_destroy(led_class, MKDEV(major, 0));
	class_destroy(led_class);
	
	unregister_chrdev(major, "hello_led");
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.

2.驱动程序测试部分

2.1 测试模块实现思路

在linux内核中注册相应的驱动模块后,通过glibc库函数提供的open,read,write接口访问驱动程序绑定驱动字符设备的IO文件就可以直接调用到对应的驱动程序了。

2.2 测试部分完成实现代码

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


// ledtest /dev/helloled on
// ledtest /dev/helloled off

int main(int argc, char **argv)
{
	int fd;
	char status = 0;
	
	if (argc != 3)
	{
		printf("Usage: %s <dev> <on|off>\n", argv[0]);
		printf("  eg: %s /dev/helloled on\n", argv[0]);
		printf("  eg: %s /dev/helloled off\n", argv[0]);
		return -1;
	}
	// open
	fd = open(argv[1], O_RDWR);
	if (fd < 0)
	{
		printf("can not open %s\n", argv[0]);
		return -1;
	}

	// write
	if (strcmp(argv[2], "on") == 0)
	{
		status = 1;
	}

	write(fd, &status, 1);
	return 0;	
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

3.编译

3.1 编译思路:

  • 1.首先需要将驱动程序编译成ko文件。
  • 2.将测试程序编译成可执行文件。

3.2 完成实现代码如下:


KERN_DIR = /home/qzk/code/imx6ullPro/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o ledtest ledtest.c 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f ledtest

obj-m	+= led_drv.o
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 上述代码需要的注意的,大家在使用时候需要换掉KERN_DIR中的值,换成大家自己的内核目录,因为编译时候会去这个目录下找头文件。

4.安装驱动进行测试

4.1 安装驱动思路

通过上述的步骤,大家会发现驱动程序编译好了放在了ubuntu系统中,我们的目标是需要将驱动程序安装进入imx6ull中,所以我们的目标是将驱动程序放入imx6ull中。这里的方案是:将网线插入电脑,然后串口连接imx6ull,先各自写死ip地址,目标是二者能够ping通,然后搭建nfs,这样就达到了imx6ull访问ubuntu下的驱动程序的目的

4.2 搭建好环境后进行安装驱动

通过insmod命令进行安装。
HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯-鸿蒙开发者社区

4.3 执行测试文件去点亮,熄灭灯

如下图:执行命令
HDF驱动框架探路(五):对比linux原生驱动开发在imx6ull板子点灯-鸿蒙开发者社区
没什么意外的话,这盏灯就在你的掌控之中了

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
imx6ull_led.zip 4.02K 20次下载
已于2021-12-13 15:18:05修改
7
收藏 7
回复
举报
7
1
7
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

3516的资料和文章还是太少了,感谢老师开路

1
回复
2021-12-14 10:14:36


回复
    相关推荐
    这个用户很懒,还没有个人简介
    觉得TA不错?点个关注精彩不错过
    帖子
    视频
    声望
    粉丝
    社区精华内容