libraries/AP_HAL/HAL.h
定义了所有外设的基础抽象类集合
一个 HAL 抽象类世界,由 HAL 层的cpu外设的抽象类基础组件组聚而成;
也就是一个 HAL 派生类子对象 代表了一个 cpu 的所有外设的集合;这里
借鉴这种面向对象的设计思想。后面一一分析每一个子组件。
#pragma once
class AP_Param;
#include "AP_HAL_Namespace.h" ///< 名字空间
#include "AnalogIn.h" ///< 模拟输入,抽象基类
#include "GPIO.h" ///< gpio,抽象基类
#include "RCInput.h" ///< 无线电接收器输入抽象基类
#include "RCOutput.h" ///< 无线电接收器输入抽象基类
#include "SPIDevice.h" ///< spi 设备 抽象基类
#include "Storage.h" ///< 存储设备 抽象基类
#include "UARTDriver.h" ///< 串口设备 抽象基类
#include "system.h" ///< 系统方法
#include "OpticalFlow.h" ///< 光流 抽象基类
#if HAL_WITH_UAVCAN
#include "CAN.h" ///< can设备 抽象基类
#endif
#if defined(HAL_NEEDS_PARAM_HELPER)
#include <AP_Param/AP_Param.h> ///< 参数 抽象基类
class AP_Param_Helper;
#endif
class AP_HAL::HAL {
public:
HAL(AP_HAL::UARTDriver* _uartA, // console
AP_HAL::UARTDriver* _uartB, // 1st GPS
AP_HAL::UARTDriver* _uartC, // telem1
AP_HAL::UARTDriver* _uartD, // telem2
AP_HAL::UARTDriver* _uartE, // 2nd GPS
AP_HAL::UARTDriver* _uartF, // extra1
AP_HAL::I2CDeviceManager* _i2c_mgr, ///< i2c 设备管理器 抽象基类组件
AP_HAL::SPIDeviceManager* _spi, ///< spi 设备管理器 抽象基类组件
AP_HAL::AnalogIn* _analogin, ///< analogin 抽象基类组件
AP_HAL::Storage* _storage, ///< stroage 抽象基类组件
AP_HAL::UARTDriver* _console, ///< 控制台 抽象基类组件
AP_HAL::GPIO* _gpio, ///< gpio 抽象基类组件
AP_HAL::RCInput* _rcin, ///< rcin 抽象基类组件
AP_HAL::RCOutput* _rcout, ///< rcout 抽象基类组件
AP_HAL::Scheduler* _scheduler, ///< scheduler 抽象基类组件
AP_HAL::Util* _util, ///< utils 抽象基类组件
AP_HAL::OpticalFlow *_opticalflow, ///< opticalflow 抽象基类组件
#if HAL_WITH_UAVCAN
AP_HAL::CANManager* _can_mgr[MAX_NUMBER_OF_CAN_DRIVERS]) ///< can 设备管理器 抽象基类组件
#else
AP_HAL::CANManager** _can_mgr)
#endif
: ///< 实际中基类指针指向子类对象多态 初始化列表
uartA(_uartA),
uartB(_uartB),
uartC(_uartC),
uartD(_uartD),
uartE(_uartE),
uartF(_uartF),
i2c_mgr(_i2c_mgr),
spi(_spi),
analogin(_analogin),
storage(_storage),
console(_console),
gpio(_gpio),
rcin(_rcin),
rcout(_rcout),
scheduler(_scheduler),
util(_util),
opticalflow(_opticalflow)
{
#if HAL_WITH_UAVCAN
if (_can_mgr == nullptr) {
for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_DRIVERS; i++)
can_mgr[i] = nullptr;
} else {
for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_DRIVERS; i++)
can_mgr[i] = _can_mgr[i];
}
#endif
// 比如 libraries/AP_HAL_PX4/system.cpp 中的方法
AP_HAL::init();
}
/**< Callbacks 基础抽象类组件 */
struct Callbacks {
virtual void setup() = 0;
virtual void loop() = 0;
};
/**< FunCallbacks 类,派生自 Callbacks 基础抽象类组件 */
struct FunCallbacks : public Callbacks {
FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void));
void setup() override { _setup(); }
void loop() override { _loop(); }
private:
void (*_setup)(void);
void (*_loop)(void);
};
/**< 一个 HAL 抽象类的 run接口方法 */
virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0;
/**< 组成一个 HAL 抽象类世界的 (数据)基础(抽象)成员组件(指针) */
AP_HAL::UARTDriver* uartA;
AP_HAL::UARTDriver* uartB;
AP_HAL::UARTDriver* uartC;
AP_HAL::UARTDriver* uartD;
AP_HAL::UARTDriver* uartE;
AP_HAL::UARTDriver* uartF;
AP_HAL::I2CDeviceManager* i2c_mgr;
AP_HAL::SPIDeviceManager* spi;
AP_HAL::AnalogIn* analogin;
AP_HAL::Storage* storage;
AP_HAL::UARTDriver* console;
AP_HAL::GPIO* gpio;
AP_HAL::RCInput* rcin;
AP_HAL::RCOutput* rcout;
AP_HAL::Scheduler* scheduler;
AP_HAL::Util *util;
AP_HAL::OpticalFlow *opticalflow;
#if HAL_WITH_UAVCAN
AP_HAL::CANManager* can_mgr[MAX_NUMBER_OF_CAN_DRIVERS];
#else
AP_HAL::CANManager** can_mgr;
#endif
};