/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Cpu.C
**     Project   : DC18
**     Processor : MC56F8006_32_LQFP
**     Component : 56F8006_32_LQFP
**     Version   : Component 01.017, Driver 02.08, CPU db: 3.00.188
**     Datasheet : MC56F8006RM Rev. 0 03/2009, MC56F8006 Rev. 2 03/2009
**     Compiler  : Metrowerks DSP C Compiler
**     Date/Time : 6/1/2010, 5:06 PM
**     Abstract  :
**
**     Comment   :
**         If you are hacking the DC18 firmware, you will need to do the following things:
**         
**         1) After re-compiling, open Vector.c (in the Generated Code project folder) and change first two lines of _vect table from _EntryPoint to 
**         BOOTLOADER_ADDR. 
**         This ensures that the bootloader will re-launch at power-up, else you are stuck with your user code unless you re-flash the whole device. 
**         
**         2) If the linker file (file ending in .cmd) changes, patch the following areas:
**         
**         a) Paste this code block to the end of the SECTIONS area
**         
**         .ApplicationConfiguration :
**         {
**         # Set the start address of the application
**         WRITEH(F_EntryPoint);
**         } > .xBootCfg
**         
**         b) Add 
**              * (.const.data.pmem)
**         underneath  
**              * (.data.pmem) 
**         in the .ApplicationCode section to enable storage of constants within program area of flash
**         
**         c) Add 
**              F_vba = .;
**         underneath
**              # interrupt vectors
**         
**         ---
**     Settings  :
**
**     Contents  :
**         EnableInt   - void Cpu_EnableInt(void);
**         DisableInt  - void Cpu_DisableInt(void);
**         SetWaitMode - void Cpu_SetWaitMode(void);
**         Delay100US  - asm void Cpu_Delay100US(word us100);
**
**     (c) Freescale Semiconductor
**     2004 All Rights Reserved
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/

/* MODULE Cpu. */
#include "Term.h"
#include "Inhr6.h"
#include "LED0.h"
#include "LED1.h"
#include "SOUT1.h"
#include "PWREN.h"
#include "EINT_SW0.h"
#include "EINT_SW1.h"
#include "LCD_CS.h"
#include "LCD_SIMO.h"
#include "LCD_SCLK.h"
#include "LCD_DC.h"
#include "LCD_RST.h"
#include "LCD_BUSY.h"
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "Events.h"
#include "Cpu.h"


/* Port array initialization. Array is used by BitIO beans with 'code size' optimization */
tBitIO_portDsc BitIO_portDsc[3]=
{
  {(word*)(61825), (word*)(61826)}, /* GPIO_A_DR */
  {(word*)(61889), (word*)(61890)}, /* GPIO_C_DR */
  {(word*)(61857), (word*)(61858)}, /* GPIO_B_DR */
};

/* Global variables */
volatile word SR_lock = 0;             /* Lock */
volatile word SR_reg;                  /* Current value of the SR register */
/*
** ===================================================================
**     Method      :  Cpu_Interrupt (component 56F8006_32_LQFP)
**
**     Description :
**         The method services unhandled interrupt vectors.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#pragma interrupt alignsp
void Cpu_Interrupt(void)
{
  asm(DEBUGHLT);                       /* Halt the core and placing it in the debug processing state */
}

/*
** ===================================================================
**     Method      :  Cpu__ivINT_GPIO_C (component 56F8006_32_LQFP)
**
**     Description :
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#pragma interrupt alignsp saveall
void Cpu__ivINT_GPIO_C(void)
{
  word regIntFlag_GPIO_C_IPR = getReg(GPIO_C_IPR);

  if (regIntFlag_GPIO_C_IPR & GPIO_C_IPR_IP0_MASK) /* Is an interrupt flag set? */
  setReg16(GPIO_C_IESR, GPIO_C_IESR_IES0_MASK);
  if (regIntFlag_GPIO_C_IPR & GPIO_C_IPR_IP1_MASK) /* Is an interrupt flag set? */
  setReg16(GPIO_C_IESR, GPIO_C_IESR_IES1_MASK);
  if (regIntFlag_GPIO_C_IPR & GPIO_C_IPR_IP0_MASK) { /* Is the interrupt flag set? */
    EINT_SW0_OnInterrupt();            /* Call the service routine */
  }
  if (regIntFlag_GPIO_C_IPR & GPIO_C_IPR_IP1_MASK) { /* Is the interrupt flag set? */
    EINT_SW1_OnInterrupt();            /* Call the service routine */
  }
}

