# hw definition file for processing by chibios_pins.py
# for Matek H743-WING

# MCU class and specific type
MCU STM32H7xx STM32H743xx

# board ID for firmware load
APJ_BOARD_ID 1013

# crystal frequency, setup to use external oscillator
OSCILLATOR_HZ 8000000

FLASH_SIZE_KB 2048
env OPTIMIZE -Os

# bootloader takes first sector
FLASH_RESERVE_START_KB 128


# ChibiOS system timer
STM32_ST_USE_TIMER 12
define CH_CFG_ST_RESOLUTION 16

# USB
PA11 OTG_FS_DM OTG1
PA12 OTG_FS_DP OTG1

PA13 JTMS-SWDIO SWD
PA14 JTCK-SWCLK SWD

# SPI1 for IMU1 (MPU6000)
PA5 SPI1_SCK SPI1
PA6 SPI1_MISO SPI1
PD7 SPI1_MOSI SPI1
PC15 IMU1_CS CS

# SPI2 for MAX7456 OSD
PB12 MAX7456_CS CS
PB13 SPI2_SCK SPI2
PB14 SPI2_MISO SPI2
PB15 SPI2_MOSI SPI2

# SPI3 - external
PB3 SPI3_SCK SPI3
PB4 SPI3_MISO SPI3
PB5 SPI3_MOSI SPI3

# external CS pins
PD4 EXT_CS1 CS
PE2 EXT_CS2 CS

# SPI4 for IMU2 (ICM20602)
PE11 IMU2_CS CS
PE12 SPI4_SCK SPI4
PE13 SPI4_MISO SPI4
PE14 SPI4_MOSI SPI4

# add ICM42605 on SPI4 as a backup to replace ICM20602
PC13 IMU3_CS CS

# two I2C bus
I2C_ORDER I2C2 I2C1

# I2C1
PB6 I2C1_SCL I2C1
PB7 I2C1_SDA I2C1

# I2C2
PB10 I2C2_SCL I2C2
PB11 I2C2_SDA I2C2

# ADC
PC0 BATT_VOLTAGE_SENS ADC1 SCALE(1)
PC1 BATT_CURRENT_SENS ADC1 SCALE(1)
PA4 BATT2_VOLTAGE_SENS ADC1 SCALE(1)
PA7 BATT2_CURRENT_SENS ADC1 SCALE(1)

define HAL_BATT_MONITOR_DEFAULT 4
define HAL_BATT_VOLT_PIN 10
define HAL_BATT_CURR_PIN 11
define HAL_BATT2_VOLT_PIN 18
define HAL_BATT2_CURR_PIN 7
define HAL_BATT_VOLT_SCALE 11.0
define HAL_BATT_CURR_SCALE 40.0
define HAL_BATT2_VOLT_SCALE 11.0

PC4 PRESSURE_SENS ADC1 SCALE(2)
define HAL_DEFAULT_AIRSPEED_PIN 4

PC5 RSSI_ADC ADC1
define BOARD_RSSI_ANA_PIN 8

# LED
# green LED1 marked as B/E
# blue LED0 marked as ACT
PE3 LED0 OUTPUT LOW GPIO(90) # blue
PE4 LED1 OUTPUT LOW GPIO(91) # green
define HAL_GPIO_A_LED_PIN 91
define HAL_GPIO_B_LED_PIN 90
define HAL_GPIO_LED_OFF 1

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

# USART1 (telem2)
PA10 USART1_RX USART1
PA9  USART1_TX USART1

# USART2 (GPS1)
PD5 USART2_TX USART2
PD6 USART2_RX USART2

# USART3 (GPS2)
PD9 USART3_RX USART3
PD8 USART3_TX USART3

# UART4 (spare)
PB9 UART4_TX UART4
PB8 UART4_RX UART4

# USART6 (RC input), SERIAL7
PC7 TIM3_CH2 TIM3 RCININT PULLDOWN LOW
PC6 USART6_TX USART6 NODMA

# as an alternative config setup the RX6 pin as a uart. This allows
# for bi-directional UART based receiver protocols such as FPort
# without any extra hardware
PC7 USART6_RX USART6 NODMA ALT(1)

# UART7 (telem1)
PE7 UART7_RX UART7
PE8 UART7_TX UART7
PE10 UART7_CTS UART7
PE9  UART7_RTS UART7

# UART8 (spare)
PE0 UART8_RX UART8
PE1 UART8_TX UART8

