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)
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/>.
29 #include "drivers/io.h"
30 #include "drivers/mco.h"
36 // - MCO output stability
37 // MCO output should not be too high.
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
,
49 RCC_MCO1SOURCE_PLLCLK
, // PLLR on G4
55 const uint32_t mcoDividers
[MCO_DIVIDER_COUNT
] = {
64 void mcoConfigure(MCODevice_e device
, const mcoConfig_t
*config
)
66 if (!config
->enabled
) {
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.
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);
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
);
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
);
93 #elif defined(STM32G4)
94 // G4 only supports one MCO on PA8
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
]);
101 #error Unsupported MCU