tin  1.5.9
bare_metal.h
Go to the documentation of this file.
1 // Copyright (c) 2026 Tinverse LLC. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Tinverse-Commercial
3 
12 
13 #pragma once
14 
15 #include <cstdint>
16 #include <utility>
17 
18 #include "tsm/runtime/executor.h"
19 
20 namespace tsm {
21 
29 {
30  static void notify() noexcept {}
31  static void wait_for_event() noexcept {}
32 };
33 
35 {
36  public:
37  explicit constexpr bare_metal_tick_source(tsm::tick_rep volatile& counter)
38  : counter_(&counter)
39  {
40  }
41 
42  [[nodiscard]] tsm::tick_rep ticks() const noexcept
43  {
44  return *counter_;
45  }
46 
47  private:
48  tsm::tick_rep volatile* counter_{};
49 };
50 
51 template<typename... Tasks>
53 {
54  public:
55  explicit constexpr bare_metal_task_executor(Tasks&... tasks)
56  : ready_(tasks...)
57  {
58  }
59 
60  [[nodiscard]] bool step()
61  {
62  return ready_.step();
63  }
64 
65  std::size_t run_ready()
66  {
67  const auto ran = ready_.run_ready();
68  if (ran == 0U) {
69  // Idle behavior belongs to the board support package. The default
70  // hook is empty; Cortex-M projects can map it to WFE or WFI.
72  }
73  return ran;
74  }
75 
76  std::size_t tick(tsm::tick_rep elapsed_ticks = 1U)
77  {
78  // SysTick or another interrupt source should translate elapsed time to
79  // integer ticks before calling this function.
80  const auto resumed = ready_.tick(elapsed_ticks);
81  if (resumed != 0U) {
82  wake();
83  }
84  return resumed;
85  }
86 
87  std::size_t tick(tsm::tick_count elapsed_ticks)
88  {
89  return tick(elapsed_ticks.count());
90  }
91 
92  void wake()
93  {
95  }
97  {
98  wake();
99  }
101  {
103  }
104  void start_all()
105  {
106  ready_.start_all();
107  }
108 
109  template<auto Entry, std::size_t Instance = 0U>
110  [[nodiscard]] spawn_result start()
111  {
112  auto result = ready_.template start<Entry, Instance>();
113  if (result == spawn_result::started) {
114  wake();
115  }
116  return result;
117  }
118 
119  template<auto Entry, typename... Args>
120  [[nodiscard]] spawn_result spawn(Args&&... args)
121  {
122  return ready_.template spawn<Entry>(std::forward<Args>(args)...);
123  }
124 
126  {
128  }
129 
130  template<auto Entry, std::size_t Instance = 0U>
131  [[nodiscard]] task_status task_status() const
132  {
133  return ready_.template task_status<Entry, Instance>();
134  }
135 
136  template<auto Entry, std::size_t Instance = 0U>
138  {
139  return ready_.template task_failure_reason<Entry, Instance>();
140  }
141 
142  template<auto Entry, std::size_t Instance = 0U>
143  [[nodiscard]] bool cancel() noexcept
144  {
145  const bool cancelled = ready_.template cancel<Entry, Instance>();
146  if (cancelled) {
147  wake();
148  }
149  return cancelled;
150  }
151 
152  void cancel_all() noexcept
153  {
154  ready_.cancel_all();
155  wake();
156  }
157 
158  private:
159  runtime::cooperative_executor<Tasks...> ready_;
160 };
161 
162 template<typename... Tasks>
164 
165 template<typename... Tasks>
167 
168 } // namespace tsm
Definition: bare_metal.h:53
bool step()
Definition: bare_metal.h:60
runtime::task_spawner< bare_metal_task_executor > spawner()
Definition: bare_metal.h:125
spawn_result spawn(Args &&... args)
Definition: bare_metal.h:120
task_status task_status() const
Definition: bare_metal.h:131
constexpr bare_metal_task_executor(Tasks &... tasks)
Definition: bare_metal.h:55
bool cancel() noexcept
Definition: bare_metal.h:143
std::size_t tick(tsm::tick_rep elapsed_ticks=1U)
Definition: bare_metal.h:76
void cancel_all() noexcept
Definition: bare_metal.h:152
std::size_t run_ready()
Definition: bare_metal.h:65
void wake()
Definition: bare_metal.h:92
spawn_result start()
Definition: bare_metal.h:110
task_failure_reason task_failure_reason() const
Definition: bare_metal.h:137
void start_all()
Definition: bare_metal.h:104
std::size_t tick(tsm::tick_count elapsed_ticks)
Definition: bare_metal.h:87
void wait_for_work()
Definition: bare_metal.h:100
void wake_from_isr()
Definition: bare_metal.h:96
Definition: bare_metal.h:35
constexpr bare_metal_tick_source(tsm::tick_rep volatile &counter)
Definition: bare_metal.h:37
tsm::tick_rep ticks() const noexcept
Definition: bare_metal.h:42
void start_all()
Definition: executor.h:197
bool step()
Definition: executor.h:161
std::size_t tick(tsm::tick_rep elapsed_ticks=1U)
Definition: executor.h:181
void cancel_all() noexcept
Definition: executor.h:276
std::size_t run_ready()
Definition: executor.h:172
Definition: executor.h:294
Definition: executor.h:132
Target-independent executors for tsm runtimes.
Definition: bare_metal.h:20
task_failure_reason
Definition: coroutine.h:170
task_status
Definition: coroutine.h:160
bare_metal_task_executor(Tasks &...) -> bare_metal_task_executor< Tasks... >
spawn_result
Definition: coroutine.h:181
TSM_TICK_REP tick_rep
Definition: ticks.h:35
Definition: bare_metal.h:29
static void notify() noexcept
Definition: bare_metal.h:30
static void wait_for_event() noexcept
Definition: bare_metal.h:31
Definition: coroutine.h:1162
Strong value type for semantic scheduler ticks.
Definition: ticks.h:54
constexpr tick_rep count() const noexcept
Definition: ticks.h:73