/******************************************************************************
*  LPC1xxx on-chip EEPROM demoprogram for MicroCore64 board (by Paul Seerden)
*******************************************************************************/

#include <LPC11Exx.h>                          // LPC11Exx definitions
#include "uart.h"
#include "command.h"
#include "eeprom.h"

const char cmdTable[]  = "HE""DE""FE""RB""WB";
const char helpTable[] = "Command set:\n\n"
                         "HE <cr>             - Help on commands and syntax\n"
                         "DE <cr>             - Display EEPROM contents\n"
                         "FE <dat> <cr>       - Fill EEPROM with dat\n"
                         "RB <add> <cr>       - Read byte from EEPROM\n"
                         "WB <add> <dat> <cr> - Write Byte to EEPROM\n\n";

static void CMD_DE(void)
/**********************/
{
  LONG addr;

    for (addr=0; addr<EE_SIZE; addr++)	
    {
        if ((addr & 0x1f) == 0)
        { 
            PrintString("\n");
            PrintWord(addr);
        }
        if ((addr & 0x7) == 0)
        {
            PrintString("  ");
        }
        PrintByte(EEPROM_Read(addr));
    }
    PrintString("\n");
}

static void CMD_FE(void)
/**********************/
{
  BYTE val;
  LONG addr;

    val = GetArgument();
    if (val > 255)
    {
        PrintString("err: data beyond BYTE size\n");
        return;
    }
    for (addr=0; addr<EE_SIZE; addr++)	
    {
        EEPROM_Write(addr,&val);
    }
    PrintString("\n");
}

static void CMD_RB(void)
/**********************/
{
  LONG addr;

    addr = GetArgument();
    if (addr < EE_SIZE)
    {
        PrintWord(addr);
        PrintString (": ");
        PrintByte(EEPROM_Read(addr));
        PrintString ("\n");
    }
    else
        PrintString("Address beyond EEPROM size\n");
}

static void CMD_WB(void)
/**********************/
{
  BYTE cha, val;
  LONG addr;

    addr = GetArgument();
    do
    {
        if (addr > EE_SIZE)
        {
            PrintString("err: address beyond EEPROM size\n");
            return;
        }
        val = GetArgument();
        if (val > 255)
        {
            PrintString("err: data beyond BYTE size\n");
            return;
        }
        EEPROM_Write(addr,&val);
        addr ++;
        cha = GetBufAsc(GetBufIndex() - 1);
    } while (cha == ',');
}

int main (void)
/*************/
{
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);       // Enable clock for GPIO
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);      // Enable clock for IOCON

    UART_Init(115200);                         // initialize UART

    PrintString("\f\nTest program LPC11E14 - EEPROM, June 2012\n");
    PrintString(helpTable);
    ua_outchar('*');
    while (1)
    {
        if (ReadCommandLine())                 // returns '1' if command entered
        {
            if (GetBufAsc(0) != '\r')
            {
                switch (GetCommand(cmdTable,4))
                {
                  case 0:  PrintString(helpTable); break;    // Help
                  case 1:  CMD_DE();               break;    // Display EEPROM	
                  case 2:  CMD_FE();               break;    // Fill EEPROM	
                  case 3:  CMD_RB();               break;    // Read Byte
                  case 4:  CMD_WB();               break;    // Write Byte
                  default: PrintString("Command error\n");
                }
                SetBufIndex(0);                // reset position pointer
            }
            ua_outchar('*');
        }
    }
}