# CAN bus
PD0 CAN1_RX CAN1
PD1 CAN1_TX CAN1
PD3 GPIO_CAN1_SILENT OUTPUT PUSHPULL SPEED_LOW LOW GPIO(70)

# Motors
PB0  TIM8_CH2N TIM8  PWM(1)  GPIO(50)
PB1  TIM8_CH3N TIM8  PWM(2)  GPIO(51)
PA0  TIM5_CH1  TIM5  PWM(3)  GPIO(52)
PA1  TIM5_CH2  TIM5  PWM(4)  GPIO(53)
PA2  TIM5_CH3  TIM5  PWM(5)  GPIO(54)
PA3  TIM5_CH4  TIM5  PWM(6)  GPIO(55)
PD12 TIM4_CH1 TIM4 PWM(7) GPIO(56)
PD13 TIM4_CH2 TIM4 PWM(8) GPIO(57)
PD14 TIM4_CH3 TIM4 PWM(9) GPIO(58)
PD15 TIM4_CH4 TIM4 PWM(10) GPIO(59)
PE5  TIM15_CH1 TIM15 PWM(11) GPIO(60)
PE6  TIM15_CH2 TIM15 PWM(12) GPIO(61)
PA8 TIM1_CH1 TIM1 PWM(13) GPIO(62) # for WS2812 LED

# Beeper
PA15 TIM2_CH1 TIM2 GPIO(32) ALARM

# microSD support
PC8 SDMMC1_D0 SDMMC1
PC9 SDMMC1_D1 SDMMC1
PC10 SDMMC1_D2 SDMMC1
PC11 SDMMC1_D3 SDMMC1
PC12 SDMMC1_CK SDMMC1
PD2 SDMMC1_CMD SDMMC1

# GPIOs
PD10  PINIO1 OUTPUT GPIO(81) LOW
PD11  PINIO2 OUTPUT GPIO(82) LOW

DMA_PRIORITY S*

define HAL_STORAGE_SIZE 16384

# use last 2 pages for flash storage
# H743 has 16 pages of 128k each
STORAGE_FLASH_PAGE 14

# spi devices
SPIDEV icm42688  SPI1 DEVID1 IMU1_CS      MODE3  2*MHZ 16*MHZ   # Clock is 100Mhz so highest clock <= 24Mhz is 100Mhz/8
SPIDEV mpu6000   SPI1 DEVID1 IMU1_CS      MODE3  1*MHZ  4*MHZ
SPIDEV icm20602  SPI4 DEVID1 IMU2_CS      MODE3  1*MHZ  4*MHZ
SPIDEV icm42605  SPI4 DEVID1 IMU3_CS      MODE3  2*MHZ 16*MHZ
SPIDEV osd       SPI2 DEVID4 MAX7456_CS   MODE0 10*MHZ 10*MHZ


DMA_NOSHARE SPI1* SPI4*

# SPI3 external connections
SPIDEV pixartflow   SPI3 DEVID1  EXT_CS1  MODE3   2*MHZ  2*MHZ

# no built-in compass, but probe the i2c bus for all possible
# external compass types
define ALLOW_ARM_NO_COMPASS
define HAL_PROBE_EXTERNAL_I2C_COMPASSES
define HAL_I2C_INTERNAL_MASK 0
define HAL_COMPASS_AUTO_ROT_DEFAULT 2

# two IMUs
# H743-V1,      ICM20602, MPU6000
# H743-V1.5/V2, ICM42605, MPU6000
# H743-V3,      ICM42688P, ICM42605
IMU Invensensev3 SPI:icm42688 ROTATION_YAW_180
IMU Invensensev3 SPI:icm42605 ROTATION_YAW_270
IMU Invensense SPI:icm20602 ROTATION_ROLL_180_YAW_270
IMU Invensense SPI:mpu6000 ROTATION_ROLL_180_YAW_270
define HAL_DEFAULT_INS_FAST_SAMPLE 1

# DPS310 integrated on I2C2 bus, multiple possible choices for external barometer
BARO MS56XX I2C:0:0x77
BARO DPS310 I2C:0:0x76
BARO BMP280 I2C:0:0x76

define HAL_OS_FATFS_IO 1
define HAL_BOARD_LOG_DIRECTORY "/APM/LOGS"
define HAL_BOARD_TERRAIN_DIRECTORY "/APM/TERRAIN"

# setup for OSD
define OSD_ENABLED 1
define HAL_OSD_TYPE_DEFAULT 1
ROMFS_WILDCARD libraries/AP_OSD/fonts/font*.bin
