012——LED模块驱动开发(基于I.MX6uLL)

目录

一、 硬件原理图

二、 驱动程序

三、 应用程序

四、 Makefile

五、操作


一、 硬件原理图

        又是非常经典的点灯环节 ,每次学新语言第一步都是hello world,拿到新板子或者学习新的操作系统,第一步就是点灯。

        LED 的驱动方式,常见的有四种。
① 使用引脚输出 3.3V 点亮 LED,输出 0V 熄灭 LED。
② 使用引脚拉低到 0V 点亮 LED,输出 3.3V 熄灭 LED。
有的芯片为了省电等原因,其引脚驱动能力不足,这时可以使用三极管驱动。
③ 使用引脚输出 1.2V 点亮 LED,输出 0V 熄灭 LED。
④ 使用引脚输出 0V 点亮 LED,输出 1.2V 熄灭 LED。
由此,主芯片引脚输出高电平/低电平,即可改变 LED 状态,而无需关注 GPIO
引脚输出的是 3.3V 还是 1.2V。所以简称输出 1 或 0:
⚫ 逻辑 1-->高电平
⚫ 逻辑 0-->低电平
        SOC级别的芯片通常电压都比较低,像我们之前用的exynos4412他是1.8V的,我们的i.MX6ULL则是可以做到更低的逻辑1,1.2V。现在最新的技术好像是0.8V的。电压降低的好处就是我们的功耗大幅减小。MCU为什么不降低呢,因为它是控制器需要高电压的驱动环境,所以一般都是3.3V和5V的。

这是板子上的LED的原理图

6ull的GPIO是这样描述的

看上面的原理图我找到了

        第五组GPIO的第三个也就是4*32+4-1 = 131

        每组GPIO有32个,我们0开始所以就是128+3 ,131就是我们的GPIO号

        知道这个就差不多可以写驱动程序了,这就是由操作系统和无操作系统的区别,裸机开发的话我们还要找到其它的寄存器,上面说到的那八个都要找到,但是因为GPIO是通用外设,操作系统已经处理过了,所以我们用的话就会很轻松,甚至可以直接给dev下的GPIO设备写值来控制。

然后我们就可以写代码了

二、 驱动程序

#include "asm-generic/errno-base.h"
#include "asm-generic/gpio.h"
#include "asm/uaccess.h"
#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

struct gpio_desc{
	int gpio;
	int irq;
    char *name;
    int key;
	struct timer_list key_timer;
} ;

static struct gpio_desc gpios[2] = {
    {131, 0, "led0", },
    //{132, 0, "led1", },
};

/* 主设备号                                                                 */
static int major = 0;
static struct class *gpio_class;


/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t gpio_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	char tmp_buf[2];
	int err;
    int count = sizeof(gpios)/sizeof(gpios[0]);

	if (size != 2)
		return -EINVAL;

	err = copy_from_user(tmp_buf, buf, 1);

	if (tmp_buf[0] >= count)
		return -EINVAL;

	tmp_buf[1] = gpio_get_value(gpios[tmp_buf[0]].gpio);

	err = copy_to_user(buf, tmp_buf, 2);
	
	return 2;
}

static ssize_t gpio_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    unsigned char ker_buf[2];
    int err;

    if (size != 2)
        return -EINVAL;

    err = copy_from_user(ker_buf, buf, size);
    
    if (ker_buf[0] >= sizeof(gpios)/sizeof(gpios[0]))
        return -EINVAL;

    gpio_set_value(gpios[ker_buf[0]].gpio, ker_buf[1]);
    return 2;    
}



/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {
	.owner	 = THIS_MODULE,
	.read    = gpio_drv_read,
	.write   = gpio_drv_write,
};


