Committer:
segundo
Date:
Thu Nov 11 10:30:39 2010 +0000
Revision:
7:6fab7e5aa489
Parent:
6:74e9f8b6e352

        

Who changed what in which revision?

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