Read and write an I2C EEPROM

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to use the I2C bus to read and write an EXTERNAL EEPROM.

Introduction to I2C

I2C protocol is a 2 wires serial communication. Respect to the UART, the I2C has a Master-Slave architecture, where the Master starts all the communication requests. The Master provides the baud rate of which the most used values are 100kb/s (low speed) and 400kb/s (high speed). The I2C bus needs pull-up resistors: on Flyport it is possible to enable by software internal pull-up resistors using the IOInit() function.

How to use it

The I2C protocol has a specific sequence of operations for starting, stopping, write, read and checking the data transmission. Hereis available an external library for simple manage the read and write operation in an EEPROM, find here a tutorial to learn the import of external library.

FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the circuitry since the pinout is slightly different.

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 1x EEPROM (24LC512)
  • 2x Resistor (2,2K Ohm)
  • 1x Proto Nest Board
  • 1x Breadboard


Schematic

I2C Schematic.png


I2C Image.png

Code example


The functions used in this tutorial are the following:

Name Function Description
 void EepromInit(BYTE  deviceAddr, BYTE  speed, WORD  maxEepromSize ) This function initializes the EEPROM on the I2C bus.

Parameters: deviceAddr, i2c address of eeprom device (use EEPROM_ADDR_DEF to use EEPROM aboard the Flyport LITE). speed accepts only the following parameters to set the bus speed:

  • LOW_SPEED to set @100kHz
  • HIGH_SPEED to set @400kHz

maxEepromSize, size of eeprom (use EEPROM_SIZE_DEF to use EEPROM aboard Flyport LITE). Can be calculated with (eeprom model size)*1024/8.

 BYTE EepromSaveData( WORD  addr, void * dataToWr, WORD  dataLen, BYTE  dataType ) Writes some data on EEPROM.

Parameters: addr address were to start to write. dataToWr data array to write. dataLen data length (amount of data) to be written. dataType EEPROM_BYTE to write a BYTE array EEPROM_WORD to write a WORD array EEPROM_DWORD to write a DWORD array.
Returns: report of operation

  • EEPROM_ERR_NO 0x00
  • EEPROM_ERR_TIMEOUT 0x01
  • EEPROM_ERR_NO_SPACE 0x02
  • EEPROM_ERR_UNK_DATA 0x03
  • EEPROM_ERR_POLLING 0x04
 BYTE EepromSaveString( WORD  addr, char * strToWr, WORD  strLen ) Writes a string on EEPROM .

Parameters: addr address were to start to write. strToWr char array to be stored. strLen string length to be stored.
Returns: report of operation

  • EEPROM_ERR_NO 0x00
  • EEPROM_ERR_TIMEOUT 0x01
  • EEPROM_ERR_NO_SPACE 0x02
  • EEPROM_ERR_UNK_DATA 0x03
  • EEPROM_ERR_POLLING 0x04
 BYTE EepromLoadData ( WORD  addr, void * dataToRd, WORD  dataLen, BYTE  dataType ) Reads some data from EEPROM.

Parameters: addr address were to start to read. dataToRd data array to be filled with data. dataLen data length (amount of data) to be read. dataType EEPROM_BYTE to read a BYTE array EEPROM_WORD to read a WORD array EEPROM_DWORD to read a DWORD array.
Returns: report of operation

  • EEPROM_ERR_NO 0x00
  • EEPROM_ERR_TIMEOUT 0x01
  • EEPROM_ERR_NO_SPACE 0x02
  • EEPROM_ERR_UNK_DATA 0x03
  • EEPROM_ERR_POLLING 0x04
 BYTE EepromLoadString ( WORD  addr, void * strToRd, WORD  strLen ) Reads a string from EEPROM.

Parameters: addr address were to start to read. strToRd char array to be filled with data. strLen string length to be read.
Returns: report of operation

  • EEPROM_ERR_NO 0x00
  • EEPROM_ERR_TIMEOUT 0x01
  • EEPROM_ERR_NO_SPACE 0x02
  • EEPROM_ERR_UNK_DATA 0x03
  • EEPROM_ERR_POLLING 0x04

Attention: this function doesn't add the terminator char '\0'.


Source code for taskFlyport.c:

#include "taskFlyport.h"
#include "eeprom_helper.h"
 
void FlyportTask()
{
    char bff[100];
 
    DWORD numberTOwrite[1];
    numberTOwrite[0]=0xABCDEF;
 
    DWORD numberFROMeeprom[1];
    numberFROMeeprom[0]=0;
 
    char stringTOwrite[]="\"I'm an external I2C EEPROM\"";
    char stringFROMeeprom[100];
 
 
    vTaskDelay(150);	
 
    UARTWrite(1,"Flyport Wi-fi PRO is started... hello world!\r\n\r\n");
 
    EepromInit(0xA0, HIGH_SPEED, 65535); // (512*1024/128)-1
    UARTWrite(1,"The I2C EEPROM is initialized\r\n\r\n");
 
    UARTWrite(1,"The writing (DATA) is in progress...\r\n");
 
    if(EepromSaveData ( 0, numberTOwrite, 1, EEPROM_DWORD)==0)
         UARTWrite(1,"The writing (DATA) is completed\r\n");
    else
	 UARTWrite(1,"The writing (DATA) is uncompleted\r\n");
 
    vTaskDelay(150);	
 
    UARTWrite(1,"The reading (DATA) is in progress...\r\n");
 
    if(EepromLoadData ( 0, numberFROMeeprom, 1, EEPROM_DWORD)==0)
	 UARTWrite(1,"The reading (DATA) is completed\r\n");
    else
	 UARTWrite(1,"The reading (DATA) is uncompleted\r\n");
 
    sprintf(bff,"\r\nThe number is: 0x%04X%04X or %2.0f\r\n", (WORD)(numberFROMeeprom[0]>>16),(WORD)numberFROMeeprom[0],(double)numberFROMeeprom[0]);	
    UARTWrite(1,bff);
 
    vTaskDelay(100);		
    UARTWrite(1,"\r\n----------------\r\n");
    vTaskDelay(100);
 
    UARTWrite(1,"\r\nThe writing (STRING) is in progress...\r\n");
 
    if(EepromSaveString ( 0, stringTOwrite, strlen(stringTOwrite))==0)
	 UARTWrite(1,"The writing (STRING) is completed\r\n");
    else
	 UARTWrite(1,"The writing (STRING) is uncompleted\r\n");
 
    vTaskDelay(150);	
 
    UARTWrite(1,"The reading (STRING) is in progress...\r\n");
 
    if(EepromLoadString ( 0, stringFROMeeprom, strlen(stringTOwrite))==0)
         UARTWrite(1,"The reading (STRING) is completed\r\n");
    else
	 UARTWrite(1,"The reading (STRING) is uncompleted\r\n");
 
    stringFROMeeprom[strlen(stringTOwrite)]='\0';
 
    sprintf(bff,"\r\nThe string is: %s\r\n",stringFROMeeprom);	
    UARTWrite(1,bff);
 
    while(1)
    {
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox