STM32F4 System Clock Testing – MCOx

Keywords: Embedded systems, ARM, STM32F4, Clock, Microcontroller Clock Output

Code Link: Source Code Github Keil- Bare-Metal

Code Link: Source Code Github Keil- HAL Libraries

In previous tutorials we discussed what are different clock source available on STM32 and how each of them can be selected programmatically. Although we set clock selection bits in respective registers as per STM32F4 Reference Manual [1] yet there was (may be) still a teeny tiny doubt:

  1. Is the clock really selected?
  2. Does the system really switched to selected clock source?
  3. What actual clock frequency does the system receiving as apposed to expected clock!

In short – how can the configured clock be verified? 😉

Well the good news is there is a way around to verify system clock. The Microntroller Clock Ouput (MCOx) feature of STM32 allows to reroute one of six system clocks i.e. HSI, HSE, LSE, SYSCLK, PLL, and PLLI2S to specific GPIOs Pins for verification purpose or providing clock to external devices in case if required.

The STM32 onchip clock and power control unit i.e. RCC provides the facility to route various system clock sources to specific GPIOs Output Pins (MCO1-PA8, MCO2-PC9) as a part of alternate function. This output clock routed from system clock sources can be measured externally via a logic analyzer etc.

In this tutorial we will discuss how clock from various clock sources can be routed to GPIO pins for test purpose. The board we are using is STM32F4-Discovery. If we look at STM32F407 Clock tree [1], we will see that there are two pins i.e. MCO1 and MCO2 to which various clock source are connected via a multiplexer and prescalers. Figure-1.

Figure-1: STM32 Clock Tree

The MCO1 and MCO2 are the pins which can be used to verify various System clock sources. According to datasheet [2]:

Figure-2: STM32 Clock Source to Pins Multiplexing

For demonstration purpose we will verify the default connected clock source i.e. the internal 16MHZ HSI Oscillator on MCO1 (PA8).

Following are the steps…

Steps:

1. As we are using MCO1 pin which is multiplexed with GPIOA Pin#8 (PA.8) so the first step is to configure PA.8 to be used for Clock output and not as General Purpose GPIO. The configuration includes enabling clock to GPIOA and programming PA.8 for Alternate function AF0. We have a separate tutorial for GPIO configuration as an Alternate Function. The tutorial can be accessed via the following link.

PA.8 is configured as Alternate Function – 0 via the following code snippet.

  /* MCO1 --> PA8 */
  
  /*
    Enable clock to GPIOA
  */
  RCC->AHB1ENR |= 1U;
  
  /*********************
    PA.8 Configuration
  *********************/
  
  /* PA8 will be used for clock output (MCO1) and not 
    as GPIO so configure it for alternate funciton
  */
  
  GPIOA->MODER &amp;= ~(1U << 16);
  GPIOA->MODER |= (1U << 17);
  
  /* Push Pull -> clock inlcludes both states */
  GPIOA->OTYPER &amp;= ~(1U << 8);
  
  /* high speed */
  GPIOA->OSPEEDR |= (2U << 16);
  
  /* connect to alternate function AF0 (System Function)*/
  GPIOA->AFR[1] &amp;= ~(0xFU);

2. As shown in Figure-2, four different clock sources can be output on MCO1. For demonstration purpose we will select the internal RC Oscillator HSI. The clock sources for both MCO1 and MCO2 can be selected via RCC Clock Configuration Register (RCC_CFGR) – Figure-3.

Figure-3: RCC Clock Configuration Register (RCC_CFGR)

For MCO1, bit#21,22 are used to select clock source.
00: HSI clock selected
01: LSE oscillator selected
10: HSE oscillator clock selected
11: PLL clock selected

As we want HSI output on MCO1 so bit#21,22 are both set to 0

  /********************
      MCO1 Settings 
  ********************/
  /*
    00: HSI clock selected    [WE WILL SELECT THIS ON]
    01: LSE oscillator selected
    10: HSE oscillator clock selected
    11: PLL clock selected
  */
  RCC->CFGR &amp;= ~(3U << 21);

3. This step is kind of optional. If both output clock frequency comes under the range of:

  • Bus frequency on which GPIOx is interfaced (Max Bus Speed)
  • Measuring Instrument

If both of these conditions are satisfied then you may skip this step. Otherwise in either case, you need to scale down the frequency internally by a factor so that it can be measured accurately. The actual frequency will be the measured frequency multiplied by the scaled factor. The scale factor can be set via bit#24-26 (for MCO1) and bit#27-29 (for MCO2) in RCC Clock Configuration Register (RCC_CFGR) – Figure-3.
0xx: no division
100: division by 2
101: division by 3
110: division by 4
111: division by 5

