Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_BoardConfig/board_drivers.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15* AP_BoardConfig - driver loading and setup16*/171819#include <AP_HAL/AP_HAL.h>20#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS21#include <hal.h>22#endif23#include "AP_BoardConfig.h"24#include <GCS_MAVLink/GCS.h>25#include <AP_Math/crc.h>26#include <stdio.h>2728extern const AP_HAL::HAL& hal;2930/*31init safety state32*/33void AP_BoardConfig::board_init_safety()34{35bool force_safety_off = (state.safety_enable.get() == 0);36if (!force_safety_off && hal.util->was_watchdog_safety_off()) {37GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Forcing safety off for watchdog\n");38force_safety_off = true;39}40if (force_safety_off) {41hal.rcout->force_safety_off();42// wait until safety has been turned off43uint8_t count = 20;44while (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_ARMED && count--) {45hal.scheduler->delay(20);46}47}48}4950/*51init debug pins. We set debug pins as input if BRD_OPTIONS bit for debug enable is not set52this prevents possible ESD issues on the debug pins53*/54void AP_BoardConfig::board_init_debug()55{56#if !defined(HAL_BUILD_AP_PERIPH) && !defined(HAL_DEBUG_BUILD)57if ((_options & BOARD_OPTION_DEBUG_ENABLE) == 0) {58#ifdef HAL_GPIO_PIN_JTCK_SWCLK59palSetLineMode(HAL_GPIO_PIN_JTCK_SWCLK, PAL_MODE_INPUT);60#endif61#ifdef HAL_GPIO_PIN_JTMS_SWDIO62palSetLineMode(HAL_GPIO_PIN_JTMS_SWDIO, PAL_MODE_INPUT);63#endif64}65#endif // HAL_BUILD_AP_PERIPH && HAL_DEBUG_BUILD66}676869#if AP_FEATURE_BOARD_DETECT7071AP_BoardConfig::px4_board_type AP_BoardConfig::px4_configured_board;7273void AP_BoardConfig::board_setup_drivers(void)74{75if (state.board_type == PX4_BOARD_OLDDRIVERS) {76printf("Old drivers no longer supported\n");77state.board_type.set(PX4_BOARD_AUTO);78}7980// run board auto-detection81board_autodetect();8283px4_configured_board = (enum px4_board_type)state.board_type.get();8485switch (px4_configured_board) {86case PX4_BOARD_PX4V1:87case PX4_BOARD_PIXHAWK:88case PX4_BOARD_PIXHAWK2:89case PX4_BOARD_FMUV5:90case PX4_BOARD_FMUV6:91case PX4_BOARD_SP01:92case PX4_BOARD_PIXRACER:93case PX4_BOARD_PHMINI:94case PX4_BOARD_AUAV21:95case PX4_BOARD_PH2SLIM:96case VRX_BOARD_BRAIN51:97case VRX_BOARD_BRAIN52:98case VRX_BOARD_BRAIN52E:99case VRX_BOARD_UBRAIN51:100case VRX_BOARD_UBRAIN52:101case VRX_BOARD_CORE10:102case VRX_BOARD_BRAIN54:103case PX4_BOARD_AEROFC:104case PX4_BOARD_PIXHAWK_PRO:105case PX4_BOARD_PCNC1:106case PX4_BOARD_MINDPXV2:107case FMUV6_BOARD_HOLYBRO_6X:108case FMUV6_BOARD_HOLYBRO_6X_REV6:109case FMUV6_BOARD_HOLYBRO_6X_45686:110case FMUV6_BOARD_CUAV_6X:111break;112default:113config_error("Unknown board type");114break;115}116}117118#define SPI_PROBE_DEBUG 0119120/*121check a SPI device for a register value122*/123bool AP_BoardConfig::spi_check_register(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag)124{125auto dev = hal.spi->get_device(devname);126if (!dev) {127#if SPI_PROBE_DEBUG128hal.console->printf("%s: no device\n", devname);129#endif130return false;131}132dev->set_read_flag(read_flag);133WITH_SEMAPHORE(dev->get_semaphore());134dev->set_speed(AP_HAL::Device::SPEED_LOW);135uint8_t v;136if (!dev->read_registers(regnum, &v, 1)) {137#if SPI_PROBE_DEBUG138hal.console->printf("%s: reg %02x read fail\n", devname, (unsigned)regnum);139#endif140return false;141}142#if SPI_PROBE_DEBUG143hal.console->printf("%s: reg %02x expected:%02x got:%02x\n", devname, (unsigned)regnum, (unsigned)value, (unsigned)v);144#endif145return v == value;146}147148149#define INV2REG_BANK_SEL 0x7F150/*151check a SPI device for a register value152*/153bool AP_BoardConfig::spi_check_register_inv2(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag)154{155auto dev = hal.spi->get_device(devname);156if (!dev) {157#if SPI_PROBE_DEBUG158hal.console->printf("%s: no device\n", devname);159#endif160return false;161}162dev->set_read_flag(read_flag);163WITH_SEMAPHORE(dev->get_semaphore());164dev->set_speed(AP_HAL::Device::SPEED_LOW);165uint8_t v;166// select bank 0 for who am i167dev->write_register(INV2REG_BANK_SEL, 0, false);168if (!dev->read_registers(regnum, &v, 1)) {169#if SPI_PROBE_DEBUG170hal.console->printf("%s: reg %02x read fail\n", devname, (unsigned)regnum);171#endif172return false;173}174#if SPI_PROBE_DEBUG175hal.console->printf("%s: reg %02x expected:%02x got:%02x\n", devname, (unsigned)regnum, (unsigned)value, (unsigned)v);176#endif177return v == value;178}179180#if defined(HAL_VALIDATE_BOARD)181bool AP_BoardConfig::check_ms5611(const char* devname) {182auto dev = hal.spi->get_device(devname);183if (!dev) {184#if SPI_PROBE_DEBUG185hal.console->printf("%s: no device\n", devname);186#endif187return false;188}189190AP_HAL::Semaphore *dev_sem = dev->get_semaphore();191192if (!dev_sem) {193return false;194}195WITH_SEMAPHORE(dev_sem);196197static const uint8_t CMD_MS56XX_RESET = 0x1E;198static const uint8_t CMD_MS56XX_PROM = 0xA0;199200dev->transfer(&CMD_MS56XX_RESET, 1, nullptr, 0);201hal.scheduler->delay(4);202203uint16_t prom[8];204bool all_zero = true;205for (uint8_t i = 0; i < 8; i++) {206const uint8_t reg = CMD_MS56XX_PROM + (i << 1);207uint8_t val[2];208if (!dev->transfer(®, 1, val, sizeof(val))) {209#if SPI_PROBE_DEBUG210hal.console->printf("%s: transfer fail\n", devname);211#endif212return false;213}214prom[i] = (val[0] << 8) | val[1];215216if (prom[i] != 0) {217all_zero = false;218}219}220221uint16_t crc_read = prom[7]&0xf;222prom[7] &= 0xff00;223224if (crc_read != crc_crc4(prom) || all_zero) {225#if SPI_PROBE_DEBUG226hal.console->printf("%s: crc fail\n", devname);227#endif228return false;229}230231#if SPI_PROBE_DEBUG232hal.console->printf("%s: found successfully\n", devname);233#endif234235return true;236}237#endif // HAL_VALIDATE_BOARD238239#define MPUREG_WHOAMI 0x75240#define MPU_WHOAMI_MPU60X0 0x68241#define MPU_WHOAMI_MPU9250 0x71242#define MPU_WHOAMI_ICM20608 0xaf243#define MPU_WHOAMI_ICM20602 0x12244245#define LSMREG_WHOAMI 0x0f246#define LSM_WHOAMI_LSM303D 0x49247#define LSM_WHOAMI_L3GD20 0xd4248249#define INV2REG_WHOAMI 0x00250251#define INV2_WHOAMI_ICM20948 0xEA252#define INV2_WHOAMI_ICM20649 0xE1253254#define INV3REG_WHOAMI 0x75255#define INV3REG_456_WHOAMI 0x72256257#define INV3_WHOAMI_ICM42688 0x47258#define INV3_WHOAMI_ICM42670 0x67259#define INV3_WHOAMI_ICM45686 0xE9260#define INV3_WHOAMI_IIM42652 0x6f261262/*263validation of the board type264*/265void AP_BoardConfig::validate_board_type(void)266{267/* some boards can be damaged by the user setting the wrong board268type. The key one is the cube which has a heater which can269cook the IMUs if the user uses an old paramater file. We270override the board type for that specific case271*/272#if defined(HAL_CHIBIOS_ARCH_FMUV3)273if (state.board_type == PX4_BOARD_PIXHAWK &&274(spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||275spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||276spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||277spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||278spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&279(spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||280spi_check_register_inv2("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {281// Pixhawk2 has LSM303D and MPUxxxx on external bus. If we282// detect those, then force PIXHAWK2, even if the user has283// configured for PIXHAWK1284#if !defined(HAL_CHIBIOS_ARCH_FMUV3)285// force user to load the right firmware286config_error("Pixhawk2 requires FMUv3 firmware");287#endif288state.board_type.set(PX4_BOARD_PIXHAWK2);289DEV_PRINTF("Forced PIXHAWK2\n");290}291#endif292}293294/*295auto-detect board type296*/297void AP_BoardConfig::board_autodetect(void)298{299#if defined(HAL_VALIDATE_BOARD)300if((_options & SKIP_BOARD_VALIDATION) == 0) {301const char* errored_check = HAL_VALIDATE_BOARD;302if (errored_check == nullptr) {303return;304} else {305config_error("Board Validation %s Failed", errored_check);306return;307}308}309#endif310311if (state.board_type != PX4_BOARD_AUTO) {312validate_board_type();313// user has chosen a board type314return;315}316317#if defined(HAL_CHIBIOS_ARCH_FMUV3)318if ((spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||319spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||320spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||321spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||322spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||323spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||324spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&325(spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||326spi_check_register_inv2("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {327// Pixhawk2 has LSM303D and MPUxxxx on external bus328state.board_type.set(PX4_BOARD_PIXHAWK2);329DEV_PRINTF("Detected PIXHAWK2\n");330} else if ((spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||331spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&332spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250)) {333// PHMINI has an ICM20608 and MPU9250 on sensor bus334state.board_type.set(PX4_BOARD_PHMINI);335DEV_PRINTF("Detected PixhawkMini\n");336} else if (spi_check_register("lsm9ds0_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) &&337(spi_check_register("mpu6000", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||338spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||339spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||340spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250))) {341342// classic or upgraded Pixhawk1343state.board_type.set(PX4_BOARD_PIXHAWK);344DEV_PRINTF("Detected Pixhawk\n");345} else {346config_error("Unable to detect board type");347}348#elif defined(HAL_CHIBIOS_ARCH_FMUV4)349// only one choice350state.board_type.set_and_notify(PX4_BOARD_PIXRACER);351DEV_PRINTF("Detected Pixracer\n");352#elif defined(HAL_CHIBIOS_ARCH_MINDPXV2)353// only one choice354state.board_type.set_and_notify(PX4_BOARD_MINDPXV2);355DEV_PRINTF("Detected MindPX-V2\n");356#elif defined(HAL_CHIBIOS_ARCH_FMUV4PRO)357// only one choice358state.board_type.set_and_notify(PX4_BOARD_PIXHAWK_PRO);359DEV_PRINTF("Detected Pixhawk Pro\n");360#elif defined(HAL_CHIBIOS_ARCH_FMUV5)361state.board_type.set_and_notify(PX4_BOARD_FMUV5);362DEV_PRINTF("Detected FMUv5\n");363#elif defined(HAL_CHIBIOS_ARCH_FMUV6)364detect_fmuv6_variant();365#elif defined(HAL_CHIBIOS_ARCH_BRAINV51)366state.board_type.set_and_notify(VRX_BOARD_BRAIN51);367DEV_PRINTF("Detected VR Brain 5.1\n");368#elif defined(HAL_CHIBIOS_ARCH_BRAINV52)369state.board_type.set_and_notify(VRX_BOARD_BRAIN52);370DEV_PRINTF("Detected VR Brain 5.2\n");371#elif defined(HAL_CHIBIOS_ARCH_UBRAINV51)372state.board_type.set_and_notify(VRX_BOARD_UBRAIN51);373DEV_PRINTF("Detected VR Micro Brain 5.1\n");374#elif defined(HAL_CHIBIOS_ARCH_COREV10)375state.board_type.set_and_notify(VRX_BOARD_CORE10);376DEV_PRINTF("Detected VR Core 1.0\n");377#elif defined(HAL_CHIBIOS_ARCH_BRAINV54)378state.board_type.set_and_notify(VRX_BOARD_BRAIN54);379DEV_PRINTF("Detected VR Brain 5.4\n");380#endif381382}383384#endif // AP_FEATURE_BOARD_DETECT385386/*387setup flow control on UARTs388*/389void AP_BoardConfig::board_setup_uart()390{391#if AP_FEATURE_RTSCTS392#ifdef HAL_HAVE_RTSCTS_SERIAL1393if (hal.serial(1) != nullptr) {394hal.serial(1)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[1].get());395}396#endif397#ifdef HAL_HAVE_RTSCTS_SERIAL2398if (hal.serial(2) != nullptr) {399hal.serial(2)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[2].get());400}401#endif402#ifdef HAL_HAVE_RTSCTS_SERIAL3403if (hal.serial(3) != nullptr) {404hal.serial(3)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[3].get());405}406#endif407#ifdef HAL_HAVE_RTSCTS_SERIAL4408if (hal.serial(4) != nullptr) {409hal.serial(4)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[4].get());410}411#endif412#ifdef HAL_HAVE_RTSCTS_SERIAL5413if (hal.serial(5) != nullptr) {414hal.serial(5)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[5].get());415}416#endif417#endif418}419420/*421setup SBUS422*/423void AP_BoardConfig::board_setup_sbus(void)424{425#if AP_FEATURE_SBUS_OUT426if (state.sbus_out_rate.get() >= 1) {427static const struct {428uint8_t value;429uint16_t rate;430} rates[] = {431{ 1, 50 },432{ 2, 75 },433{ 3, 100 },434{ 4, 150 },435{ 5, 200 },436{ 6, 250 },437{ 7, 300 }438};439uint16_t rate = 300;440for (uint8_t i=0; i<ARRAY_SIZE(rates); i++) {441if (rates[i].value == state.sbus_out_rate) {442rate = rates[i].rate;443}444}445if (!hal.rcout->enable_px4io_sbus_out(rate)) {446hal.console->printf("Failed to enable SBUS out\n");447}448}449#endif450}451452453/*454setup peripherals and drivers455*/456void AP_BoardConfig::board_setup()457{458#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS459// init needs to be done after boardconfig is read so parameters are set460hal.gpio->init();461hal.rcin->init();462hal.rcout->init();463#endif464465#ifdef HAL_GPIO_PWM_VOLT_PIN466if (_pwm_volt_sel == 0) {467hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, HAL_GPIO_PWM_VOLT_3v3); //set pin for 3.3V PWM Output468} else if (_pwm_volt_sel == 1) {469hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, !HAL_GPIO_PWM_VOLT_3v3); //set pin for 5V PWM Output470}471#endif472board_setup_uart();473board_setup_sbus();474#if AP_FEATURE_BOARD_DETECT475board_setup_drivers();476#endif477}478479480#ifdef HAL_CHIBIOS_ARCH_FMUV6481482#define BMI088REG_CHIPID 0x00483#define CHIPID_BMI088_G 0x0F484485/*486detect which FMUV6 variant we are running on487*/488void AP_BoardConfig::detect_fmuv6_variant()489{490if (((spi_check_register_inv2("icm20649", INV2REG_WHOAMI, INV2_WHOAMI_ICM20649) ||491spi_check_register("bmi088_g", BMI088REG_CHIPID, CHIPID_BMI088_G)) && // alternative config492spi_check_register("icm42688", INV3REG_WHOAMI, INV3_WHOAMI_ICM42688) &&493spi_check_register("icm42670", INV3REG_WHOAMI, INV3_WHOAMI_ICM42670))) {494state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X);495DEV_PRINTF("Detected Holybro 6X\n");496} else if ((spi_check_register_inv2("icm20649_2", INV2REG_WHOAMI, INV2_WHOAMI_ICM20649) &&497spi_check_register("icm42688", INV3REG_WHOAMI, INV3_WHOAMI_ICM42688) &&498spi_check_register("bmi088_g", BMI088REG_CHIPID, CHIPID_BMI088_G))) {499state.board_type.set_and_notify(FMUV6_BOARD_CUAV_6X);500DEV_PRINTF("Detected CUAV 6X\n");501AP_Param::load_defaults_file("@ROMFS/param/CUAV_V6X_defaults.parm", false);502} else if (spi_check_register("icm45686-1", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686) &&503spi_check_register("icm45686-2", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686) &&504spi_check_register("icm45686-3", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686)) {505state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X_45686);506DEV_PRINTF("Detected Holybro 6X_45686\n");507} else if (spi_check_register("iim42652", INV3REG_WHOAMI, INV3_WHOAMI_IIM42652) &&508spi_check_register("icm45686", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686)) {509state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X_REV6);510DEV_PRINTF("Detected Holybro 6X_Rev6\n");511}512}513#endif514515516