GPIO子系统点六盏灯
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
struct device_node* node;
struct device_node* child_node1;
struct device_node* child_node2;
unsigned int gpionumb1[3];
unsigned int gpionumb2[3];
char *name[3]={"led1","led2","led3"};//字符指针数组
int i=0;
static int __init my_init(void)
{
//解析设备树myled节点
node=of_find_node_by_path("/myled");
if(node==NULL)
{
printk("node error:");
return -EIO;
}
child_node1 = of_get_child_by_name(node,"extend-led");
if(child_node1==NULL)
{
printk("node error:");
return -EIO;
}
child_node2 = of_get_child_by_name(node,"core-led");
if(child_node2==NULL)
{
printk("node error:");
return -EIO;
}
for(i=0;i<3;i++){
//从设备树中读取gpio编号
gpionumb1[i]=of_get_named_gpio(child_node1,name[i],0);
if(gpionumb1[i]<0)
{
printk("of get name gpio is error:");
return -EIO;
}
//申请gpio编号
gpio_request(gpionumb1[i],NULL);
//设置gpio编号输出高电平
gpio_direction_output(gpionumb1[i],1);
//从设备树中读取gpio编号
gpionumb2[i]=of_get_named_gpio(child_node2,"led",i);
if(gpionumb2[i]<0)
{
printk("of get name gpio is error:");
return -EIO;
}
//申请gpio编号
gpio_request(gpionumb2[i],NULL);
//设置gpio编号输出高电平
gpio_direction_output(gpionumb2[i],1);
}
return 0;
}
static void __exit my_exit(void)
{
for(i=0;i<3;i++){
//设置gpio编号输出低电平
gpio_direction_output(gpionumb1[i],0);
//释放gpio编号
gpio_free(gpionumb1[i]);
//设置gpio编号输出低电平
gpio_direction_output(gpionumb2[i],0);
//释放gpio编号
gpio_free(gpionumb2[i]);
}
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
GPIOD子系统点六盏灯
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
struct device_node* node;
struct device_node* child_node1_expend;
struct device_node* child_node2_core;
unsigned int gpionumb1[3];
unsigned int gpionumb2[3];
struct gpio_desc* edesc[3];
struct gpio_desc* cdesc[3];
char *name[3]={"led1","led2","led3"};//字符指针数组
int i=0;
/*
myled{ //添加基于gpio子系统设备树节点
core-led{ //核心板子节点
led = <&gpioz 5 0>,<&gpioz 6 0>,<&gpioz 7 0>;
};
extend-led{ //扩展板子节点
led1 = <&gpioe 10 0>; //led1 PE10
led2 = <&gpiof 10 0>; //led2 PF10
led3 = <&gpioe 8 0>; //led3 PE8
};
};
*/
static int __init my_init(void)
{
//解析设备树myled节点
node=of_find_node_by_path("/myled");
if(node==NULL)
{
printk("node error:");
return -EIO;
}
//扩展板LED子节点
child_node1_expend = of_get_child_by_name(node,"extend-led");
if(child_node1_expend==NULL)
{
printk("node error:");
return -EIO;
}
//核心板LED子节点
child_node2_core = of_get_child_by_name(node,"core-led");
if(child_node2_core==NULL)
{
printk("node error:");
return -EIO;
}
for(i=0;i<3;i++){
//直接从设备中获取struct gpio_desc*结构体指针
edesc[i]=gpiod_get_from_of_node(child_node1_expend,name[i],0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(edesc[i])){//判断地址是否在4K空间内
printk("gpiod get from node is error\n");
return PTR_ERR(edesc[i]);//将地址转换成错误码
}
gpiod_direction_output(edesc[i],1);//设置gpio引脚输出高电平
cdesc[i]=gpiod_get_from_of_node(child_node2_core,"led",i,GPIOD_OUT_LOW,NULL);
if(IS_ERR(cdesc[i])){//判断地址是否在4K空间内
printk("gpiod get from node is error\n");
return PTR_ERR(cdesc[i]);//将地址转换成错误码
}
gpiod_direction_output(cdesc[i],1);//设置gpio引脚输出高电平
}
return 0;
}
static void __exit my_exit(void)
{
for(i=0;i<3;i++){
//设置gpio编号输出低电平
gpiod_set_value(edesc[i],0);
//释放gpio编号
gpiod_put(edesc[i]);
//设置gpio编号输出低电平
gpiod_set_value(cdesc[i],0);
//释放gpio编号
gpiod_put(cdesc[i]);
}
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");