linux驱动入门学习第四天----韦东山驱动课后作业(点亮多个LED灯,以及读取引脚状态)
前几次博文中的驱动程序都只是能够点亮一个LED灯,今天我们来点亮两个LED灯(同一时间操作一个灯),除了板载GPIO5_3的LED灯之外,我使用扩展板外接了LED灯(低电平点亮),外接LED使用的引脚是GPIO4_19。除了点灯之外,新加入了引脚读取功能,想要使用这个功能时我将外接的LED换成了三针火焰传感器来检测GPIO4_19的状态。 应用程序通过以下方式来操作硬件:./ledtest /de
目录
CCM_CCGR1:物理地址(需要映射成虚拟地址):0x20C406C
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3:(GPIO5_3IO复用选择寄存器)
IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC(GPIO4_19IO复用选择寄存器)
一.功能描述
前几次博文中的驱动程序都只是能够点亮一个LED灯,今天我们来点亮两个LED灯(同一时间操作一个灯),除了板载GPIO5_3的LED灯之外,我使用扩展板外接了LED灯(低电平点亮),外接LED使用的引脚是GPIO4_19。
除了点灯之外,新加入了引脚读取功能,想要使用这个功能时我将外接的LED换成了三针火焰传感器来检测GPIO4_19的状态。
应用程序通过以下方式来操作硬件:
./ledtest /dev/ledx(0/1) -w(-r) on/off
ledtest:可执行程序
/dev/ledx:设备名称(根据自己定义的设备进行修改)
-w/-r:写入电平,或者是读取引脚状态
on/off:只适用于-w(读写状态),用于操作GPIO输出电平
二.硬件以及功能概述
翻阅硬件原理图可以看到硬件原理图:
LED低电平点亮
三针火焰传感器:
默认输出高电平,检测到有火焰存在时输出低电平
三.寄存器配置
通过 前面博文了解到:配置一个IO过程如下
时钟:
CCM_CCGR1:
CCM_CCGR1:物理地址(需要映射成虚拟地址):0x20C406C
注:imx6ull_pro的GPIO5默认使能,可以选择不配置
由下图可知应该将控制时钟的两位配置成:11
CCM_CCGR3:GPIO4时钟
CCM_CCGR3:物理地址(需要映射成虚拟地址):0x20C4074
配置同上,将控制时钟的两位配置成:11
IO复用选择:
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3:(GPIO5_3IO复用选择寄存器)
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3:物理地址(需要映射成虚拟地址):0x2290014
配置方式:将低四位首先进行全部清零,之后将后三位设置成101,作为GPIO5_3使用
IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC(GPIO4_19IO复用选择寄存器)
IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC:物理地址(需要映射成虚拟地址):0x20E01DC
配置方式:将低五位首先进行全部清零,之后四位设置成0101,作为GPIO4_19使用
剩余寄存器:
GPIO5_GDIR :0x20AC004(GPIO5输出方向寄存器)
GPIO5_PSR :0x20AC008(输出读取电平寄存器)
GPIO5_DR:0x20AC000(输出电平控制器)
GPIO4_GDIR:0x20A8004
GPIO4_DR:0x20A8000
GPIO4_PSR:0x20A8008
三.上层应用代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
/*
* ./ledtest /dev/100ask_led0 on
* ./ledtest /dev/100ask_led0 off
*/
int main(int argc, char **argv)
{
int fd;
char status;
/* 1. 判断参数 */
// if (argc != 4)
// {
// printf("Usage: %s <dev> <on | off>\n", argv[0]);
// return -1;
// }
/* 2. 打开文件 */
fd = open(argv[1], O_RDWR);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
/* 3. 写文件 */
if (0 == strcmp(argv[2], "-r"))
{
read(fd, &status, 1);
printf("led status is:%d\n", status); // 添加换行符
}
else if (0 == strcmp(argv[2], "-w"))
{
if ((strcmp("on", argv[3]) == 0))
{
status = 0;
write(fd, &status, 1);
}
else if (strcmp("off", argv[3]) == 0) // 添加右括号
{
status = 1;
printf("off:%d\n", write(fd, &status, 1));
}
}
close(fd);
return 0;
}
四.驱动代码(上层)
#include <linux/module.h>
#include "led_opr.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/io.h>
/*1.确定主设备号,*/
static int major;
struct opr_ctl *opr_led;
struct class *led_class;
static ssize_t led_read(struct file * filep, char __user * outbuf, size_t n, loff_t * ppos)
{
int err;
char status;
struct inode *inode = file_inode(filep);
int minor = iminor(inode);
status = opr_led->read(minor);
err = copy_to_user(outbuf, &status, 1);
return 0;
}
static ssize_t led_write(struct file *file, const char __user *in, size_t size, loff_t *off)
{
int err;
char status;
struct inode *inode = file_inode(file);
int minor = iminor(inode);
err = copy_from_user(&status, in, 1);
status = opr_led->write(minor, status);
return status;
}
static int led_open(struct inode *inode, struct file *file)
{
int minor = iminor(inode);
printk("%s %s %d minor:%d\n", __FILE__, __FUNCTION__, __LINE__, minor);
opr_led->init(minor);
return 0;
}
/*2.实现自己的file_operations结构体*/
static struct file_operations led_opr = {
.read = led_read,
.write =led_write ,
.open = led_open,
.owner = THIS_MODULE
};
int __init led_init(void)
{
int i;
printk("%s %s %d", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "ZHANGJIN_LED", &led_opr);
led_class = class_create(THIS_MODULE, "ZHANGJIN_LED");
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "ZHANGJIN_LED");
return -1;
}
opr_led = export_imx6ull_API();
for(i = 0;i < opr_led->num;i++)
{
device_create(led_class, NULL, MKDEV(major, i), NULL, "led%d", i);
}
return 0;
}
static void __exit led_exit(void)
{
int i;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
for(i = 0; i < opr_led->num;i++)
{
device_destroy(led_class, MKDEV(major, i));
opr_led->free(i);
}
class_destroy(led_class);
unregister_chrdev(major, "ZHANGJIN_LED");
}
/*告诉内核*/
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
五.驱动代码(下层--对应单板)
在这里犯了几个非常低级的错误,在写led1时(GPIO4_19),在读写操作时操作的寄存器写错了,原本应该操作GPIO4_DR寄存器,居然写成了,GPIO5_DR, 由于选择设备1时设备0没有初始化,也就是没有地址映射,导致指针为空。导致了段错误的产生,困扰了我一下午
#include <linux/module.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/io.h>
#include "led_opr.h"
/*板载LED灯*/
static unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = NULL;
static unsigned int *GPIO5_GDIR = NULL;
static unsigned int *GPIO5_DR = NULL;
static unsigned int *GPIO5_PSR = NULL;
/*外接GPIO4_19LED灯*/
static unsigned int *IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC = NULL;
static unsigned int *GPIO4_GDIR = NULL;
static unsigned int *GPIO4_DR = NULL;
static unsigned int *GPIO4_PSR = NULL;
static unsigned int *CCM_CCGR3 = NULL;
/*imx6ull 设备初始化函数*/
int imx6ull_led_init(int which_led)
{
if(which_led == 0)
{
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);
GPIO5_GDIR = ioremap(0x20AC004, 4);
GPIO5_PSR = ioremap(0x20AC008, 4);
GPIO5_DR = ioremap(0x20AC000, 4);
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &= ~(0xf);
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |= (5);
}
else if(which_led == 1)
{
IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC = ioremap(0x20E01DC, 4);
GPIO4_GDIR = ioremap(0x20A8004, 4);
GPIO4_DR = ioremap(0x20A8000, 4);
GPIO4_PSR = ioremap(0x20A8008, 4);
CCM_CCGR3 = ioremap(0x20C4074, 4);
*CCM_CCGR3 |= (3 << 12);
*IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC &= ~(0x1f);
*IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC |= (5);
}
/* b. 设置GPIO5_IO03用于GPIO
* set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3
* to configure GPIO5_IO03 as GPIO
* IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 0x2290014
* bit[3:0] = 0b0101 alt5
*/
/*设置GPIO4_19作为GPIO输出*/
return 0;
}
/*imx6ull 所有led读函数*/
int imx6ull_led_read(int which_led)
{
int val;
int status;
/*将IO设置成输入模式*/
if(which_led == 0)
{
*GPIO5_GDIR &= ~(1 << 3);
val = *GPIO5_PSR;
status = (val << 3) & 1;
return status;
}
else if(which_led == 1)
{
*GPIO4_GDIR &= ~(1 << 19);
val = *GPIO4_PSR;
status = (val >> 19) & 1;
return status;
}
return 0;
}
/*imx6ull 所有led读函数*/
int imx6ull_led_write(int which_led, char status)
{
if(which_led == 0)
{
*GPIO5_GDIR |= (1 << 3);
if(status)
{
*GPIO5_DR |= (1 << 3);
}
else
{
*GPIO5_DR &= ~(1 << 3);
}
}
else if(which_led == 1)
{
*GPIO4_GDIR |= (1 << 19);
if(status)
{
/*这个位置,困扰了我一下午,md
之前写成了
*GPIO5_DR &= ~(1 << 19);
*/
*GPIO4_DR |= (1 << 19);
}
else
{
*GPIO4_DR &= ~(1 << 19);
}
}
return 0;
}
/*读函数*/
int imx6ull_free_Mem(int which_led)
{
if(which_led == 0)
{
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_DR);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_PSR);
iounmap(CCM_CCGR3);
printk("device 0 clear OK\n");
}
else if(which_led == 1)
{
iounmap(IOMUXC_SW_MUX_CTL_PAD_CSI_VSYNC);
iounmap(GPIO4_DR);
iounmap(GPIO4_GDIR);
iounmap(GPIO4_PSR);
printk("device 1 clear OK\n");
}
return 0;
}
struct opr_ctl imx6ull_led_ctl = {
.init = imx6ull_led_init,
.read = imx6ull_led_read,
.write = imx6ull_led_write,
.num = 2,
.free = imx6ull_free_Mem,
};
struct opr_ctl *export_imx6ull_API(void)//将imx6ull的接口暴露出去
{
return &imx6ull_led_ctl;
}
六.头文件
#ifndef _LED_OPR_H
#define _LED_OPR_H
struct opr_ctl {
int (*init) (int which_led);
int (*write) (int which_led, char status);
int (*read) (int which_led);
int (*free) (int which_led);
int num;
};
struct opr_ctl *export_imx6ull_API(void);//将imx6ull的接口暴露出去
#endif
七.Makefile
# 1. ʹ�ò�ͬ�Ŀ������ں�ʱ, һ��Ҫ��KERN_DIR
# 2. KERN_DIR�е��ں�Ҫ�������á�����, Ϊ���ܱ����ں�, Ҫ���������л�������:
# 2.1 ARCH, ����: export ARCH=arm64
# 2.2 CROSS_COMPILE, ����: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, ����: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# ע��: ��ͬ�Ŀ����岻ͬ�ı���������3������������һ����ͬ,
# ��ο���������ĸ��û�ʹ���ֲ�
KERN_DIR = /home/book/100ask_imx6ull-sdk/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
# �ο��ں�Դ��drivers/char/ipmi/Makefile
# Ҫ���a.c, b.c�����ab.ko, ��������ָ��:
# ab-y := a.o b.o
# obj-m += ab.o
# leddrv.c board_demo.c ����� 100ask.ko
100ask_led-y := leddrv.o board_imx6ull_PRO.o
obj-m += 100ask_led.o
八.程序思想
面向对象:
字符设备驱动程序抽象出一个file_operations结构体
我们写的程序针对硬件部分抽象出opr_ctl(led_operations)结构体
分层:
实现了上下分层:
程序思路(自己理解):
这一个图其实就解释的差不多了:应用调用open去执行上层应用的open,根据上层应用想要打开哪一个设备去下层驱动程序去找这个设备并打开,其它读/写都是同理
更多推荐
所有评论(0)