EEPROM Memory Library

This library provides routines for accessing microcontroller's (internal) EEPROM memory. Only the STM32L and Tiva series have internal EEPROM memory.

STM32L Series Library Routines

Tiva Series Library Routines

EEPROM_Unlock

Prototype

void EEPROM_Unlock();

Description

This routine will unlock the EEPROM control register access.

This routine is valid only for STM32L devices.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
EEPROM_Unlock();
Notes

This routine is valid only for STM32L devices.

EEPROM_lock

Prototype

void EEPROM_lock();

Description

This routine will lock the EEPROM control register access.

This routine is valid only for STM32L devices.

Parameters

None.

Returns

Nothing.

Requires

Nothing.

Example
EEPROM_lock();
Notes

This routine is valid only for STM32L devices.

It is recommended that EEPROM lock is performed right after the EEPROM write is finished to avoid any unwanted subsequent EEPROM writing.

EEPROM_EraseWord

Prototype

unsigned long EEPROM_EraseWord(unsigned long Address);

Description

This routine will erase a word (32-bit) from the specified EEPROM address.

This routine is valid only for STM32L devices.

Parameters
  • Address: Specifies the address from which the EEPROM data will be erased. It has to be in the EEPROM address range or else the result can be unpredictable.
Returns
  • EEPROM memory status. The returned value can be:
    • 0 : EEPROM_COMPLETE,
    • 1 : EEPROM_BUSY,
    • 2 : EEPROM_ERROR_PROGRAM,
    • 3 : EEPROM_ERROR_WRP
    • 4 : EEPROM_ERROR_OPERATION.
Requires

Requires unlock procedure to be performed afterwards.

Example
status = EEPROM_EraseWord(0);
Notes

This routine is valid only for STM32L devices.

The address has to be in the EEPROM address range or else the result can be unpredictable.

EEPROM_Write_Word

Prototype

unsigned long EEPROM_Write_Word(unsigned long Address, unsigned long lData);

Description

Writes a word (32-bit) at a specified address. The address has to be in the EEPROM address range or else the result can be unpredictable.

This routine is valid only for STM32L devices.

Parameters
  • Address:: specifies the address to be programmed.
  • lData: specifies the data to be programmed.
Returns
  • EEPROM memory status. The returned value can be:
    • 0 : EEPROM_COMPLETE,
    • 1 : EEPROM_BUSY,
    • 2 : EEPROM_ERROR_PROGRAM,
    • 3 : EEPROM_ERROR_WRP
    • 4 : EEPROM_ERROR_OPERATION.
Requires

Requires unlock procedure to be performed afterwards.

Example
status = EEPROM_Write_Word();
Notes

This routine is valid only for STM32L devices.

The address has to be in the EEPROM address range or else the result can be unpredictable.

EEPROM_Write_HalfWord

Prototype

unsigned long EEPROM_Write_HalfWord(unsigned long Address, unsigned int lData);

Description

Writes a half word (16-bit) at a specified address. The address has to be in the EEPROM address range or else the result can be unpredictable.

This routine is valid only for STM32L devices.

Parameters
  • Address:: specifies the address to be programmed.
  • lData: specifies the data to be programmed.
Returns
  • EEPROM memory status. The returned value can be:
    • 0 : EEPROM_COMPLETE,
    • 1 : EEPROM_BUSY,
    • 2 : EEPROM_ERROR_PROGRAM,
    • 3 : EEPROM_ERROR_WRP
    • 4 : EEPROM_ERROR_OPERATION.
Requires

Requires unlock procedure to be performed afterwards.

Example
status = EEPROM_Write_HalfWord();
Notes

This routine is valid only for STM32L devices.

The address has to be in the EEPROM address range or else the result can be unpredictable.

EEPROM_Write_Byte

Prototype

unsigned long EEPROM_Write_Byte(unsigned long Address, unsigned char lData);

Description

Writes a byte (8-bit) at a specified address. The address has to be in the EEPROM address range or else the result can be unpredictable.