/*
** ===================================================================
**     Method      :  Cpu_Delay100US (component 56F8006_32_LQFP)
**
**     Description :
**         This method realizes software delay. The length of delay
**         is at least 100 microsecond multiply input parameter
**         [us100]. As the delay implementation is not based on real
**         clock, the delay time may be increased by interrupt
**         service routines processed during the delay. Adding read
**         wait states for external program memory can cause delay
**         extension as well. The method is independent on selected
**         speed mode.
**     Parameters  :
**         NAME            - DESCRIPTION
**         us100           - Number of 100 us delay repetitions.
**     Returns     : Nothing
** ===================================================================
*/
/*lint -save -e???? Disable MISRA rule (all) checking. */
asm void Cpu_Delay100US(word us100)
{
  /* Total irremovable overhead: about 16 cycles */
  /* move.w: 2 cycles overhead (load parameter into register) */
  /* jsr:    5 cycles overhead (jump to subroutine) */
  /* rts:    8 cycles overhead (return from subroutine) */
  /* nop:    1 cycles overhead (aditional nops) */

  loop:
  /* 100 us delay block begin */
  /*
   * Delay
   *   - requested                  : 100 us @ 4MHz,
   *   - possible                   : 400 c, 100000 ns
   *   - without removable overhead : 394 c, 98500 ns
   */
  adda #2, SP                          /* (1 c: 250 ns) move SP forward */
  move.l A10, X:(SP)                   /* (2 c: 500 ns) push A */
  move.w #378, A                       /* (2 c: 500 ns) number of iterations */
  do A, label0                         /* (8 c: 2000 ns) repeat 378x nop */
    nop                                /* (1 c: 250 ns) wait for 1 c */
  label0:
  move.l X:(SP), A                     /* (2 c: 500 ns) pop A */
  suba #2, SP                          /* (1 c: 250 ns) move SP back */
  /* 100 us delay block end */
  dec.w Y0                             /* us100 parameter is passed via Y0 register */
  jne loop                             /* next loop */
  nop                                  /* avoid pipeline conflicts */
  rts                                  /* return from subroutine */
}
/*lint -restore */

/*
** ===================================================================
**     Method      :  Cpu_DisableInt (component 56F8006_32_LQFP)
**
**     Description :
**         Disables all maskable interrupts
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
void Cpu_DisableInt(void)

**      This method is implemented as macro in the header module. **
*/

/*
** ===================================================================
**     Method      :  Cpu_EnableInt (component 56F8006_32_LQFP)
**
**     Description :
**         Enables all maskable interrupts
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
void Cpu_EnableInt(void)

**      This method is implemented as macro in the header module. **
*/

/*
** ===================================================================
**     Method      :  Cpu_SetWaitMode (component 56F8006_32_LQFP)
**
**     Description :
**         Sets low power mode - Wait mode.
**         For more information about the wait mode see this CPU
**         documentation.
**         Release from wait mode: Reset or interrupt
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
/*
void Cpu_SetWaitMode(void)

**      This method is implemented as macro in the header module. **
*/

/*
** ===================================================================
**     Method      :  _EntryPoint (component 56F8006_32_LQFP)
**
**     Description :
**         Initializes the whole system like timing and so on. At the end 
**         of this function, the C startup is invoked to initialize stack,
**         memory areas and so on.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
extern void init_56800_(void);         /* Forward declaration of external startup function declared in startup file */

/*** !!! Here you can place your own code using property "User data declarations" on the build options tab. !!! ***/

