/******************************************************************************
 *
 * Copyright:
 *    (C) 2000 - 2008 Embedded Artists AB
 *
 * Description:
 *    Sample application that demonstrates how to create processes.
 *
 *****************************************************************************/

#include <general.h>
#include <printf_P.h>
#include <ea_init.h>
#include <lpc246x.h>
#include "uart.h"

/*****************************************************************************
 *
 * Description:
 *    Delay execution by a specified number of milliseconds by using
 *    timer #1. A polled implementation.
 *
 * Params:
 *    [in] delayInMs - the number of milliseconds to delay.
 *
 ****************************************************************************/
static void
delayMs(tU16 delayInMs)
{
  /*
   * setup timer #1 for delay
   */
  T1TCR = 0x02;          //stop and reset timer
  T1PR  = 0x00;          //set prescaler to zero
  T1MR0 = delayInMs * (Fpclk / 1000);
  T1IR  = 0xff;          //reset all interrrupt flags
  T1MCR = 0x04;          //stop timer on match
  T1TCR = 0x01;          //start timer
  
  //wait until delay time has elapsed
  while (T1TCR & 0x01)
    ;
}


/*****************************************************************************
 *
 * Description:
 *    The first function to execute 
 *
 ****************************************************************************/
int
main(void)
{
  eaInit();

  printf("\n***************************************************");
  printf("\n*                                                 *");
  printf("\n* Test program for LPC2478 EDU Board...           *");
  printf("\n* (C) Embedded Artists 2008                       *");
  printf("\n*                                                 *");
  printf("\n* Test - UART1 RS232                              *");
  printf("\n*                                                 *");
  printf("\n***************************************************\n");

  //initialize uart #1: 9600 bps, 8N1, no FIFO
  initUart1(B9600(Fpclk), UART_8N1, UART_FIFO_4);

  //print welcome message
  uart1SendString((tU8*)"\n*********************************************************");
  uart1SendString((tU8*)"\n*");
  uart1SendString((tU8*)"\n* (C) 2008 Embedded Artists AB");
  uart1SendString((tU8*)"\n*");
  uart1SendString((tU8*)"\n* Welcome to this program that demonstrates how the UART");
  uart1SendString((tU8*)"\n* can be used in interrupt mode.");
  uart1SendString((tU8*)"\n* It's a simple echo application...");
  uart1SendString((tU8*)"\n*");
  uart1SendString((tU8*)"\n*********************************************************");

#define MODEM_CTS 0x00040000  //P3.18
#define MODEM_CD  0x00080000  //P3.19
#define MODEM_DSR 0x00100000  //P3.20
#define MODEM_RI  0x00400000  //P3.22

#define MODEM_RTS 0x40000000  //P3.30
#define MODEM_DTR 0x00200000  //P3.21

  FIO3DIR |= (MODEM_RTS | MODEM_DTR);
  delayMs(500);
  FIO3SET = MODEM_RTS;
  delayMs(500);
  FIO3CLR = MODEM_RTS;
  delayMs(500);
  FIO3SET = MODEM_RTS;
  delayMs(500);
  FIO3CLR = MODEM_RTS;
  delayMs(500);
  FIO3SET = MODEM_RTS;
  delayMs(500);
  FIO3CLR = MODEM_RTS;
  delayMs(500);
  FIO3SET = MODEM_DTR;
  delayMs(500);
  FIO3CLR = MODEM_DTR;
  delayMs(500);
  FIO3SET = MODEM_DTR;
  delayMs(500);
  FIO3CLR = MODEM_DTR;
  delayMs(500);
  FIO3SET = MODEM_DTR;
  delayMs(500);
  FIO3CLR = MODEM_DTR;

  while(1)
  {
    tU8 rxChar;

    if (uart1GetChar(&rxChar) == TRUE)
    {
      printf("\nReceived char: %c", rxChar);
    }
    
    if (consolGetChar(&rxChar) == TRUE)
    {
      uart1SendString((tU8*)"\nReceived char: ");
      uart1SendChar(rxChar);
    }

    delayMs(5);
    
    if (!(FIO3PIN & MODEM_CTS))
      printf("\nCTS");
    if (!(FIO3PIN & MODEM_CD))
      printf("\nCD");
    if (!(FIO3PIN & MODEM_DSR))
      printf("\nDSR");
    if (!(FIO3PIN & MODEM_RI))
      printf("\nRI");
  }
}