This routine is valid only for STM32L devices.

Parameters
  • Address:: specifies the address to be programmed.
  • lData: specifies the data to be programmed.
Returns
  • EEPROM memory status. The returned value can be:
    • 0 : EEPROM_COMPLETE,
    • 1 : EEPROM_BUSY,
    • 2 : EEPROM_ERROR_PROGRAM,
    • 3 : EEPROM_ERROR_WRP
    • 4 : EEPROM_ERROR_OPERATION.
Requires

Requires unlock procedure to be performed afterwards.

Example
status = EEPROM_Write_Byte();
Notes

This routine is valid only for STM32L devices.

The address has to be in the EEPROM address range or else the result can be unpredictable.

EEPROM_Init

Prototype

unsigned int EEPROM_Init();

Description

This routine will initialize the EEPROM.

Parameters

None.

Returns
  • 0 - if initialization was successful.
  • 1 - if an error occurred.
Requires

Nothing.

Example
value = EEPROM_Init();
Notes

This routine is valid only for Tiva devices.

EEPROM_BlockCountGet

Prototype

unsigned int EEPROM_BlockCountGet();

Description

This routine will get the number of blocks in the EEPROM.

Parameters

None.

Returns

Total number of blocks in the device EEPROM.

Requires

Nothing.

Example
blockcount = EEPROM_BlockCountGet();
Notes

This routine is valid only for Tiva devices.

EEPROM_BlockHide

Prototype

void EEPROM_BlockHide(unsigned int block);

Description

This routine hides an EEPROM block until the next reset.

Parameters
  • block: EEPROM block number which is to be hidden.
Returns

Nothing.

Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define HIDE_ADDRESS 128
#define HIDE_BLOCK_NUMBER HIDE_ADDRESS/BLOCK_SIZE

// hide block containing written data this block is now inaccesible until next reset
EEPROM_BlockHide(HIDE_BLOCK_NUMBER);
Notes
  • This function hides an EEPROM block other than block 0. Once hidden, a block is completely inaccessible until the next reset.
    This mechanism allows initialization code to have access to data which is to be hidden from the rest of the application.
    Unlike applications using passwords, an application making using of block hiding need not contain any embedded passwords which could be found through disassembly.
  • This routine is valid only for Tiva devices.

EEPROM_BlockProtectGet

Prototype

unsigned long EEPROM_BlockProtectGet(unsigned int block);

Description

This routine returns the current protection level for an EEPROM block.

Parameters
  • block: EEPROM block number for which the protection level is to be queried.
Returns
  • current protection level for an EEPROM block,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define LOCK_ADDRESS 256
#define LOCK_BLOCK_NUMBER LOCK_ADDRESS/BLOCK_SIZE

// read the protection level selected for a block
i = EEPROM_BlockProtectGet(LOCK_BLOCK_NUMBER);
Notes
  • This function returns the current protection settings for a given EEPROM block.
    If block 0 is currently locked, it must be first unlocked before calling this function to query the protection setting for other blocks.
  • This routine is valid only for Tiva devices.

EEPROM_BlockProtectSet

Prototype

unsigned long EEPROM_BlockProtectSet(unsigned int block, unsigned long protect);

Description

This routine sets the current protection options for an EEPROM block.

Parameters
  • block: EEPROM block number for which the protection options are to be set.
  • protect: EEPROM block protect value.
Returns
  • value of EEPROM_EEDONE register,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define LOCK_ADDRESS 256
#define LOCK_BLOCK_NUMBER LOCK_ADDRESS/BLOCK_SIZE

// This setting is the default. If there is no password, the block is not protected and is readable and writable.
// If there is a password, the block is readable, but only writable when unlocked.
EEPROM_BlockProtectSet(LOCK_BLOCK_NUMBER, 0x0);
Notes
  • This function locks an EEPROM block that has previously been protected by writing a password.
    Access to the block once it is locked is determined by the protection settings applied via a previous call to this function.
    Password must be set for the block or this function has no effect. Locking block 0 make all other blocks inaccessible.
  • This routine is valid only for Tiva devices.

