# hw definition file for processing by chibios_hwdef.py
# for the Pixhawk5X hardware

# MCU class and specific type
MCU STM32F7xx STM32F767xx

# crystal frequency
OSCILLATOR_HZ 16000000

# USB setup
USB_VENDOR 0x3162 # ONLY FOR USE BY Holybro
USB_PRODUCT 0x0051
USB_STRING_MANUFACTURER "Holybro"

# board ID for firmware load
APJ_BOARD_ID 51

FLASH_RESERVE_START_KB 32

# flash size
FLASH_SIZE_KB 2048

env OPTIMIZE -O2

# order of UARTs (and USB)
SERIAL_ORDER OTG1 UART7 UART5 USART1 UART8 USART2 UART4 USART3 OTG2

# default the 2nd interface to MAVLink2
define HAL_OTG2_PROTOCOL SerialProtocol_MAVLink2

# default to all pins low to avoid ESD issues
DEFAULTGPIO OUTPUT LOW PULLDOWN

# USB
PA11 OTG_FS_DM OTG1
PA12 OTG_FS_DP OTG1
PA9 VBUS INPUT OPENDRAIN

# pins for SWD debugging
PA13 JTMS-SWDIO SWD
PA14 JTCK-SWCLK SWD

# ethernet (not implemented yet)
#PA1 ETH_RMII_REF_CLK ETH
#PA2 ETH_MDIO         ETH
#PA7 ETH_RMII_CRS_DV  ETH
#PB11 ETH_RMII_TX_EN OUTPUT LOW
#PB13 ETH_RMII_TXD1 ETH
#PC1  ETH_MDC ETH
#PC4 ETH_RMII_RXD0 ETH
#PC5 ETH_RMII_RXD1 ETH
#PG15 ETH_PWR_EN OUTPUT LOW
#PG13 ETH_RMII_TXD0 ETH

# ADC
PA0 SCALED1_V3V3 ADC1 SCALE(2)
PA4 SCALED2_V3V3 ADC1 SCALE(2)
PB0 SCALED3_V3V3 ADC1 SCALE(2)
PC2 SCALED4_V3V3 ADC1 SCALE(2)
PB1 VDD_5V_SENS  ADC1 SCALE(2)
PC0 ADC1_6V6 ADC1 SCALE(2)
PC3 ADC1_3V3 ADC1 SCALE(1)

# SPI1 - ICM20602
PA5 SPI1_SCK SPI1
PB4 SPI1_MISO SPI1
PB5 SPI1_MOSI SPI1
PI9 ICM20602_CS CS

# SPI2 - ICM42688
PI1 SPI2_SCK SPI2
PI2 SPI2_MISO SPI2
PI3 SPI2_MOSI SPI2
PH5 ICM42688_CS CS
PH12 ICM42688_DRDY INPUT

# SPI3 - ICM20649
PB2  SPI3_MOSI SPI3
PC10 SPI3_SCK  SPI3
PC11 SPI3_MISO SPI3
PI4 ICM20649_CS CS
PI8 SENSORS3_CS2 CS
PI6 SENSORS3_DRDY2 INPUT
PI7 ICM20649_DRDY INPUT

# SPI4 - unused?
#PE12 SPI4_SCK SPI4
#PE13 SPI4_MISO SPI4
#PE6  SPI4_MOSI SPI4
#PF3  DRDY1_BMM150 INPUT
PH15 BMM150_CS CS

# SPI5 - FRAM
PF7  SPI5_SCK  SPI5
PF8  SPI5_MISO SPI5
PF11 SPI5_MOSI SPI5
PG7 FRAM_CS CS


# SPI6 - external1 (disabled to save DMA channels)
# PB3  SPI6_SCK SPI6
# PA6  SPI6_MISO SPI6
# PG14 SPI6_MOSI SPI6

