Mark3 Realtime Kernel
timer.h
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 =========================================================================== */
21 #pragma once
22 #include "kerneltypes.h"
23 #include "mark3cfg.h"
24 
25 #include "ll.h"
26 
27 namespace Mark3
28 {
29 class Thread;
30 
31 //---------------------------------------------------------------------------
32 static constexpr auto uMaxTimerTicks = uint32_t { 0x7FFFFFFF };
33 static constexpr auto uTimerTicksInvalid = uint32_t { 0 };
34 static constexpr auto uTimerFlagOneShot = uint8_t { 0x01 };
35 static constexpr auto uTimerFlagActive = uint8_t { 0x02 };
36 static constexpr auto uTimerFlagCallback = uint8_t { 0x04 };
37 static constexpr auto uTimerFlagExpired = uint8_t { 0x08 };
38 
39 //---------------------------------------------------------------------------
50 using TimerCallback = void (*)(Thread* pclOwner_, void* pvData_);
51 
52 //---------------------------------------------------------------------------
53 class TimerList;
54 class TimerScheduler;
55 class Quantum;
56 
57 //---------------------------------------------------------------------------
68 class Timer : public TypedLinkListNode<Timer>
69 {
70 public:
71  void* operator new(size_t sz, void* pv) { return reinterpret_cast<Timer*>(pv); }
72  ~Timer() {}
73 
79  Timer();
80 
85  void Init();
86 
97  void Start(bool bRepeat_, uint32_t u32IntervalMs_, TimerCallback pfCallback_, void* pvData_);
98 
106  void Start();
107 
113  void Stop();
114 
115 private:
116  friend class TimerList;
117 
122 
128 
129  static inline uint32_t SecondsToTicks(uint32_t x) { return (x) * 1000; }
130  static inline uint32_t MSecondsToTicks(uint32_t x) { return (x); }
131  static inline uint32_t USecondsToTicks(uint32_t x) { return ((x + 999) / 1000); }
132 
133  static constexpr auto m_uTimerInvalidCookie = uint8_t { 0x3C };
134  static constexpr auto m_uTimerInitCookie = uint8_t { 0xC3 };
135 
138 
140  uint8_t m_u8Flags;
141 
144 
146  uint32_t m_u32Interval;
147 
149  uint32_t m_u32TimeLeft;
150 
153 
155  void* m_pvData;
156 };
157 } // namespace Mark3
uint32_t m_u32Interval
Interval of the timer in timer ticks.
Definition: timer.h:146
the TimerList class. This class implements a doubly-linked-list of timer objects. ...
Definition: timerlist.h:40
bool IsInitialized(void)
IsInitialized.
Definition: timer.h:127
uint8_t m_u8Flags
Flags for the timer, defining if the timer is one-shot or repeated.
Definition: timer.h:140
static constexpr auto uTimerFlagCallback
Timer is pending a callback.
Definition: timer.h:36
Basic data type primatives used throughout the OS.
static uint32_t USecondsToTicks(uint32_t x)
Definition: timer.h:131
void SetInitialized()
SetInitialized.
Definition: timer.h:121
uint32_t m_u32TimeLeft
Time remaining on the timer.
Definition: timer.h:149
TimerCallback m_pfCallback
Pointer to the callback function.
Definition: timer.h:143
void Init()
Init Re-initialize the Timer to default values.
Definition: timer.cpp:36
~Timer()
Definition: timer.h:72
static constexpr auto m_uTimerInitCookie
Definition: timer.h:134
Definition: atomic.cpp:23
Mark3 Kernel Configuration This file is used to configure the kernel for your specific application in...
void Stop()
Stop Stop a timer already in progress. Has no effect on timers that have already been stopped...
Definition: timer.cpp:86
Timer()
Timer Default Constructor - Do nothing. Allow the init call to perform the necessary object initializ...
Definition: timer.cpp:29
void(*)(Thread *pclOwner_, void *pvData_) TimerCallback
Definition: timer.h:50
The Thread Class. This object providing the fundamental thread control data structures and functions ...
Definition: thread.h:64
static constexpr auto m_uTimerInvalidCookie
Definition: timer.h:133
static uint32_t SecondsToTicks(uint32_t x)
Definition: timer.h:129
static uint32_t MSecondsToTicks(uint32_t x)
Definition: timer.h:130
The Quantum Class. Static-class used to implement Thread quantum functionality, which is fundamental ...
Definition: quantum.h:42
The TimerScheduler Class. This implements a "Static" class used to manage a global list of timers use...
void Start()
Start Start or restart a timer using parameters previously configured via calls to Start(<with args>)...
Definition: timer.cpp:73
static constexpr auto uTimerFlagOneShot
Timer is one-shot.
Definition: timer.h:34
The Timer Class. This class provides kernel-managed timers, used to provide high-precision delays...
Definition: timer.h:68
uint8_t m_u8Initialized
Cookie used to determine whether or not the timer is initialized.
Definition: timer.h:137
void * m_pvData
Pointer to the callback data.
Definition: timer.h:155
static constexpr auto uMaxTimerTicks
Maximum value to set.
Definition: timer.h:32
static constexpr auto uTimerFlagActive
Timer is currently active.
Definition: timer.h:35
Core linked-list declarations, used by all kernel list types At the heart of RTOS data structures are...
Thread * m_pclOwner
Pointer to the owner thread.
Definition: timer.h:152
static constexpr auto uTimerTicksInvalid
Definition: timer.h:33
static constexpr auto uTimerFlagExpired
Timer is actually expired.
Definition: timer.h:37