Path: blob/master/libraries/AP_BoardConfig/AP_BoardConfig.h
9573 views
#pragma once12#include "AP_BoardConfig_config.h"3#include <AP_Common/AP_Common.h>4#include <AP_Param/AP_Param.h>5#include <AP_RTC/AP_RTC.h>6#include <AC_PID/AC_PI.h>7#include <AP_Radio/AP_Radio_config.h>89#if AP_RADIO_ENABLED10#include <AP_Radio/AP_Radio.h>11#endif1213extern "C" typedef int (*main_fn_t)(int argc, char **);1415class AP_BoardConfig {16public:17AP_BoardConfig();1819/* Do not allow copies */20CLASS_NO_COPY(AP_BoardConfig);2122// singleton support23static AP_BoardConfig *get_singleton(void) {24return _singleton;25}2627void init(void);28void init_safety(void);2930static const struct AP_Param::GroupInfo var_info[];3132// notify user of a fatal startup error related to available sensors.33static void config_error(const char *reason, ...) FMT_PRINTF(1, 2) NORETURN;3435// notify user of a non-fatal startup error related to allocation failures.36static void allocation_error(const char *reason, ...) FMT_PRINTF(1, 2) NORETURN;3738// permit other libraries (in particular, GCS_MAVLink) to detect39// that we're never going to boot properly:40static bool in_config_error(void) { return _in_error_loop; }4142// valid types for BRD_TYPE: these values need to be in sync with the43// values from the param description44enum px4_board_type {45BOARD_TYPE_UNKNOWN = -1,46PX4_BOARD_AUTO = 0,47PX4_BOARD_PX4V1 = 1,48PX4_BOARD_PIXHAWK = 2,49PX4_BOARD_PIXHAWK2 = 3,50// PX4_BOARD_PIXRACER = 4,51PX4_BOARD_PHMINI = 5,52PX4_BOARD_PH2SLIM = 6,53PX4_BOARD_AEROFC = 13,54// PX4_BOARD_PIXHAWK_PRO = 14,55PX4_BOARD_AUAV21 = 20,56// PX4_BOARD_PCNC1 = 21,57// PX4_BOARD_MINDPXV2 = 22,58// PX4_BOARD_SP01 = 23,59// PX4_BOARD_FMUV5 = 24,60// VRX_BOARD_BRAIN51 = 30,61// VRX_BOARD_BRAIN52 = 32,62// VRX_BOARD_BRAIN52E = 33,63// VRX_BOARD_UBRAIN51 = 34,64// VRX_BOARD_UBRAIN52 = 35,65// VRX_BOARD_CORE10 = 36,66// VRX_BOARD_BRAIN54 = 38,67PX4_BOARD_FMUV6 = 39,68FMUV6_BOARD_HOLYBRO_6X = 40,69FMUV6_BOARD_CUAV_6X = 41,70FMUV6_BOARD_HOLYBRO_6X_REV6 = 42,71FMUV6_BOARD_HOLYBRO_6X_45686 = 43,72PX4_BOARD_OLDDRIVERS = 100,73};7475// set default value for BRD_SAFETY_MASK76void set_default_safety_ignore_mask(uint32_t mask);7778static enum px4_board_type get_board_type(void) {79#if AP_FEATURE_BOARD_DETECT80return px4_configured_board;81#else82return BOARD_TYPE_UNKNOWN;83#endif84}8586// ask if IOMCU is enabled. This is a uint8_t to allow87// developer debugging by setting BRD_IO_ENABLE=100 to avoid the88// crc check of IO firmware on startup89static uint8_t io_enabled(void) {90#if HAL_WITH_IO_MCU91return _singleton?uint8_t(_singleton->state.io_enable.get()):0;92#else93return 0;94#endif95}9697static bool io_dshot(void) {98#if HAL_WITH_IO_MCU_DSHOT99return io_enabled() && _singleton?_singleton->state.io_dshot.get():false;100#else101return false;102#endif103}104105// get alternative config selection106uint8_t get_alt_config(void) {107return uint8_t(_alt_config.get());108}109110enum board_safety_button_option {111BOARD_SAFETY_OPTION_BUTTON_ACTIVE_SAFETY_OFF= (1 << 0),112BOARD_SAFETY_OPTION_BUTTON_ACTIVE_SAFETY_ON= (1 << 1),113BOARD_SAFETY_OPTION_BUTTON_ACTIVE_ARMED= (1 << 2),114BOARD_SAFETY_OPTION_SAFETY_ON_DISARM= (1 << 3),115};116117// return safety button options. Bits are in enum board_safety_button_option118uint16_t get_safety_button_options(void) const {119return uint16_t(state.safety_option.get());120}121122// return the value of BRD_SAFETY_MASK123uint16_t get_safety_mask(void) const {124return uint32_t(state.ignore_safety_channels.get());125}126127uint32_t get_serial_number() const {128return (uint32_t)vehicleSerialNumber.get();129}130131#if HAL_HAVE_BOARD_VOLTAGE132// get minimum board voltage133static float get_minimum_board_voltage(void) {134return _singleton?_singleton->_vbus_min.get():0;135}136#endif137138#if HAL_HAVE_SERVO_VOLTAGE139// get minimum servo voltage140static float get_minimum_servo_voltage(void) {141return _singleton?_singleton->_vservo_min.get():0;142}143#endif144145#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS146static uint8_t get_sdcard_slowdown(void) {147return _singleton?_singleton->_sdcard_slowdown.get():0;148}149#endif150151#if AP_CPU_IDLE_STATS_ENABLED152static bool use_idle_stats(void) {153return _singleton?_singleton->state.idle_stats.get():0;154}155#endif156157enum board_options {158BOARD_OPTION_WATCHDOG = (1 << 0),159DISABLE_FTP = (1<<1),160ALLOW_SET_INTERNAL_PARM = (1<<2),161BOARD_OPTION_DEBUG_ENABLE = (1<<3),162UNLOCK_FLASH = (1<<4),163WRITE_PROTECT_FLASH = (1<<5),164WRITE_PROTECT_BOOTLOADER = (1<<6),165SKIP_BOARD_VALIDATION = (1<<7),166DISABLE_ARMING_GPIO = (1<<8),167IO_SAFETY_PINS_AS_PROFILED = (1<<9),168};169170//return true if arming gpio output is disabled171static bool arming_gpio_disabled(void) {172return _singleton?(_singleton->_options & DISABLE_ARMING_GPIO)!=0:1;173}174175#ifndef HAL_ARM_GPIO_POL_INVERT176#define HAL_ARM_GPIO_POL_INVERT 0177#endif178179// return true if ftp is disabled180static bool ftp_disabled(void) {181return _singleton?(_singleton->_options & DISABLE_FTP)!=0:1;182}183184// return true if watchdog enabled185static bool watchdog_enabled(void) {186return _singleton?(_singleton->_options & BOARD_OPTION_WATCHDOG)!=0:HAL_WATCHDOG_ENABLED_DEFAULT;187}188189// return true if flash should be unlocked190static bool unlock_flash(void) {191return _singleton && (_singleton->_options & UNLOCK_FLASH) != 0;192}193194// return true if flash should be write protected195static bool protect_flash(void) {196return _singleton && (_singleton->_options & WRITE_PROTECT_FLASH) != 0;197}198199// return true if bootloader should be write protected200static bool protect_bootloader(void) {201return _singleton && (_singleton->_options & WRITE_PROTECT_BOOTLOADER) != 0;202}203204// return true if we allow setting of internal parameters (for developers)205static bool allow_set_internal_parameters(void) {206return _singleton?(_singleton->_options & ALLOW_SET_INTERNAL_PARM)!=0:false;207}208209#if HAL_WITH_IO_MCU210static bool use_safety_as_led(void) {211return _singleton?(_singleton->_options & IO_SAFETY_PINS_AS_PROFILED)!=0:false;212}213#endif214215// handle press of safety button. Return true if safety state216// should be toggled217bool safety_button_handle_pressed(uint8_t press_count);218219#if HAL_HAVE_IMU_HEATER220void set_imu_temp(float current_temp_c);221222// heater duty cycle is as a percentage (0 to 100)223float get_heater_duty_cycle(void) const {224return heater.output;225}226227// getters for current temperature and min arming temperature, return false if heater disabled228bool get_board_heater_temperature(float &temperature) const;229bool get_board_heater_arming_temperature(int8_t &temperature) const;230#endif231232#if AP_SDCARD_STORAGE_ENABLED233// return number of kb of mission storage to use on microSD234static uint16_t get_sdcard_mission_kb(void) {235return _singleton? _singleton->sdcard_storage.mission_kb.get() : 0;236}237238// return number of kb of fence storage to use on microSD239static uint16_t get_sdcard_fence_kb(void) {240return _singleton? _singleton->sdcard_storage.fence_kb.get() : 0;241}242#endif243244private:245static AP_BoardConfig *_singleton;246247AP_Int32 vehicleSerialNumber;248249struct {250AP_Int8 safety_enable;251AP_Int16 safety_option;252AP_Int32 ignore_safety_channels;253#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS254AP_Int8 ser_rtscts[9];255AP_Int8 sbus_out_rate;256#endif257#if AP_CPU_IDLE_STATS_ENABLED258AP_Int8 idle_stats;259#endif260AP_Int8 board_type;261AP_Int8 io_enable;262AP_Int8 io_dshot;263} state;264265#if AP_SDCARD_STORAGE_ENABLED266struct {267AP_Int16 mission_kb;268AP_Int16 fence_kb;269} sdcard_storage;270#endif271272#if AP_FEATURE_BOARD_DETECT273static enum px4_board_type px4_configured_board;274275void board_setup_drivers(void);276bool spi_check_register(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag = 0x80);277bool spi_check_register_inv2(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag = 0x80);278void validate_board_type(void);279void board_autodetect(void);280void detect_fmuv6_variant(void);281bool check_ms5611(const char* devname);282283#endif // AP_FEATURE_BOARD_DETECT284285void board_init_safety(void);286void board_init_debug(void);287288void board_setup_uart(void);289void board_setup_sbus(void);290void board_setup(void);291292// common method to throw errors293static void throw_error(const char *err_str, const char *fmt, va_list arg) NORETURN;294295static bool _in_error_loop;296297#if HAL_HAVE_IMU_HEATER298struct {299AC_PI pi_controller;300AP_Int8 imu_target_temperature;301uint32_t last_update_ms;302uint16_t count;303float sum;304float output;305uint32_t last_log_ms;306float temperature;307AP_Int8 imu_arming_temperature_margin_low;308} heater;309#endif310311#if AP_RADIO_ENABLED312// direct attached radio313AP_Radio _radio;314#endif315316#if AP_RTC_ENABLED317// real-time-clock; private because access is via the singleton318AP_RTC rtc;319#endif320321#if HAL_HAVE_BOARD_VOLTAGE322AP_Float _vbus_min;323#endif324325#if HAL_HAVE_SERVO_VOLTAGE326AP_Float _vservo_min;327#endif328329AP_Int8 _pwm_volt_sel;330331#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS332AP_Int8 _sdcard_slowdown;333#endif334335AP_Int16 _boot_delay_ms;336337AP_Int32 _options;338339AP_Int8 _alt_config;340};341342namespace AP {343AP_BoardConfig *boardConfig(void);344};345346347