i2c resource busy

karan arora
Attachment: Screenshot.png (147.84 KB)
we are unable to read/write on the eeprom ,device is showing busy or
resource busy . This problem is occured again and again , even after device
restarted.

kindly suggest how to use the i2c inbuilt image.

#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "24cXX.h"

#define usage_if(a) do { do_usage_if( a , __LINE__); } while(0);
void do_usage_if(int b, int line)
{
  const static char *eeprog_usage = 
    "I2C-24C08(256 bytes) Read/Write Program, ONLY FOR TEST!\n"
    "I am lal embedded eng.\n";
  if(!b)
    return;
  fprintf(stderr, "%s\n[line %d]\n", eeprog_usage, line);
  exit(1);
}


#define die_if(a, msg) do { do_die_if( a , msg, __LINE__); } while(0);
void do_die_if(int b, char* msg, int line)
{
  if(!b)
    return;
  fprintf(stderr, "Error at line %d: %s\n", line, msg);
  fprintf(stderr, "  sysmsg: %s\n", strerror(errno));
  exit(1);
}


static int read_from_eeprom(struct eeprom *e, int addr, int size)
{
  int ch, i;
  for(i = 0; i < size; ++i, ++addr)
  {
    die_if((ch = eeprom_read_byte(e, addr)) < 0, "read error");
    if( (i % 16) == 0 ) 
      printf("\n %.4x|  ", addr);
    else if( (i % 8) == 0 ) 
      printf("  ");
    printf("%.2x ", ch);
    fflush(stdout);
  }
  fprintf(stderr, "\n\n");
  return 0;
}

static int write_to_eeprom(struct eeprom *e, int addr)
{
  int i;
  for(i=0, addr=0; i<256; i++, addr++)
  {
    if( (i % 16) == 0 ) 
      printf("\n %.4x|  ", addr);
    else if( (i % 8) == 0 ) 
      printf("  ");
    printf("%.2x ", i);
    fflush(stdout);
    die_if(eeprom_write_byte(e, addr, i), "write error");
  }
  fprintf(stderr, "\n\n");
  return 0;
}

int main(int argc, char** argv)
{
  struct eeprom e;
  int op;

  op = 0;

  usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0');
  op = argv[1][1];

  fprintf(stderr, "Open /dev/mi2c-0 with 8bit mode\n");
  die_if(eeprom_open("/dev/mi2c", 0x50, EEPROM_TYPE_8BIT_ADDR, &e) < 0, 
      "unable to open eeprom device file "
      "(check that the file exists and that it's readable)");
  switch(op)
  {
  case 'r':
    fprintf(stderr, "  Reading 256 bytes from 0x0\n");
    read_from_eeprom(&e, 0, 256);
    break;
  case 'w':
    fprintf(stderr, "  Writing 0x00-0xff into 24C08 \n");
    write_to_eeprom(&e, 0);
    break;
  default:
    usage_if(1);
    exit(1);
  }
  eeprom_close(&e);

  return 0;
}

kangear
My advice is that you try to use stock images(no modify) at first.