Dependencies: PinDetect TextLCD mbed mRotaryEncoder
scmRTOS/Common/OS_Kernel.h@0:afb2650fb49a, 2012-02-13 (annotated)
- Committer:
- cicklaus
- Date:
- Mon Feb 13 02:11:20 2012 +0000
- Revision:
- 0:afb2650fb49a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cicklaus | 0:afb2650fb49a | 1 | //****************************************************************************** |
cicklaus | 0:afb2650fb49a | 2 | //* |
cicklaus | 0:afb2650fb49a | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
cicklaus | 0:afb2650fb49a | 4 | //* |
cicklaus | 0:afb2650fb49a | 5 | //* NICKNAME: scmRTOS |
cicklaus | 0:afb2650fb49a | 6 | //* |
cicklaus | 0:afb2650fb49a | 7 | //* PURPOSE: OS Kernel Header. Declarations And Definitions |
cicklaus | 0:afb2650fb49a | 8 | //* |
cicklaus | 0:afb2650fb49a | 9 | //* Version: 3.10 |
cicklaus | 0:afb2650fb49a | 10 | //* |
cicklaus | 0:afb2650fb49a | 11 | //* $Revision: 256 $ |
cicklaus | 0:afb2650fb49a | 12 | //* $Date:: 2010-01-22 #$ |
cicklaus | 0:afb2650fb49a | 13 | //* |
cicklaus | 0:afb2650fb49a | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
cicklaus | 0:afb2650fb49a | 15 | //* |
cicklaus | 0:afb2650fb49a | 16 | //* Permission is hereby granted, free of charge, to any person |
cicklaus | 0:afb2650fb49a | 17 | //* obtaining a copy of this software and associated documentation |
cicklaus | 0:afb2650fb49a | 18 | //* files (the "Software"), to deal in the Software without restriction, |
cicklaus | 0:afb2650fb49a | 19 | //* including without limitation the rights to use, copy, modify, merge, |
cicklaus | 0:afb2650fb49a | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
cicklaus | 0:afb2650fb49a | 21 | //* and to permit persons to whom the Software is furnished to do so, |
cicklaus | 0:afb2650fb49a | 22 | //* subject to the following conditions: |
cicklaus | 0:afb2650fb49a | 23 | //* |
cicklaus | 0:afb2650fb49a | 24 | //* The above copyright notice and this permission notice shall be included |
cicklaus | 0:afb2650fb49a | 25 | //* in all copies or substantial portions of the Software. |
cicklaus | 0:afb2650fb49a | 26 | //* |
cicklaus | 0:afb2650fb49a | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
cicklaus | 0:afb2650fb49a | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
cicklaus | 0:afb2650fb49a | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
cicklaus | 0:afb2650fb49a | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
cicklaus | 0:afb2650fb49a | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
cicklaus | 0:afb2650fb49a | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
cicklaus | 0:afb2650fb49a | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
cicklaus | 0:afb2650fb49a | 34 | //* |
cicklaus | 0:afb2650fb49a | 35 | //* ================================================================= |
cicklaus | 0:afb2650fb49a | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
cicklaus | 0:afb2650fb49a | 37 | //* information, license and contact details. |
cicklaus | 0:afb2650fb49a | 38 | //* ================================================================= |
cicklaus | 0:afb2650fb49a | 39 | //* |
cicklaus | 0:afb2650fb49a | 40 | //***************************************************************************** |
cicklaus | 0:afb2650fb49a | 41 | |
cicklaus | 0:afb2650fb49a | 42 | #ifndef OS_KERNEL_H |
cicklaus | 0:afb2650fb49a | 43 | #define OS_KERNEL_H |
cicklaus | 0:afb2650fb49a | 44 | |
cicklaus | 0:afb2650fb49a | 45 | #include <stddef.h> |
cicklaus | 0:afb2650fb49a | 46 | #include <commdefs.h> |
cicklaus | 0:afb2650fb49a | 47 | #include <usrlib.h> |
cicklaus | 0:afb2650fb49a | 48 | |
cicklaus | 0:afb2650fb49a | 49 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 50 | |
cicklaus | 0:afb2650fb49a | 51 | //============================================================================== |
cicklaus | 0:afb2650fb49a | 52 | extern "C" void OS_Start(TStackItem* sp); |
cicklaus | 0:afb2650fb49a | 53 | |
cicklaus | 0:afb2650fb49a | 54 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0 |
cicklaus | 0:afb2650fb49a | 55 | extern "C" void OS_ContextSwitcher(TStackItem** Curr_SP, TStackItem* Next_SP); |
cicklaus | 0:afb2650fb49a | 56 | #else |
cicklaus | 0:afb2650fb49a | 57 | extern "C" TStackItem* OS_ContextSwitchHook(TStackItem* sp); |
cicklaus | 0:afb2650fb49a | 58 | #endif |
cicklaus | 0:afb2650fb49a | 59 | |
cicklaus | 0:afb2650fb49a | 60 | //============================================================================== |
cicklaus | 0:afb2650fb49a | 61 | |
cicklaus | 0:afb2650fb49a | 62 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 63 | // |
cicklaus | 0:afb2650fb49a | 64 | // |
cicklaus | 0:afb2650fb49a | 65 | // NAME : OS |
cicklaus | 0:afb2650fb49a | 66 | // |
cicklaus | 0:afb2650fb49a | 67 | // PURPOSE : Namespace for all OS stuff |
cicklaus | 0:afb2650fb49a | 68 | // |
cicklaus | 0:afb2650fb49a | 69 | // DESCRIPTION: Includes: Kernel, |
cicklaus | 0:afb2650fb49a | 70 | // Processes, |
cicklaus | 0:afb2650fb49a | 71 | // Mutexes, |
cicklaus | 0:afb2650fb49a | 72 | // Event Flags, |
cicklaus | 0:afb2650fb49a | 73 | // Byte-wide Channels, |
cicklaus | 0:afb2650fb49a | 74 | // Arbitrary-type Channels, |
cicklaus | 0:afb2650fb49a | 75 | // Messages |
cicklaus | 0:afb2650fb49a | 76 | // |
cicklaus | 0:afb2650fb49a | 77 | namespace OS |
cicklaus | 0:afb2650fb49a | 78 | { |
cicklaus | 0:afb2650fb49a | 79 | class TBaseProcess; |
cicklaus | 0:afb2650fb49a | 80 | |
cicklaus | 0:afb2650fb49a | 81 | INLINE inline void SetPrioTag(TProcessMap& pm, const TProcessMap PrioTag) { pm |= PrioTag; } |
cicklaus | 0:afb2650fb49a | 82 | INLINE inline void ClrPrioTag(TProcessMap& pm, const TProcessMap PrioTag) { pm &= ~PrioTag; } |
cicklaus | 0:afb2650fb49a | 83 | |
cicklaus | 0:afb2650fb49a | 84 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 85 | // |
cicklaus | 0:afb2650fb49a | 86 | // NAME : TKernel |
cicklaus | 0:afb2650fb49a | 87 | // |
cicklaus | 0:afb2650fb49a | 88 | /// Implements kernel-level operations such as |
cicklaus | 0:afb2650fb49a | 89 | /// process management, process-level scheduling, |
cicklaus | 0:afb2650fb49a | 90 | /// ISR-level scheduling, system timing. |
cicklaus | 0:afb2650fb49a | 91 | // |
cicklaus | 0:afb2650fb49a | 92 | // DESCRIPTION: |
cicklaus | 0:afb2650fb49a | 93 | // |
cicklaus | 0:afb2650fb49a | 94 | // |
cicklaus | 0:afb2650fb49a | 95 | class TKernel |
cicklaus | 0:afb2650fb49a | 96 | { |
cicklaus | 0:afb2650fb49a | 97 | //----------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 98 | // |
cicklaus | 0:afb2650fb49a | 99 | // Declarations |
cicklaus | 0:afb2650fb49a | 100 | // |
cicklaus | 0:afb2650fb49a | 101 | |
cicklaus | 0:afb2650fb49a | 102 | |
cicklaus | 0:afb2650fb49a | 103 | friend class TISRW; |
cicklaus | 0:afb2650fb49a | 104 | friend class TISRW_SS; |
cicklaus | 0:afb2650fb49a | 105 | friend class TBaseProcess; |
cicklaus | 0:afb2650fb49a | 106 | friend class TMutex; |
cicklaus | 0:afb2650fb49a | 107 | friend class TEventFlag; |
cicklaus | 0:afb2650fb49a | 108 | friend class TChannel; |
cicklaus | 0:afb2650fb49a | 109 | friend class TBaseMessage; |
cicklaus | 0:afb2650fb49a | 110 | |
cicklaus | 0:afb2650fb49a | 111 | template<typename T, word size, class S> friend class channel; |
cicklaus | 0:afb2650fb49a | 112 | template<typename T> friend class message; |
cicklaus | 0:afb2650fb49a | 113 | |
cicklaus | 0:afb2650fb49a | 114 | friend void Run(); |
cicklaus | 0:afb2650fb49a | 115 | friend void WakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 116 | friend void ForceWakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 117 | friend inline bool IsProcessSleeping(const TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 118 | friend inline bool IsProcessSuspended(const TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 119 | friend inline dword GetTickCount(); |
cicklaus | 0:afb2650fb49a | 120 | |
cicklaus | 0:afb2650fb49a | 121 | //----------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 122 | // |
cicklaus | 0:afb2650fb49a | 123 | // Data |
cicklaus | 0:afb2650fb49a | 124 | // |
cicklaus | 0:afb2650fb49a | 125 | private: |
cicklaus | 0:afb2650fb49a | 126 | byte CurProcPriority; |
cicklaus | 0:afb2650fb49a | 127 | TProcessMap ReadyProcessMap; |
cicklaus | 0:afb2650fb49a | 128 | TBaseProcess* ProcessTable[scmRTOS_PROCESS_COUNT+1]; |
cicklaus | 0:afb2650fb49a | 129 | volatile byte ISR_NestCount; |
cicklaus | 0:afb2650fb49a | 130 | |
cicklaus | 0:afb2650fb49a | 131 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
cicklaus | 0:afb2650fb49a | 132 | byte SchedProcPriority; |
cicklaus | 0:afb2650fb49a | 133 | #endif |
cicklaus | 0:afb2650fb49a | 134 | |
cicklaus | 0:afb2650fb49a | 135 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 136 | volatile dword SysTickCount; |
cicklaus | 0:afb2650fb49a | 137 | #endif |
cicklaus | 0:afb2650fb49a | 138 | |
cicklaus | 0:afb2650fb49a | 139 | //----------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 140 | // |
cicklaus | 0:afb2650fb49a | 141 | // Functions |
cicklaus | 0:afb2650fb49a | 142 | // |
cicklaus | 0:afb2650fb49a | 143 | public: |
cicklaus | 0:afb2650fb49a | 144 | INLINE TKernel() |
cicklaus | 0:afb2650fb49a | 145 | : CurProcPriority(pr0) |
cicklaus | 0:afb2650fb49a | 146 | , ReadyProcessMap( (1 << (scmRTOS_PROCESS_COUNT + 1)) - 1) // set all processes ready |
cicklaus | 0:afb2650fb49a | 147 | , ISR_NestCount(0) |
cicklaus | 0:afb2650fb49a | 148 | { |
cicklaus | 0:afb2650fb49a | 149 | } |
cicklaus | 0:afb2650fb49a | 150 | |
cicklaus | 0:afb2650fb49a | 151 | private: |
cicklaus | 0:afb2650fb49a | 152 | INLINE inline void RegisterProcess(TBaseProcess* const p); |
cicklaus | 0:afb2650fb49a | 153 | |
cicklaus | 0:afb2650fb49a | 154 | void Sched(); |
cicklaus | 0:afb2650fb49a | 155 | INLINE void Scheduler() { if(ISR_NestCount) return; else Sched(); } |
cicklaus | 0:afb2650fb49a | 156 | INLINE inline void SchedISR(); |
cicklaus | 0:afb2650fb49a | 157 | |
cicklaus | 0:afb2650fb49a | 158 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
cicklaus | 0:afb2650fb49a | 159 | INLINE inline bool IsContextSwitchDone() const volatile; |
cicklaus | 0:afb2650fb49a | 160 | #endif |
cicklaus | 0:afb2650fb49a | 161 | INLINE void SetProcessReady (const byte pr) { TProcessMap PrioTag = GetPrioTag(pr); SetPrioTag( ReadyProcessMap, PrioTag); } |
cicklaus | 0:afb2650fb49a | 162 | INLINE void SetProcessUnready(const byte pr) { TProcessMap PrioTag = GetPrioTag(pr); ClrPrioTag( ReadyProcessMap, PrioTag); } |
cicklaus | 0:afb2650fb49a | 163 | |
cicklaus | 0:afb2650fb49a | 164 | public: |
cicklaus | 0:afb2650fb49a | 165 | INLINE inline void SystemTimer(); |
cicklaus | 0:afb2650fb49a | 166 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
cicklaus | 0:afb2650fb49a | 167 | INLINE inline TStackItem* ContextSwitchHook(TStackItem* sp); |
cicklaus | 0:afb2650fb49a | 168 | #endif |
cicklaus | 0:afb2650fb49a | 169 | |
cicklaus | 0:afb2650fb49a | 170 | }; // End of TKernel class definition |
cicklaus | 0:afb2650fb49a | 171 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 172 | extern TKernel Kernel; |
cicklaus | 0:afb2650fb49a | 173 | |
cicklaus | 0:afb2650fb49a | 174 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 175 | // |
cicklaus | 0:afb2650fb49a | 176 | /// BaseProcess |
cicklaus | 0:afb2650fb49a | 177 | /// |
cicklaus | 0:afb2650fb49a | 178 | /// Implements base class-type for application processes |
cicklaus | 0:afb2650fb49a | 179 | // |
cicklaus | 0:afb2650fb49a | 180 | // DESCRIPTION: |
cicklaus | 0:afb2650fb49a | 181 | // |
cicklaus | 0:afb2650fb49a | 182 | // |
cicklaus | 0:afb2650fb49a | 183 | class TBaseProcess |
cicklaus | 0:afb2650fb49a | 184 | { |
cicklaus | 0:afb2650fb49a | 185 | friend class TKernel; |
cicklaus | 0:afb2650fb49a | 186 | friend class TISRW; |
cicklaus | 0:afb2650fb49a | 187 | friend class TISRW_SS; |
cicklaus | 0:afb2650fb49a | 188 | friend class TEventFlag; |
cicklaus | 0:afb2650fb49a | 189 | friend class TMutex; |
cicklaus | 0:afb2650fb49a | 190 | friend class TBaseMessage; |
cicklaus | 0:afb2650fb49a | 191 | |
cicklaus | 0:afb2650fb49a | 192 | template<typename T, word size, class S> friend class channel; |
cicklaus | 0:afb2650fb49a | 193 | template<typename T> friend class message; |
cicklaus | 0:afb2650fb49a | 194 | |
cicklaus | 0:afb2650fb49a | 195 | |
cicklaus | 0:afb2650fb49a | 196 | friend void Run(); |
cicklaus | 0:afb2650fb49a | 197 | friend void WakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 198 | friend void ForceWakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 199 | friend bool IsProcessSleeping(const TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 200 | friend bool IsProcessSuspended(const TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 201 | |
cicklaus | 0:afb2650fb49a | 202 | public: |
cicklaus | 0:afb2650fb49a | 203 | #if SEPARATE_RETURN_STACK == 0 |
cicklaus | 0:afb2650fb49a | 204 | TBaseProcess( TStackItem* Stack, word stack_size, TPriority pr, void (*exec)() ); |
cicklaus | 0:afb2650fb49a | 205 | #else |
cicklaus | 0:afb2650fb49a | 206 | TBaseProcess( TStackItem* Stack, TStackItem* RStack, TPriority pr, void (*exec)() ); |
cicklaus | 0:afb2650fb49a | 207 | #endif |
cicklaus | 0:afb2650fb49a | 208 | |
cicklaus | 0:afb2650fb49a | 209 | static void Sleep(TTimeout timeout = 0); |
cicklaus | 0:afb2650fb49a | 210 | |
cicklaus | 0:afb2650fb49a | 211 | protected: |
cicklaus | 0:afb2650fb49a | 212 | TStackItem* StackPointer; |
cicklaus | 0:afb2650fb49a | 213 | word StackSize; |
cicklaus | 0:afb2650fb49a | 214 | TTimeout Timeout; |
cicklaus | 0:afb2650fb49a | 215 | TPriority Priority; |
cicklaus | 0:afb2650fb49a | 216 | |
cicklaus | 0:afb2650fb49a | 217 | }; |
cicklaus | 0:afb2650fb49a | 218 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 219 | |
cicklaus | 0:afb2650fb49a | 220 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 221 | // |
cicklaus | 0:afb2650fb49a | 222 | /// process |
cicklaus | 0:afb2650fb49a | 223 | /// |
cicklaus | 0:afb2650fb49a | 224 | /// Implements template for application processes instantiation |
cicklaus | 0:afb2650fb49a | 225 | // |
cicklaus | 0:afb2650fb49a | 226 | // DESCRIPTION: |
cicklaus | 0:afb2650fb49a | 227 | // |
cicklaus | 0:afb2650fb49a | 228 | // |
cicklaus | 0:afb2650fb49a | 229 | #if SEPARATE_RETURN_STACK == 0 |
cicklaus | 0:afb2650fb49a | 230 | |
cicklaus | 0:afb2650fb49a | 231 | template<TPriority pr, word stack_size> |
cicklaus | 0:afb2650fb49a | 232 | class process : public TBaseProcess |
cicklaus | 0:afb2650fb49a | 233 | { |
cicklaus | 0:afb2650fb49a | 234 | public: |
cicklaus | 0:afb2650fb49a | 235 | INLINE_PROCESS_CTOR process(); |
cicklaus | 0:afb2650fb49a | 236 | #ifdef DEBUG_STACK |
cicklaus | 0:afb2650fb49a | 237 | word UsedStackSize(); |
cicklaus | 0:afb2650fb49a | 238 | #endif //DEBUG_STACK |
cicklaus | 0:afb2650fb49a | 239 | OS_PROCESS static void Exec(); |
cicklaus | 0:afb2650fb49a | 240 | |
cicklaus | 0:afb2650fb49a | 241 | private: |
cicklaus | 0:afb2650fb49a | 242 | TStackItem Stack[stack_size/sizeof(TStackItem)]; |
cicklaus | 0:afb2650fb49a | 243 | }; |
cicklaus | 0:afb2650fb49a | 244 | |
cicklaus | 0:afb2650fb49a | 245 | template<TPriority pr, word stack_size> |
cicklaus | 0:afb2650fb49a | 246 | OS::process<pr, stack_size>::process() : TBaseProcess( &Stack[stack_size/sizeof(TStackItem)] |
cicklaus | 0:afb2650fb49a | 247 | , stack_size // debug |
cicklaus | 0:afb2650fb49a | 248 | , pr |
cicklaus | 0:afb2650fb49a | 249 | , reinterpret_cast<void (*)()>(Exec) ) |
cicklaus | 0:afb2650fb49a | 250 | { |
cicklaus | 0:afb2650fb49a | 251 | } |
cicklaus | 0:afb2650fb49a | 252 | #ifdef DEBUG_STACK |
cicklaus | 0:afb2650fb49a | 253 | template<TPriority pr, word stack_size> |
cicklaus | 0:afb2650fb49a | 254 | word process<pr,stack_size>::UsedStackSize() |
cicklaus | 0:afb2650fb49a | 255 | { |
cicklaus | 0:afb2650fb49a | 256 | TStackItem* Idx = Stack; |
cicklaus | 0:afb2650fb49a | 257 | while(*(Idx++) == STACK_FILL_CONST); |
cicklaus | 0:afb2650fb49a | 258 | return ((Stack + (StackSize/sizeof(TStackItem))) - Idx)*sizeof(TStackItem); |
cicklaus | 0:afb2650fb49a | 259 | } |
cicklaus | 0:afb2650fb49a | 260 | #endif //DEBUG_STACK |
cicklaus | 0:afb2650fb49a | 261 | #else |
cicklaus | 0:afb2650fb49a | 262 | |
cicklaus | 0:afb2650fb49a | 263 | template<TPriority pr, word stack_size, word rstack_size> |
cicklaus | 0:afb2650fb49a | 264 | class process : public TBaseProcess |
cicklaus | 0:afb2650fb49a | 265 | { |
cicklaus | 0:afb2650fb49a | 266 | public: |
cicklaus | 0:afb2650fb49a | 267 | INLINE_PROCESS_CTOR process(); |
cicklaus | 0:afb2650fb49a | 268 | |
cicklaus | 0:afb2650fb49a | 269 | OS_PROCESS static void Exec(); |
cicklaus | 0:afb2650fb49a | 270 | |
cicklaus | 0:afb2650fb49a | 271 | private: |
cicklaus | 0:afb2650fb49a | 272 | TStackItem Stack [stack_size/sizeof(TStackItem)]; |
cicklaus | 0:afb2650fb49a | 273 | TStackItem RStack[rstack_size/sizeof(TStackItem)]; |
cicklaus | 0:afb2650fb49a | 274 | }; |
cicklaus | 0:afb2650fb49a | 275 | |
cicklaus | 0:afb2650fb49a | 276 | template<TPriority pr, word stack_size, word rstack_size> |
cicklaus | 0:afb2650fb49a | 277 | process<pr, stack_size, rstack_size>::process() : TBaseProcess( &Stack[stack_size/sizeof(TStackItem)] |
cicklaus | 0:afb2650fb49a | 278 | , &RStack[rstack_size/sizeof(TStackItem)] |
cicklaus | 0:afb2650fb49a | 279 | , pr |
cicklaus | 0:afb2650fb49a | 280 | , reinterpret_cast<void (*)()>(Exec)) |
cicklaus | 0:afb2650fb49a | 281 | { |
cicklaus | 0:afb2650fb49a | 282 | } |
cicklaus | 0:afb2650fb49a | 283 | |
cicklaus | 0:afb2650fb49a | 284 | #endif |
cicklaus | 0:afb2650fb49a | 285 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 286 | |
cicklaus | 0:afb2650fb49a | 287 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 288 | // |
cicklaus | 0:afb2650fb49a | 289 | // Miscellaneous |
cicklaus | 0:afb2650fb49a | 290 | // |
cicklaus | 0:afb2650fb49a | 291 | // |
cicklaus | 0:afb2650fb49a | 292 | INLINE inline void Run(); |
cicklaus | 0:afb2650fb49a | 293 | INLINE inline void LockSystemTimer() { TCritSect cs; LOCK_SYSTEM_TIMER(); } |
cicklaus | 0:afb2650fb49a | 294 | INLINE inline void UnlockSystemTimer() { TCritSect cs; UNLOCK_SYSTEM_TIMER(); } |
cicklaus | 0:afb2650fb49a | 295 | void WakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 296 | void ForceWakeUpProcess(TBaseProcess& p); |
cicklaus | 0:afb2650fb49a | 297 | INLINE inline void Sleep(TTimeout t = 0) { TBaseProcess::Sleep(t); } |
cicklaus | 0:afb2650fb49a | 298 | |
cicklaus | 0:afb2650fb49a | 299 | INLINE inline bool IsProcessSleeping(const TBaseProcess& p) |
cicklaus | 0:afb2650fb49a | 300 | { |
cicklaus | 0:afb2650fb49a | 301 | TCritSect cs; |
cicklaus | 0:afb2650fb49a | 302 | if(p.Timeout) |
cicklaus | 0:afb2650fb49a | 303 | return true; |
cicklaus | 0:afb2650fb49a | 304 | else |
cicklaus | 0:afb2650fb49a | 305 | return false; |
cicklaus | 0:afb2650fb49a | 306 | } |
cicklaus | 0:afb2650fb49a | 307 | |
cicklaus | 0:afb2650fb49a | 308 | INLINE inline bool IsProcessSuspended(const TBaseProcess& p) |
cicklaus | 0:afb2650fb49a | 309 | { |
cicklaus | 0:afb2650fb49a | 310 | TCritSect cs; |
cicklaus | 0:afb2650fb49a | 311 | if(Kernel.ReadyProcessMap & GetPrioTag(p.Priority)) |
cicklaus | 0:afb2650fb49a | 312 | return false; |
cicklaus | 0:afb2650fb49a | 313 | else |
cicklaus | 0:afb2650fb49a | 314 | return true; |
cicklaus | 0:afb2650fb49a | 315 | } |
cicklaus | 0:afb2650fb49a | 316 | //-------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 317 | |
cicklaus | 0:afb2650fb49a | 318 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 319 | INLINE inline dword GetTickCount() { TCritSect cs; return Kernel.SysTickCount; } |
cicklaus | 0:afb2650fb49a | 320 | #endif |
cicklaus | 0:afb2650fb49a | 321 | |
cicklaus | 0:afb2650fb49a | 322 | #if scmRTOS_SYSTIMER_HOOK_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 323 | INLINE_SYS_TIMER_HOOK void SystemTimerUserHook(); |
cicklaus | 0:afb2650fb49a | 324 | #endif |
cicklaus | 0:afb2650fb49a | 325 | |
cicklaus | 0:afb2650fb49a | 326 | #if scmRTOS_CONTEXT_SWITCH_USER_HOOK_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 327 | INLINE_CONTEXT_SWITCH_HOOK void ContextSwitchUserHook(); |
cicklaus | 0:afb2650fb49a | 328 | #endif |
cicklaus | 0:afb2650fb49a | 329 | |
cicklaus | 0:afb2650fb49a | 330 | } |
cicklaus | 0:afb2650fb49a | 331 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 332 | |
cicklaus | 0:afb2650fb49a | 333 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 334 | // |
cicklaus | 0:afb2650fb49a | 335 | /// Register Process |
cicklaus | 0:afb2650fb49a | 336 | /// |
cicklaus | 0:afb2650fb49a | 337 | /// Places pointer to process in kernel's process table |
cicklaus | 0:afb2650fb49a | 338 | // |
cicklaus | 0:afb2650fb49a | 339 | void OS::TKernel::RegisterProcess(OS::TBaseProcess* const p) |
cicklaus | 0:afb2650fb49a | 340 | { |
cicklaus | 0:afb2650fb49a | 341 | ProcessTable[p->Priority] = p; |
cicklaus | 0:afb2650fb49a | 342 | } |
cicklaus | 0:afb2650fb49a | 343 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 344 | // |
cicklaus | 0:afb2650fb49a | 345 | /// System Timer Implementation |
cicklaus | 0:afb2650fb49a | 346 | /// |
cicklaus | 0:afb2650fb49a | 347 | /// Performs process's timeouts checking and |
cicklaus | 0:afb2650fb49a | 348 | /// moving processes to ready-to-run state |
cicklaus | 0:afb2650fb49a | 349 | // |
cicklaus | 0:afb2650fb49a | 350 | void OS::TKernel::SystemTimer() |
cicklaus | 0:afb2650fb49a | 351 | { |
cicklaus | 0:afb2650fb49a | 352 | SYS_TIMER_CRIT_SECT(); |
cicklaus | 0:afb2650fb49a | 353 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 354 | SysTickCount++; |
cicklaus | 0:afb2650fb49a | 355 | #endif |
cicklaus | 0:afb2650fb49a | 356 | |
cicklaus | 0:afb2650fb49a | 357 | #if scmRTOS_PRIORITY_ORDER == 0 |
cicklaus | 0:afb2650fb49a | 358 | const byte BaseIndex = 0; |
cicklaus | 0:afb2650fb49a | 359 | #else |
cicklaus | 0:afb2650fb49a | 360 | const byte BaseIndex = 1; |
cicklaus | 0:afb2650fb49a | 361 | #endif |
cicklaus | 0:afb2650fb49a | 362 | |
cicklaus | 0:afb2650fb49a | 363 | for(byte i = BaseIndex; i < (scmRTOS_PROCESS_COUNT + BaseIndex); i++) |
cicklaus | 0:afb2650fb49a | 364 | { |
cicklaus | 0:afb2650fb49a | 365 | TBaseProcess* p = ProcessTable[i]; |
cicklaus | 0:afb2650fb49a | 366 | |
cicklaus | 0:afb2650fb49a | 367 | if(p->Timeout > 0) |
cicklaus | 0:afb2650fb49a | 368 | { |
cicklaus | 0:afb2650fb49a | 369 | if(--p->Timeout == 0) |
cicklaus | 0:afb2650fb49a | 370 | { |
cicklaus | 0:afb2650fb49a | 371 | SetProcessReady(p->Priority); |
cicklaus | 0:afb2650fb49a | 372 | } |
cicklaus | 0:afb2650fb49a | 373 | } |
cicklaus | 0:afb2650fb49a | 374 | } |
cicklaus | 0:afb2650fb49a | 375 | } |
cicklaus | 0:afb2650fb49a | 376 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 377 | // |
cicklaus | 0:afb2650fb49a | 378 | /// ISR optimized scheduler |
cicklaus | 0:afb2650fb49a | 379 | /// |
cicklaus | 0:afb2650fb49a | 380 | /// !!! IMPORTANT: This function must be call from ISR services only !!! |
cicklaus | 0:afb2650fb49a | 381 | // |
cicklaus | 0:afb2650fb49a | 382 | // |
cicklaus | 0:afb2650fb49a | 383 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0 |
cicklaus | 0:afb2650fb49a | 384 | void OS::TKernel::SchedISR() |
cicklaus | 0:afb2650fb49a | 385 | { |
cicklaus | 0:afb2650fb49a | 386 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
cicklaus | 0:afb2650fb49a | 387 | if(NextPrty != CurProcPriority) |
cicklaus | 0:afb2650fb49a | 388 | { |
cicklaus | 0:afb2650fb49a | 389 | TStackItem* Next_SP = ProcessTable[NextPrty]->StackPointer; |
cicklaus | 0:afb2650fb49a | 390 | TStackItem** Curr_SP_addr = &(ProcessTable[CurProcPriority]->StackPointer); |
cicklaus | 0:afb2650fb49a | 391 | CurProcPriority = NextPrty; |
cicklaus | 0:afb2650fb49a | 392 | OS_ContextSwitcher(Curr_SP_addr, Next_SP); |
cicklaus | 0:afb2650fb49a | 393 | } |
cicklaus | 0:afb2650fb49a | 394 | } |
cicklaus | 0:afb2650fb49a | 395 | #else |
cicklaus | 0:afb2650fb49a | 396 | void OS::TKernel::SchedISR() |
cicklaus | 0:afb2650fb49a | 397 | { |
cicklaus | 0:afb2650fb49a | 398 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
cicklaus | 0:afb2650fb49a | 399 | if(NextPrty != CurProcPriority) |
cicklaus | 0:afb2650fb49a | 400 | { |
cicklaus | 0:afb2650fb49a | 401 | SchedProcPriority = NextPrty; |
cicklaus | 0:afb2650fb49a | 402 | RaiseContextSwitch(); |
cicklaus | 0:afb2650fb49a | 403 | } |
cicklaus | 0:afb2650fb49a | 404 | } |
cicklaus | 0:afb2650fb49a | 405 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 406 | bool OS::TKernel::IsContextSwitchDone() const volatile |
cicklaus | 0:afb2650fb49a | 407 | { |
cicklaus | 0:afb2650fb49a | 408 | byte cur = CurProcPriority; ///< reading to temporary vars is performed due to |
cicklaus | 0:afb2650fb49a | 409 | byte sched = SchedProcPriority; ///< suppress warning about order of volatile access |
cicklaus | 0:afb2650fb49a | 410 | return cur == sched; |
cicklaus | 0:afb2650fb49a | 411 | } |
cicklaus | 0:afb2650fb49a | 412 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 413 | TStackItem* OS::TKernel::ContextSwitchHook(TStackItem* sp) |
cicklaus | 0:afb2650fb49a | 414 | { |
cicklaus | 0:afb2650fb49a | 415 | ProcessTable[CurProcPriority]->StackPointer = sp; |
cicklaus | 0:afb2650fb49a | 416 | sp = ProcessTable[SchedProcPriority]->StackPointer; |
cicklaus | 0:afb2650fb49a | 417 | |
cicklaus | 0:afb2650fb49a | 418 | #if scmRTOS_CONTEXT_SWITCH_USER_HOOK_ENABLE == 1 |
cicklaus | 0:afb2650fb49a | 419 | ContextSwitchUserHook(); |
cicklaus | 0:afb2650fb49a | 420 | #endif |
cicklaus | 0:afb2650fb49a | 421 | |
cicklaus | 0:afb2650fb49a | 422 | CurProcPriority = SchedProcPriority; |
cicklaus | 0:afb2650fb49a | 423 | return sp; |
cicklaus | 0:afb2650fb49a | 424 | } |
cicklaus | 0:afb2650fb49a | 425 | //------------------------------------------------------------------------------ |
cicklaus | 0:afb2650fb49a | 426 | #endif // scmRTOS_CONTEXT_SWITCH_SCHEME |
cicklaus | 0:afb2650fb49a | 427 | |
cicklaus | 0:afb2650fb49a | 428 | //----------------------------------------------------------------------------- |
cicklaus | 0:afb2650fb49a | 429 | /// Start Operation |
cicklaus | 0:afb2650fb49a | 430 | INLINE inline void OS::Run() |
cicklaus | 0:afb2650fb49a | 431 | { |
cicklaus | 0:afb2650fb49a | 432 | TStackItem* sp = Kernel.ProcessTable[pr0]->StackPointer; |
cicklaus | 0:afb2650fb49a | 433 | OS_Start(sp); |
cicklaus | 0:afb2650fb49a | 434 | } |
cicklaus | 0:afb2650fb49a | 435 | |
cicklaus | 0:afb2650fb49a | 436 | #include <OS_Services.h> |
cicklaus | 0:afb2650fb49a | 437 | |
cicklaus | 0:afb2650fb49a | 438 | #endif // OS_KERNEL_H |
cicklaus | 0:afb2650fb49a | 439 |