void _EntryPoint(void)
{
  #pragma constarray off

  /*** !!! Here you can place your own code before PE initialization using property "User code before PE initialization" on the build options tab. !!! ***/

  /*** ### MC56F8006_32_LQFP "Cpu" init code ... ***/
  /*** PE initialization code after reset ***/
  /* System clock initialization */
  setRegBitGroup(OCCS_OCTRL, TRIM, (word)getReg(FM_OPT1) & 1023); /* Set the trim osc freq with factory programmed value */
  setRegBit(OCCS_OCTRL, CLK_MODE);     /* Select an internal oscillator mode */
  clrRegBit(OCCS_CTRL, PRECS);         /* Select an internal clock source for the CPU core */
  setRegBitGroup(OCCS_CTRL, ZSRC, 1);  /* Select clock source from MSTR_OSC */
  setRegBit(OCCS_CTRL, PLLPD);         /* Disable PLL */
  /* FM_CLKDIV: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DIVLD=0,PRDIV8=0,DIV=40 */
  setReg16(FM_CLKDIV, 40U);            /* Set the flash clock prescaler */ 
  /*** End of PE initialization code after reset ***/

  /*** !!! Here you can place your own code after PE initialization using property "User code after PE initialization" on the build options tab. !!! ***/

  setRegBit(SIM_PCE, COP);             /* Enable COP peripheral clocks */
  setReg(COP_CTRL, 0);                 /* Disable COP running after reset */
  clrRegBit(SIM_PCE, COP);             /* Disble COP peripheral clocks */
  asm(JMP init_56800_);                /* Jump to C startup code */
}