/* 在入口函数 */
static int __init gpio_drv_init(void)
{
    int err;
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	
	for (i = 0; i < count; i++)
	{		
		/* set pin as output */
		err = gpio_request(gpios[i].gpio, gpios[i].name);
		if (err < 0) {
			printk("can not request gpio %s %d\n", gpios[i].name, gpios[i].gpio);
			return -ENODEV;
		}
		
		gpio_direction_output(gpios[i].gpio, 1);
	}

	/* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_led", &gpio_key_drv);  /* /dev/gpio_desc */

	gpio_class = class_create(THIS_MODULE, "100ask_led_class");
	if (IS_ERR(gpio_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_led_class");
		return PTR_ERR(gpio_class);
	}

	device_create(gpio_class, NULL, MKDEV(major, 0), NULL, "100ask_led"); /* /dev/100ask_gpio */
	
	return err;
}

/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 */
static void __exit gpio_drv_exit(void)
{
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	device_destroy(gpio_class, MKDEV(major, 0));
	class_destroy(gpio_class);
	unregister_chrdev(major, "100ask_led");

	for (i = 0; i < count; i++)
	{
		gpio_free(gpios[i].gpio);		
	}
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(gpio_drv_init);
module_exit(gpio_drv_exit);

MODULE_LICENSE("GPL");


三、 应用程序


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

static int fd;


//int led_on(int which);
//int led_off(int which);
//int led_status(int which);

/*
 * ./led_test <0|1|2|..>  on 
 * ./led_test <0|1|2|..>  off
 * ./led_test <0|1|2|..>
 */
int main(int argc, char **argv)
{
	int ret;
	char buf[2];

	int i;
	
	/* 1. 判断参数 */
	if (argc < 2) 
	{
		printf("Usage: %s <0|1|2|...> [on | off]\n", argv[0]);
		return -1;
	}


	/* 2. 打开文件 */
	fd = open("/dev/100ask_led", O_RDWR);
	if (fd == -1)
	{
		printf("can not open file /dev/100ask_led\n");
		return -1;
	}

	if (argc == 3)
	{
		/* write */
		buf[0] = strtol(argv[1], NULL, 0);

		if (strcmp(argv[2], "on") == 0)
			buf[1] = 0;
		else
			buf[1] = 1;
		
		ret = write(fd, buf, 2);
	}
	else
	{
		buf[0] = strtol(argv[1], NULL, 0);
		ret = read(fd, buf, 2);
		if (ret == 2)
		{
			printf("led %d status is %s\n", buf[0], buf[1] == 0 ? "on" : "off");
		}
	}
	
	close(fd);
	
	return 0;
}


四、 Makefile

韦东山老师的makefile写的有点太潦草了,我们来优化一下

CC := $(CROSS_COMPILE)gcc
FILE_NAME = led_test
DRIVER_NAME = led_drv
# 定义NFS根文件系统目录  
FS_FILE = ~/nfs_rootfs

KERN_DIR =  /home/book/program/100ask_imx6ull_mini-sdk/Linux-4.9.88 # 板子所用内核源码的目录

# all:
# 	make -C $(KERN_DIR) M=`pwd` modules 
# 	$(CROSS_COMPILE)gcc -o led_test led_test.c
# 默认目标  
all:  
	@echo "Starting build process..."  
	@echo "Building kernel modules..."  
	make -C $(KERN_DIR) M=$(PWD) modules  
	@echo "Building $(FILE_NAME) test program..."  
	$(CC) -o $(FILE_NAME) $(FILE_NAME).c  

# 安装目标  
install:  
	@echo "Installing $(DRIVER_NAME).ko to $(FS_FILE)..."  
	cp ./$(DRIVER_NAME).ko $(FS_FILE)  
	@echo "$(DRIVER_NAME).ko installed."  
	@echo "Installing $(FILE_NAME) to $(FS_FILE)..."  
	cp ./$(FILE_NAME) $(FS_FILE)  
	@echo "$(FILE_NAME) installed."  

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

# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o

obj-m += led_drv.o
# 声明伪目标  
.PHONY: all clean install

五、操作

每次都要重新配置网络很难受所以我这面写了个脚本上机自动配置ip并且挂载nfs 

#!/bin/bash  
  
# 定义变量  
NFS_SERVER="192.168.5.10"  
NFS_SHARE="/home/book/nfs_rootfs"  
IPADDR="192.168.5.110"
MOUNT_POINT="/mnt"
INTERFACE="eth0"  
# 设置本机IP
sleep 1
ifconfig $INTERFACE $IPADDR
sleep 1

# 测试与NFS服务器的连通性  
ping -c 1 $NFS_SERVER > /dev/null 2>&1  
if [ $? -eq 0 ]; then  
    echo "NFS服务器 $NFS_SERVER 连通性正常"  
else  
    echo "无法与NFS服务器 $NFS_SERVER 建立连接"  
    exit 1  
fi  
  
# 检查挂载点是否存在,如果不存在则创建  
if [ ! -d "$MOUNT_POINT" ]; then  
    mkdir -p "$MOUNT_POINT"  
fi  
  
# 尝试挂载NFS共享  
mount -t nfs -o nolock,vers=3 $NFS_SERVER:$NFS_SHARE $MOUNT_POINT  
if [ $? -eq 0 ]; then  
    echo "NFS共享已成功挂载到 $MOUNT_POINT"  
else  
    echo "无法挂载NFS共享到 $MOUNT_POINT"  
    exit 1  
fi

最后我们上传一下

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HD2010操作说明(HD2010 operating instructions) ⅰ.软件安装(Installed software) 双击光盘HD2010 V2.0目录下的安装文件图标 ,将控制系统安装到个人电脑.如下图2-1 (Open Profile HD2010 V2.0 in the compact disk,Dblclick Installed the HD2010 V2.0 in your personal computer.As shown in Figure 2-1) 图(Figure)2-1 点击确定后进入下一步如图2-2 (Click确定intro- next step.As shown in Figure 2-2 ) 图(Figure)2-2 点击下一步后进入下一步(英文环境)在中文环境直接点击下一步完成安装)如图2-3 2-4 2-5 2-6 2-7 (Click next intro- next step.As shown in Figure 2-3 2-4 2-5 2-6 2-7) 图(Figure)2-3 注意:发送的文件保存在安装目录下的ProjFile文件夹里,点击下一步完成安装 (Announcements:The file what you send saved in the ProjFile in path of Install.And click next to install and finish) 图(Figure)2-4 图(Figure)2-5 图(Figure)2-6 安装最后一步如图2-7 (The final step of install. As shown in Figure 2-7) 图(Figure)2-7 点击完成打开软件主界面如图2-8 (Click finish open main interface of the software. As shown in Figure 2-8) ⅱ.软件设置(Setting software) ⅱ.ⅰ主界面如图2-8(Main interface.As shown in Figure 2-8) 图(Figure)2-8 ⅱ.ⅱ软件属性(Software properties) 1.文件菜单(File menu) a.新建--新建一个新的显示屏(New—Create a new screen) b.打开--打开一个显示屏(Open—Open a exist screen) c.保存--保存建立的文件(Save—Save file) d.另存为--保存副本(Save as—save a copy) e.导出.hds--导出.hds文件,用于u盘读取文件 (Export.hds—Export.hds file,for usb reading) f.退出--退出软件(Exit—Exit the software) 2.设置菜单(Settings menu) a.屏参设置--设置显示屏属性 (Screen settings—Display Screen configuration attributes) b.通信设置--通信端口/方式设置 (Communication settings—Set Communication prot and fashion) c.系统设置--配置系统默认项 (System settings—Set the acquice properties of software) 3.操作菜单(Operate menu) a.发送项目--发送节目(Send project—send the programmes) b.导出到U盘--把节目导入到U盘 (Export to U disk—send the programme to your U disk) c.时间设置--时间校对 (Time setting—get the right time im your computer) d.亮度设置--选择亮度模式 (Luminance setting—Choice the module of Luminance) e.固件更新--固件升级(Update Firmware—Update your controller Firmware) 4.语言菜单(Language menu) a.简体中文(Simplified Chinese) b.繁体中文(Chinese Traditional) c.英语(English) ⅱ.ⅲ节目编辑(Edit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇努力学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值