samples: stm32: mco: Add example on how to configure MCO
The documentation does not provide any guides on how to configure
the MCO pin, and responsebility is given to the application.
Signed-off-by: Joakim Andersson <joerchan@gmail.com>
diff --git a/samples/boards/stm32/mco/CMakeLists.txt b/samples/boards/stm32/mco/CMakeLists.txt
new file mode 100644
index 0000000..54f7880
--- /dev/null
+++ b/samples/boards/stm32/mco/CMakeLists.txt
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.20.0)
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+project(mco)
+
+target_sources(app PRIVATE src/main.c)
diff --git a/samples/boards/stm32/mco/README.rst b/samples/boards/stm32/mco/README.rst
new file mode 100644
index 0000000..d3989d4
--- /dev/null
+++ b/samples/boards/stm32/mco/README.rst
@@ -0,0 +1,29 @@
+.. _samples_boards_stm32_mco:
+
+STM32 MCO example
+#################
+
+Overview
+********
+
+This sample is a minimum application to demonstrate how to output one of the internal clocks for
+external use by the application.
+
+Requirements
+************
+
+The SoC should support MCO functionality and use a pin that has the MCO alternate function.
+To support another board, add an overlay in boards folder.
+Make sure that the output clock is enabled in dts overlay file.
+
+
+Building and Running
+********************
+
+.. zephyr-app-commands::
+ :app: samples/boards/stm32/mco
+ :board: nucleo_u5a5zj_q
+ :goals: build flash
+
+After flashing, the LSE clock will be output on the MCO pin enabled in Device Tree.
+The clock can be observed using a probing device, such as a logic analyzer.
diff --git a/samples/boards/stm32/mco/boards/nucleo_u5a5zj_q.overlay b/samples/boards/stm32/mco/boards/nucleo_u5a5zj_q.overlay
new file mode 100644
index 0000000..9550a1d
--- /dev/null
+++ b/samples/boards/stm32/mco/boards/nucleo_u5a5zj_q.overlay
@@ -0,0 +1,12 @@
+/* The clock that is output must be enabled. */
+&clk_lse {
+ status = "okay";
+};
+
+/ {
+ zephyr,user {
+ /* Select MCO pin to use. */
+ pinctrl-0 = <&rcc_mco_pa8>;
+ pinctrl-names = "default";
+ };
+};
diff --git a/samples/boards/stm32/mco/prj.conf b/samples/boards/stm32/mco/prj.conf
new file mode 100644
index 0000000..4a6f627
--- /dev/null
+++ b/samples/boards/stm32/mco/prj.conf
@@ -0,0 +1,3 @@
+# Set MCO1 source to desired clock.
+CONFIG_CLOCK_STM32_MCO1_SRC_LSE=y
+CONFIG_CLOCK_STM32_MCO1_DIV=1
diff --git a/samples/boards/stm32/mco/sample.yaml b/samples/boards/stm32/mco/sample.yaml
new file mode 100644
index 0000000..51e4105
--- /dev/null
+++ b/samples/boards/stm32/mco/sample.yaml
@@ -0,0 +1,6 @@
+sample:
+ name: STM32 microcontroller clock output (MCO) example
+tests:
+ sample.board.stm32.mco:
+ platform_allow: nucleo_u5a5zj_q
+ tags: board
diff --git a/samples/boards/stm32/mco/src/main.c b/samples/boards/stm32/mco/src/main.c
new file mode 100644
index 0000000..edabdfa
--- /dev/null
+++ b/samples/boards/stm32/mco/src/main.c
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2024 Joakim Andersson
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <zephyr/kernel.h>
+#include <zephyr/drivers/pinctrl.h>
+
+/* Define the pinctrl information for the MCO pin. */
+PINCTRL_DT_DEFINE(DT_PATH(zephyr_user));
+
+int main(void)
+{
+ /* Configure the MCO pin using pinctrl in order to set the alternate function of the pin. */
+ const struct pinctrl_dev_config *pcfg = PINCTRL_DT_DEV_CONFIG_GET(DT_PATH(zephyr_user));
+ (void)pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
+
+ printk("\nMCO pin configured, end of example.\n");
+ return 0;
+}