This file is part of my co-routines framework for small microcontrollers

by Isaac Marino Bavaresco

This is file "TestTasks.c"

/*============================================================================*/
/*
 Copyright (c) 2014, 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.
*/
/*============================================================================*/
#include "C-R.h"
/*============================================================================*/

/*
Must be used exactly once in the application code. The argument is the
maximum number of tasks that can exist at the same time
*/
DECLARETASKS( 5 );

taskreturn_t Task2( void );
taskreturn_t Task3( void );
taskreturn_t Task4( void );
taskreturn_t Task5( void );

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

volatile int a = 0, b = 0;

/*
The co-routine engine creates just task "Task1" and executes it.
"Task1" must initialize the system and create other tasks.
*/

taskreturn_t Task1( void )
    {
    /* All local variables must be declared as static */
    static int i;

    /*
    No code should go before the "TaskStart" statement.

    Any code put here will run every time the scheduler calls
    the task.
    */

    TaskStart();

    /*
    Code after the "TaskStart" statement but before the "while(1)"
    statement will be executed only once when the task is first run
    after its creation.

    This is a good place to initialize variables, peripherals, etc.
    */


    /*
    "Task1" must create the other tasks that must run from the start.
    Other tasks can be created later if needed.
    */

    CreateTask( Task2 );
    CreateTask( Task3 );
    CreateTask( Task4 );
    CreateTask( Task5 );

    while( 1 )
        {
        /* Do something... */
        Yield( 1 );
        for( i = 0; i < 10; i++ )
            {
            /* Do another something... */
            a++;
            Yield( 2 );
            }
        /* And so on... */
        Sleep( 3, 5 );
        }

    TaskEnd();

    /* Will never reach here */
    return -1;
    }

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

taskreturn_t Task2( void )
    {
    /* Declare all variables as static */
    static int  j;

    TaskStart();

    while( 1 )
        {
        /* Do something... */
        Yield( 1 );
        for( j = 0; j < 10; j++ )
            {
            /* Do another something... */
            b++;
            Yield( 2 );
            }
        /* And so on... */
        Sleep( 3, 5 );
        }

    TaskEnd();

    /* Will never reach here */
    return -1;
    }

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

taskreturn_t Task3( void )
    {
    /* Declare all local variables as static */

    TaskStart();

    while( 1 )
        {
        Sleep( 1, 1 );
        }

    TaskEnd();

    /* Will never reach here */
    return -1;
    }

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

taskreturn_t Task4( void )
    {
    /* Declare all local variables as static */

    TaskStart();

    while( 1 )
        {
        Sleep( 1, 4 );
        }

    TaskEnd();

    /* Will never reach here */
    return -1;
    }

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

taskreturn_t Task5( void )
    {
    /* Declare all local variables as static */

    TaskStart();

    while( 1 )
        {
        Sleep( 1, 3 );
        }

    TaskEnd();

    /* Will never reach here */
    return -1;
    }

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

/*----------------------------------------------------------------------------*/
#if         defined HI_TECH_C   /* HI-TECH PICC */
/*----------------------------------------------------------------------------*/

    void InitializeTimer( void )
        {
        /* Initialize things... */
        TMR1    = 0;
        TMR1IF  = 0;
        TMR1IE  = 1;
        PEIE    = 1;
        GIE     = 1;
        TMR1ON  = 1;
        }
    
    /*----------------------------------------------------------------------------*/
    
    void interrupt ISR( void )
        {
        if( TMR1IE && TMR1IF )
            {
            TMR1IF  = 0;
            INCREMENTTICK();
            }
    
        /* Service other interrupts... */
    
        }

/*----------------------------------------------------------------------------*/
#elif       defined __18CXX     /* MPLAB-C18 or XC8 */
/*----------------------------------------------------------------------------*/

    void InitializeTimer( void )
        {
        /* Initialize things... */
        TMR1L               = 0;
        TMR1H               = 0;
        PIR1bits.TMR1IF     = 0;
        PIE1bits.TMR1IE     = 1;
        INTCON1bits.PEIE    = 1;
        INTCON1bits.GIE     = 1;
        T1CONbits.TMR1ON    = 1;
        }
    
    /*------------------------------------------------------------------------*/
    
    #pragma interruptlow low_isr
    
    void low_isr( void )
        {
        if( PIE1bits.TMR1IE && PIR1bits.TMR1IF )
            {
            PIR1bits.TMR1IF = 0;
            INCREMENTTICK();
            }
    
        /* Service other interrupts... */
    
        }
    
    #pragma code low_vector=0x18
    
    void interrupt_at_low_vector( void )
        {
        _asm GOTO low_isr _endasm
        }
    
    #pragma code /* return to the default code section */

/*----------------------------------------------------------------------------*/
#elif       defined __XC16__
/*----------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------*/
    #if       defined __PIC24E__ || defined __dsPIC33E__
    /*------------------------------------------------------------------------*/
    
        #error Not working due to a suspected bug in XC16 compiler.
    
    /*------------------------------------------------------------------------*/
    #else   /*  defined __PIC24E__ || defined __dsPIC33E__ */
    /*------------------------------------------------------------------------*/
    
        void InitializeTimer( void )
            {
            /* Initialize things... */
            TMR1                = 0;
            IFS0bits.T1IF       = 0;
            IEC0bits.T1IE       = 1;
            T1CONbits.TON       = 1;
            }
        
        /*--------------------------------------------------------------------*/
        
        void __attribute__((interrupt,auto_psv)) _T1Interrupt( void )
            {
            IFS0bits.T1IF       = 0;
            INCREMENTTICK();
            }

    /*------------------------------------------------------------------------*/
    #endif  /*  defined __PIC24E__ || defined __dsPIC33E__ */
    /*------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
#elif       defined __PIC32MX__
/*----------------------------------------------------------------------------*/

    #include <plib.h>
    void InitializeTimer( void )
        {
        INTSetVectorPriority( INT_TIMER_1_VECTOR, 1 );
        INTSetVectorSubPriority( INT_TIMER_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0 );
        INTClearFlag( INT_T1 );
        INTEnable( INT_T1, INT_ENABLED );
    
        /* configure for multi-vectored mode*/
        INTConfigureSystem( INT_SYSTEM_CONFIG_MULT_VECTOR );
    
        OpenTimer1( T1_ON, 40000 );              /* load with the period */
    
        /* enable interrupts*/
        INTEnableInterrupts();
        }

    /*------------------------------------------------------------------------*/

    void __attribute__(( vector( _TIMER_1_VECTOR ), interrupt( IPL1SOFT ), nomips16 ))T1Interrupt(void)
        {
        INTClearFlag( INT_T1 );
        INCREMENTTICK();
        }

/*----------------------------------------------------------------------------*/
#endif  /*  defined HI_TECH_C */
/*----------------------------------------------------------------------------*/

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