LED Fork

Dependencies:   mbed

Fork of RtosTest by Daniel Peter

Committer:
mbed714
Date:
Tue Sep 21 19:48:05 2010 +0000
Revision:
0:331db0b44b67

        

Who changed what in which revision?

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