Read/Write the on-chip EEPROM on an LPC1347
Posted: Sat Mar 23, 2013 4:54 am
A useful feature of MCUs of the LPC1347-family is that they have up to 4 kB of on-chip EEPROM data memory for storing data that is preserved when power is removed from the MCU.
The EEPROM data can be read and written using new built-in In-Application Programming (IAP) features of the LPC1347 MCU. This is simpler and safer than the older way of storing persistent data in unused areas of the flash ROM along with your program.
Whenever you wanted to store data in flash ROM you needed to have a knowledge of the addresses and (varying) sizes of the sectors of flash ROM and avoid overwriting your program code. Also, the update required the use of two IAP operations :
Prepare Sector(s)
Copy RAM to Flash
In comparison, updating EEPROM requires just one simple IAP operation and a zero-based byte address which is isolated from your program code.
Astrobe users can easily read and write EEPROM data using two procedures that we have added to the IAP library module for the LPC1347 family:
addr starts at 0 and is the location of the data in the EEPROM.
data is declared as ARRAY OF BYTE so that any Oberon data type (INTEGER, REAL, ARRAY, RECORD etc.) can be passed as an actual parameter.
The example below shows how the procedures can be used to write / read a RECORD containing an eight-character string and an integer counter to / from the EEPROM. The counter keeps track of how many times the MCU has been reset since the program was installed.
The example should be used with the latest copy of the Astrobe library module IAP which is attached. Copy this into your LPC1347\Lib folder to replace the older version.
NOTE: As well as including the two new procedures, this version of IAP no longer requires you to include cclk as a parameter to the WriteFlash and EraseSectors procedures.
The EEPROM data can be read and written using new built-in In-Application Programming (IAP) features of the LPC1347 MCU. This is simpler and safer than the older way of storing persistent data in unused areas of the flash ROM along with your program.
Whenever you wanted to store data in flash ROM you needed to have a knowledge of the addresses and (varying) sizes of the sectors of flash ROM and avoid overwriting your program code. Also, the update required the use of two IAP operations :
Prepare Sector(s)
Copy RAM to Flash
In comparison, updating EEPROM requires just one simple IAP operation and a zero-based byte address which is isolated from your program code.
Astrobe users can easily read and write EEPROM data using two procedures that we have added to the IAP library module for the LPC1347 family:
Code: Select all
PROCEDURE EEPROMRead(addr: INTEGER; VAR data: ARRAY OF BYTE): INTEGER;
PROCEDURE EEPROMWrite(addr: INTEGER; data: ARRAY OF BYTE): INTEGER;
data is declared as ARRAY OF BYTE so that any Oberon data type (INTEGER, REAL, ARRAY, RECORD etc.) can be passed as an actual parameter.
The example below shows how the procedures can be used to write / read a RECORD containing an eight-character string and an integer counter to / from the EEPROM. The counter keeps track of how many times the MCU has been reset since the program was installed.
The example should be used with the latest copy of the Astrobe library module IAP which is attached. Copy this into your LPC1347\Lib folder to replace the older version.
NOTE: As well as including the two new procedures, this version of IAP no longer requires you to include cclk as a parameter to the WriteFlash and EraseSectors procedures.
Code: Select all
MODULE EEPROM;
(* =========================================================================
Example Cortex-M3 Oberon Program
Description:
Writes / Reads a string and an integer counter to on-chip EEPROM via IAP
The counter keeps track of how many times the MCU has been reset since
the program was installed.
Target:
LPC1347 systems
Refs:
NXP UM10524 LPC1315/16/17/45/46/47 User manual
Oberon for Cortex-M3 Microcontrollers
(c) 2013 CFB Software
http://www.astrobe.com
========================================================================= *)
IMPORT IAP, Main, Out;
TYPE
IdString = ARRAY 8 OF CHAR;
Data = RECORD
id: IdString;
counter: INTEGER
END;
PROCEDURE OutData(data: Data);
BEGIN
Out.String("ID = ");
Out.String(data.id);
Out.Ln();
Out.String("Counter = ");
Out.Int(data.counter, 8);
Out.Ln();
END OutData;
PROCEDURE OutLine(msg: ARRAY OF CHAR);
BEGIN
Out.String(msg);
Out.Ln()
END OutLine;
PROCEDURE Run;
VAR
data: Data;
id: IdString;
BEGIN
data.id := "";
id := "Astrobe";
(* Look for the id in the EEPROM to see if it has been written previously *)
OutLine("Reading data");
ASSERT(IAP.EEPROMRead(0, data) = IAP.OK, 20);
IF data.id # id THEN
(* No valid id so EEPROM has not previously been written by this program. Initialise the data *)
OutLine("Initialising data");
data.id := id;
data.counter := 0
END;
OutData(data);
INC(data.counter);
OutLine("Writing data");
ASSERT(IAP.EEPROMWrite(0, data) = IAP.OK, 21);
OutLine("Finished");
END Run;
BEGIN
Run()
END EEPROM.