5.【linux驱动】GPIO

演示代码使用GPIO_B_31引脚输出模式

寄存器版本

查看芯片手册GPIO描述
在这里插入图片描述
输出模式需要设置引脚功能,nexell的cpu GPIO脚一共有四种可选功能,每个脚的选项不一样。这里说要用作IO得设置为00,其实是错误的,不得不说nexell的设计很奇葩因为GPIO功能并不一定都是00,这次的GPIO_B_31就得设置为01,如下图描述。
在这里插入图片描述
另外使能使出和设置电平就可以了。看下寄存器描述
在这里插入图片描述
输入使能和高低电平很简单,对位设置就好。功能设置部分每两位配置一个引脚。定义寄存器结构体,吧用到的定义一下就好

#pragma pack(4)
static struct GPIO_B{
	unsigned int out_put;
	unsigned int out_enb;
	unsigned int detect_md_1;
	unsigned int detect_md_2;
	unsigned int int_enb;
	unsigned int event_detect;
	unsigned int pad_state;
	unsigned int resv;
	unsigned int func0;
	unsigned int func1;
}* gpio_b;
#pragma pack()

整体代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/io.h>

MODULE_LICENSE("GPL");

#pragma pack(4)
static struct GPIO_B{
	unsigned int out_put;
	unsigned int out_enb;
	unsigned int detect_md_1;
	unsigned int detect_md_2;
	unsigned int int_enb;
	unsigned int event_detect;
	unsigned int pad_state;
	unsigned int resv;
	unsigned int func0;
	unsigned int func1;
}* gpio_b;
#pragma pack()

static int __init hello_init(void){
	gpio_b = (struct GPIO_B *)ioremap(0xc001b000,sizeof(struct GPIO_B));//映射地址
	gpio_b->func1 |= (1 << 30);											//设置功能为1
	gpio_b->out_enb |= (1 << 31);										//使能输出
	gpio_b->out_put |= (1 << 31);										//高电平

	printk("hello init\n");
	return 0;
}

static void __exit hello_exit(void){
	gpio_b->out_put &= ~(1 << 31);
	printk("hello exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

api版本

api版本没什么好说的,读一下库函数,直接写就行,下面是整体代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <mach/soc.h>
#include <mach/platform.h>

MODULE_LICENSE("GPL");

static int __init hello_init(void){
	nxp_soc_gpio_set_io_func(PAD_GPIO_B + 31, 1);	// 设置gpio功能
	nxp_soc_gpio_set_io_dir(PAD_GPIO_B + 31, 1); 	// 输出
	nxp_soc_gpio_set_out_value(PAD_GPIO_B + 31, 1); // 高电平

	printk("hello init\n");
	return 0;
}

static void __exit hello_exit(void){
	nxp_soc_gpio_set_out_value(PAD_GPIO_B + 31, 0); // 低电平
	printk("hello exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

猜你喜欢

转载自blog.csdn.net/qq_16054639/article/details/106673547
今日推荐