Quick and dirty port of scmRTOS demo to mbed 1768. scmRTOS is a small RTOS written using C++. Offers (static) processes, critical sections, mutexes, messages, channels.

Dependencies:   mbed

Committer:
igorsk
Date:
Thu Sep 09 21:19:01 2010 +0000
Revision:
0:a405220cf420

        

Who changed what in which revision?

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