[Description]

Location = "FLASH\FLASH Detector";
Info = " Copyright (C) ETL˙2006;
Buffer1 = "FLASH",32768;
UART = 1;
Voltage = 5.0;
Adapter = 1;

[Menu]

ReadSignature = "&ReadSignature",ChipInfoButton,"Detect FLASH";

[Interface]

ComboBox(DetectType,"Detect algorithm",10,10,200,"JEDEC low voltage for 28-pin chip",
                                                 "JEDEC low voltage for 32-pin chip",   
                                                 "High voltage for 28-pin chip",
                                                 "High voltage for 32-pin chip",1);

[Host]


Proc ReadSignature 
{
    var MC,DC,Error,Signature;
    print("");
    print("   Auto detect.");
    if(DetectType < 2)
    {
        print("\; Low voltage algorithm");
        Run(JEDECDetect);
        if(DetectType = 0) { USB = 2; }
        else               { USB = 1; }
    }
    else
    {
        print("\; High voltage algorithm");
        Run(DetectA9High);
        if(DetectType = 2) { USB = 2; }
        else               { USB = 1; }
    }
    MC = USB;
    DC = USB;
    Signature = MC << 8;
    Signature = Signature | DC;
    Error = FindSignature(Signature);
    if(Error = 0)
    {
        print("");
        print("   Next signature code was readed: %b%b. But not found in signature database.",MC,DC);      
    }
    else
    {
        Return(Signature);
    }
}


[Device]

ereg    ADRL,   0,1
ereg    ADRH,   1,1
ereg    CTRL,   2,1
ereg    DATAIN, 3,0    
ereg    DATAOUT,4,1
ereg    PWRCTRL,5,1

define  CS_AND_OE_LOW       0b11010111
define  CS_AND_OE_HIGH      0b00101000

define  OE  3
define  WE  4
define  CS  5
 
var Value,Address,AddressH,Temp,JEDECByte,Control;


Proc JEDECDetect
{
    EP = 0x08;
    PWRCTRL = 3;
    Temp = USB;
    PWRCTRL = Temp;         // POWER ON
    Control = 0b11111110;   // Chip disable, A16 = 0;
    CTRL = Control;

    Delay(50000);

    //--------- Three-Byte Sequence for Software ID Entry

    JEDECByte = 0x90;
    JEDECSequence;

    Delay(10000);

    //--------- Read ID Bytes

    DATAOUT = 255;                          // Release bus
    ADRH = 0;ADRL = 0;                      // Address = 0
    Control = Control & CS_AND_OE_LOW;      // Enable chip for read
    CTRL = Control;                         
    Value = DATAIN;USB = Value;             // Manufacturer code
    ADRL = 1;
    Value = DATAIN;USB = Value;             // Device code
    CTRL = 0b11111110;                      // Disable chip
    PWRCTRL = 0b00000011;                   // Power OFF
}

Proc WritePulse
{
    Control.WE = 0;CTRL = Control;
    Control.WE = 1;CTRL = Control;
}

Proc JEDECSequence
{
    Control.CS = 0;CTRL = Control;              // Select chip for write
    ADRH = 0x55;ADRL = 0x55;DATAOUT = 0xAA;     // Address = 0x5555, Data = 0xAA
    WritePulse;                                 // Write pulse
    ADRH = 0x2A;ADRL = 0xAA;DATAOUT = 0x55;
    WritePulse;
    ADRH = 0x55;ADRL = 0x55;DATAOUT = JEDECByte;
    WritePulse;
    //Control.DE = 1;CTRL = Control;
    Control.CS = 1;CTRL = Control;
}

Proc DetectA9High
{
    PWRCTRL = 3;
    EP = 0x08;
    Value = USB;
    if(Value = 2)
    {
        //------ 28-pin package
        Control = 255;
        CTRL = 0b11111111;    
        PWRCTRL = 0b00001010;
    }
    else
    {
        //------ 32-pin package
        Control = 0b11111000;
        CTRL = 0b11111000;
        PWRCTRL = 0b00001001;
    }
    Delay(1000);
    DATAOUT = 255;
    ADRH = 0;ADRL = 0;
    Control = Control & CS_AND_OE_LOW;
    CTRL = Control;
    Value = DATAIN;
    USB = Value;
    ADRL = 1;
    Value = DATAIN;
    USB = Value;
    CTRL = 255;
    PWRCTRL = 0x03;
}


