embkernel
 All Classes Functions Variables Typedefs Groups Pages
Rtos.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
8 #ifndef RTOS_HPP_
9 #define RTOS_HPP_
10 
11 #include "RtosCfg.hpp"
12 #include "RtosRunnable.hpp"
13 #include "RtosPortable.hpp"
14 #include "RtosPortMacros.hpp"
15 #include <stdint.h>
16 
17 class RtosTask;
18 class RtosList;
19 
25 class Rtos {
26  friend class RtosTask;
27  friend class RtosInterrupt;
28  friend class RtosSyncObject;
29  friend class RtosPortable;
30 public:
31  typedef uint32_t TICK;
32  typedef unsigned int PRIORITY;
33  static constexpr TICK TICK_MAX = 0xFFFFFFFF;
34  static constexpr TICK TICK_INFINITE = 0xFFFFFFFF;
35 
36  static void start();
37  static void sleep(TICK tick);
38  static void sleepUntil(TICK tick);
39  static bool isElapsed(TICK tick);
40 
46  static constexpr TICK convertMsToTick(uint32_t ms) {
47  return ((TICK) ms * RTOS_CFG_TICK_FREQUENCY / 1000);
48  }
49 
55  static constexpr uint32_t convertTickToMs(TICK tick) {
56  return ((uint32_t) tick * 1000 / RTOS_CFG_TICK_FREQUENCY);
57  }
58 
63  inline static TICK getTick() {
64  return sTick;
65  }
66 
71  inline static RtosTask* getCurrentTask() {
72  return sCurrentTask;
73  }
74 
78  inline static void enterCriticalSection() {
79  RtosPortable::enterCriticalSection();
80  }
81 
85  inline static void exitCriticalSection() {
86  RtosPortable::exitCriticalSection();
87  }
88 
92  inline static void disableAllInt() {
93  RtosPortable::disableAllInt();
94  }
95 
99  inline static void enableAllInt() {
100  RtosPortable::enableAllInt();
101  }
102 
106  inline static void nop() {
107  RTOS_NOP();
108  }
109 
110 private:
111 
112  static size_t sMaxPriorities;
113  static void* sMemToFree;
116 
117  static TICK sTick;
118  static RtosList sListReady[RTOS_CFG_MAX_PRIORITIES];
121  static size_t sCurrentTaskCount;
122 
123  static void addTask(RtosTask* task);
124  static void removeTask(RtosTask* task);
125  static void putOnSleepingList(RtosTask* task, TICK tick);
126  static void putOnSuspendedList(RtosTask* task);
127  static void onTick();
128  static void* schedule(void* stackPointer);
129  static void idleRun(RtosTask* task);
130 
131 #if RTOS_CFG_TRACE_BUFFER_SIZE
132 public:
133  typedef struct {
134  uint32_t cycles;
135  RtosTask* task;
136  } TRACE_TAG;
137  static int getTraceBuffer(TRACE_TAG** p);
138  static void releaseBuffer(int count);
139  static void clearTraceBuffer();
140 private:
141  typedef struct {
142  bool overloadError;
143  int indexIn;
144  int indexOut;
145  int count;
146  TRACE_TAG buffer[RTOS_CFG_TRACE_BUFFER_SIZE];
147  } TRACE_BUFFER;
148  static TRACE_BUFFER sTraceBuffer;
149 #endif
150 
151 };
152 
153 #endif /* RTOS_HPP_ */
154