C/C++ API Reference
Loading...
Searching...
No Matches
external_source.h
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_clock_tree/clock_tree.h"
17#include "pw_digital_io/digital_io.h"
18#include "pw_thread/sleep.h"
19
21namespace pw::clock_tree {
22
24
26class ExternalClockSource final : public ClockSource<ElementBlocking> {
27 public:
32 pw::digital_io::DigitalOut& enable_line,
33 pw::chrono::SystemClock::duration activation_delay =
34 pw::chrono::SystemClock::duration::zero(),
35 pw::chrono::SystemClock::duration deactivation_delay =
36 pw::chrono::SystemClock::duration::zero())
37 : enable_line_(&enable_line),
38 activation_delay_(activation_delay),
39 deactivation_delay_(deactivation_delay) {}
40 constexpr ExternalClockSource(
41 pw::chrono::SystemClock::duration activation_delay =
42 pw::chrono::SystemClock::duration::zero(),
43 pw::chrono::SystemClock::duration deactivation_delay =
44 pw::chrono::SystemClock::duration::zero())
45 : activation_delay_(activation_delay),
46 deactivation_delay_(deactivation_delay) {}
47
48 void SetOutLine(pw::digital_io::DigitalOut& enable_line) {
49 enable_line_ = &enable_line;
50 }
51
52 private:
53 pw::digital_io::DigitalOut* enable_line_ = nullptr;
54 pw::chrono::SystemClock::duration activation_delay_;
55 pw::chrono::SystemClock::duration deactivation_delay_;
56
58 Status DoEnable() final {
59 if (enable_line_ == nullptr) {
61 }
62 PW_TRY(enable_line_->SetStateActive());
63 if (activation_delay_ > pw::chrono::SystemClock::duration::zero()) {
64 pw::this_thread::sleep_for(activation_delay_);
65 }
66 return pw::OkStatus();
67 }
68
70 Status DoDisable() final {
71 if (enable_line_ == nullptr) {
73 }
74 PW_TRY(enable_line_->SetStateInactive());
75 if (deactivation_delay_ > pw::chrono::SystemClock::duration::zero()) {
76 pw::this_thread::sleep_for(deactivation_delay_);
77 }
78 return pw::OkStatus();
79 }
80};
81
82} // namespace pw::clock_tree
Definition: status.h:120
static constexpr Status FailedPrecondition()
Definition: status.h:243
Definition: clock_tree.h:241
Class that represents an external clock source enabled by a GPIO line.
Definition: external_source.h:26
Definition: digital_io.h:485
Status SetStateInactive()
Definition: digital_io.h:151
Status SetStateActive()
Definition: digital_io.h:136
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
Status DoEnable() final
Activate external clock source.
Definition: external_source.h:58
Status DoDisable() final
Deactivate external clock source.
Definition: external_source.h:70
constexpr ExternalClockSource(pw::digital_io::DigitalOut &enable_line, pw::chrono::SystemClock::duration activation_delay=pw::chrono::SystemClock::duration::zero(), pw::chrono::SystemClock::duration deactivation_delay=pw::chrono::SystemClock::duration::zero())
Definition: external_source.h:31
#define PW_TRY(expr)
Returns early if expr is a non-OK Status or Result.
Definition: try.h:27
constexpr Status OkStatus()
Definition: status.h:450
Clock tree management library.
Definition: clock_tree.h:30