/*
 * File: rappid_osek_main.c
 *
 * Real-Time Workshop code generated for Simulink model Test_1.
 *
 * Model version                        : 1.29
 * Real-Time Workshop file version      : 6.3  (R14SP3)  26-Jul-2005
 * Real-Time Workshop file generated on : Thu Sep 25 20:48:24 2008
 * TLC version                          : 6.3 (Aug  5 2005)
 * C source code generated on           : Thu Sep 25 20:48:24 2008
 */

 /* Edited by: jwschmo, March 14 2009

/* Model's headers */
#include "Test_1.h"
#include "OSEKIncludes.h"
#include "prefix.h"
#include "osprop.h"
#include "cfg.h"
#include "osapi.h"
#include "RAppID_Blockset.h"
#include "mpc5554.h"

/*    extern const vuint32_t IntcIsrVectorTable[];
   extern IVOR4Handler();
 */
extern __rom OSDWORD OsIsr[];

TASK(init) {
  /* Initialize model */

  Test_1_initialize(1);

  /* Base rate will run every           : 1.0 seconds
     Original rate specified in model   : 1.0 seconds */
  SetRelAlarm(baseAlarm, 3, 3);  /* This has highest priority */

  /* Sub rate will run every            : 2.0 seconds,
     Original rate specified in model   : 1.0 seconds */
  SetRelAlarm(subAlarm_1, 2, 2);

  TerminateTask();
}

/* Using RTW multitasking execution of model */
TASK(baseRate)
{
	int i = 0;
  /* Set model inputs associated with base rate here */

  /* Test_1_step(0);*/
  /* John's Code This is user edited */
  {
  	SIU.GPDO[36].R = 1;   													/* Turn on LED[36] */
  	SIU.GPDO[29].R = ((!(0x1 & SIU.GPDO[29].R)) & 0x1);  					/* This oscillates the LED[29] XOR */

		while(SIU.GPDI[125].R ==1)
		{
			i++;
				if(i%200000 == 0)  											/* This defines the rate the LED oscillates */
				{
					SIU.GPDO[30].R = ((!(0x1 & SIU.GPDO[30].R)) & 0x1);  	/* This oscillates the LED[30] XOR */
				}
		}  																	/* waits until dip switch 125 is down */
	SIU.GPDO[30].R = 0;  /* turns off LED */

  	GetResource( RateTransition );   										/* Get Resource */
  		if(SIU.GPDI[126].R == 1)     										/* If DS[126] == 1*/
  		{
  			SIU.GPDO[43].R = 0;												/* Turn off LED[43] */
		}

	i = 0;

		for(i = 0;i<2000000;i++){};

  	SIU.GPDO[28].R = ((!(0x1 & SIU.GPDO[28].R)) & 0x1); 					/* This oscillates the LED[28] XOR */


  	ReleaseResource( RateTransition );  									/* Release Resource */
	SIU.GPDO[36].R = 0;

  }

  /* Get model outputs associated with base rate here */
  TerminateTask();
}

TASK(subRate_1)
{
	int i=0;
	SIU.GPDO[35].R = 1;														/* Turn on LED[35] */
  /* Set model inputs associated with sub rate here */

  /* Test_1_step(1);*/

  /* John's Code This is user edited */
    {

	while(SIU.GPDI[124].R == 1)   											/* loops forever if DS[124] ==1 */
	{
		i++;
		if(i%200000 == 0)  													/* This defines the rate the LED oscillates */
		{
			SIU.GPDO[40].R = ((!(0x1 & SIU.GPDO[40].R)) & 0x1);  			/* This oscillates the LED[40] XOR */
		}
	}  /* waits until dip switch 124 is down */
	SIU.GPDO[40].R = 0;  /* turns off LED[40] */

    GetResource( RateTransition );
	for(i = 0;i<2000000;i++){};
	i = 0;

  	SIU.GPDO[43].R = 1;  /* Turns on LED[43]*/
  	SIU.GPDO[42].R = ((!(0x1 & SIU.GPDO[42].R)) & 0x1);   					/* This oscillates the LED[42] XOR */
  	/*SIU.GPDO[28].R = 0;*/
  	while(SIU.GPDI[123].R == 1)  											/* loops forever if DS[123] ==1 */
  	{
		i++;
		if(i%200000 == 0)  													/* This defines the rate the LED oscillates */
		{
			SIU.GPDO[41].R = ((!(0x1 & SIU.GPDO[41].R)) & 0x1);  			/* This oscillates the LED[41] XOR */
		}
	}  /* waits until dip switch 123 is down*/
	SIU.GPDO[41].R = 0;  /* turns off LED */

  	ReleaseResource( RateTransition );
  }
  SIU.GPDO[35].R = 0;
  /* Get model outputs associated with sub rate here */
  TerminateTask();
}

/* End of User Edited CODE */

void initINTC(void)
{
  INTC.MCR.B.HVEN = 0;                  /* Initialize INTC for software vector mode */
  INTC.MCR.B.VTES = 0;                  /* Use default vector table entry offsets of 4 B */
  /*INTC.IACKR.R = (uint32_t) &IntcIsrVectorTable[0];*/
  /* Set INTC ISR vector table base addr */
  INTC.IACKR.R = (uint32_t) &OsIsr[0];
}

void enableIrq(void)
{
  asm(" wrteei 1");                     /* Enable external interrupts */
}

void main(void)
{
  initINTC();                           /* Initialize INTC for software vector mode */
  INTC.CPR.B.PRI = 0;                   /* Ensure INTC's current priority is 0 */

  StartOS(mode0);
}

/* File trailer for Real-Time Workshop generated code.
 *
 * [EOF]
 */
