本节主要学习Ardupilot的Hal类,欢迎批评指正!
Ardupilot中最重要的命名空间
namespace AP_HAL
{
/*顶级纯虚拟类HAL---------Toplevel pure virtual class Hal.*/
class HAL;
/*驱动程序的顶级类名:-------Toplevel class names for drivers: */
class UARTDriver; //串口驱动类
class I2CDevice; //I2C设备类
class I2CDeviceManager; //I2C设备类管理
class Device; //设备类
class SPIDevice; //SPI设备类
class SPIDeviceDriver; //SPI设备驱动类
class SPIDeviceManager; //SPI设备管理类
class AnalogSource; //模拟资源类
class AnalogIn; //模拟输入类
class Storage; //存储类
class DigitalSource; //数字资源类
class GPIO; //GPIO类
class RCInput; //遥控器输入类
class RCOutput; //电机输出类
class Scheduler; //任务调度类
class Semaphore; //信号量类
class OpticalFlow; //光流传感器类
class CANManager; //CAN管理类
class CAN; //CAN类
class Util; //公用类
/*实用类--------Utility Classes */
class Print;
class Stream;
class BetterStream;
/********************************************************************************************************************************
* 定义指针函数的(过程、成员过程)------Typdefs for function pointers (Procedure, Member Procedure)
* 对于成员函数,我们使用FastDelegate委托类,它允许我们将成员函数封装为一个类型
* For member functions we use the FastDelegate delegates class which allows us to encapculate a member function as a type
*******************************************************************************************************************************/
typedef void(*Proc)(void);
FUNCTOR_TYPEDEF(MemberProc, void);
/************************************************************************************************************************************
* 所有平台上所有现有SPI设备的全局名称。--------Global names for all of the existing SPI devices on all platforms.
*************************************************************************************************************************************/
enum SPIDeviceType
{
//设备使用AP_HAL:SPIDevice抽象类--------Devices using AP_HAL::SPIDevice abstraction
SPIDevice_Type = -1,
};
//必须有HALs具体实现-------------------- Must be implemented by the concrete HALs.
const HAL& get_HAL();
}
class AP_HAL::HAL
{
public:
//构造函数初始化
HAL(AP_HAL::UARTDriver* _uartA, // console-------usb
AP_HAL::UARTDriver* _uartB, // 1st GPS-------uart1
AP_HAL::UARTDriver* _uartC, // telem1--------uart2
AP_HAL::UARTDriver* _uartD, // telem2--------uart3
AP_HAL::UARTDriver* _uartE, // 2nd GPS-------uart4
AP_HAL::UARTDriver* _uartF, // extra1--------uart6
AP_HAL::UARTDriver* _uartG, // extra2--------uart7
AP_HAL::I2CDeviceManager* _i2c_mgr, //i2c信号
AP_HAL::SPIDeviceManager* _spi, //spi信号
AP_HAL::AnalogIn* _analogin, //模拟信号输入
AP_HAL::Storage* _storage, //存储
AP_HAL::UARTDriver* _console, //终端
AP_HAL::GPIO* _gpio, //gpio
AP_HAL::RCInput* _rcin, //rc输入
AP_HAL::RCOutput* _rcout, //rc输出
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) //can总线
#endif
:
uartA(_uartA),
uartB(_uartB),
uartC(_uartC),
uartD(_uartD),
uartE(_uartE),
uartF(_uartF),
uartG(_uartG),
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
AP_HAL::init();
}
//类中定义结构体
struct Callbacks
{
virtual void setup() = 0;
virtual void loop() = 0;
};
//回调构造函数
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);
};
//纯虚函数
virtual void run(int argc, char * const argv[], Callbacks* callbacks) const = 0;
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::UARTDriver* uartG;
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
};
之前讲过Ardupilot为什么一套代码可以支持的不同的硬件,这里就不再说明。从这里可以看出Ardupilot的代码是非常规范的,为什么?代码采用C++语言,把C++用到了极致,所有的芯片底层板子类,都要按照父类的接口去实现,这样无论你增加什么样的硬件,你只要按照这种模式去写,就可以。
AVR is only supported on its separate branch
同一套操作系统如何实现支持不同的硬件平台???
注意:hwdef文件夹的定义
这里以自己的代码fmuv6为例进行配置,如果你想配置自己的板子,只需copy文件夹,然后修改对应于自己的硬件引脚就OK
# hw definition file for processing by chibios_hwdef.py
# for FMUv3 hardware (ie. for CUAV-PixHack-v5 and Pixhawk4)
# MCU class and specific type. It is a F765, which is the same as F767
# but without the TFT interface
MCU STM32F7xx STM32F767xx
# crystal frequency
OSCILLATOR_HZ 16000000
define STM32_LSECLK 32768U
define STM32_LSEDRV (3U << 3U)
define STM32_PLLSRC STM32_PLLSRC_HSE
define STM32_PLLM_VALUE 8
define STM32_PLLN_VALUE 216
define STM32_PLLP_VALUE 2
define STM32_PLLQ_VALUE 9
define CONFIG_HAL_BOARD_SUBTYPE HAL_BOARD_SUBTYPE_CHIBIOS_FMUV5
define HAL_CHIBIOS_ARCH_FMUV5 1
# board ID for firmware load
APJ_BOARD_ID 50
FLASH_RESERVE_START_KB 32
# board voltage
STM32_VDD 330U
# flash size
FLASH_SIZE_KB 2048
# order of UARTs (and USB)
UART_ORDER OTG1 USART1 USART2 USART3 UART4 USART6 UART7
# now we define the pins that USB is connected on
PA11 OTG_FS_DM OTG1
PA12 OTG_FS_DP OTG1
# these are the pins for SWD debugging with a STlinkv2 or black-magic probe
PA13 JTMS-SWDIO SWD
PA14 JTCK-SWCLK SWD
# SPI1 - internal sensors
PG11 SPI1_SCK SPI1
PA6 SPI1_MISO SPI1
PD7 SPI1_MOSI SPI1
# SPI2 - FRAM
PI1 SPI2_SCK SPI2
PI2 SPI2_MISO SPI2
PI3 SPI2_MOSI SPI2
# SPI4 - sensors2
PE2 SPI4_SCK SPI4
PE13 SPI4_MISO SPI4
PE6 SPI4_MOSI SPI4
# SPI5 - external1 (disabled to save DMA channels)
PF7 SPI5_SCK SPI5
PF8 SPI5_MISO SPI5
PF9 SPI5_MOSI SPI5
# SPI6 - external2 (disabled to save DMA channels)
# PG13 SPI6_SCK SPI6
# PG12 SPI6_MISO SPI6
# PB5 SPI6_MOSI SPI6
# sensor CS
PF10 MS5611_CS CS
#PF2 ICM20689_CS CS SPEED_VERYLOW
PI4 ICM20689_CS CS SPEED_VERYLOW
PF3 ICM20602_CS CS SPEED_VERYLOW
PF4 BMI055_G_CS CS
PG10 BMI055_A_CS CS
PF5 FRAM_CS CS SPEED_VERYLOW
#SPI4片选引脚---PI8
PI8 SPL06_CS CS SPEED_VERYLOW
PI10 SPL06_001_CS CS SPEED_VERYLOW
# I2C buses
PB8 I2C1_SCL I2C1
PB9 I2C1_SDA I2C1
PF1 I2C2_SCL I2C2
PF0 I2C2_SDA I2C2
PH7 I2C3_SCL I2C3
PH8 I2C3_SDA I2C3
PF14 I2C4_SCL I2C4
PF15 I2C4_SDA I2C4
# order of I2C buses
I2C_ORDER I2C3 I2C1 I2C2 I2C4
# enable pins
PE3 VDD_3V3_SENSORS_EN OUTPUT HIGH
# start peripheral power off, then enable after init
# this prevents a problem with radios that use RTS for
# bootloader hold
PF12 nVDD_5V_HIPOWER_EN OUTPUT HIGH
PG4 nVDD_5V_PERIPH_EN OUTPUT HIGH
PG5 VDD_5V_RC_EN OUTPUT HIGH
PG6 VDD_5V_WIFI_EN OUTPUT HIGH
PG7 VDD_3V3_SD_CARD_EN OUTPUT HIGH
# drdy pins
PE7 DRDY8_NC INPUT
PB4 DRDY1_ICM20689 INPUT
PB14 DRDY2_BMI055_GYRO INPUT
PB15 DRDY3_BMI055_ACC INPUT
PC5 DRDY4_ICM20602 INPUT
PC13 DRDY5_BMI055_GYRO INPUT
PD10 DRDY6_BMI055_ACC INPUT
PD15 DRDY7_EXTERNAL1 INPUT
# UARTs
# USART2 is telem1
PD6 USART2_RX USART2
PD5 USART2_TX USART2
PD3 USART2_CTS USART2
PD4 USART2_RTS USART2
# USART1 is GPS1
PB7 USART1_RX USART1 NODMA
PB6 USART1_TX USART1 NODMA
# USART3 is telem2
PD9 USART3_RX USART3
PD8 USART3_TX USART3
PD11 USART3_CTS USART3
PD12 USART3_RTS USART3
# UART4 GPS2
PD0 UART4_RX UART4 NODMA
PD1 UART4_TX UART4 NODMA
# USART6 is telem3
PG9 USART6_RX USART6 NODMA
PG14 USART6_TX USART6 NODMA
PG15 USART6_CTS USART6
PG8 USART6_RTS USART6
# UART7 is debug
PF6 UART7_RX UART7 NODMA
PE8 UART7_TX UART7 NODMA
# UART8 is for IOMCU
PE0 UART8_RX UART8
PE1 UART8_TX UART8
# UART for IOMCU
IOMCU_UART UART8
# enable RTSCTS support
define AP_FEATURE_RTSCTS 1
# enable SBUS_OUT on IOMCU (if you have an IOMCU)
define AP_FEATURE_SBUS_OUT 1
# PWM AUX channels
PE14 TIM1_CH4 TIM1 PWM(1) GPIO(50)
PA10 TIM1_CH3 TIM1 PWM(2) GPIO(51)
PE11 TIM1_CH2 TIM1 PWM(3) GPIO(52)
PE9 TIM1_CH1 TIM1 PWM(4) GPIO(53)
PD13 TIM4_CH2 TIM4 PWM(5) GPIO(54)
PD14 TIM4_CH3 TIM4 PWM(6) GPIO(55)
# we need to disable DMA on the last 2 FMU channels
# as timer 12 doesn't have a TIMn_UP DMA option
PH6 TIM12_CH1 TIM12 PWM(7) GPIO(56) NODMA
PH9 TIM12_CH2 TIM12 PWM(8) GPIO(57) NODMA
define BOARD_PWM_COUNT_DEFAULT 8
# PWM output for buzzer
PE5 TIM9_CH1 TIM9 GPIO(77) ALARM
#PWM Control RGB_LED
PH10 TIM5_CH1 TIM5 PWM(9) GPIO(81)
PH11 TIM5_CH2 TIM5 PWM(10) GPIO(82)
PH12 TIM5_CH3 TIM5 PWM(11) GPIO(83)
#IMU control temp
PA7 TIM3_CH2 TIM3 PWM(12) GPIO(84)
# setup for the possibility of an IMU heater as the pixhawk2 cube has
# an IMU header
define HAL_HAVE_IMU_HEATER 1
# analog in
PA0 BATT_VOLTAGE_SENS ADC1 SCALE(1)
PA1 BATT_CURRENT_SENS ADC1 SCALE(1)
PA2 BATT2_VOLTAGE_SENS ADC1 SCALE(1)
PA3 BATT2_CURRENT_SENS ADC1 SCALE(1)
PC4 SPARE1_ADC1 ADC1 SCALE(1)
PA4 SPARE2_ADC1 ADC1 SCALE(1)
PB0 RSSI_IN ADC1 SCALE(1)
#PC3 HW_REV_SENS ADC1 SCALE(1)
#PC2 HW_VER_SENS ADC1 SCALE(1)
PC0 VDD_5V_SENS ADC1 SCALE(2)
PC1 SCALED_V3V3 ADC1 SCALE(2)
# setup scaling defaults for PixHackV5 power brick
define HAL_BATT_VOLT_SCALE 18.0
define HAL_BATT_CURR_SCALE 24.0
define HAL_BATT_VOLT_PIN 0
define HAL_BATT_CURR_PIN 1
# CAN bus
PI9 CAN1_RX CAN1
PH13 CAN1_TX CAN1
PB12 CAN2_RX CAN2
PB13 CAN2_TX CAN2
# GPIOs
#PA7 HEATER_EN OUTPUT LOW GPIO(80)
#define HAL_HEATER_GPIO_PIN 80
PG1 VDD_BRICK_VALID INPUT PULLUP
PG2 VDD_BRICK2_VALID INPUT PULLUP
PG3 VBUS INPUT
PF13 VDD_5V_HIPOWER_OC INPUT PULLUP
PE15 VDD_5V_PERIPH_OC INPUT PULLUP
PB10 nSPI5_RESET_EXTERNAL1 OUTPUT HIGH
# capture pins
#PA5 FMU_CAP1 INPUT GPIO(58)
PB3 FMU_CAP2 INPUT GPIO(59)
PB11 FMU_CAP3 INPUT GPIO(60)
PI0 FMU_SPARE_4 INPUT GPIO(61)
# SPI devices
SPIDEV icm20602 SPI1 DEVID1 ICM20602_CS MODE3 2*MHZ 8*MHZ
SPIDEV bmi055_g SPI1 DEVID2 BMI055_G_CS MODE3 10*MHZ 10*MHZ
SPIDEV bmi055_a SPI1 DEVID3 BMI055_A_CS MODE3 10*MHZ 10*MHZ
SPIDEV ms5611 SPI1 DEVID4 MS5611_CS MODE3 20*MHZ 20*MHZ
#SPIDEV icm20689 SPI5 DEVID1 ICM20689_CS MODE3 2*MHZ 8*MHZ
SPIDEV spl06 SPI4 DEVID1 SPL06_CS MODE3 2*MHZ 8*MHZ
SPIDEV ramtron SPI2 DEVID1 FRAM_CS MODE3 8*MHZ 8*MHZ
# microSD support
PC8 SDMMC_D0 SDMMC1
PC9 SDMMC_D1 SDMMC1
PC10 SDMMC_D2 SDMMC1
PC11 SDMMC_D3 SDMMC1
PC12 SDMMC_CK SDMMC1
PD2 SDMMC_CMD SDMMC1
# enable RAMTROM parameter storage
define HAL_STORAGE_SIZE 16384
define HAL_WITH_RAMTRON 1
# allow to have have a dedicated safety switch pin
define HAL_HAVE_SAFETY_SWITCH 1
define HAL_BARO_DEFAULT HAL_BARO_MS5611_SPI
define HAL_COMPASS_DEFAULT HAL_COMPASS_NONE
define HAL_COMPASS_AUTO_ROT_DEFAULT 2
DMA_PRIORITY SDMMC* UART8* ADC* SPI* TIM*
#define CH_DBG_ENABLE_ASSERTS TRUE
#define CH_DBG_ENABLE_CHECKS TRUE
#define CH_DBG_SYSTEM_STATE_CHECK TRUE
#define CH_DBG_ENABLE_STACK_CHECK TRUE
# define HAL_SPI_CHECK_CLOCK_FREQ 1
# enable FAT filesystem support (needs a microSD defined via SDMMC)
define HAL_OS_FATFS_IO 1
define HAL_BOARD_LOG_DIRECTORY "/APM/LOGS"
define HAL_BOARD_TERRAIN_DIRECTORY "/APM/TERRAIN"
ROMFS io_firmware.bin Tools/IO_Firmware/fmuv2_IO.bin
FLYMAPLE is only supported on its separate [branch](https://github.com/ArduPilot/ardupilot/tree/master-AVR)