Question
MCO1 of STM32H755 cannot be properly configured
I am trying to write code to enable the MCO1 clock output on pin PA8 on the STM32H755ZIT6U microcontroller, the code is fairly simple.
The code below is my attempt to do so. I couldn't find the problem.
/**
This code activates MCO1 and MCO2 so we can probe the clock frequecy of the STM32 MCU
**/
#include "stm32h755xx.h"
int main(void){
// enable the clock to AHB4
RCC->AHB4ENR &= ~(RCC_AHB4ENR_GPIOAEN | RCC_AHB4ENR_GPIOCEN); // reset
RCC->AHB4ENR |= (RCC_AHB4ENR_GPIOAEN | RCC_AHB4ENR_GPIOCEN); // enable clock
// set PA8 as alternate function
GPIOA->MODER &= ~(GPIO_MODER_MODE8_0 | GPIO_MODER_MODE8_1); // reset
GPIOA->MODER |= (GPIO_MODER_MODE8_1); // AF
// set PC9 to alternate function
GPIOC->MODER &= ~(GPIO_MODER_MODE9_0 | GPIO_MODER_MODE9_1); // reset
GPIOC->MODER |= (GPIO_MODER_MODE9_1);
// set the speed of PA8 and PC9 to very high speed
GPIOA->OSPEEDR |= (GPIO_OSPEEDR_OSPEED8_0 | GPIO_OSPEEDR_OSPEED8_1);
GPIOC->OSPEEDR |= (GPIO_OSPEEDR_OSPEED9_0 | GPIO_OSPEEDR_OSPEED9_1);
// set the alternate function of PA8 and PC9 to AF0
GPIOA->AFR[1] &= ~(GPIO_AFRH_AFSEL8_0 | GPIO_AFRH_AFSEL8_1 | GPIO_AFRH_AFSEL8_2 | GPIO_AFRH_AFSEL8_3);
GPIOC->AFR[1] &= ~(GPIO_AFRH_AFSEL9_0 | GPIO_AFRH_AFSEL9_1 | GPIO_AFRH_AFSEL9_2 | GPIO_AFRH_AFSEL9_3);
// set the MCO1 to HSI
RCC->CFGR &= ~(RCC_CFGR_MCO1_0 | RCC_CFGR_MCO1_1 | RCC_CFGR_MCO1_2);
// divide the output by 15
RCC->CFGR |= (RCC_CFGR_MCO1PRE_0 | RCC_CFGR_MCO1PRE_1 | RCC_CFGR_MCO1PRE_2 | RCC_CFGR_MCO1PRE_3);
while(1);
}
I tested it by configuring the clock tree in CubeMX and it worked, the output is about 4.2MHz, which is what I expected, but I couldn't do the same without the autogenerated code.



