/*
Reads a character sent from a terminal program
and writes it back with a leading '*'.
The terminal program should be adjusted to the following settings
Baud rate = 9600
1 stop bit
No parity
No flow control
*/


#include "lpc214x.h"
#include "rs232.h"

void Initialize(void);
void feed(void);

void IRQ_Routine (void)   __attribute__ ((interrupt("IRQ")));
void FIQ_Routine (void)   __attribute__ ((interrupt("FIQ")));
void SWI_Routine (void)   __attribute__ ((interrupt("SWI")));
void UNDEF_Routine (void) __attribute__ ((interrupt("UNDEF")));


int main (void) 
{
  int j;				// loop counter (stack variable)
  char ch_1;
  // Initialize the system
  Initialize();		
  UART0Initialize(9600);		//Init UART0
  // UART1Initialize(9600);		//Init UART1
  // endless loop to toggle the two leds
  while (1) {
    ch_1 = UART0ReadChar_nostop();
    if(ch_1 != 0) { UART0WriteChar(ch_1); UART0WriteChar('*'); ch_1 = 0; }
  }
}

#define PLOCK 0x400

void Initialize(void)
{
  // Setting Multiplier and Divider values
  PLLCFG=0x23;
  feed();
  // Enabling the PLL */
  PLLCON=0x1;
  feed();
  // Wait for the PLL to lock to set frequency
  while(!(PLLSTAT & PLOCK)) ;
  // Connect the PLL as the clock source
  PLLCON=0x3;
  feed();
  // Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
  MAMCR=0x2;
  MAMTIM=0x4;
  // Setting peripheral Clock (pclk) to System Clock (cclk)
  VPBDIV=0x1;
}

void feed(void)
{
  PLLFEED=0xAA;
  PLLFEED=0x55;
}

/*  Stubs for various interrupts (may be replaced later)  */
/*  ----------------------------------------------------  */

void IRQ_Routine (void) {
  while (1) ;	
}

void FIQ_Routine (void)  {
  while (1) ;	
}

void SWI_Routine (void)  {
  while (1) ;	
}

void UNDEF_Routine (void) {
  while (1) ;	
}