In our case, as we want the clock output to be HSI (16MHz), so lets scale it down by a factor of 4. Now the clock frequency on MCO1 pin should be ~4Mhz (16/4).

  /*
    Prescale by factor of 4 --> 4Mhz output on MCO1 (in range of low quality analyzer)
  */
  RCC->CFGR &amp;= ~(1U << 24);
  RCC->CFGR |=  (3U << 25);
  
  /*
    Now HSI clock is connected to MCO1
    MCO1 pin will now provide clock of ~4Mhz.
  */

For complete code refer to the link given at the start of this tutorial.

References:

[1] – Reference manual-RM0090



84 thoughts on “STM32F4 System Clock Testing – MCOx”

  • Hi this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have
    to manually code with HTML. I’m starting a blog soon but have no
    coding knowledge so I wanted to get guidance from someone with experience.
    Any help would be enormously appreciated! 0mniartist asmr

  • Have you ever thought about adding a little bit more
    than just your articles? I mean, what you say is important
    and everything. But imagine if you added some great visuals
    or videos to give your posts more, “pop”! Your content is excellent
    but with pics and clips, this website could certainly
    be one of the best in its field. Very good blog! 0mniartist asmr

  • Have you ever considered publishing an ebook or guest authoring on other websites?
    I have a blog centered on the same information you discuss and
    would love to have you share some stories/information. I know my
    visitors would enjoy your work. If you’re even remotely interested,
    feel free to shoot me an e-mail. asmr 0mniartist

  • I used to be suggested this website through my cousin. I’m no longer positive whether this publish is written by way
    of him as nobody else realize such specific about my trouble.
    You are wonderful! Thank you!

  • Hey there! This post could not be written any better!
    Reading through this post reminds me of my old room mate!
    He always kept talking about this. I will forward this
    post to him. Pretty sure he will have a good read.
    Many thanks for sharing!

  • Fantastic goods from you, man. I have understand your stuff previous to
    and you’re just extremely great. I really like what you have acquired here, certainly like what
    you are saying and the way in which you say it.
    You make it enjoyable and you still take care of to keep it smart.
    I cant wait to read much more from you. This is actually a wonderful web site.

  • whoah this weblog is fantastic i really like reading your articles.
    Stay up the good work! You already know, lots of persons are
    searching round for this info, you can aid them greatly.

  • Whats up are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and create my own. Do you require any coding knowledge to make your own blog?
    Any help would be greatly appreciated!

  • Hello, i think that i saw you visited my site thus i came to “return the favor”.I am attempting to find
    things to improve my site!I suppose its ok to use a few of your
    ideas!!

  • scoliosis
    I think this is among the most vital information for me.
    And i am glad reading your article. But should remark on few general things, The web site style is wonderful, the articles is really excellent : D.
    Good job, cheers scoliosis

  • scoliosis
    Hi! I know this is kinda off topic however ,
    I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog
    post or vice-versa? My blog covers a lot of the same subjects as yours and I believe
    we could greatly benefit from each other. If you are
    interested feel free to shoot me an email. I look forward to hearing from you!
    Wonderful blog by the way! scoliosis

  • Thank you a lot for sharing this with all people you actually understand what
    you are talking about! Bookmarked. Kindly additionally consult with my website =).
    We could have a link alternate arrangement among us

  • naturally like your web-site but you have to check the spelling on several of your posts.
    Many of them are rife with spelling issues and I
    find it very bothersome to inform the reality however I’ll surely come back again.

  • When I initially left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I
    get 4 emails with the exact same comment.
    Is there a means you are able to remove me from that service?
    Kudos!

  • Hey there would you mind stating which blog platform you’re using?
    I’m looking to start my own blog soon but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs
    and I’m looking for something completely unique. P.S
    Apologies for being off-topic but I had to ask!

  • Esse artigo é fantástico ! Eu achei esse artigo por acaso e vim te
    dizer que curti todos os posts . Vou te adicionar e te desejo muito sucesso!

  • Hello, i think that i saw you visited my site thus i came to “return the favor”.I’m attempting to find things to enhance my site!I
    suppose its ok to use a few of your ideas!!

  • Hello, I think your website might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads up!
    Other then that, wonderful blog!

  • Hi exceptional blog! Does running a blog similar to this require a massive amount work?
    I have no understanding of programming however I had been hoping to
    start my own blog soon. Anyway, if you have any recommendations or tips for
    new blog owners please share. I understand this is off topic but I simply had to ask.
    Cheers!

  • Greate article. Keep posting such kind of information on your site.
    Im really impressed by your blog.
    Hey there, You’ve done an incredible job. I will definitely digg it and in my
    view recommend to my friends. I’m sure they will be benefited from this web site.

  • It’s actᥙallу a cool and useful pіece of information. I аm glad that you jut snared this
    useful innfo with us. Рlease keep us informed likoe this.
    Thanks for sharing.

  • Hi, I do think this is a great site. I stumbledupon it 😉 I’m going to come back yet again since i have
    saved as a favorite it. Money and freedom is the greatest way to change,
    may you be rich and continue to guide other
    people.

  • I do accept as true with all the concepts you have introduced in your post.

    They’re very convincing and can definitely work.
    Still, the posts are too quick for novices. May you please extend them a bit from subsequent
    time? Thanks for the post.

  • Excellent post. I was checking continuously this blog and I’m inspired!
    Very useful info specially the final part 🙂 I take care
    of such info much. I used to be looking for this particular info for a very long time.
    Thank you and good luck.

  • What’s up everyone, it’s my first pay a visit at
    this website, and piece of writing is really fruitful in support of me, keep up posting these content.

  • I am not sure where you’re getting your info, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for wonderful information I was looking for this
    information for my mission.

  • Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is wonderful, as well as the content!

  • you are really a just right webmaster. The site loading velocity is amazing.
    It kind of feels that you are doing any unique trick. Furthermore,
    The contents are masterpiece. you’ve done a
    great activity on this subject!

  • Hi there! Someone in my Facebook group shared this website with us so I came to check it out.

    I’m definitely loving the information. I’m book-marking and will be tweeting
    this to my followers! Exceptional blog and wonderful design.

  • Hi! I know this is kinda off topic but I was wondering which blog platform
    are you using for this website? I’m getting sick and tired of
    Wordpress because I’ve had problems with hackers and I’m looking at options for another platform.
    I would be fantastic if you could point me in the direction of a good platform.

  • I have been browsing on-line more than 3 hours
    these days, yet I by no means discovered any interesting article
    like yours. It is beautiful price enough for me. In my opinion, if all webmasters and bloggers made just right content material as you did, the web
    might be much more helpful than ever before.

  • An interesting discussion is worth comment. I do think
    that you should publish more on this subject,
    it might not be a taboo subject but usually
    people do not speak about these subjects. To the next!
    All the best!!

  • You really make it seem so easy with your presentation but I find this topic to
    be actually something which I think I would never understand.
    It seems too complex and extremely broad for me.

    I’m looking forward for your next post, I’ll try to
    get the hang of it!

  • I think that what you composed made a lot of sense.
    However, what about this? suppose you added a little information? I am not saying your
    information isn’t solid, however suppose you added a title that makes people want more?
    I mean STM32F4 System Clock Testing – MCOx – EcoderLenz is a little plain. You might
    peek at Yahoo’s front page and note how they write
    news headlines to grab viewers to click.
    You might add a related video or a pic or two to get people excited about everything’ve written.
    In my opinion, it might bring your blog a little bit more interesting.

  • Thanks for the marvelous posting! I really enjoyed reading it, you might be a great author.I will make sure to bookmark your blog and will often come back someday.
    I want to encourage you to continue your great job, have a nice weekend!

  • Heya just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly.
    I’m not sure why but I think its a linking issue.

    I’ve tried it in two different browsers and both show the same outcome.

  • Hello are using WordPress for your blog platform? I’m new to the blog world
    but I’m trying to get started and set up my own. Do you need any coding knowledge to make your own blog?
    Any help would be really appreciated!

  • Wow, awesome blog structure! How lengthy have you ever been running a blog for?

    you make running a blog glance easy. The full glance of your web site is excellent, as well as
    the content material!

  • Does your site have a contact page? I’m having problems locating it but, I’d like to
    shoot you an e-mail. I’ve got some creative ideas for your blog you might be
    interested in hearing. Either way, great blog
    and I look forward to seeing it grow over time.

  • Excellent weblog here! Additionally your web site a lot up very fast!
    What web host are you the usage of? Can I get your associate link for your host?
    I wish my website loaded up as fast as yours lol

  • Do you mind if I quote a couple of your posts as long as I provide credit and
    sources back to your website? My blog is in the very same niche as yours and my visitors would genuinely benefit from
    a lot of the information you provide here. Please let
    me know if this okay with you. Regards! quest bars http://bit.ly/3jZgEA2 quest bars

  • Thanks for some other informative site. Where else may just I am getting that type of information written in such an ideal method? I’ve a project that I’m just now operating on, and I’ve been on the look out for such info.

  • hello there and thank you for your information – I have definitely picked up anything new from
    right here. I did however expertise a few technical points
    using this site, as I experienced to reload the site lots of times previous to I could get it to load properly.

    I had been wondering if your web hosting is OK?
    Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your quality score if ads and marketing with Adwords.
    Well I am adding this RSS to my e-mail and could look out for much more of your respective fascinating content.
    Make sure you update this again soon.

Leave a Reply

Your email address will not be published. Required fields are marked *