{"id":2757,"date":"2024-08-24T16:52:14","date_gmt":"2024-08-24T16:52:14","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2757"},"modified":"2024-08-30T23:17:20","modified_gmt":"2024-08-30T23:17:20","slug":"verify-external-oscillator-using-master-clock-output","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2757","title":{"rendered":"Verify External Oscillator using Master Clock Output"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-1024x768.png\" alt=\"\" class=\"wp-image-2758\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-1024x768.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-300x225.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-768x576.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-1536x1152.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-2048x1536.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-1150x863.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-750x563.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-400x300.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/AdobeStock_773424893-250x188.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this guide, we shall use the Master Clock Output (MCO) feature of STM32 to check the external crystal connected to STM32.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide,  we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What is MCO.<\/li>\n\n\n\n<li>Configuration.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is MCO:<\/h2>\n\n\n\n<p>MCO (Microcontroller Clock Output) is a feature available in many STM32 microcontrollers that allows the clock signal from an internal or external clock source to be output on a specific pin. This feature can be useful for various purposes, such as providing a clock signal to other devices in your system or monitoring the clock frequency for debugging.<\/p>\n\n\n\n<p>Key Points About MCO:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Clock Sources:<br>\u2022 The MCO pin can output different clock signals, including:<br>\u2022 HSI (High-Speed Internal) oscillator: Typically 8 MHz or other frequencies depending on the MCU.<br>\u2022 HSE (High-Speed External) oscillator: This could be a crystal or an external clock source.<br>\u2022 PLL (Phase-Locked Loop) output: The PLL can generate higher frequencies by multiplying the input clock.<br>\u2022 SYSCLK: The system clock, which is the main clock driving the CPU and peripherals.<br>\u2022 LSI (Low-Speed Internal) oscillator: Typically used for low-power modes and watchdog timers.<br>\u2022 LSE (Low-Speed External) oscillator: Typically a 32.768 kHz crystal used for real-time clocks.<\/li>\n\n\n\n<li>MCO Pins:<br>\u2022 The MCO signal is usually available on specific GPIO pins, such as MCO1 and MCO2, depending on the STM32 series.<br>\u2022 MCO1 and MCO2 are often located on different pins, allowing two different clock sources to be output simultaneously.<\/li>\n\n\n\n<li>Configuration:<br>\u2022 MCO is configured via registers in the STM32\u2019s clock control (RCC) peripheral.<br>\u2022 You can select the clock source and set the division factor (prescaler) to adjust the output frequency.<br>\u2022 For example, you might set the MCO1 pin to output the HSI clock divided by 4, giving you a 2 MHz output.<\/li>\n\n\n\n<li>Common Applications:<br>\u2022 Clock Sharing: Providing a clock signal to other microcontrollers or devices in your system that need synchronization.<br>\u2022 Debugging: Verifying the clock configuration by measuring the MCO output with an oscilloscope.<br>\u2022 Synchronization: Outputting a stable clock source for peripherals that require precise timing.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configuration:<\/h2>\n\n\n\n<p>Create new project with name of Verify_External_Clock. For how to create project, please follow this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=1255\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1255\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>After creating new project. In main.c file, include main STM32F4 header as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f4xx.h&quot;<\/pre><\/div>\n\n\n\n<p>Next, we need to know which pins support MCO.<\/p>\n\n\n\n<p>From STM32F411 datasheet:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"398\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-398x1024.png\" alt=\"\" class=\"wp-image-2759\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-398x1024.png 398w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-117x300.png 117w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-597x1536.png 597w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-400x1029.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM-250x643.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.41.30\u202fPM.png 702w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We can see that PA8 supports MCO1, hence we shall use it.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>First, enable clock access to GPIOA and configure PA8 in alternate function as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int main(void)\n{\n\n\t\/*Enable Clock Access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\n\t\/*Set PA8 as alternate function*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE8_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8_0;<\/pre><\/div>\n\n\n\n<p>Next, we shall configure MCO as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MCO output from HSE.<\/li>\n\n\n\n<li>Set prescaller to MCO to 5.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"905\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-1024x905.png\" alt=\"\" class=\"wp-image-2760\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-1024x905.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-300x265.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-768x679.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-1536x1357.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-2048x1810.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-1150x1016.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-750x663.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-400x353.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.45.04\u202fPM-250x221.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set MCO1 to be HSE*\/\n\tRCC-&gt;CFGR|=RCC_CFGR_MCO1_1;\n\n\t\/*Set prescaler to 5*\/\n\tRCC-&gt;CFGR|=(RCC_CFGR_MCO1PRE);<\/pre><\/div>\n\n\n\n<p>Finally, enable external oscillator as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"333\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-1024x333.png\" alt=\"\" class=\"wp-image-2761\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-1024x333.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-300x98.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-768x250.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-1536x500.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-2048x667.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-1150x374.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-750x244.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-400x130.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.48.19\u202fPM-250x81.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable external oscillator*\/\n\tRCC-&gt;CR|=RCC_CR_HSEON;\n\n\t\/*Wait until the external oscillator is enabled*\/\n\twhile((RCC-&gt;CR &amp; RCC_CR_HSERDY) !=RCC_CR_HSERDY);<\/pre><\/div>\n\n\n\n<p>hats it for the code.<\/p>\n\n\n\n<p>Save the project, build it and run it on your STM32F411 Nucleo-64.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p>By probing PA8 using either logic analyzer or oscilloscope, you can see that the frequency is 1.6MHz. By multiplying by 5, we will get 8MHz which is the external frequency supplied by the embedded ST-Link.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-1024x521.png\" alt=\"\" class=\"wp-image-2762\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-1024x521.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-300x152.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-768x390.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-1536x781.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-2048x1041.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-1150x585.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-750x381.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-400x203.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-at-7.29.03\u202fPM-250x127.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\"><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall use the Master Clock Output (MCO) feature of STM32 to check the external crystal connected to STM32. In this guide, we shall cover the following: 1. What is MCO: MCO (Microcontroller Clock Output) is a feature available in many STM32 microcontrollers that allows the clock signal from an internal or [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-2757","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2757"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2757"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2757\/revisions"}],"predecessor-version":[{"id":2783,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2757\/revisions\/2783"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}