The dark side of porting Arduino sketches on Intel Galileo (part one)

….Hi folks, here we are again!

As promised in my last post, this time we start to talk about the porting procedures (and issues) from Arduino UNO/Mega ADK to Intel Galileo.

One can think the porting is painless (since Intel Galileo is born from a “collaboration” between Intel and Arduino guys), but unfortunately the truth is different. My experience tell me that’s the naked reality behind the embedded world…but this is a sad

Ok, the first thing you will notice porting a sketch from Arduino IDE to Galileo IDE is that NOT ALL Arduino libraries have been ported correctly in the Galileo environment (do you remember my old post? The String class is one of these examples…), so you could encounter a great number of “undeclared” variable or function errors. Someone told me that also TFT library doesn’t work correctly in Galileo….I’m a little sad…. 😦

But today we talk about some “working” library, in order to gain some succesful result before the “real  battles”! 😉

For example, the I2C library seems to work in the same way in Arduino environment and in the Galileo environment. The I2C is a serial communication protocol used to connect Arduino (and in general all processors) with external devices.

An exampre of therse devices are EEPROMs of 24cXX series. I used these EEPROMs for maaaaaaany years, because simply they use the I2C protocol, so they are serial (=slower, I know it)….but they use few wires to be commanded. And it isn’t a little thing when using Arduino UNO! 🙂

…The parallel EEPROMs such as 28cXX are much faster but they use too much electrical links (and Arduino UNO doesn’t have so many GPIOs!).

Right, let’s go ahead. In order to read/write from/to the 24cXX EEPROM I used a very nice library found on Arduino Playground .

Then I designed a little circuit (with Fritzing) in order to use one 24c64 (64kbytes) EEPROM and one 24c256 (256 kbytes). This is the circuit (please…use Intel Galileo instead Arduino UNO, with a pin-to-pin correspondance, and all should be work correctly! 😉 ):

EEPROM_doppia_v2_bb

Then I wrote a very simple “dummy” sketch in order to execute some basic operation on the EEPROMs.

#include <Wire.h> //Arduino I2C library...which works also on Intel Galileo!

//////LIBRARY functions taken from http://playground.arduino.cc/Code/I2CEEPROM

  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.write(rdata);
    Wire.endTransmission();
  }

  // WARNING: address is a page address, 6-bit end will wrap around
  // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddresspage >> 8)); // MSB
    Wire.write((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.write(data[c]);
    Wire.endTransmission();
  }

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.read();
    return rdata;
  }

  // maybe let's not read more than 30 or 32 bytes at a time!
  void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available()) buffer[c] = Wire.read();
  }

///////////end of I2C EEPROM library

  void setup() 
  {
    //pinMode(9, INPUT); 
    pinMode(10, OUTPUT); //used to power on/off 24C256 EEPROM
    pinMode(11, OUTPUT); //used to power on/off 24C64 EEPROM

    //power off EEPROMs
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);

    Wire.begin(); // initialise the connection
    Serial.begin(115200);

  }

  void loop() 
  {
    byte incomingCommand='F';
    int selected =-1;
     char somedata256[] = "BANK 1: ML test writing to 256K EEPROM"; // data to write
    char somedata64[] = "BANK 2: ML test writing to 64K EEPROM"; // data to write

    // see if there's incoming serial data:
    if (Serial.available() > 0) {

      // read the contetnt of the serial buffer:
      incomingCommand = Serial.read();

    if (incomingCommand == '1') { //write on Bank 1
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      selected=256;
    } 
    else if (incomingCommand == '2') { //write on Bank 2
      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
      selected=64;
    }
    else if (incomingCommand == '0') //erase the Bank 1 and 2
    {
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      selected=0;
    }
    else if (incomingCommand == '3') { //read Bank 1
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      selected=2561;
    }

   else if (incomingCommand == '4') { //read Bank 2
      digitalWrite(11, HIGH);
      digitalWrite(10, LOW);
      selected=641;
    }

  }

////depending on command received on serial terminal, execute some action on EEPROMs

    if (selected==256)
    {
      Serial.println("256K Memory writing...");
      for (int i=0;i<sizeof(somedata256);i++)
      {
        i2c_eeprom_write_byte(0x50,i,somedata256[i]);
        delay(10); 
      }
     Serial.println("256K Memory written!");
     Serial.println("256K Memory reading...");
     int addr=0; //first address
     byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
     while (b!=0) 
        {
          Serial.print((char)b); //print content to serial port
          addr++; //increase address
          b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
        }
    Serial.println(" ");
    Serial.println("256K memory read finished!");
    delay(2000);
    digitalWrite(10,LOW);
    }

    else if (selected==64)
    {
       Serial.println("64K Memory writing...");
      for (int i=0;i<sizeof(somedata64);i++)
      {
        i2c_eeprom_write_byte(0x50,i,somedata64[i]);
        delay(10); 
      }
     Serial.println("64K Memory written");
     Serial.println("64K Memory reading...");
     int addr=0; //first address
     byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
     while (b!=0) 
        {
          Serial.print((char)b); //print content to serial port
          addr++; //increase address
          b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
        }
      Serial.println(" ");
      Serial.println("64K memory read finished!");
      delay(2000);
      digitalWrite(11,LOW);
    }

    else if (selected==0)
    {
      digitalWrite(11, HIGH);
      delay(10);
      Serial.println("64K Memory erasing...");
      for (int i=0;i<100;i++)
      {
        i2c_eeprom_write_byte(0x50,i,'0');
        delay(10); 
      }
     Serial.println("64K Memory erased");
     digitalWrite(11, LOW);
     digitalWrite(10, HIGH);
     delay(10);
     Serial.println("256K Memory erasing...");
      for (int i=0;i<100;i++)
      {
        i2c_eeprom_write_byte(0x50,i,'0');
        delay(10); 
      }
     Serial.println("256K Memory erased");
     digitalWrite(10, LOW);
    }

    else if (selected==641)
    {
      Serial.println("64K Memory reading...");
     int addr=0; //first address
     byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
     while (b!=0) 
        {
          Serial.print((char)b); //print content to serial port
          addr++; //increase address
          b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
        }
      Serial.println(" ");
      Serial.println("64K memory read finished!");
      delay(2000);
      digitalWrite(11,LOW);
    }
    else if (selected==2561)
    {
       Serial.println("256K Memory reading...");
       int addr=0; //first address
       byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
       while (b!=0) 
        {
          Serial.print((char)b); //print content to serial port
          addr++; //increase address
          b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
        }
      Serial.println(" ");
      Serial.println("256K memory read finished!");
      delay(2000);
      digitalWrite(10,LOW);
    }
  }

Well, once the code will be downloaded on the Galileo, open the IDE serial monitor and use the following commands in order to read/write the EEPROMs:

  • <1+Enter> will write a fixed pattern (somedata256[]) on the 24c256
  • <2+Enter> will write a pattern (somedata64[])on the 24c64
  • <0+Enter> will erase both EEPROMs
  • <3+Enter> will read content from the 24c256 and write it on the serial monitor
  • <4+Enter> will read content from the 24c64 and write it on the serial monitor

You can see the behavior of the code and the access times looking at the ON/OFF status of the two leds present in the circuit . I know this project is a toy-example…but it can be a good simple step to start using I2C interface (and Arduino I2C library) on Intel Galileo.

Ok. This time we joked dear guys ;-), because in the next episode… we will encounter the real “dark side” of the porting from Arduino to Galileo. Cross your fingers, drink a beer and ….relax!

…Bye bye!

Advertisements