write_eeprom.c

#include <asm/io.h>
#include <stdio.h>


delay ()
{
  int i;
  for (i = 0; i < 3000; ++i)
    {
      i++;
      i--;
    }
}

int
get_d ()
{
  int q;

  delay ();
  q = (inb (0x379) & 0x80) ? 0 : 1;
//printf("R     D %d\n",q);
  return q;
}

set_cd (int c, int d)
{
  int o = 0;

  if (c)
    o |= 1;
  if (!d)
    o |= 4;

//printf("W C %d D %d\n",o&1,(o& 4)?0:1);

  outb (o, 0x378);
  delay ();
}


start ()
{
  set_cd (1, 1);
  set_cd (1, 0);
  set_cd (0, 0);
}


send_bit (int i)
{
//printf("Sending %d",i?1:0);
  set_cd (0, i);
  set_cd (1, i);
  set_cd (0, i);
}

stop ()
{
  set_cd (0, 0);
  set_cd (1, 0);
  set_cd (1, 1);
}

int
read_bit ()
{
  int r;
  set_cd (0, 1);
  set_cd (1, 1);
  r = get_d ();
  set_cd (0, 1);

  return r;
}


int
send_7bits (int b)
{
  int c = 0x40;

  while (c)
    {
      send_bit (c & b);
      c >>= 1;
    }

  return read_bit ();
}

int
send_byte (int b)
{
  int c = 0x80;

  while (c)
    {
      send_bit (c & b);
      c >>= 1;
    }

  return read_bit ();
}

int
read_byte ()
{
  int i;
  int r = 0;
  for (i = 0; i < 8; ++i)
    {
      r <<= 1;
      r |= read_bit ();
    }

  read_bit ();
  return r;
}


int
read_address (int u, int a)
{
  int r;
  start ();
  send_byte (0xa0 | (u << 1) | 0);
  send_byte (a);
  start ();
  send_byte (0xa0 | (u << 1) | 1);
  r = read_byte ();
  stop ();
  usleep (1000);

  return r;
}

write_address (int u, int a, int d)
{
  int r;
  start ();
  send_byte (0xa0 | (u << 1) | 0);
  send_byte (a);
  send_byte (d);
  stop ();
  sleep (1);

  return r;
}


int
read_word (int u, int a)
{
  int r;

  r = read_address (u, a);
  r |= (read_address (u, a + 1) << 8);

  return r;
}

void
write_word (int u, int a, int w)
{
  write_address (u, a, w & 0xff);
  write_address (u, a + 1, w >> 8);
}

main ()
{
  int fcc;
  int cc;
  int dc;
  int sn;

  iopl (3);


  set_cd (1, 1);
  usleep (200000);

  start ();

  fcc = read_word (0, 0x60);
  printf ("fcc = %d mAh\n", fcc);

  cc = read_word (0, 0xe);
  printf ("cc = %d cycles\n", cc);

  dc = read_word (0, 0x10);
  printf ("dcc = %d mAh\n", dc);

  sn = read_word (0, 0x18);
  printf ("sn = %d\n", sn);

  fcc = 1500;
  write_word (0, 0x60, fcc);

  cc = 0;
  write_word (0, 0xe, cc);

  dc = 1500;
  write_word (0, 0x10, dc);

  sn = 31415;
  write_word (0, 0x18, sn);


  fcc = read_word (0, 0x60);
  printf ("fcc = %d mAh\n", fcc);

  cc = read_word (0, 0xe);
  printf ("cc = %d cycles\n", cc);

  dc = read_word (0, 0x10);
  printf ("dcc = %d mAh\n", dc);

  sn = read_word (0, 0x18);
  printf ("sn = %d\n", sn);

}