Dependencies:   keypad SDHCFileSystem TextLCD mbed FPointer wave_player

Committer:
daryl2110
Date:
Mon Feb 20 07:32:56 2012 +0000
Revision:
0:57ece500234e

        

Who changed what in which revision?

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