Skip to main content
Visitor II
March 24, 2024
Question

MCO1 of STM32H755 cannot be properly configured

  • March 24, 2024
  • 2 replies
  • 1219 views

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.

 

FadiEid_0-1711339003865.pngFadiEid_1-1711339030104.png

FadiEid_2-1711339061349.png

 

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    March 24, 2024

    Ahlayn @FadiEid and welcome to the ST Community :smiling_face_with_smiling_eyes:.

    What i suggest you is to debug step by step the code generated from CubeMX. Then follow which registers are configured in each step. After that, reproduce it using the register programming.

    Best Regards.

    STTwo-32 

    FadiEidAuthor
    Visitor II
    March 24, 2024

    Hey man, I will do this. I just thought there is something obviously wrong in my code and decided to reach out for help. shoukran

    Technical Moderator
    March 24, 2024

    Sure there is something wrong on your code. What i suggest you is the best way to do it.

    If you are looking for any other details. You can always create new posts on our community for each request habibe.

    Best Regards.

    STTwo-32