EEPROM_BlockLock

Prototype

unsigned long EEPROM_BlockLock(unsigned int block);

Description

This routine locks a password-protected EEPROM block.

Parameters
  • block: EEPROM block number which is to be locked.
Returns
  • 0 if block is locked,
  • 1 if block is unlocked,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define LOCK_ADDRESS 256
#define LOCK_BLOCK_NUMBER LOCK_ADDRESS/BLOCK_SIZE

// lock block containing written data; this block is now inaccesible
EEPROM_BlockLock(LOCK_BLOCK_NUMBER);
Notes
  • This function locks an EEPROM block that has previously been protected by writing a password.
    Access to the block once it is locked is determined by the protection settings applied via a previous call to the EEPROM_BlockProtectSet function.
    Password must be set for the block or this function has no effect. Locking block 0 make all other blocks inaccessible.
  • This routine is valid only for Tiva devices.

EEPROM_BlockUnlock

Prototype

unsigned long EEPROM_BlockUnlock(unsigned int block, unsigned long *password, unsigned int count);

Description

This routine unlocks a password-protected EEPROM block.

Parameters
  • block: EEPROM block number which is to be unlocked.
  • password: points to an array of unsigned long values containing the password for the block. Each element must match the password originally set via a call to EEPROM_BlockPasswordSet.
  • count: provides the number of elements in the password array. Valid values are 1, 2 and 3.
Returns
  • 0 if block is locked,
  • 1 if block is unlocked,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define LOCK_ADDRESS 256
#define LOCK_BLOCK_NUMBER LOCK_ADDRESS/BLOCK_SIZE
#define PASS_SIZE 2

unsigned long password[PASS_SIZE];

password[0] = 0x0a;
password[1] = 0xc2;

// unlock locked block
EEPROM_BlockUnlock(LOCK_BLOCK_NUMBER, password, PASS_SIZE);
Notes
  • This function unlocks an EEPROM block that has previously been protected by writing a password. Access to the block once it is unlocked is determined by the protection settings applied via a previous call to the EEPROM_BlockProtectSet.
    To successfully unlock an EEPROM block, the password provided must match the password provided on the original call to EEPROM_BlockPasswordSet. If an incorrect password is provided, the block remains locked.
    Unlocking block 0 has the effect of making all other blocks in the device accessible according to their own access protection settings. When block 0 is locked, all other EEPROM blocks are inaccessible.
  • This routine is valid only for Tiva devices.

EEPROM_BlockPasswordSet

Prototype

unsigned long EEPROM_BlockPasswordSet(unsigned int block, unsigned long *password, unsigned int count);

Description

This routine sets the password used to protect an EEPROM block.

Parameters
  • block: EEPROM block number which is to be password protected.
  • password: points to an array of unsigned long values containing the password for the block. Each element must match the password originally set via a call to EEPROM_BlockPasswordSet.
  • count: provides the number of elements in the password array. Valid values are 1, 2 and 3.
Returns
  • value of EEPROM_EEDONE register,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define LOCK_ADDRESS 256
#define LOCK_BLOCK_NUMBER LOCK_ADDRESS/BLOCK_SIZE
#define PASS_SIZE 2

unsigned long password[PASS_SIZE];

password[0] = 0x0a;
password[1] = 0xc2;

// lock block
EEPROM_BlockPasswordSet(LOCK_BLOCK_NUMBER, password, PASS_SIZE);
Notes
  • This function allows the password used to unlock an EEPROM block to be set. Valid passwords may be either 32, 64 or 96 bits comprising words with any value other than 0xFFFFFFFF.
    The password may only be set once. Any further attempts to set the password result in an error. Once the password is set, the block remains unlocked until EEPROM_BlockLock is called for that block or block 0, or a reset occurs.
    If a password is set on block 0, this affects locking of the peripheral as a whole. When block 0 is locked, all other EEPROM blocks are inaccessible until block 0 is unlocked.
    Once block 0 is unlocked, other blocks become accessible according to any passwords set on those blocks and the protection set for that block via a call to EEPROM_BlockProtectSet.
  • This routine is valid only for Tiva devices.

