android11使用gpio口控制led状态灯

devtools/2025/3/7 2:25:58/

目录

一、简介

二、解决方法

A、底层驱动

B、上层调用

C、验证


一、简介

1、需求:这里是用2个gpio口来控制LED灯,开机时默认亮蓝灯,按开机键,休眠亮红灯,唤醒亮蓝灯。

原理图:

这里由于主板上电阻R635未贴,所以led_sleep不启用。

2、分析:

a.一开始是想将这2个gpio口的控制写在背光pwm驱动中,但是该设备是不接屏幕(mipi/edp/lvds)的,直接由cpu输出信号到hdmi屏,所以无法控制背光pwm。

同理,想写在和屏启动相关的驱动里面,也是无法控制的。例如由i2c控制的gm8775c。

b.所以想到在底层驱动写一个文件节点,由上层应用去控制。

二、解决方法

A、底层驱动

这里写了一个c文件,gpio_led.c

/** Driver for keys on GPIO lines capable of generating interrupts.** Copyright (C) 2015, Fuzhou Rockchip Electronics Co., Ltd* Copyright 2005 Phil Blundell** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License version 2 as* published by the Free Software Foundation.*/#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/wakelock.h>#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>struct vanzeak_gpio_drvdata {struct gpio_desc *power_gpio;struct gpio_desc *sleep_gpio;
};static const struct of_device_id vanzeak_gpio_match[] = {{ .compatible = "vanzeak,gpio", .data = NULL},{},
};
MODULE_DEVICE_TABLE(of, vanzeak_gpio_match);static ssize_t led_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);int val = val = simple_strtol(buf, NULL, 8);if(val){gpiod_direction_output(ddata->power_gpio, 1);gpiod_direction_output(ddata->sleep_gpio, 0);}else{gpiod_direction_output(ddata->power_gpio, 0);gpiod_direction_output(ddata->sleep_gpio, 1);}return size;
}static ssize_t led_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
{return 0;
}static DEVICE_ATTR(led_enable, 0644, led_enable_show, led_enable_store);static struct attribute *led_enable_attrs[] = {&dev_attr_led_enable.attr,NULL,
};static struct attribute_group led_enable_attr_group = {.name   = "led_enable",.attrs  = led_enable_attrs,
};static int vanzeak_gpio_probe(struct platform_device *pdev)
{struct vanzeak_gpio_drvdata *ddata = NULL;struct device *dev = &pdev->dev;int ret;ddata = devm_kzalloc(dev, sizeof(struct vanzeak_gpio_drvdata),GFP_KERNEL);
//	if(ddata = NULL)
//		return -1;platform_set_drvdata(pdev, ddata);dev_set_drvdata(&pdev->dev, ddata);ddata->power_gpio = devm_gpiod_get_optional(dev, "enable", 0);if (IS_ERR(ddata->power_gpio)) {ret = PTR_ERR(ddata->power_gpio);dev_err(dev, "failed to request power GPIO: %d\n", ret);goto fail0;}ddata->sleep_gpio = devm_gpiod_get_optional(dev, "sleep", 0);if (IS_ERR(ddata->sleep_gpio)) {ret = PTR_ERR(ddata->sleep_gpio);dev_err(dev, "failed to request sleep GPIO: %d\n", ret);goto fail0;}gpiod_direction_output(ddata->power_gpio, 1);gpiod_direction_output(ddata->sleep_gpio, 0);ret = sysfs_create_group(&pdev->dev.kobj, &led_enable_attr_group);if (ret) {pr_err("failed to create attr group\n");}return 0;fail0:platform_set_drvdata(pdev, NULL);return -1;
}static int vanzeak_gpio_remove(struct platform_device *pdev)
{return 0;
}#ifdef CONFIG_PM
static int vanzeak_gpio_suspend(struct device *dev)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);printk("DICKE printk %s : %d\n", __func__, __LINE__);gpiod_direction_output(ddata->power_gpio, 0);return 0;
}static int vanzeak_gpio_resume(struct device *dev)
{struct vanzeak_gpio_drvdata *ddata = dev_get_drvdata(dev);printk("DICKE printk %s : %d\n", __func__, __LINE__);gpiod_direction_output(ddata->power_gpio, 1);return 0;
}static const struct dev_pm_ops vanzeak_gpio_pm_ops = {.suspend	= vanzeak_gpio_suspend,.resume		= vanzeak_gpio_resume,
};
#endifstatic struct platform_driver vanzeak_gpio_device_driver = {.probe		= vanzeak_gpio_probe,.remove		= vanzeak_gpio_remove,.driver		= {.name	= "vanzeak-gpio",.owner	= THIS_MODULE,.of_match_table = vanzeak_gpio_match,
#ifdef CONFIG_PM.pm	= &vanzeak_gpio_pm_ops,
#endif}
};static int __init vanzeak_gpio_driver_init(void)
{return platform_driver_register(&vanzeak_gpio_device_driver);
}static void __exit vanzeak_gpio_driver_exit(void)
{platform_driver_unregister(&vanzeak_gpio_device_driver);
}module_init(vanzeak_gpio_driver_init);
module_exit(vanzeak_gpio_driver_exit);
B、上层调用

由上层的休眠唤醒来控制LED的亮灭。

diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index af7d91cf7ba6..1bbc51a9ed91 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -119,6 +119,14 @@ import java.util.Arrays;import java.util.List;import java.util.Objects;+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.BufferedWriter;
+/*** The power manager service is responsible for coordinating power management* functions on the device.
@@ -1598,6 +1606,46 @@ public final class PowerManagerService extends SystemService}}+   private void closeLed(int i){
+        String path = "/sys/devices/platform/vanzeak-gpio/led_enable/led_enable";
+        String value;
+	if(i == 1)
+		value = "1";
+	else if(i == 0)
+		value = "0";
+	else{
+		Slog.e(TAG, "data error");
+		return;
+	}
+        //  Log.i(TAG,"setGpioValue, path = [" + path + "] value = [" + value + "]");
+        File file = new File(path); 
+        if (!file.exists()) {
+            Slog.i("dxb","initOpenGpio , file is not exist!!!!");
+            return;
+        }
+        FileOutputStream fileOutputStream = null;
+        BufferedWriter bufferedWriter = null;
+        try {
+            fileOutputStream = new FileOutputStream(file);
+            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8")); 
+            bufferedWriter.write(value);
+            bufferedWriter.flush();
+            bufferedWriter.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+            Slog.i("dxb","input data error " + e.getMessage());
+        } finally {
+            if (bufferedWriter != null) {
+                try {
+                    bufferedWriter.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+    }
+private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) {if (DEBUG_SPEW) {Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime
@@ -1690,6 +1738,8 @@ public final class PowerManagerService extends SystemServiceTrace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp");try {
+		Slog.i(TAG, "ctrol led to working mode");
+		closeLed(1);Slog.i(TAG, "Waking up from "+ PowerManagerInternal.wakefulnessToString(getWakefulnessLocked())+ " (uid=" + reasonUid
@@ -1748,6 +1798,8 @@ public final class PowerManagerService extends SystemServicetry {reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX,Math.max(reason, PowerManager.GO_TO_SLEEP_REASON_MIN));
+		Slog.i(TAG, "ctrol led to sleeping mode");
+		closeLed(0);Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason)+ " (uid " + uid + ")...");
C、验证

按开机键,休眠亮红灯,唤醒亮蓝灯。


http://www.ppmy.cn/devtools/165132.html

相关文章

基于海思soc的智能产品开发(芯片sdk和linux开发关系)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 随着国产化芯片的推进&#xff0c;在soc领域&#xff0c;越来越多的项目使用国产soc芯片。这些soc芯片&#xff0c;通常来说运行的os不是linux&…

微信小程序投票系统的构建与实现

在数字化时代&#xff0c;微信小程序已经成为人们日常生活的重要组成部分。无论是企业宣传、活动组织还是社交互动&#xff0c;小程序都展现出了其强大的功能和便捷的用户体验。其中&#xff0c;微信小程序的投票系统尤为突出&#xff0c;它不仅能简化投票流程&#xff0c;还能…

iOS UICollectionViewCell 点击事件自动化埋点

iOS 中经常要进行埋点&#xff0c;我们这里支持 UICollectionViewCell. 进行自动化埋点&#xff0c;思路&#xff1a; 通过hook UICollectionViewCell 的setSelected:方法&#xff0c; 则新的方法中执行埋点逻辑&#xff0c;并调用原来的方法 直接上代码 implementation UICol…

Linux安装Redis以及Redis三种启动方式

目录树 一、安装前的软件准备二、Redis的安装三、Redis的三种启动方式&#xff01;&#xff01;&#xff01; 1、直接启动Redis2.后台进程方式启动Redis3.通过开机启动方式 四、Window上桌面连接Linux上的Redis 一、安装前的软件准备 Xshell —— 连接Linux并操作其终端的软…

HCIA-IP路由动态-RIP

一、概念 动态路由是指路由器通过运行动态路由协议&#xff08;RIP、OSPF等&#xff09;&#xff0c;自动学习和发现网络中的路由信息。路由器之间通过交换路由协议数据包&#xff0c;互相通告自己所知道的网络信息&#xff0c;从而构建和更新路由表。 二、RIP(路由信息协议)…

CSS—px与rem:3分钟掌握rem适配原理

个人博客&#xff1a;haichenyi.com。感谢关注 1. 目录 1–目录2–px3–rem4–移动端适配 2. px(像素) px我们用的很多&#xff0c;一直都在用。我们真的了解px吗&#xff1f;   之前最常见的720P的分辨率和1080P的分辨率是啥意思&#xff1f;这里的720P的对应的就是1280x72…

SOUI基于Zint生成EAN码

EAN码广泛应用与欧洲的零售业。包括EAN-2、EAN-5、EAN-8和EAN-12码。分别编码 2、5、7 或 12 位数字。此外&#xff0c;可以使用 字符将 EAN-2 和 EAN-5 附加符号添加到 EAN-8 和 EAN-13 符号中&#xff0c;就像 UPC 符号一样。 EAN-8校验码计算&#xff1a; 从左往右奇数位的…

Android动态适配中英文开发指南

Android动态适配中英文开发。首先&#xff0c;我需要确定用户的需求到底是什么。可能他们正在开发一个需要支持中英文的Android应用&#xff0c;想要知道如何动态切换语言&#xff0c;而不仅仅是依赖系统默认语言。或者他们可能遇到了在应用内切换语言时的一些问题&#xff0c;…