# CS and GPIOs for SPI6 EXT1
PI10 EXT1_CS1 CS
PA15 EXT1_CS2 CS
PD11 DRDY1_EXT1 INPUT
PD12 DRDY2_EXT1 INPUT
PF10 nRESET_EXT1 OUTPUT HIGH

# PWM output pins
PE14 TIM1_CH4 TIM1 PWM(1) GPIO(50)
PA10 TIM1_CH3 TIM1 PWM(2) GPIO(51)
PE11 TIM1_CH2 TIM1 PWM(3) GPIO(52)
PA8  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


# CAN bus
PD0  CAN1_RX CAN1
PD1  CAN1_TX CAN1

PB12 CAN2_RX CAN2
PB6  CAN2_TX CAN2


# I2C buses

# I2C1, GPS+MAG
PB7 I2C1_SDA I2C1
PB8 I2C1_SCL I2C1

# I2C2, GPS2+MAG
PF1 I2C2_SCL I2C2
PF0 I2C2_SDA I2C2
PG5 DRDY1_BMP388 INPUT

# I2C3, MS5611, external
PH7 I2C3_SCL I2C3
PH8 I2C3_SDA I2C3

# I2C4 internal
PF14 I2C4_SCL I2C4
PF15 I2C4_SDA I2C4

# this board is very tight on DMA channels. To allow for more UART DMA
# we disable DMA on I2C
NODMA I2C*
define STM32_I2C_USE_DMA FALSE

# order of I2C buses
I2C_ORDER I2C4 I2C1 I2C2 I2C3
define HAL_I2C_INTERNAL_MASK 1

# telem1
PF6  UART7_RX  UART7
PE8  UART7_TX  UART7
PE10 UART7_CTS UART7
PE9  UART7_RTS UART7

# telem2
PB9 UART5_TX  UART5
PD2 UART5_RX  UART5
PC9 UART5_CTS UART5
PC8 UART5_RTS UART5

# telem3
PA3 USART2_RX USART2
PD5 USART2_TX USART2
PD3 USART2_CTS USART2
PD4 USART2_RTS USART2

# USART1 is GPS1
PB15 USART1_RX USART1 NODMA
PB14 USART1_TX USART1 NODMA

# USART3 is debug
PD8 USART3_TX USART3 NODMA
PD9 USART3_RX USART3 NODMA

# UART8 is GPS2
PE0 UART8_RX UART8 NODMA
PE1 UART8_TX UART8 NODMA

# UART4 spare
PH13 UART4_TX UART4 NODMA
PH14 UART4_RX UART4 NODMA

# USART6 is for IOMCU
PC6 USART6_TX USART6
PC7 USART6_RX USART6
IOMCU_UART USART6

# heater
PB10 HEATER_EN OUTPUT LOW GPIO(80)
define HAL_HEATER_GPIO_PIN 80

# Setup the IMU heater
define HAL_HAVE_IMU_HEATER 1
define HAL_IMU_TEMP_DEFAULT 45
define HAL_IMUHEAT_P_DEFAULT 50
define HAL_IMUHEAT_I_DEFAULT 0.07

# armed indication
PC12 nARMED OUTPUT HIGH

# power enable pins
PC13 VDD_3V3_SD_CARD_EN OUTPUT HIGH
PI11 VDD_3V3_SENSORS1_EN OUTPUT HIGH
PD15 VDD_3V3_SENSORS2_EN OUTPUT HIGH
PE7  VDD_3V3_SENSORS3_EN OUTPUT HIGH
PG8  VDD_3V3_SENSORS4_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

# Control of Spektrum power pin
PH2 SPEKTRUM_PWR OUTPUT HIGH GPIO(73)
define HAL_GPIO_SPEKTRUM_PWR 73

# Spektrum Power is Active High
define HAL_SPEKTRUM_PWR_ENABLED 1

