Skip to content

Commit

Permalink
pw_clock_tree_mcuxpresso: Move example code out of docs
Browse files Browse the repository at this point in the history
This change moves out the example from the docs.rst file
into the examples.cc file, so that we can validate that
the code compiles correctly.

I removed the CLK IN example, since it would require
more logic to be used, however I integrated the MLCK IN
by using it by Ctimer0. The new documentation also uses
mermaid diagrams to visualize the clock tree element
definitions.

Test: pw presubmit --step=gn_mimxrt595_build
Test: pw presubmit --step=gn_mimxrt595_freertos_build

Bug: 331672574
Change-Id: I122d8a19b2e7bbbec11ad0edb9904f80a55b0178
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/213852
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Austin Foxley <[email protected]>
Commit-Queue: Christoph Klee <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
  • Loading branch information
Christoph Klee authored and CQ Bot Account committed Jun 4, 2024
1 parent 21277f9 commit 1aa7f44
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 51 deletions.
13 changes: 13 additions & 0 deletions pw_clock_tree_mcuxpresso/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
# License for the specific language governing permissions and limitations under
# the License.

load(
"//pw_build:pigweed.bzl",
"pw_cc_test",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])
Expand All @@ -29,3 +34,11 @@ cc_library(
"@pigweed//targets:mcuxpresso_sdk",
],
)

pw_cc_test(
name = "clock_tree_mcuxpresso_examples",
srcs = ["examples.cc"],
deps = [
":pw_clock_tree_mcuxpresso",
],
)
16 changes: 13 additions & 3 deletions pw_clock_tree_mcuxpresso/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ if (pw_third_party_mcuxpresso_SDK != "") {
pw_third_party_mcuxpresso_SDK,
]
}
}

pw_test_group("tests") {
tests = []
pw_test("clock_tree_mcuxpresso_examples") {
sources = [ "examples.cc" ]
deps = [ ":pw_clock_tree_mcuxpresso" ]
}

pw_test_group("tests") {
tests = [ ":clock_tree_mcuxpresso_examples" ]
}
} else {
pw_test_group("tests") {
tests = []
}
}

pw_doc_group("docs") {
inputs = [ "examples.cc" ]
sources = [ "docs.rst" ]
}
122 changes: 74 additions & 48 deletions pw_clock_tree_mcuxpresso/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,96 +27,122 @@ Other clock tree components such as PLLs can be instantiated by deriving custom
from the abstract class `DependentElement` and overwriting `DoEnable` and `DoDisable` methods.

Examples
==============
========

.. inclusive-language: disable
Definition of clock tree elements:

.. code-block:: cpp
.. mermaid::

// Define FRO_DIV_4 clock source
constinit ClockMcuxpressoFro fro_div_4(kCLOCK_FroDiv4OutEn);
flowchart LR
A(fro_div_4) -->B(frg_0)
B-->C(flexcomm_selector_0)
style A fill:#0f0,stroke:#333,stroke-width:2px
style B fill:#0f0,stroke:#333,stroke-width:2px
style C fill:#0f0,stroke:#333,stroke-width:2px

// Define FRO_DIV_8 clock source
constinit ClockMcuxpressoFro fro_div_8(kCLOCK_FroDiv8OutEn);
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Flexcomm0]
:end-before: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Flexcomm0]

// Define Low-Power Oscillator
constinit ClockMcuxpressoLpOsc lp_osc_clk;
.. mermaid::

// Define Master clock
constinit ClockMcuxpressoMclk mclk(19200000);
flowchart LR
D(fro_div_8)--> E(i3c_selector)
E --> F(i3c_divider)
style D fill:#f0f,stroke:#333,stroke-width:2px
style E fill:#f0f,stroke:#333,stroke-width:2px
style F fill:#f0f,stroke:#333,stroke-width:2px

// Define extern clock input
constinit ClockMcuxpressoClkIn clk_in(25000000);
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-i3c0]
:end-before: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-i3c0]

// Define FRG0 configuration
const clock_frg_clk_config_t g_frg0Config_BOARD_BOOTCLOCKRUN =
{
.num = 0,
.sfg_clock_src = _clock_frg_clk_config::kCLOCK_FrgFroDiv4,
.divider = 255U,
.mult = 144
};
.. mermaid::

constinit ClockMcuxpressoFrgNonBlocking frg0(fro_div_4, g_frg0Config_BOARD_BOOTCLOCKRUN);
flowchart LR
G(mclk) --> H(ctimer_0)
style G fill:#0ff,stroke:#333,stroke-width:2px
style H fill:#0ff,stroke:#333,stroke-width:2px

// Define clock source selector I3C01FCLKSEL
constinit ClockMcuxpressoSelectorNonBlocking i3c0_selector(fro_div_8,
kFRO_DIV8_to_I3C_CLK,
kNONE_to_I3C_CLK);
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Ctimer0]
:end-before: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Ctimer0]

