/***************************************************************************/
/*  Description : EEPROM driver for LPC13xx microcontrollers.              */
/*                                                                         */
/*                  (C) Copyright 2012 NXP Semiconductors                  */
/*                                                                         */
/***************************************************************************/

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

static const IAP iap_entry = (IAP) IAP_LOCATION;

void EEPROM_Write(LONG add, BYTE* val)
/************************************/
{	
  LONG command[5], result[4];	

    command[0] = 61;                           // Command code: 61
    command[1] = add;                          // Param0: eeprom address (byte, half-word or word aligned)
    command[2] = (LONG) val;                   // Param1: RAM address (byte, half-word or word aligned)
    command[3] = 1;                            // Param2: Number of bytes to be written (always 1)
    command[4] = SystemCoreClock/1000;         // Param3: System Clock Frequency (CCLK) in kHz

    iap_entry(command, result);                // Invoke IAP call . . .
    if (result[0] != 0)
    {
        PrintString("EEPROM Write error\n");
    }
}

BYTE EEPROM_Read(LONG add)
/************************/
{
  BYTE res;
  LONG command[5], result[4];
		
    command[0] = 62;                           // Command code: 62
    command[1] = add;                          // Param0: eeprom address (byte, half-word or word aligned)
    command[2] = (LONG) &res;                  // Param1: RAM address (byte, half-word or word aligned)
    command[3] = 1;                            // Param2: Number of bytes to be written (always 1)
    command[4] = SystemCoreClock/1000;         // Param3: System Clock Frequency (CCLK) in kHz
	
    iap_entry(command, result);                // Invoke IAP call . . .
    if (result[0] != 0)
    {
        PrintString("EEPROM Read error\n");
    }
    return res;
}
