Showing results for 
Search instead for 
Did you mean: 

How can I output a clock signal to the STM32’s MCO pin?

Stefanie LAU
ST Employee
How can I output a clock signal to the STM32’s MCO pin?STM32CubeMX is a graphical tool that allows users to configure STM32 MCUs (microcontrollers) and MPUs (microprocessors) quickly and simply. In addition, the tool also assists in generating the corresponding initialization C code for the Arm Cortex-M core(s) or partial Linux device tree for Arm Cortex-A cores.
One of the many features included in STM32CubeMX is the ability to allow users to select and configure the MCU’s or MPU’s clocks through a GUI (graphical user interface).
In the clock configuration tab, users can:
  • select from various clock sources
  • apply different pre-scalers to adjust the clock sources to various peripherals and buses
  • configure individual peripheral frequencies with the click of a button
  • select to output a clock source of the user’s choosing to a MCO (microcontroller output clock) pin
In this article we will further discuss how to locate and configure your target device’s MCO pin(s), and how to generate a project in which outputs the clock source of your choosing to the configured MCO pin.
The MCO pin provides users with capability to output one of the target device’s internal clocks for external use by the application. Another use case for this MCO pin is that it can also be helpful during debugging sessions to determine the clock frequency selected.
In this article, we will use the STM32WB55RG as our STM32 target device of choice.
Step 1. Locate available MCO pin(s) in the target device’s datasheet using the pin and ball definitions table. In the case of the target device STM32WB55RG, there are three GPIOs that support MCO alternate function capability:
  • PA8
  • PA15
  • PB6
Note that is also possible to use the STM32CubeMX GUI to locate the MCO pins. Simply do this by typing ‘MCO’ in the search bar and you will see the available MCO pins highlighted as so:
Step 2. Launch STM32CubeMX and start a new project with your target STM32 device
Step 3. Select and configure one of the GPIOs located in step 1 as your MCO pin. As an example, we will use and configure GPIO PA8. To do this, search for the GPIO in the bottom right-hand corner search bar, left click on the flashing GPIO icon and select RCC_MCO from the populated alternate functions list.
Note that you can also configure the MCO pin from the Pinout & Configuration tab. On the Pinout & Configuration table, on the left-hand side of the GUI under categories > expand the System Core tab > Select RCC > Under the RCC Mode window, check the Master Clock Output option. By default, GPIO PA8 is selected and configured as MCO.
To remap this MCO configuration to another GPIO supporting the MCO alternate function, on your keyboard hit CTRL + left click your mouse and drag the PA8 configuration to the available blinking GPIOs on the pin out view.
Step 4. Navigate to the ‘clock configuration’ tab and scroll to the bottom of the clock tree. On the bottom left-hand corner, you should see the MCO source mux. At this point, you can choose between the various clock sources available to output to the configured MCO pin.
At this point, if you have selected an available clock source to output to the MCO pin, you can go ahead and skip to step 7. However, shown in the MCO source mux, the LSE and HSE clock sources are greyed out and cannot be selected.
We will now work on enabling the LSE clock source in the project and configure the MCO to output the Nucleo-WB55RG’s LSE clock signal.
Step 5. To enable the LSE in STM32CubeMX, return to the ‘Pinout & Configuration’ tab. On the Pinout & Configuration table, on the left-hand side of the GUI under categories > expand the System Core tab > Select RCC > Under the RCC Mode window, select the LSE clock source using the drop-down menu.
Step 6. After the LSE has been enabled, return to the Clock Configuration tab, and scroll down to the MCO source mux. Now the LSE is no longer greyed out and can be selected as the MCO clock source.
Note that it is also possible to enable the HSE. Simply re-follow steps 3 to 6, but instead this time enable the HSE found above the LSE in RCC Mode window:
Step 7. Now navigate to the Project Manager tab, give the project a name and generate the code for your preferred Toolchain / IDE of choice.
Step 8. Flash the generated project onto your STM32 target device.
Step 9. To check our MCO on GPIO PA8 configuration, we will locate this pin using the Nucleo-WB55RG schematic and probe the pin to check the output clock signal using an oscilloscope.
Using the Nucleo-WB55RG schematic, we can confirm that PA8 is pulled out to a header pin available on the morpho connector CN10 and Arduino Shield Connector CN9.
After using an oscilloscope and probing the PA8 GPIO pin, we can confirm that the MCO is outputting the expected 32.768 kHz clock source as configured in our project above. 
Congratulations! You have successfully configured the MCO pin on your STM32 target device to output your selected clock source.
Relevant Documentation and Links: