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