Framework如何调用Hardware以及Hardware的简单实现

前言

在上一篇博客中介绍了Android9.0中的手动调节亮度的流程,但当时只讲到framwork的整个调用过程,并未讲到如何调用hardware层,这一章来讲一下如何调用hardware,以及如何实现简单的hareware.

Kernel: v4.4.126
Device: rk3399
Platform Version: Android 7.1

如何调用(以亮度调节为例,其他的也一样)

主要分为下面3个步骤:

  1. 使用hardware.c中定义的hw_get_module获取hal库中hw_module_t全局变量的地址。
    1

  2. 利用全局变量hw_module_t地址调用它内部结构体变量hw_module_methods_t的open函数指针,并且将hw_module_t作为参数传入open函数,在open函数内将hw_module_t赋值给hw_device_t中定义的hw_module_t变量,最后获取到hw_device_t的地址。
    1

  3. 利用上面的hw_device_t的地址就可以调用hw_device_t中定义的指针函数对应的实现。例如:set_light()
    1

上面图中代码全在com_android_server_SerialService.cpp中,如果看完还是不太理解,请看完下面的实现步骤,再折回来看。

如何实现简单的hardware?

首先要知道hardware.h中有3个重要的公用结构体

struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;

它们之间的关系

hw_module_t结构体中包含hw_module_methods_t的结构体指针
hw_device_t结构体中包含hw_module_t的结构体指针
结合它们之间的关系,再理解一下上面的3个调用步骤
1
1

接下讲如何实现:

  1. 首先是否基于hw_module_t和hw_device_t扩充自己的xx_module_t和xx_device_t

    如果基于hw_module_t扩充,第一个成员变量必须包含 hw_module_t
    如果基于hw_device_t扩充,第一个成员变量必须包含 hw_device_t

例如相机:
1

例如亮度:

1
2. 在源码中定义全局变量hw_module_t或者基于hw_module_t扩充的结构体全局变量

例如相机:
1

例如亮度:

1
3. 接下来就需要实现hw_module_t中的hw_module_methods_t结构体指针的open函数了。

1
4. 然后就是在open里面,创建基于hw_device_t的扩充结构体变量,并把hw_module_t赋值给hw_device_t中定义的hw_module_t指针

1
5. 最后我们得到了基于hw_device_t的结构体变量,里面包含了hw_module_t的信息,并且此时就可以调用hw_device_t中定义的可被调用的方法了。

1

结合后面hardware的实现流程,再回过头去看调用,就会清晰很多了。

转载请注明出处:http://www.wolfnx.com/2020/03/16/Brightness-Set-Stream

作者 : wolfnx
邮箱 : wolfnx@outlook.com
邮箱2 : lostnx@gmail.com

### Keil 5 Hardware Configuration and Issues Solution In addressing hardware-related configurations or issues within Keil 5, it is essential to understand how the development environment interacts with specific hardware components. For instance, displaying various device information such as hardware version, firmware version, and current bus status can be crucial for debugging purposes[^1]. The hardware abstraction layer (HAL), which resides under `platform/hal` directory, plays a significant role in facilitating this interaction between software and hardware[^2]. By using HAL libraries, developers ensure compatibility across different platforms while maintaining ease of use. For configuring hardware settings effectively: - **Device Information Display**: Ensure that the interface driver used supports retrieving necessary details about connected devices. If not already set up correctly, consider implementing custom drivers tailored specifically towards target hardware requirements. - **Hardware Abstraction Layer Utilization**: Leverage feature header files provided within the HAL framework to simplify access to peripheral functionalities without delving into low-level specifics. When encountering problems related to state transitions influenced by inputs—such as those found in traffic light control systems implemented via Keil—it becomes imperative to design logic carefully so that next-state determination accounts for both present conditions and incoming stimuli rather than merely iterating over possible states sequentially[^3]: ```c // Example C code snippet demonstrating conditional state transition based on input if ((currentState == RED_LIGHT) && (inputSignal == PED_REQUEST)) { nextState = FLASHING_RED; } else if (...) { /* Other conditions */ } ``` This approach ensures robustness against unexpected behavior caused by improper handling of external events during operation. --related questions-- 1. How does one implement an efficient method for gathering detailed diagnostic data from embedded systems? 2. What are some best practices when designing applications requiring complex finite state machines? 3. Can you provide examples where utilizing a well-defined hardware abstraction layer significantly improved project outcomes?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值