diff --git a/mk/source.mk b/mk/source.mk index 3849b92a22..90a7cda738 100644 --- a/mk/source.mk +++ b/mk/source.mk @@ -104,7 +104,6 @@ COMMON_SRC = \ drivers/io.c \ drivers/io_preinit.c \ drivers/light_led.c \ - drivers/mco.c \ drivers/motor.c \ drivers/pinio.c \ drivers/pin_pull_up_down.c \ diff --git a/src/main/cli/settings.c b/src/main/cli/settings.c index 5b5b0c4790..d2b1c49158 100644 --- a/src/main/cli/settings.c +++ b/src/main/cli/settings.c @@ -1685,7 +1685,7 @@ const clivalue_t valueTable[] = { #endif // end of #ifdef USE_OSD // PG_SYSTEM_CONFIG -#if defined(STM32F4) || defined(STM32G4) || defined(APM32F4) +#if PLATFORM_TRAIT_CONFIG_HSE { "system_hse_mhz", VAR_UINT8 | HARDWARE_VALUE, .config.minmaxUnsigned = { 0, 30 }, PG_SYSTEM_CONFIG, offsetof(systemConfig_t, hseMhz) }, #endif { "task_statistics", VAR_INT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_SYSTEM_CONFIG, offsetof(systemConfig_t, task_statistics) }, @@ -1867,11 +1867,12 @@ const clivalue_t valueTable[] = { #endif #endif #ifdef USE_MCO -#ifdef STM32G4 +#if defined(USE_MCO_DEVICE1) +// TODO: remove pin specific names { "mco_on_pa8", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MCO_CONFIG, PG_ARRAY_ELEMENT_OFFSET(mcoConfig_t, 0, enabled) }, { "mco_source", VAR_UINT8 | HARDWARE_VALUE, .config.minmaxUnsigned = { 0, MCO_SOURCE_COUNT - 1 }, PG_MCO_CONFIG, PG_ARRAY_ELEMENT_OFFSET(mcoConfig_t, 0, source) }, { "mco_divider", VAR_UINT8 | HARDWARE_VALUE, .config.minmaxUnsigned = { 0, MCO_DIVIDER_COUNT - 1 }, PG_MCO_CONFIG, PG_ARRAY_ELEMENT_OFFSET(mcoConfig_t, 0, divider) }, -#else +#elif defined(USE_MCO_DEVICE2) { "mco2_on_pc9", VAR_UINT8 | HARDWARE_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_MCO_CONFIG, PG_ARRAY_ELEMENT_OFFSET(mcoConfig_t, 1, enabled) }, #endif #endif diff --git a/src/main/drivers/mco.h b/src/main/drivers/mco.h index 72ced7e0e8..d7c902098e 100644 --- a/src/main/drivers/mco.h +++ b/src/main/drivers/mco.h @@ -27,9 +27,4 @@ typedef enum { MCODEV_2, } MCODevice_e; -#ifdef STM32G4 -#define MCO_SOURCE_COUNT 8 -#define MCO_DIVIDER_COUNT 5 -#endif - -void mcoConfigure(MCODevice_e device, const mcoConfig_t *config); +void mcoInit(void); diff --git a/src/main/fc/init.c b/src/main/fc/init.c index c156a4d0a6..b4cc324396 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -483,9 +483,7 @@ void init(void) } #endif -#if defined(STM32F4) || defined(STM32G4) || defined(APM32F4) - // F4 has non-8MHz boards - // G4 for Betaflight allow 8, 16, 24, 26 or 27MHz oscillator +#if PLATFORM_TRAIT_CONFIG_HSE systemClockSetHSEValue(systemConfig()->hseMhz * 1000000U); #endif @@ -495,18 +493,7 @@ void init(void) // Configure MCO output after config is stable #ifdef USE_MCO - // Note that mcoConfigure must be augmented with an additional argument to - // indicate which device instance to configure when MCO and MCO2 are both supported - -#if defined(STM32F4) || defined(STM32F7) || defined(APM32F4) - // F4 and F7 support MCO on PA8 and MCO2 on PC9, but only MCO2 is supported for now - mcoConfigure(MCODEV_2, mcoConfig(MCODEV_2)); -#elif defined(STM32G4) - // G4 only supports one MCO on PA8 - mcoConfigure(MCODEV_1, mcoConfig(MCODEV_1)); -#else -#error Unsupported MCU -#endif + mcoInit(); #endif // USE_MCO #ifdef USE_TIMER @@ -574,7 +561,7 @@ void init(void) initFlags |= QUAD_OCTO_SPI_BUSSES_INIT_ATTEMPTED; } -#if defined(USE_SDCARD_SDIO) && !defined(CONFIG_IN_SDCARD) && defined(STM32H7) +#if defined(USE_SDCARD_SDIO) && !defined(CONFIG_IN_SDCARD) && PLATFORM_TRAIT_SDIO_INIT sdioPinConfigure(); SDIO_GPIO_Init(); #endif diff --git a/src/platform/APM32/include/platform/platform.h b/src/platform/APM32/include/platform/platform.h index 368440773c..1f6e9cef42 100644 --- a/src/platform/APM32/include/platform/platform.h +++ b/src/platform/APM32/include/platform/platform.h @@ -117,6 +117,7 @@ #define USE_USB_MSC #define USE_PERSISTENT_MSC_RTC #define USE_MCO +#define USE_MCO_DEVICE2 #define USE_DMA_SPEC #define USE_PERSISTENT_OBJECTS #define USE_LATE_TASK_STATISTICS @@ -127,6 +128,7 @@ #define SCHEDULER_DELAY_LIMIT 10 #define DEFAULT_CPU_OVERCLOCK 0 +#define PLATFORM_TRAIT_CONFIG_HSE 1 #define FAST_IRQ_HANDLER diff --git a/src/platform/APM32/mk/APM32F4.mk b/src/platform/APM32/mk/APM32F4.mk index 2b1a09750d..1f4eae9f0a 100644 --- a/src/platform/APM32/mk/APM32F4.mk +++ b/src/platform/APM32/mk/APM32F4.mk @@ -150,6 +150,7 @@ MCU_COMMON_SRC = \ common/stm32/system.c \ common/stm32/io_impl.c \ common/stm32/config_flash.c \ + common/stm32/mco.c \ APM32/startup/system_apm32f4xx.c \ drivers/inverter.c \ drivers/dshot_bitbang_decode.c \ diff --git a/src/platform/AT32/mk/AT32F4.mk b/src/platform/AT32/mk/AT32F4.mk index 8c8833e8c9..5dd73b15d8 100644 --- a/src/platform/AT32/mk/AT32F4.mk +++ b/src/platform/AT32/mk/AT32F4.mk @@ -85,6 +85,7 @@ MCU_COMMON_SRC = \ common/stm32/system.c \ common/stm32/io_impl.c \ common/stm32/config_flash.c \ + common/stm32/mco.c \ AT32/startup/at32f435_437_clock.c \ AT32/startup/system_at32f435_437.c \ AT32/adc_at32f43x.c \ diff --git a/src/platform/STM32/include/platform/platform.h b/src/platform/STM32/include/platform/platform.h index 3f9e13e5a1..d2b51c0008 100644 --- a/src/platform/STM32/include/platform/platform.h +++ b/src/platform/STM32/include/platform/platform.h @@ -130,6 +130,7 @@ #define USE_USB_MSC #define USE_PERSISTENT_MSC_RTC #define USE_MCO +#define USE_MCO_DEVICE2 #define USE_DMA_SPEC #define USE_PERSISTENT_OBJECTS #define USE_LATE_TASK_STATISTICS @@ -152,6 +153,7 @@ #define USE_USB_MSC #define USE_PERSISTENT_MSC_RTC #define USE_MCO +#define USE_MCO_DEVICE2 #define USE_DMA_SPEC #define USE_PERSISTENT_OBJECTS #define USE_LATE_TASK_STATISTICS @@ -188,6 +190,7 @@ #define USE_USB_MSC #define USE_USB_CDC_HID #define USE_MCO +#define USE_MCO_DEVICE1 #define USE_DMA_SPEC #define USE_LATE_TASK_STATISTICS #endif @@ -447,4 +450,16 @@ extern uint8_t _dmaram_end__; #define QUADSPI_TRAIT_AF_PIN 1 #define QUADSPI_TRAIT_HANDLE 1 #define MAX_QUADSPI_PIN_SEL 3 +#define PLATFORM_TRAIT_SDIO_INIT 1 +#endif + +// F4 has non-8MHz boards +// G4 for Betaflight allow 8, 16, 24, 26 or 27MHz oscillator +#if defined(STM32F4) || defined(STM32G4) +#define PLATFORM_TRAIT_CONFIG_HSE 1 +#endif + +#if defined(STM32G4) +#define MCO_SOURCE_COUNT 8 +#define MCO_DIVIDER_COUNT 5 #endif diff --git a/src/platform/STM32/mk/STM32_COMMON.mk b/src/platform/STM32/mk/STM32_COMMON.mk index 6b0c074964..6bae4a84a1 100644 --- a/src/platform/STM32/mk/STM32_COMMON.mk +++ b/src/platform/STM32/mk/STM32_COMMON.mk @@ -5,6 +5,7 @@ MCU_COMMON_SRC += \ common/stm32/system.c \ common/stm32/config_flash.c \ common/stm32/bus_spi_pinconfig.c \ + common/stm32/mco.c \ drivers/bus_spi_config.c \ drivers/serial_pinconfig.c \ common/stm32/bus_i2c_pinconfig.c \ diff --git a/src/main/drivers/mco.c b/src/platform/common/stm32/mco.c similarity index 82% rename from src/main/drivers/mco.c rename to src/platform/common/stm32/mco.c index 74d6a75d31..11959bd65f 100644 --- a/src/main/drivers/mco.c +++ b/src/platform/common/stm32/mco.c @@ -61,7 +61,7 @@ const uint32_t mcoDividers[MCO_DIVIDER_COUNT] = { }; #endif -void mcoConfigure(MCODevice_e device, const mcoConfig_t *config) +static void mcoConfigure(MCODevice_e device, const mcoConfig_t *config) { if (!config->enabled) { return; @@ -69,15 +69,19 @@ void mcoConfigure(MCODevice_e device, const mcoConfig_t *config) IO_t io; -#if defined(STM32F4) || defined(STM32F7) || defined(APM32F4) // Only configure MCO2 with PLLI2SCLK as source for now. // Other MCO1 and other sources can easily be added. switch(device) { case MCODEV_1: // MCO1 on PA8 - return; // Not supported (yet) - +#if defined(STM32G4) + io = IOGetByTag(DEFIO_TAG_E(PA8)); + IOInit(io, OWNER_MCO, 1); + HAL_RCC_MCOConfig(RCC_MCO, mcoSources[config->source], mcoDividers[config->divider]); +#endif + break; case MCODEV_2: // MCO2 on PC9 +#if defined(STM32F4) || defined(STM32F7) || defined(APM32F4) io = IOGetByTag(DEFIO_TAG_E(PC9)); IOInit(io, OWNER_MCO, 2); #if defined(STM32F7) @@ -90,16 +94,23 @@ void mcoConfigure(MCODevice_e device, const mcoConfig_t *config) // All F4s RCC_MCO2Config(RCC_MCO2Source_PLLI2SCLK, RCC_MCO2Div_4); IOConfigGPIOAF(io, IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL), GPIO_AF_MCO); +#endif #endif break; + default: + // No MCO configured + return; } +} + +void mcoInit(void) +{ +#if defined(STM32F4) || defined(STM32F7) || defined(APM32F4) + // F4 and F7 support MCO on PA8 and MCO2 on PC9, but only MCO2 is supported for now + mcoConfigure(MCODEV_2, mcoConfig(MCODEV_2)); #elif defined(STM32G4) // G4 only supports one MCO on PA8 - UNUSED(device); - - io = IOGetByTag(DEFIO_TAG_E(PA8)); - IOInit(io, OWNER_MCO, 1); - HAL_RCC_MCOConfig(RCC_MCO, mcoSources[config->source], mcoDividers[config->divider]); + mcoConfigure(MCODEV_1, mcoConfig(MCODEV_1)); #else #error Unsupported MCU #endif