Read and write the on-board I2C EEPROM

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows you how to use the I2C bus to read and write the on-board EEPROM.
If you want to use an external I2C EEPROM, you can use I2C module #1 following this other tutorial.

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

All those functions are embedded in the IDEpro and pressing the F1 key you get all the information.

Code example


The functions used in this tutorial are the following:

Name Function Description
 void EEInit(BYTE  deviceAddr, BYTE  speed, WORD  maxEepromSize ) Initializes Flyport PRO Onboard EEPROM.

Parameters: deviceAddr, i2c address of eeprom device (use EE_ADDR_DEF to use onboard EEPROM). 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 EE_SIZE_DEF to use onboard EEPROM). Can be calculated with (eeprom model size)*1024/8.

 BYTE EESaveData( 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 EE_BYTE to write a BYTE array EE_WORD to write a WORD array EE_DWORD to write a DWORD array.
Returns: report of operation

  • EE_ERR_NO 0x00
  • EE_ERR_TIMEOUT 0x01
  • EE_ERR_NO_SPACE 0x02
  • EE_ERR_UNK_DATA 0x03
  • EE_ERR_POLLING 0x04
 BYTE EESaveString( 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

  • EE_ERR_NO 0x00
  • EE_ERR_TIMEOUT 0x01
  • EE_ERR_NO_SPACE 0x02
  • EE_ERR_UNK_DATA 0x03
  • EE_ERR_POLLING 0x04
 BYTE EELoadData ( 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 EE_BYTE to read a BYTE array EE_WORD to read a WORD array EE_DWORD to read a DWORD array.
Returns: report of operation

  • EE_ERR_NO 0x00
  • EE_ERR_TIMEOUT 0x01
  • EE_ERR_NO_SPACE 0x02
  • EE_ERR_UNK_DATA 0x03
  • EE_ERR_POLLING 0x04
 BYTE EELoadString ( 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

  • EE_ERR_NO 0x00
  • EE_ERR_TIMEOUT 0x01
  • EE_ERR_NO_SPACE 0x02
  • EE_ERR_UNK_DATA 0x03
  • EE_ERR_POLLING 0x04

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


Source code for taskFlyport.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
    char bff[100];
 
    DWORD numberTOwrite[1];
    numberTOwrite[0]=0xABCDEF;
 
    DWORD numberFROMeeprom[1];
    numberFROMeeprom[0]=0;
 
    char stringTOwrite[]="\"I'm the on-board I2C EEPROM\"";
    char stringFROMeeprom[100];
 
 
    vTaskDelay(150);	
 
    UARTWrite(1,"Flyport Wi-fi PRO is started... hello world!\r\n\r\n");
 
    EEInit( EE_ADDR_DEF, HIGH_SPEED, EE_SIZE_DEF);
    UARTWrite(1,"The on-board I2C EEPROM is initialized\r\n\r\n");
 
    UARTWrite(1,"The writing (DATA) is in progress...\r\n");
 
    if(EESaveData ( 0, numberTOwrite, 1, EE_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(EELoadData ( 0, numberFROMeeprom, 1, EE_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(EESaveString ( 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(EELoadString ( 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