EEPROM_MassErase

Prototype

unsigned long EEPROM_MassErase();

Description

This routine erases the EEPROM and returns it to the factory default condition.

Parameters

None.

Returns
  • value of EEPROM_EEDONE register.
Requires

Nothing.

Example
// delete entire EEPROM
EEPROM_MassErase();
Notes
  • This function completely erases the EEPROM and removes any and all access protection on its blocks, leaving the device in the factory default condition.
    After this operation, all EEPROM words contain the value 0xFFFFFFFF and all blocks are accessible for both read and write operations in all CPU modes. No passwords are active.
  • This routine is valid only for Tiva devices.

EEPROM_Program

Prototype

unsigned long EEPROM_Program(unsigned long *data_, unsigned int address, unsigned int count);

Description

This routine writes data to EEPROM.

Parameters
  • data_: pointer to the first word of data to be written to the EEPROM.
  • address: byte address within the EEPROM that the data is to be written to. This value must be a multiple of 4.
  • count: the number of bytes of data that is to be written. This value must be a multiple of 4.
Returns
  • value of EEPROM_EEDONE register,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define DATA_SIZE BLOCK_SIZE/4       // block size in words (unsigned long)
#define HIDE_ADDRESS 128
#define LOCK_ADDRESS 256

unsigned long writeData[DATA_SIZE];
int i;

for (i = 0; i < DATA_SIZE; i++) {
  writeData[i] = i;
}

// write data to EEPROM
EEPROM_Program(writeData, LOCK_ADDRESS, BLOCK_SIZE);
EEPROM_Program(writeData, HIDE_ADDRESS, BLOCK_SIZE);
Notes
  • This function may be called to write data into the EEPROM at a given word-aligned address.
    The call is synchronous and returns only after all data has been written or an error occurs.
  • This routine is valid only for Tiva devices.

EEPROM_ProgramWord

Prototype

unsigned long EEPROM_ProgramWord(unsigned long data_, unsigned int address);

Description

This routine writes a word to EEPROM.

Parameters
  • data_: word of data to be written to the EEPROM.
  • address: byte address within the EEPROM that the data is to be written to. This value must be a multiple of 4.
Returns
  • value of EEPROM_EEDONE register,
  • 0xFFFFFFFF - if an error occurred.
Requires

Nothing.

Example
#define START_ADDRESS 0

// program one word
EEPROM_ProgramWord(0xbb, START_ADDRESS);
Notes
  • This function may be called start the process of writing a single word of data into the EEPROM at a given word-aligned address.
    The call is asynchronous and returns immediately without waiting for the write to complete.
  • This routine is valid only for Tiva devices.

EEPROM_Read

Prototype

void EEPROM_Read(unsigned long *data_, unsigned int address, unsigned int count);

Description

This routine reads data from EEPROM.

Parameters
  • data_: storage pointer for the data to be read from the EEPROM.
  • address: byte address within the EEPROM that the data is to be read from. This value must be a multiple of 4.
  • count: the number of bytes of data that is to be read. This value must be a multiple of 4.
Returns

Nothing.

Requires

Nothing.

Example
#define BLOCK_SIZE 64                // block size in bytes
#define DATA_SIZE BLOCK_SIZE/4       // block size in words (unsigned long)
#define START_ADDRESS 0

unsigned long readData[DATA_SIZE];

// read DATA_SIZE words of data from EEPROM starting from START_ADDRESS
EEPROM_Read(readData, START_ADDRESS, BLOCK_SIZE);
Notes
  • This function may be called to read a number of words of data from a word-aligned address within the EEPROM.
    Data read is copied into the buffer pointed to by the data_ parameter.
  • This routine is valid only for Tiva devices.

Library Example

Copy Code To ClipboardCopy Code To Clipboard

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code