µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os-timer.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#if defined(OS_USE_OS_APP_CONFIG_H)
14#include <cmsis-plus/os-app-config.h>
15#endif
16
17#include <cmsis-plus/rtos/os.h>
18
19// ----------------------------------------------------------------------------
20
21#if defined(__clang__)
22#pragma clang diagnostic ignored "-Wc++98-compat"
23#endif
24
25// ----------------------------------------------------------------------------
26
27namespace os
28{
29 namespace rtos
30 {
31 // ------------------------------------------------------------------------
32
54 const timer::attributes timer::once_initializer;
55
66 const timer::attributes_periodic timer::periodic_initializer;
67
68 // ------------------------------------------------------------------------
69
112 // ========================================================================
139 timer::timer (func_t function, func_args_t args, const attributes& attr) :
140 timer
141 { nullptr, function, args, attr }
142 {
143 }
144
171 timer::timer (const char* name, func_t function, func_args_t args,
172 const attributes& attr) :
173 object_named_system
174 { name }
175 {
176#if defined(OS_TRACE_RTOS_TIMER)
177 trace::printf ("%s() @%p %s\n", __func__, this, this->name ());
178#endif
179
180 // Don't call this from interrupt handlers.
182 // Don't call this from critical regions.
183 os_assert_throw(function != nullptr, EINVAL);
184
185 type_ = attr.tm_type;
186 func_ = function;
187 func_args_ = args;
188
189#if !defined(OS_USE_RTOS_PORT_TIMER)
190 clock_ = attr.clock != nullptr ? attr.clock : &sysclock;
191#endif
192
193#if defined(OS_USE_RTOS_PORT_TIMER)
194
195 port::timer::create (this, function, args);
196
197#else
198
199 period_ = 0;
200
201#endif
202 state_ = state::initialized;
203 }
204
216 {
217#if defined(OS_TRACE_RTOS_TIMER)
218 trace::printf ("%s() @%p %s\n", __func__, this, name ());
219#endif
220
221#if defined(OS_USE_RTOS_PORT_TIMER)
222
223 port::timer::destroy (this);
224
225#else
226
227 {
228 // ----- Enter critical section -------------------------------------
230
231 if (state_ == state::running)
232 {
233 timer_node_.unlink ();
234 }
235 // ----- Exit critical section --------------------------------------
236 }
237
238#endif
239 state_ = state::destroyed;
240 }
241
250 {
251#if defined(OS_TRACE_RTOS_TIMER)
252#pragma GCC diagnostic push
253#if defined(__clang__)
254#elif defined(__GNUC__)
255#pragma GCC diagnostic ignored "-Wuseless-cast"
256#endif
257 trace::printf ("%s(%u) @%p %s\n", __func__,
258 static_cast<unsigned int> (period), this, name ());
259#pragma GCC diagnostic pop
260#endif
261
262 // Don't call this from interrupt handlers.
264
265 if (period == 0)
266 {
267 period = 1;
268 }
269
270 result_t res;
271
272#if defined(OS_USE_RTOS_PORT_TIMER)
273
274 res = port::timer::start (this, period);
275
276#else
277
278 period_ = period;
279
280 timer_node_.timestamp = clock_->steady_now () + period;
281
282 {
283 // ----- Enter critical section -------------------------------------
285
286 // If started, stop.
287 timer_node_.unlink ();
288
289 clock_->steady_list ().link (timer_node_);
290 // ----- Exit critical section --------------------------------------
291 }
292 res = result::ok;
293
294#endif
295
296 if (res == result::ok)
297 {
298 state_ = state::running;
299 }
300 return res;
301 }
302
314 {
315#if defined(OS_TRACE_RTOS_TIMER)
316 trace::printf ("%s() @%p %s\n", __func__, this, name ());
317#endif
318
319 // Don't call this from interrupt handlers.
321
322 if (state_ != state::running)
323 {
324 return EAGAIN;
325 }
326
327 result_t res;
328
329#if defined(OS_USE_RTOS_PORT_TIMER)
330
331 res = port::timer::stop (this);
332
333#else
334
335 {
336 // ----- Enter critical section -------------------------------------
338
339 timer_node_.unlink ();
340 // ----- Exit critical section --------------------------------------
341 }
342 res = result::ok;
343
344#endif
345
346 state_ = state::stopped;
347 return res;
348 }
349
350#if !defined(OS_USE_RTOS_PORT_TIMER)
351
356 void
357 timer::internal_interrupt_service_routine (void)
358 {
359
360 if (type_ == run::periodic)
361 {
362 // Re-arm the timer for the next period.
363 timer_node_.timestamp += period_;
364
365 // No need for critical section in ISR.
366 clock_->steady_list ().link (timer_node_);
367 }
368 else
369 {
370 state_ = state::completed;
371 }
372
373#if defined(OS_USE_RTOS_PORT_TIMER)
374 trace::puts (name ());
375#endif
376
377 // Call the user function.
378 func_ (func_args_);
379 }
380
385#endif
386
387 // --------------------------------------------------------------------------
388
389 } /* namespace rtos */
390} /* namespace os */
391
392// ----------------------------------------------------------------------------
const char * name(void) const
Get object name.
Definition os-decls.h:759
Interrupts critical section RAII helper.
Definition os-sched.h:498
Timer attributes.
Definition os-timer.h:135
User single-shot or periodic timer.
Definition os-timer.h:57
~timer()
Destruct the timer object instance.
Definition os-timer.cpp:215
timer(func_t function, func_args_t args, const attributes &attr=once_initializer)
Construct a timer object instance.
Definition os-timer.cpp:139
static const attributes once_initializer
Default one shot timer initialiser.
Definition os-timer.h:208
result_t stop(void)
Stop the timer.
Definition os-timer.cpp:313
result_t start(clock::duration_t period)
Start or restart the timer.
Definition os-timer.cpp:249
int puts(const char *s)
Write the string and a line terminator to the trace device.
Definition trace.cpp:103
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:76
clock_systick sysclock
The system clock object instance.
void * func_args_t
Timer call back function arguments.
Definition os-timer.h:64
void(*)(func_args_t args) func_t
Entry point of a timer call back function.
Definition os-timer.h:70
static const attributes_periodic periodic_initializer
Default periodic timer initialiser.
Definition os-timer.h:255
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1108
@ ok
Function completed; no errors or events occurred.
Definition os-decls.h:181
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:96
System namespace.
#define os_assert_throw(__e, __er)
Assert or throw a system error exception.
Definition os-decls.h:1130
#define os_assert_err(__e, __er)
Assert or return an error.
Definition os-decls.h:1115
Single file µOS++ RTOS definitions.
@ periodic
Run periodically.
Definition os-timer.h:95