/*
** ===================================================================
**     Method      :  PE_low_level_init (component 56F8006_32_LQFP)
**
**     Description :
**         Initializes beans and provides common register initialization. 
**         The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void PE_low_level_init(void)
{
  /* GPIO_A_DRIVE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DRIVE7=0,DRIVE6=0,DRIVE5=0,DRIVE4=0,DRIVE3=0,DRIVE2=0,DRIVE1=0,DRIVE0=0 */
  setReg16(GPIO_A_DRIVE, 0U);          /* Set High/Low drive strength on GPIOA pins according to the CPU bean settings */ 
  /* GPIO_B_DRIVE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DRIVE7=0,DRIVE6=0,DRIVE5=0,DRIVE4=0,DRIVE3=0,DRIVE2=0,DRIVE1=0,DRIVE0=0 */
  setReg16(GPIO_B_DRIVE, 0U);          /* Set High/Low drive strength on GPIOB pins according to the CPU bean settings */ 
  /* GPIO_C_DRIVE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DRIVE7=0,DRIVE6=1,DRIVE5=0,DRIVE4=0,DRIVE3=0,DRIVE2=0,DRIVE1=0,DRIVE0=0 */
  setReg16(GPIO_C_DRIVE, 64U);         /* Set High/Low drive strength on GPIOC pins according to the CPU bean settings */ 
  /* GPIO_D_DRIVE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DRIVE3=0,DRIVE2=0,DRIVE1=0,DRIVE0=0 */
  setReg16(GPIO_D_DRIVE, 0U);          /* Set High/Low drive strength on GPIOD pins according to the CPU bean settings */ 
  /* GPIO_F_DRIVE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DRIVE3=0,DRIVE2=0,DRIVE1=0,DRIVE0=0 */
  setReg16(GPIO_F_DRIVE, 0U);          /* Set High/Low drive strength on GPIOF pins according to the CPU bean settings */ 
  /* GPIO_A_IFE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,IFE7=0,IFE6=0,IFE5=0,IFE4=0,IFE3=0,IFE2=0,IFE1=0,IFE0=0 */
  setReg16(GPIO_A_IFE, 0U);            /* Set the input filter on GPIOA pins according to the CPU bean settings */ 
  /* GPIO_B_IFE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,IFE7=0,IFE6=0,IFE5=0,IFE4=0,IFE3=0,IFE2=0,IFE1=0,IFE0=0 */
  setReg16(GPIO_B_IFE, 0U);            /* Set the input filter on GPIOB pins according to the CPU bean settings */ 
  /* GPIO_C_IFE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,IFE7=0,IFE6=0,IFE5=0,IFE4=0,IFE3=0,IFE2=0,IFE1=0,IFE0=0 */
  setReg16(GPIO_C_IFE, 0U);            /* Set the input filter on GPIOC pins according to the CPU bean settings */ 
  /* GPIO_D_IFE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,IFE3=0,IFE2=0,IFE1=0,IFE0=0 */
  setReg16(GPIO_D_IFE, 0U);            /* Set the input filter on GPIOD pins according to the CPU bean settings */ 
  /* GPIO_F_IFE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,IFE3=0,IFE2=0,IFE1=0,IFE0=0 */
  setReg16(GPIO_F_IFE, 0U);            /* Set the input filter on GPIOF pins according to the CPU bean settings */ 
  /* GPIO_A_SLEW: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,SLEW7=1,SLEW6=1,SLEW5=1,SLEW4=1,SLEW3=1,SLEW2=1,SLEW1=0,SLEW0=0 */
  setReg16(GPIO_A_SLEW, 252U);         /* Set the input filter on GPIOA pins according to the CPU bean settings */ 
  /* GPIO_B_SLEW: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,SLEW7=1,SLEW6=1,SLEW5=1,SLEW4=1,SLEW3=1,SLEW2=1,SLEW1=1,SLEW0=1 */
  setReg16(GPIO_B_SLEW, 255U);         /* Set the input filter on GPIOB pins according to the CPU bean settings */ 
  /* GPIO_C_SLEW: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,SLEW7=1,SLEW6=1,SLEW5=1,SLEW4=1,SLEW3=0,SLEW2=1,SLEW1=1,SLEW0=1 */
  setReg16(GPIO_C_SLEW, 247U);         /* Set the input filter on GPIOC pins according to the CPU bean settings */ 
  /* GPIO_D_SLEW: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,SLEW3=1,SLEW2=1,SLEW1=1,SLEW0=1 */
  setReg16(GPIO_D_SLEW, 15U);          /* Set the input filter on GPIOD pins according to the CPU bean settings */ 
  /* GPIO_F_SLEW: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,SLEW3=0,SLEW2=0,SLEW1=0,SLEW0=1 */
  setReg16(GPIO_F_SLEW, 1U);           /* Set the input filter on GPIOF pins according to the CPU bean settings */ 
  /* SIM_PCR: TMR_CR=0,??=0,PWM_CR=0,SCI_CR=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0 */
  setReg16(SIM_PCR, 0U);               /* Set the TMR; PWM; SCI module clock rates */ 
  /* SIM_PCE: CMP2=0,CMP1=0,CMP0=0,ADC1=0,ADC0=0,PGA1=0,PGA0=0,I2C=0,SCI=1,SPI=0,PWM=0,COP=0,PDB=0,PIT=0,TA1=0,TA0=0 */
  setReg16(SIM_PCE, 128U);             /* Set up the peripheral clock enable register */ 
  /* SIM_SDR: CMP2=0,CMP1=0,CMP0=0,ADC1=0,ADC0=0,PGA1=0,PGA0=0,I2C=0,SCI=0,SPI=0,PWM=0,COP=0,PDB=0,PIT=0,TA1=0,TA0=0 */
  setReg16(SIM_SDR, 0U);               /* Set up the STOP disable register */ 
  /* SIM_CTRL: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,ONCEEBL=0,SWRST=0,STOP_DISABLE=1,WAIT_DISABLE=0 */
  setReg16(SIM_CTRL, 4U);              /* Set up the SIM control register */ 
  /* SIM_CLKOUT: ??=0,??=0,CLKDIS1=1,??=0,??=0,CLKOSEL1=1,??=0,??=0,CLKDIS0=1,CLKOSEL0=6 */
  setReg16(SIM_CLKOUT, 8486U);         /* Set up the SIM clock output select register */ 
  /* PMC_SCR: OORF=0,LVDF=0,PPDF=0,PORF=0,OORIE=0,LVDIE=0,LVDRE=0,PPDE=0,LPR=0,LPRS=0,LPWUI=0,BGBE=0,LVDE=0,LVLS=0,PROT=0 */
  setReg16(PMC_SCR, 0U);                
  /* Common initialization of the CPU registers */
  /* SIM_GPSA: GPS_A6=3 */
  clrSetReg16Bits(SIM_GPSA, 256U, 192U); 
  /* SIM_GPSB0: GPS_B4=4 */
  clrSetReg16Bits(SIM_GPSB0, 6144U, 8192U); 
  /* GPIO_B_PER: PE7=0,PE6=0,PE5=0,PE4=1,PE3=0,PE2=0,PE1=0,PE0=0 */
  clrSetReg16Bits(GPIO_B_PER, 239U, 16U); 
  /* GPIO_A_PER: PE6=1,PE5=0,PE4=0,PE3=0,PE2=0,PE1=0,PE0=0 */
  clrSetReg16Bits(GPIO_A_PER, 63U, 64U); 
  /* GPIO_A_IENR: IEN1=0,IEN0=0 */
  clrReg16Bits(GPIO_A_IENR, 3U);        
  /* GPIO_A_IESR: IES1=1,IES0=1 */
  setReg16Bits(GPIO_A_IESR, 3U);        
  /* GPIO_A_DR: D1=0,D0=0 */
  clrReg16Bits(GPIO_A_DR, 3U);          
  /* GPIO_A_DDR: DD5=0,DD4=0,DD3=0,DD2=0,DD1=1,DD0=1 */
  clrSetReg16Bits(GPIO_A_DDR, 60U, 3U); 
  /* GPIO_C_IENR: IEN6=0,IEN2=0 */
  clrReg16Bits(GPIO_C_IENR, 68U);       
  /* GPIO_C_IESR: IES6=1,IES2=1 */
  setReg16Bits(GPIO_C_IESR, 68U);       
  /* GPIO_C_DR: D6=1 */
  setReg16Bits(GPIO_C_DR, 64U);         
  /* GPIO_C_DDR: DD6=1,DD5=0,DD4=0,DD2=0,DD1=0,DD0=0 */
  clrSetReg16Bits(GPIO_C_DDR, 55U, 64U); 
  /* GPIO_C_PER: PE6=0,PE5=0,PE4=0,PE2=0,PE1=0,PE0=0 */
  clrReg16Bits(GPIO_C_PER, 119U);       
  /* GPIO_B_IENR: IEN7=0,IEN6=0,IEN5=0,IEN3=0,IEN2=0,IEN1=0 */
  clrReg16Bits(GPIO_B_IENR, 238U);      
  /* GPIO_B_IESR: IES7=1,IES6=1,IES5=1,IES3=1,IES2=1,IES1=1 */
  setReg16Bits(GPIO_B_IESR, 238U);      
  /* GPIO_B_PUR: PU2=1,PU0=0 */
  clrSetReg16Bits(GPIO_B_PUR, 1U, 4U);  
  /* GPIO_B_DR: D7=0,D6=0,D5=0,D3=0,D1=0 */
  clrReg16Bits(GPIO_B_DR, 234U);        
  /* GPIO_B_DDR: DD7=1,DD6=1,DD5=1,DD3=1,DD2=0,DD1=1,DD0=0 */
  clrSetReg16Bits(GPIO_B_DDR, 5U, 234U); 
  /* GPIO_C_IPR: IP1=0,IP0=0 */
  clrReg16Bits(GPIO_C_IPR, 3U);         
  /* GPIO_C_IPOLR: IPOL1=1,IPOL0=1 */
  setReg16Bits(GPIO_C_IPOLR, 3U);       
  /* GPIO_C_PUR: PU5=0,PU4=0,PU2=0,PU1=1,PU0=1 */
  clrSetReg16Bits(GPIO_C_PUR, 52U, 3U); 
  /* GPIO_A_PUR: PU5=0,PU4=0,PU3=0,PU2=0 */
  clrReg16Bits(GPIO_A_PUR, 60U);        
  /* GPIO_F_PUR: PU0=0 */
  clrReg16Bits(GPIO_F_PUR, 1U);         
  /* GPIO_F_DDR: DD0=0 */
  clrReg16Bits(GPIO_F_DDR, 1U);         
  /* GPIO_F_PER: PE0=0 */
  clrReg16Bits(GPIO_F_PER, 1U);         
  /* ### Asynchro serial "Inhr6" init code ... */
  Inhr6_Init();
  /* ###  "Term" init code ... */
  /* Common peripheral initialization - ENABLE */
  /* GPIO_C_IESR: IES1=1,IES0=1 */
  setReg16Bits(GPIO_C_IESR, 3U);        
  /* GPIO_C_IENR: IEN1=1,IEN0=1 */
  setReg16Bits(GPIO_C_IENR, 3U);        
  __EI(0);                             /* Enable interrupts of the selected priority level */
}

/* END Cpu. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.00 [04.35]
**     for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/
