/******************************************************************************
*  PCF2129A SSP-RTC demoprogram for MicroCore64 board (by Paul Seerden)
*******************************************************************************/

#include <LPC11Uxx.h>                          // LPC11Uxx definitions
#include "uart.h"
#include "command.h"
#include "rtc.h"

const char cmdTable[]  = "HE""ST""SD""SC""RT""RD";
const char helpTable[] = "Command set:\n\n"
                         "HE <cr>                      - Help on commands and syntax\n"
                         "ST <hours> <minutes> <cr>    - Set Time\n"
                         "SD <day> <month> <year> <cr> - Set Date\n"
                         "SC <val> <cr>                - Set CLKOUT frequency selection\n"
                         "RT <cr>                      - Read Time (<Esc> = exit)\n"
                         "RD <cr>                      - Read Date and Time\n\n";

int main (void)
{
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);           // Enable GPIO clock
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);          // Enable IO config clock
    UART_Init(38400);                              // initialize UART
    RTC_Init();                                    // initialise RTC + SSP

    PrintString("\f\nTest program PCF2129A - (SPI)RTC, June 2012\n");
    PrintString(helpTable);
    ua_outchar('*');
    while (1)
    {
        if (ReadCommandLine())                 // returns '1' if command entered
        {
            if (GetBufAsc(0) != '\r')
            {
                switch (GetCommand(cmdTable,6))
                {
                  case 0:  PrintString(helpTable); break;    // Help
                  case 1:  RTC_ST();               break;    // Set Time	
                  case 2:  RTC_SD();               break;    // Set Date
                  case 3:  RTC_SC();               break;    // Set CLKOUT
                  case 4:  RTC_RT();               break;    // Read Time
                  case 5:  RTC_RD();               break;    // Read Date
                  default: PrintString("Command error\n");
                }
                SetBufIndex(0);                // reset position pointer
            }
            ua_outchar('*');
        }
    }
}