# power sensing
PE15 VDD_5V_PERIPH_nOC INPUT PULLUP
PF13 VDD_5V_HIPOWER_nOC INPUT PULLUP
PG1 VDD_BRICK_nVALID INPUT PULLUP
PG2 VDD_BRICK2_nVALID INPUT PULLUP
PG3 VDD_BRICK3_nVALID INPUT PULLUP

# microSD support
PD6  SDMMC2_CK SDMMC2
PD7  SDMMC2_CMD SDMMC2
PG9  SDMMC2_D0 SDMMC2
PG10 SDMMC2_D1 SDMMC2
PG11 SDMMC2_D2 SDMMC2
PG12 SDMMC2_D3 SDMMC2
define FATFS_HAL_DEVICE SDCD2

# safety
PD10 LED_SAFETY OUTPUT
PH4 SAFETY_IN INPUT PULLDOWN

# LEDs
PE3 LED_RED OUTPUT OPENDRAIN GPIO(90) HIGH
PE4 LED_GREEN OUTPUT OPENDRAIN GPIO(91) HIGH
PE5 LED_BLUE OUTPUT OPENDRAIN GPIO(92) HIGH

# setup for BoardLED2
define HAL_GPIO_A_LED_PIN 90
define HAL_GPIO_B_LED_PIN 92
define HAL_GPIO_LED_ON 0

# ID pins
PG0 HW_VER_REV_DRIVE OUTPUT LOW
# PF5 HW_REV_SENS ADC1 SCALE(1)
# PF4 HW_VER_SENS ADC1 SCALE(1)

# PWM output for buzzer
PF9 TIM14_CH1 TIM14 GPIO(77) ALARM

# GPIOs
PI0 FMU_CAP1 INPUT GPIO(58)
PG6 FMU_CAP2 INPUT GPIO(59)
PH3 NFC_GPIO INPUT GPIO(60)
PH11 FMU_CAN2 INPUT GPIO(61)
PH10 GPIO_SPIX_SYNC INPUT

# RC input
PI5 TIM8_CH1 TIM8 RCININT PULLDOWN LOW

# other I2C devices
# SE050C1HQ1 security, address 0x48 on I2C4
# 24LC64T eeprom 64Kbit, address 0x50 on I2C4

# barometers
BARO BMP388 I2C:2:0x76
BARO BMP388 I2C:0:0x77

# compass
define HAL_PROBE_EXTERNAL_I2C_COMPASSES
COMPASS BMM150 I2C:0:0x10 false ROTATION_NONE
# COMPASS VCM5883 I2C:0:0x18 false ROTATION_NONE

# SPI devices
SPIDEV icm20602  SPI1 DEVID1  ICM20602_CS MODE3  2*MHZ  8*MHZ
SPIDEV icm42688  SPI2 DEVID1  ICM42688_CS MODE3  2*MHZ  8*MHZ
SPIDEV ramtron   SPI5 DEVID1  FRAM_CS     MODE3  8*MHZ  8*MHZ
SPIDEV icm20649  SPI3 DEVID1  ICM20649_CS MODE3  10*MHZ 10*MHZ

# 3 IMUs
IMU Invensensev3 SPI:icm42688 ROTATION_PITCH_180_YAW_90
IMU Invensensev2 SPI:icm20649 ROTATION_YAW_180
IMU Invensense SPI:icm20602 ROTATION_PITCH_180

define HAL_DEFAULT_INS_FAST_SAMPLE 7

# enable RAMTROM parameter storage
define HAL_STORAGE_SIZE 32768
define HAL_WITH_RAMTRON 1

# INA226 battery monitor
define HAL_BATTMON_INA2XX_BUS 1
define HAL_BATTMON_INA2XX_ADDR 0x41
define HAL_BATT_MONITOR_DEFAULT 21

# allow to have have a dedicated safety switch pin
define HAL_HAVE_SAFETY_SWITCH 1

DMA_PRIORITY SDMMC* USART6* ADC* UART* USART* SPI* TIM*

# 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/iofirmware_lowpolh.bin
