/* ------------------------------------------------------------
Microchip 24AAXX/24LCXX/24FCXX I2C EEPROM : XJEase device file
(c)2002-20010 XJTAG Limited

The test reads the first address, modifies the data and
then writes back, to check that the chip is present and
working and to check the SDA and SCL lines for opens.

The original data is wrttten back into the PROM at the
end of the test so the test can be used on a programmed device.

Disclaimer: XJTAG makes no guarantees whatsoever
about this code.  You use it at your own risk ...
This code requires XJTAG version 1.4 or later.

This file tests Non-JTAG EEPROM using Test()(INT result)

The IIC_ADDRESS_BYTES variable needs to be set correctly
for the Test function to pass.
------------------------------------------------------ */
DEVICE NAME := "24XXXX EEPROM"
  COMPAT_VERSION := 1;
  DESCRIPTION := "I2C EEPROM";

  PINS
    SDA := 5;
    SCL := 6;
    WP  := 7;
  END;

  DISABLE DEVICE
    SDA := Z;
  END;

  TEST COVERAGE
    SDA := OPEN LO HI;
    SCL := OPEN LO HI;
  END;

  // Additional files
  FILES
    "IIC.xje";
  END;
END;

//------------------------------------------------------
// Constants
//------------------------------------------------------

INT IIC_ADDRESS         := 0xA0;
// For devices of 16Kb or less
INT IIC_ADDRESS_BYTES   := 1;
// For devices of 32Kb or more
//INT IIC_ADDRESS_BYTES   := 2;

INT IIC_WRITE_PAGE_SIZE := 1;
INT IIC_READ_PAGE_SIZE  := 1;

CONST INT DEBUG               := FALSE;

//------------------------------------------------------
// Returns FALSE for success, TRUE for failure.
Test()(INT result)
//------------------------------------------------------
  INT data;

  result := RESULT_PASS;

  PRINT("Testing IIC EEPROM (", DEVICE_REF,")\n");

  IIC_CheckPresent()(result);
  IF result THEN
    PRINT("Device (",DEVICE_REF,") failed to acknowledge IIC address 0x",HEX(IIC_ADDRESS),".\n");
    RETURN;
  END;

  IIC_Read(0, 1)(data, result);
  IF result THEN
    PRINT("Failed to read device (",DEVICE_REF,").\n");
    RETURN;
  END;

  IF DEBUG THEN PRINT("Initial Data := 0x",HEX(data),"\n"); END;

  IF data = 0xFF THEN
    SET WP := 0;
    IIC_Write(0, 1, 0x55)(result);
    IF result THEN
      PRINT("Failed to write to device (",DEVICE_REF,").\n");
      result := RESULT_FAIL;
      RETURN;
    END;
    IIC_Read(0, 1)(data, result);
    IF result THEN
      PRINT("Failed to read device (",DEVICE_REF,").\n");
      result := RESULT_FAIL;
      RETURN;
    END;
    IF data != 0x55 THEN
      PRINT("Failed to modify data in device (",DEVICE_REF,").\n");
      IF DEBUG THEN
        PRINT("    Data := 0x",HEX(data),"\n");
        PRINT("Expected := 0x",HEX(0x55),"\n");
      END;
      result := RESULT_FAIL;
      RETURN;
    END;
    IIC_Write(0, 1, 0xFF)(result);
    SET WP := 1;
  ELSE
    PRINT("Read data from PROM.\n");
    IF DEBUG THEN
      PRINT("Data := 0x",HEX(data),"\n");
    END;
  END;
  
END;

/* // The following code can be used to setup and program one or more PROMs in one circuit 

//------------------------------------------------------
SelectFile()(STRING fileName)
// Edit board and device references as required for each setup
//------------------------------------------------------  
  SWITCH BOARD_NAME
    CASE "Board_1"
      SWITCH DEVICE_REF
        CASE "PROM_1"
          IIC_ADDRESS         := 0xA0;
          IIC_ADDRESS_BYTES   := 2;
          IIC_WRITE_PAGE_SIZE := 128;
          IIC_READ_PAGE_SIZE  := 128;
          fileName := "Prom1.bin";
        END;
        CASE "PROM_2"
          IIC_ADDRESS         := 0xA2;
          IIC_ADDRESS_BYTES   := 1;
          IIC_WRITE_PAGE_SIZE := 32;
          IIC_READ_PAGE_SIZE  := 32;
          fileName := "Prom2.bin";
        END;
      END;
    END;
  END;
END;

//------------------------------------------------------
Program()(INT Result)
//------------------------------------------------------
  FILE file;
  STRING filename;
  
  SelectFile()(filename);
  PRINT("\nProgramming EEPROM (", DEVICE_REF, ") with ",filename,"\n");
  FOPEN(filename,"r")(file);
  IF WRITEABLE(WP) THEN
    SET WP := 0;
  END;
  IIC_Write_Bin_File(file,0,0,0xFFFFFFF)(Result);
  IF WRITEABLE(WP) THEN
    SET WP := I;
  END;
  IF Result THEN
    PRINT("Programming FAILED.\n");
  END;
END;

//------------------------------------------------------
Verify()(INT Result)
//------------------------------------------------------
  STRING filename;
  SelectFile()(filename); 
  PRINT("\nVerifing EEPROM (", DEVICE_REF, ") against ",filename,"\n");
  IF WRITEABLE(WP) THEN
    SET WP := 0;
  END;
  IIC_Verify_Bin_File(filename)(Result);
  IF WRITEABLE(WP) THEN
    SET WP := 0;
  END;
END;

// */