Simple RTOS for Microchip Baseline and Midrange MCUs

by Isaac Marino Bavaresco

//==============================================================================
// 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.

*/
//==============================================================================

//==============================================================================
// This module MUST HAVE ASSEMBLER OPTIMIZATIONS OFF, because it uses certain
// macros defined in "SimpleRTOS.h".
//==============================================================================


#include <string.h>
#include "SimpleRTOS.h"
//==============================================================================
short   SerialReceive( void );
bit     SerialTransmit( unsigned char c );
bit     TransmitInProgress( void );
//==============================================================================
TS_Mutex    WorthlessMutex = { NULL, NULL };
//==============================================================================
// We cannot add 'FUNC_INIT' to strlen, unless we change and rebuild the library
// (not a bad idea at all), so we 'wrap' it into this function.

size_t strlen_wrapper( const char *s )
    {
    // This produces a silly pre-processor warning, but we need it.
    FUNC_INIT( strlen_wrapper );

    return strlen( s );
    }
//==============================================================================
void MutexTask1( void )
    {
    const unsigned char Message[] = "Hello world!\r\n";
    const unsigned char *p;
    unsigned char       i;

    // Needed by all task functions.
    TASK_INIT( MutexTask1 );

    while( 1 )
        {
        // We will wait undefinitely until we get the mutex.
        MutexTake( &WorthlessMutex, -1 );

        // Usually we would get here only if we got the mutex, testing
        // just to be sure.
        if( OwnTheMutex( &WorthlessMutex ))
            {
            // Transmit the whole string.
            for( i = strlen_wrapper( Message ), p = Message; i; i--, p++ )
                // Transmit one char. While there is no room for new chars...
                while( SerialTransmit( *p ) == 0 )
                    // ... sleep.
                    Sleep( 1 );

            // Wait for the last byte to be transmitted.
            while( TransmitInProgress() )
                Sleep( 1 );

            // Let's fustigate the other task a little.
            Sleep( 31 );

            // Do not forget to give the mutex back.
            MutexGive( &WorthlessMutex );
            }
        }
    }
//==============================================================================
void MutexTask2( void )
    {
    const unsigned char Message[] = "I took over the world!\r\n";
    const unsigned char *p;
    unsigned char       i;

    // Needed by all task functions.
    TASK_INIT( MutexTask2 );

    while( 1 )
        {
        // Try to take the mutex within a narrow time window
        MutexTake( &WorthlessMutex, 2 );
        // As the other task takes much more time, this one will get
        // the mutex less often.
        if( OwnTheMutex( &WorthlessMutex ))
            {
            // Transmit the whole string.
            for( i = strlen_wrapper( Message ), p = Message; i; i--, p++ )
                // While there is no room for new chars...
                while( SerialTransmit( *p ) == 0 )
                    // ... sleep.
                    Sleep( 1 );

            // Do not forget to give the mutex back.
            MutexGive( &WorthlessMutex );
            }
        else
            Sleep( 5 );
        }
    }
//==============================================================================
// Sort of a "poor man's" SIGNAL.
bit Die     = 0;

bit Alive   = 0;
//==============================================================================
void SuicideTask( void )
    {
    unsigned char i;

    // Needed by all task functions.
    TASK_INIT( SuicideTask );

    // Make sure the loop will repeat at least once.
    Die = 0;

    while( 1 )
        {
        // Somebody told us to die...
        if( Die )
            {
            // Clean-up before exitting.
            RB7     = 0;
            Alive   = 0;
            // Die piggy, die.
            DeleteTask( CurrentTask );
            }

        // Just a silly thing to do.
        // What a lack of imagination!
        RB7 = ! RB7;

        // It would be better if Ticks were 'unsigned short' or 'unsigned long'.
//      for( i = 10; i; i-- )
            Sleep( 2 );
        }
    }
//==============================================================================
void VictimTask( void )
    {
    // Needed by all task functions.
    TASK_INIT( VictimTask );

    while( 1 )
        {
        // Another silly thing.
        RB6     = ! RB6;
        Sleep( 3 );
        }
    }
//==============================================================================
void TaskRX( void )
    {
    TS_Context *Victim = NULL;
    short       i;

    // Needed by all task functions.
    TASK_INIT( TaskRX );

    while( 1 )
        {
        // Wait for a character.
        while(( i = SerialReceive() ) < 0 )
            Sleep( 1 );

        switch( (unsigned char)i )
            {
            // Start the suicide task.
            case 's':
                // We should not create an already created task.
                if( ! Alive )
                    {
                    // The task was created succesfully...
                    if( CreateTask( SuicideTask ))
                        // ... flag it.
                        Alive   = 1;
                    }
                break;

            // Tell the suicide task to die.
            case 'd':
                Die     = 1;
                break;

            // Create the victim task.
            case 'v':
                // If the task is not created...
                if( Victim == NULL )
                    // ... create it.
                    Victim = CreateTask( VictimTask );
                break;

            // Kill the victim task.
            case 'k':
                // If the task is alive...
                if( Victim != NULL )
                    {
                    // ... kill it...
                    DeleteTask( Victim );
                    // ... and flag it.
                    Victim  = NULL;
                    }
                break;
            }
        }
    }
//==============================================================================
void MainTask( void )
    {
    unsigned long i = 0;

    // Needed by all task functions.
    TASK_INIT( MainTask );

    // Create the other tasks...
    CreateTask( TaskRX );
    CreateTask( MutexTask1 );
    CreateTask( MutexTask2 );

    while( 1 )
        {
        // Somebody have to keep Cerberus happy.
        CLRWDT();

        // Some very computationally intensive calculations.
        i++;

        // Output the results.
        PORTA   = (unsigned char)i;

        // Give a chance for the other tasks, but return ASAP.
        Yield();
        }
    }
//==============================================================================