// Define clock divider I3C01FCLKDIV
constinit ClockMcuxpressoDividerNonBlocking i3c0_divider(i3c0_selector,
kCLOCK_DivI3cClk,
12);
.. mermaid::

// Create the clock tree
ClockTree clock_tree;
flowchart LR
I(lposc)
style I fill:#ff0,stroke:#333,stroke-width:2px

// Enable the low-power oscillator
clock_tree.Acquire(lp_osc_clk);
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-LpOsc]
:end-before: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-LpOsc]

// Enable the i3c0_divider
clock_tree.Acquire(i3c0_divider);
Definition of clock tree:

// Change the i3c0_divider value
PW_TRY(clock_tree.SetDividerValue(i3c0_divider, 24));
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-ClockTreeDef]
:end-before: [pw_clock_tree_mcuxpresso-examples-ClockTreeDef]

// Disable the low-power oscillator
clock_tree.Release(lp_osc_clk);
Example usage of ``clock_tree`` APIs:

.. inclusive-language: enable
.. literalinclude:: examples.cc
:language: cpp
:linenos:
:start-after: [pw_clock_tree_mcuxpresso-examples-UseExample]
:end-before: [pw_clock_tree_mcuxpresso-examples-UseExample]

APIs
====

------------------
ClockMcuxpressoFro
==================
------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoFro
:members:

--------------------
ClockMcuxpressoLpOsc
====================
--------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoLpOsc
:members:

-------------------
ClockMcuxpressoMclk
===================
-------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoMclk
:members:

--------------------
ClockMcuxpressoClkIn
====================
--------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoClkIn
:members:

------------------
ClockMcuxpressoFrg
==================
------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoFrg
:members:

-----------------------
ClockMcuxpressoSelector
=======================
-----------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoSelector
:members:

----------------------
ClockMcuxpressoDivider
======================
----------------------
.. doxygenclass:: pw::clock_tree::ClockMcuxpressoDivider
:members:
99 changes: 99 additions & 0 deletions pw_clock_tree_mcuxpresso/examples.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_clock_tree_mcuxpresso/clock_tree.h"

// Test headers
#include "pw_unit_test/framework.h"

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Flexcomm0]

// Define FRO_DIV_4 clock source
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoFro fro_div_4(kCLOCK_FroDiv4OutEn);

// Define FRG0 configuration
const clock_frg_clk_config_t g_frg0Config_BOARD_BOOTCLOCKRUN = {
.num = 0,
.sfg_clock_src = _clock_frg_clk_config::kCLOCK_FrgFroDiv4,
.divider = 255U,
.mult = 144};

PW_CONSTINIT pw::clock_tree::ClockMcuxpressoFrgNonBlocking frg_0(
fro_div_4, g_frg0Config_BOARD_BOOTCLOCKRUN);

// Define clock source selector FLEXCOMM0
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoSelectorNonBlocking flexcomm_0(
frg_0, kFRG_to_FLEXCOMM0, kNONE_to_FLEXCOMM0);

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Flexcomm0]

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-i3c0]

// Define FRO_DIV_8 clock source
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoFro fro_div_8(kCLOCK_FroDiv8OutEn);

// Define clock source selector I3C01FCLKSEL
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoSelectorNonBlocking i3c0_selector(
fro_div_8, kFRO_DIV8_to_I3C_CLK, kNONE_to_I3C_CLK);

// Define clock divider I3C01FCLKDIV
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoDividerNonBlocking i3c0_divider(
i3c0_selector, kCLOCK_DivI3cClk, 12);

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-i3c0]

// inclusive-language: disable
// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Ctimer0]

// Define Master clock
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoMclk mclk(19200000);

// Define clock selector CTIMER0
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoSelectorNonBlocking ctimer_0(
mclk, kMASTER_CLK_to_CTIMER0, kNONE_to_CTIMER0);

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-Ctimer0]
// inclusive-language: enable

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-LpOsc]

// Define Low-Power Oscillator
PW_CONSTINIT pw::clock_tree::ClockMcuxpressoLpOsc lp_osc_clk;

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeElementDefs-LpOsc]

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeDef]

// Create the clock tree
pw::clock_tree::ClockTree clock_tree;

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-ClockTreeDef]

TEST(ClockTreeMcuxpresso, UseExample) {
// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-UseExample]

// Enable the low-power oscillator
clock_tree.Acquire(lp_osc_clk);

// Enable the i3c0_divider
clock_tree.Acquire(i3c0_divider);

// Change the i3c0_divider value
clock_tree.SetDividerValue(i3c0_divider, 24);

// Disable the low-power oscillator
clock_tree.Release(lp_osc_clk);

// DOCSTAG: [pw_clock_tree_mcuxpresso-examples-UseExample]
}

0 comments on commit 1aa7f44

Please sign in to comment.