please dont rip this site

Simple RTOS for Microchip Baseline and Midrange MCUs

by Isaac Marino Bavaresco

This is file "SimpleRTOS.h"
//==============================================================================
// SimpleRTOS - Very simple RTOS for Microchip(R) Baseline and Midrange uCs
// v1.00 (2008-09-23)
// isaacbavaresco@yahoo.com.br
//==============================================================================
/*
 Copyright (c) 2007-2008, Isaac Marino Bavaresco
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Neither the name of the author nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//==============================================================================
/*
    Notes:  - Works with Hi-Tech PICC v9.60PL2;

            - Assembler optimizations must be OFF for every file that implements
              task functions (it is better to have the task functions in one or
              more files with no standard functions, to minimize the un-optimized
              code length).

            - Every task function must have a call to the macro
              "TASK_INIT( <task function name> );" as the first statement;

            - Every function called directly or indirectly by more than one task
              must have a call to the macro "FUNC_INIT( <function name> );" as the
              first statement, unless it doesn't have any parameters nor automatic
              variables, or all paths through the call-graphs that it belongs have
              at least one function that calls "FUNC_INIT" before the function is
              called.

              Unfortunately this impose certain restrictions when using library
              functions, unless each library function is called from just one
              task, or it is present only in call-graphs that at least one
              function calls "FUNC_INIT" before the library function is called,
              or you rebuild the library adding the call to "FUNC_INIT" to the
              library functions that need it.

            - SimpleRTOS functions and macros that may cause a context switch
              (Eg: Yield, Sleep, DeleteTask (when deleting itself) and
              MutexTake ) must be called only directly inside the task function,
              never indirectly by other functions.


    TO DO:  - Find a way to reuse local variables of tasks that are never active
              at the same time.

            - Implement semaphores.

            - Implement priorities and slice length.

*/
//==============================================================================
#if         !defined __SimpleRTOS_H__
#define __SimpleRTOS_H__
//==============================================================================
#include <pic.h>
//==============================================================================
#if         !defined NULL
#define NULL 0
#endif  //  !defined NULL
//==============================================================================
#define USE_BLOCKINGS
//==============================================================================
#define Yield()         {                                               \
                        SavePCLATH  = PCLATH;                           \
                        asm("global _SavePCH        " );                \
                        asm("global _SavePCL        " );                \
                        asm("global _Scheduler      " );                \
                        asm("movlw  high ($+7)      " );                \
                        asm("movwf  _SavePCH        " );                \
                        asm("movlw  low ($+5)       " );                \
                        asm("movwf  _SavePCL        " );                \
                        asm("movlw  high _Scheduler " );                \
                        asm("movwf  10              " );                \
                        asm("goto   _Scheduler      " );                \
                        PCLATH      = SavePCLATH;                       \
                        }
//==============================================================================
#define DeleteTask(t)   {                                               \
                        if( _DeleteTask((t)) )                          \
                            {                                           \
                            asm( "global    _AbortTask      " );        \
                            asm( "movlw     high _AbortTask " );        \
                            asm( "movwf     10              " );        \
                            asm( "goto      _AbortTask      " );        \
                            }                                           \
                        }
//==============================================================================
// Put the current task to sleep for 't' ticks. If 't' is greater than 127 then
// the task is suspended (sleeping forever).

#define Sleep(t)        {                                               \
                        _Sleep( t );                                    \
                        Yield();                                        \
                        }
//==============================================================================
#define TASK_INIT(f)    asm( "FNROOT _" #f )
//==============================================================================
#define FUNC_INIT(f)    asm( "FNCALL intlevel1,_" #f );                 \
                        #pragma interrupt_level 1
//==============================================================================
#define StartRTOS()     {                                               \
                        asm( "global    __StartRTOS     " );            \
                        asm( "movlw     high __StartRTOS" );            \
                        asm( "movwf     10              " );            \
                        asm( "goto      __StartRTOS     " );            \
                        }
//==============================================================================
#define MutexTake(m,t)  {                                               \
                        if( !_MutexTake( (m), (t) ))                    \
                        Yield();                                        \
                        }
//==============================================================================
#define OwnTheMutex(m)  ((m)->Owner == CurrentTask)
//==============================================================================
typedef bank2 struct
    {
    /// Saves the high byte of the program counter
    unsigned char   PCH;
    /// Saves the low byte of the program counter
    unsigned char   PCL;
    unsigned char   PCLATH;
    unsigned char   FSR;
    unsigned char   TDelay;

    #ifdef      USE_BLOCKINGS
        void bank2  *PreviousDelayedTask;
        void bank2  *NextDelayedTask;
        void bank2  *DelayList;
    #else   //  USE_BLOCKINGS
        #define     PreviousDelayedTask Previous
        #define     NextDelayedTask     Next
        #define     DelayList           List
    #endif  //  USE_BLOCKINGS

    #ifdef      USE_SLICE_LENGTH
        unsigned char   SliceLength;
    #endif  //  USE_SLICE_LENGTH

    #ifdef      USE_PRIORITIES
        unsigned char   Priority;
    #endif  //  USE_PRIORITIES
    void bank2      *Previous;
    void bank2      *Next;
    void bank2      *List;
    } TS_Context;
//==============================================================================
typedef bank2 struct
    {
    TS_Context  *Owner;
    TS_Context  *WaitingList;
    } TS_Mutex;
//==============================================================================
extern unsigned char    Ticks;

extern unsigned char    SavePCH;
extern unsigned char    SavePCL;
extern unsigned char    SaveFSR;
extern unsigned char    SavePCLATH;

extern TS_Context       *CurrentTask;
//==============================================================================
void        InitRTOS    ( TS_Context *Contexts, unsigned char Number );
void        _StartRTOS  ( void );
void        Scheduler   ( void );
void        _Sleep      ( unsigned char t );
TS_Context  *CreateTask ( void(*TaskFunc)(void) );
bit         _DeleteTask ( TS_Context *Task );
bit         ResumeTask  ( TS_Context *Task );
bit         _MutexTake  ( TS_Mutex *Mutex, unsigned char t );
bit         MutexGive   ( TS_Mutex *Mutex );
//==============================================================================
#endif  //  !defined __SimpleRTOS_H__
//==============================================================================

file: /Techref/member/IMB-yahoo-J86/SimpleRTOS.h.htm, 10KB, , updated: 2015/4/27 16:08, local time: 2024/11/17 19:24, owner: IMB-yahoo-J86,
TOP NEW HELP FIND: 
18.188.227.37:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.ecomorder.com/techref/member/IMB-yahoo-J86/SimpleRTOS.h.htm"> Simple RTOS for Microchip Baseline and Midrange MCUs - SimpleRTOS.h</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?