总盯着过去,你会瞎掉一只眼;然而忘掉历史,你会双目失明。
阅读IMX6ULL的参考手册,我们可以知道IMX6ULL共有 5 组 GPIO( GPIO1~GPIO5),每组引脚最多有 32 个,但是可能实际上并没有那么多。
组别 | 引脚 |
---|---|
GPIO1 有 32 个引脚: | GPIO1_IO0~GPIO1_IO31; |
GPIO2 有 22 个引脚: | GPIO2_IO0~GPIO2_IO21; |
GPIO3 有 29 个引脚: | GPIO3_IO0~GPIO3_IO28; |
GPIO4 有 29 个引脚: | GPIO4_IO0~GPIO4_IO28; |
GPIO5 有 12 个引脚: | GPIO5_IO0~GPIO5_IO11; |
GPIO 的控制涉及 3 大模块: CCM、 IOMUXC、 GPIO 模块本身,框图如下:
GPIOx 要用 CCM_CCGRy 寄存器中的 2 位来决定该组 GPIO 是否使能。各组GPIO的具体时钟控制寄存器如下所示。
而CCM_CCGR 寄存器中某 2 位的取值含义如下:
一个引脚寄存器 | 作用 |
---|---|
IOMUXC_SW_ MUX _ CTL_PAD_< PADNAME > | 选择某个 引脚的复用模式 |
IOMUXC_SW_ PAD _ CTL_PAD_< PADNAME > | 选择某个 引脚的电气属性 |
顺便一提,上图中的loopback 功能(回环测试)。设置该引脚的 loopback 功能,这样就可以从 GPIOx_PSR 中读到引脚的有实电平!
因为从 GPIOx_DR 中读回的只是上次设置的值,它并不能反应引脚的真实电平。假若硬件故障导致该引脚与地短路了,通过设置 GPIOx_DR让它输出高电平并不会起效果,这时我们通过读取GPIOx_PSR便可知道该引脚的真实输出状态!
框图如下
主要关心3个寄存器:
上一节的Hello驱动,并没有实际操作硬件,而LED点灯驱动操作硬件势在必行。这时就有一个问题需要考虑了,APP应用程序只是调用驱动程序的接口,这个不涉及具体硬件,所以无可厚非。而驱动程序肯定要操作硬件,这时如果把硬件的寄存器操作与驱动程序混在一起编写,那么我们的驱动程序的可移植性将会变得特别差。当硬件发生变动时,我们需要更改驱动程序!这显然不是我们想要的结果,那怎么样才能实现应用程序通用,驱动程序也通用呢?分层!
即把驱动拆分为通用的框架(leddrv.c)、具体的硬件操作(board_X.c),如下图所示。
分别为不同板子,编写具体的硬件操作代码!每个单板的具体硬件操作分为:LED初始化(board_led_init)和LED控制(board_led_ctrl)!而这两个函数我们又可以将其定义为一个结构体,供上层的leddrv.c调用,于是下图所示框架。
leddrv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "led_opr.h"
/* 1. 确定主设备号 */
static int major = 0;
static struct class *led_class;
struct led_operations *p_led_opr;
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
static int led_drv_open (struct inode *node, struct file *file)
{
/* 根据node确次设备号 */
int minor = iminor(node);
/* 根据次设备号初始化LED */
p_led_opr->init(minor);
return 0;
}
static int led_drv_close (struct inode *node, struct file *file)
{
return 0;
}
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
return 0;
}
/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
char status;
struct inode *node = file_inode(file);
int minor = iminor(node);
err = copy_from_user(&status, buf, 1);
/* 根据次设备号和status控制LED */
p_led_opr->ctl(minor, status);
return 1;
}
/* 2. 定义自己的file_operations结构体 */
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init led_init(void)
{
int err;
int i;
printk("LED init \r\n");
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
major = register_chrdev(0, "led", &led_drv); /* /dev/led */
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
led_class = class_create(THIS_MODULE, "led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
unregister_chrdev(major, "led");
return -1;
}
/* 注意要在创建设备之前获得led_operaions结构体(需要用到其中的num) */
p_led_opr = get_board_led_opr();
for (i = 0; i < p_led_opr->num; i++)
device_create(led_class, NULL, MKDEV(major, i), NULL, "led%d", i); /* /dev/led0,1,... */
return 0;
}
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
static void __exit led_exit(void)
{
int i;
printk("LED exit \r\n");
for (i = 0; i < p_led_opr->num; i++)
device_destroy(led_class, MKDEV(major, i)); /* /dev/led0,1,... */
class_destroy(led_class);
unregister_chrdev(major, "led");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
LED驱动程序说明:
iminor(node)
获得设备的此设备号file_inode(file)
获得node节点,然后再通过iminor(node)
获得设备的次设备号!board_qemu.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "led_opr.h"
/* 需要理解开始偏移23个int,即23x4=92=0x5C
* IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO00 地址是 20E_0000h base + 5Ch
*/
struct iomux {
volatile unsigned int unnames[23];
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO00; /* offset 0x5c*/
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO01;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO02;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO03;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO04;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO06;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO07;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO08;
volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO09;
};
struct imx6ull_gpio {
volatile unsigned int dr;
volatile unsigned int gdir;
volatile unsigned int psr;
volatile unsigned int icr1;
volatile unsigned int icr2;
volatile unsigned int imr;
volatile unsigned int isr;
volatile unsigned int edge_sel;
};
/* enable GPIO1,GPIO5 他们两个都是由CCGR1来控制的!*/
static volatile unsigned int *CCM_CCGR1;
/* set GPIO5_IO03 as GPIO 值得一提的是,IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3复用时只有一种即GPIO5_IO03*/
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
/* set GPIO1_IO03 as GPIO */
static struct iomux *iomux;
static struct imx6ull_gpio *gpio1;
static struct imx6ull_gpio *gpio5;
static int board_qemu_led_init (int which) /* 初始化LED, which-哪个LED */
{
if (!CCM_CCGR1)
{
CCM_CCGR1 = ioremap(0x20C406C, 4);
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);
iomux = ioremap(0x20E0000, sizeof(struct iomux));
gpio1 = ioremap(0x209C000, sizeof(struct imx6ull_gpio));
gpio5 = ioremap(0x20AC000, sizeof(struct imx6ull_gpio));
}
if (which == 0)
{
/* 1. enable GPIO5
* CG15, b[31:30] = 0b11
*/
*CCM_CCGR1 |= (3<<30);
/* 2. set GPIO5_IO03 as GPIO
* MUX_MODE, b[3:0] = 0b101
*/
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = 5;
/* 3. set GPIO5_IO03 as output
* GPIO5 GDIR, b[3] = 0b1
*/
gpio5->gdir |= (1<<3);
}
else if(which == 1)
{
/* 1. enable GPIO1
* CG13, b[27:26] = 0b11
*/
*CCM_CCGR1 |= (3<<26);
/* 2. set GPIO1_IO03 as GPIO
* MUX_MODE, b[3:0] = 0b101
*/
iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO03 = 5;
/* 3. set GPIO1_IO03 as output
* GPIO1 GDIR, b[3] = 0b1
*/
gpio1->gdir |= (1<<3);
}
else if(which == 2)
{
/* 1. enable GPIO1
* CG13, b[27:26] = 0b11
*/
*CCM_CCGR1 |= (3<<26);
/* 2. set GPIO1_IO05 as GPIO
* MUX_MODE, b[3:0] = 0b101
*/
iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05 = 5;
/* 3. set GPIO1_IO05 as output
* GPIO1 GDIR, b[5] = 0b1
*/
gpio1->gdir |= (1<<5);
}
else if(which == 3)
{
/* 1. enable GPIO1
* CG13, b[27:26] = 0b11
*/
*CCM_CCGR1 |= (3<<26);
/* 2. set GPIO1_IO06 as GPIO
* MUX_MODE, b[3:0] = 0b101
*/
iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO06 = 5;
/* 3. set GPIO1_IO06 as output
* GPIO1 GDIR, b[6] = 0b1
*/
gpio1->gdir |= (1<<6);
}
//printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
return 0;
}
static int board_qemu_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
if (which == 0)
{
if (status) /* on : output 0 */
gpio5->dr &= ~(1<<3);
else /* on : output 1 */
gpio5->dr |= (1<<3);
}
else if (which == 1)
{
if (status) /* on : output 0 */
gpio1->dr &= ~(1<<3);
else /* on : output 1 */
gpio1->dr |= (1<<3);
}
else if (which == 2)
{
if (status) /* on : output 0 */
gpio1->dr &= ~(1<<5);
else /* on : output 1 */
gpio1->dr |= (1<<5);
}
else if (which == 3)
{
if (status) /* on : output 0 */
gpio1->dr &= ~(1<<6);
else /* on : output 1 */
gpio1->dr |= (1<<6);
}
return 0;
}
static struct led_operations board_qemu_led_opr = {
.num = 4,
.init = board_qemu_led_init,
.ctl = board_qemu_led_ctl,
};
struct led_operations *get_board_led_opr(void)
{
return &board_qemu_led_opr;
}
单板程序编程步骤
GPIO单板控制说明
led_opr.h
#ifndef _LED_OPR_H
#define _LED_OPR_H
struct led_operations {
int num; /* num-LED数量 */
int (*init) (int which); /* 初始化LED, which-哪个LED */
int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};
struct led_operations *get_board_led_opr(void);
#endif
led_operations 需要说明的:
ledtest.c
#include
#include
#include
#include
#include
#include
/*
* ./ledtest /dev/led0 on
* ./ledtest /dev/led0 off
*/
int main(int argc, char **argv)
{
int fd;
char status;
/* 1. 判断参数 */
if (argc != 3)
{
printf("Usage: %s \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], "on"))
{
status = 1;
write(fd, &status, 1);
}
else
{
status = 0;
write(fd, &status, 1);
}
close(fd);
return 0;
}
应用程序需要说明的是:
Makefile
KERN_DIR = /home/clay/linux/qemu/kernel/100ask_imx6ull-qemu/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_qemu.c 编译成 led.ko
led-y := leddrv.o board_qemu.o
obj-m += led.o
Makefile需要说明的:
编译程序没有问题后,运行qemu虚拟开发板,并做好准备工作!将
cp *.ko ledtest ~/linux/qemu/NFS/
insmod led.ko
./ledtest /dev/led0 on
./ledtest /dev/led0 off
其余三盏灯,操作分别用led1 led2 led3即可,这里不再一一演示。
cp -rf *.ko led ~/nfs/rootfs/lib/modules/4.1.15/
depmod //第一次加载驱动的时候需要运行此命令
modprobe led.ko //加载驱动
./led /dev/led1 on
./led /dev/led1 off
rmmod led.ko
嘻嘻,好长的一篇~