Dependencies:   EthernetNetIf NetServices mbed HTTPServer

Committer:
etudiant12
Date:
Mon May 23 05:40:11 2011 +0000
Revision:
0:96cf274f19bc

        

Who changed what in which revision?

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