skelmain.c



/*
 * skelmain.c
 *
 * ------------------------------------------------------------
 * A Kla2 Module
 * Copyright (c) 2003, David Clifton
 * All Rights Reserved
 * http://www.codelode.com
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice, website reference, this permission
 * notice, and the disclaimer of warranty below shall be included
 * in all copies, derivatives, or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * -------------------------------------------------------------
 *
 *
 *   This object contains a skeleton main program for
 * use with the kla2 kernel.  It contains the code necessary
 * to power up the system, and to get an idle pfcn going,
 * except that it doesn't show any processor dependent
 * method calls.
 *
 * NOTES:
 *
 * The idle pfcn is used to sequence the start-up of the system,
 * to perform low priority processing, and to prevent control from
 * returning to the main() function which would then issue
 * an Err_shutdown().
 *
 *
 */
 #include "types.h"
 #include "err.h"
 #include "intrpt.h"
 #include "pfcn.h"
 #include "time.h"
 /*
  * idle pfcn state definitions
  */
typedef enum { START,INIT1,INIT2,INIT3,INIT4,LOOP } ISTATE;

/* --------------------------------------------------------------
 * skelmain data elements
 * NOTE: a pfcn is a prioritized function, that is a function
 * which has been allocated by  the pfcn object with a particular
 * priority relative to other pfcns.
 * -------------------------------------------------------------- */
s16 idlePfcn=NIL;        // idle pfcn index
ISTATE idleState;         // state of idle pfcn processing
s32 idleCount=0;          // times through main loop since rotate

/* ----------------------------------------------------------------
 * private methods of skelmain module
 * ---------------------------------------------------------------- */

/*
 * idle
 *      This is the idle pfcn (prioritized function) .  It re-posts itself each
 * time it completes.
 */
void idle(void)
{
    s16 i,j;
    
    switch(idleState) {
    case START:   // use these states to perform system initialization
        idleState=INIT1;
        break;

    case INIT1:  // use these states to perform system initialization      
        idleState=INIT2;
        break;

    case INIT2: // use these states to perform system initialization      
        idleState=INIT3;
        break;

    case INIT3: // use these states to perform system initialization
        idleState=INIT4;
        break;
        
    case INIT4: // use these states to perform system initialization
        idleState=LOOP;
        break;

    case LOOP:
       idleCount++;
       

       i=0x80;
       while(i>0) {
           for(j=i;j>0;j--);
           i--;
       }
       Debug_putc(0x30 + idleCount%10);  // just put digits into debug buffer
       break;

    default:
        Err_shutdown(IDLE_STATE_ERROR);
    }
  /*
   * idle pfcn reposts itself to make sure it gets
   * executed again when there is nothing of a higher
   * priority going on.
   */
    Pfcn_post(idlePfcn);
}
 /*
  * main
  *    Initialize the rtos, the timed data pipes, the timed
  * pfcn (prioritized function) which feeds the timed data pipes,
  * and start the idle pfcn.
  */
 int main(void)
 {
     Intrpt_init();            // make sure interrupts disabled
     
     Time_setup();            // set up clock and periodic interrupt

     Debug_init();             // init debug buffer

     Err_init();             // initialize error processing

     Pfcn_init();            // initialize prioritized fcn manager

     Pipe_init();            // initialize the pipe manager
     
     Time_init();         // start the periodic timer

     /*
      * set up the idle pfcn (prioritized function)
      */

     idlePfcn=Pfcn_new(PRIORITY_0,idle); // create idle pfcn

     idleState=START;

     Intrpt_enable();     // enable interrupts
          
     Pfcn_post(idlePfcn);   // start idle pfcn
     Err_shutdown(SHOULDNT_GET_HERE); // this should never happen

 }