Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / mco.c
blob692fc8d7b850aefe2b5281b9d0da0a207c20b8cb
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
25 #include "platform.h"
27 #ifdef USE_MCO
29 #include "drivers/io.h"
30 #include "drivers/mco.h"
31 #include "pg/mco.h"
33 #ifdef STM32G4
35 // Notes
36 // - MCO output stability
37 // MCO output should not be too high.
38 // For example
39 // it is required to use DIV4 for SYSCLK = 254MHz (derives 62.5MHz)
40 // it is required to use DIV2 for SYSCLK = 170MHz (derives 85MHz)
42 // - MCO frequency can be more flexible if PLLR is made configurable.
44 const uint32_t mcoSources[MCO_SOURCE_COUNT] = {
45 RCC_MCO1SOURCE_NOCLOCK,
46 RCC_MCO1SOURCE_SYSCLK,
47 RCC_MCO1SOURCE_HSI,
48 RCC_MCO1SOURCE_HSE,
49 RCC_MCO1SOURCE_PLLCLK, // PLLR on G4
50 RCC_MCO1SOURCE_LSI,
51 RCC_MCO1SOURCE_LSE,
52 RCC_MCO1SOURCE_HSI48,
55 const uint32_t mcoDividers[MCO_DIVIDER_COUNT] = {
56 RCC_MCO_DIV1,
57 RCC_MCO_DIV2,
58 RCC_MCO_DIV4,
59 RCC_MCO_DIV8,
60 RCC_MCO_DIV16,
62 #endif
64 void mcoConfigure(MCODevice_e device, const mcoConfig_t *config)
66 if (!config->enabled) {
67 return;
70 IO_t io;
72 #if defined(STM32F4) || defined(STM32F7)
73 // Only configure MCO2 with PLLI2SCLK as source for now.
74 // Other MCO1 and other sources can easily be added.
76 switch(device) {
77 case MCODEV_1: // MCO1 on PA8
78 return; // Not supported (yet)
80 case MCODEV_2: // MCO2 on PC9
81 io = IOGetByTag(DEFIO_TAG_E(PC9));
82 IOInit(io, OWNER_MCO, 2);
83 #if defined(STM32F7)
84 HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_PLLI2SCLK, RCC_MCODIV_4);
85 IOConfigGPIOAF(io, IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL), GPIO_AF0_MCO);
86 #else
87 // All F4s
88 RCC_MCO2Config(RCC_MCO2Source_PLLI2SCLK, RCC_MCO2Div_4);
89 IOConfigGPIOAF(io, IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL), GPIO_AF_MCO);
90 #endif
91 break;
93 #elif defined(STM32G4)
94 // G4 only supports one MCO on PA8
95 UNUSED(device);
97 io = IOGetByTag(DEFIO_TAG_E(PA8));
98 IOInit(io, OWNER_MCO, 1);
99 HAL_RCC_MCOConfig(RCC_MCO, mcoSources[config->source], mcoDividers[config->divider]);
100 #else
101 #error Unsupported MCU
102 #endif
104 #endif