Using ST’s I2C EEPROM 24C16 with Freescale SKEAZ128LH4

U

The I²C bus is the most popular of the three current serial EEPROM protocols because of its simplicity, high signal density, and unique write protect (WP) pin characteristics. This illustration shows the typical pin-out of an I²C device with pins 1 through 3 as address pins A0, A1, and A2. Pin 4 is designated as ground, Vcc, while pin 5 is the data line, SDA. The clock signal SCL is at pin 6, pin 7 is WP, and pin 8 is voltage, VSS.

An advantage of the I²C protocol is that it only requires two signal lines for clock and data as opposed to other protocols that require four signal lines between the EEPROM and the master device.

In this post we are using the ST Microelectronics I2C EEPROM 24C16 [en.DM00061111.pdf] along with Freescale SKEAZ128LH4.

Below is a simple library to read and write a byte.

/******************************************************************************
 * START OF HEADER FILE				     			      * 
 *****************************************************************************/

#ifndef SOURCES_EEPROM_H_

/******************************************************************************
 * Definitions						                      *
 *****************************************************************************/

#define SOURCES_EEPROM_H_

/******************************************************************************
 * Includes				  		                      *
 *****************************************************************************/

#include <stdint.h>
#include "PE_Types.h"
#include "I2C_EEPROM.h"

/******************************************************************************
 * Exposed Functions			       			              *
 *****************************************************************************/

uint8_t EEPROM_Initialize();
uint8_t EEPROM_write_byte(uint16_t address, uint8_t data);
uint8_t EEPROM_read_byte(uint16_t address, uint8_t * data);

/******************************************************************************
 * END OF HEADER FILE							      *
 *****************************************************************************/

#endif
/******************************************************************************
 * START OF SOURCE FILE		 		                              *
 *****************************************************************************/

/******************************************************************************
 * Includes				 				      *
 *****************************************************************************/

#include "eeprom.h"

/******************************************************************************
 * Variables					 			      *
 *****************************************************************************/

static LDD_TDeviceData *I2C0Ptr;

/******************************************************************************
 * Exposed Functions			 			              *
 *****************************************************************************/

uint8_t EEPROM_Initialize()
{
	I2C0Ptr=I2C_EEPROM_Init(NULL);
	return I2C0Ptr == NULL ? 1 : 0;
}

/******************************************************************************
 *****************************************************************************/

uint8_t EEPROM_write_byte(uint16_t address, uint8_t data)
{
	uint8_t bytes[2];

	bytes[0] = (address & 0xFF);
	bytes[1] = data;

	if(I2C_EEPROM_SelectSlaveDevice(I2C0Ptr,LDD_I2C_ADDRTYPE_7BITS, (0xA0 | (((uint16_t)(address & 0x700)) >> 8)) >> 1) !=0)
		return 1;

	if(I2C_EEPROM_MasterSendBlock(I2C0Ptr,bytes,2,LDD_I2C_SEND_STOP)!=0)
		return 2;

	while(I2C_EEPROM_MasterGetBlockSentStatus(I2C0Ptr)!=TRUE);

	return 0;
}

/******************************************************************************
 *****************************************************************************/

uint8_t EEPROM_read_byte(uint16_t address, uint8_t * data)
{
	uint8_t byte;

	byte = (address & 0xFF);

	if(I2C_EEPROM_SelectSlaveDevice(I2C0Ptr,LDD_I2C_ADDRTYPE_7BITS, (0xA0 | (((uint16_t)(address & 0x700)) >> 8)) >> 1) !=0)
		return 1;

	if(I2C_EEPROM_MasterSendBlock(I2C0Ptr,&byte,1,LDD_I2C_NO_SEND_STOP)!=0)
		return 2;

	while(I2C_EEPROM_MasterGetBlockSentStatus(I2C0Ptr)!=TRUE);

	if(I2C_EEPROM_SelectSlaveDevice(I2C0Ptr,LDD_I2C_ADDRTYPE_7BITS, (0xA0 | (((uint16_t)(address & 0x700)) >> 8)) >> 1) !=0)
		return 3;

	if(I2C_EEPROM_MasterReceiveBlock(I2C0Ptr,data,1,LDD_I2C_SEND_STOP)!=0)
		return 5;

	while(I2C_EEPROM_MasterGetBlockReceivedStatus(I2C0Ptr)!=TRUE);

	return 0;
}

/******************************************************************************
 * END OF SOURCE FILE							      *
 *****************************************************************************/
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags