[Description]

Location = "Seiko\2430";
Info = " Module for programming 3-Wire Serial EEPROMs.",
       " Memory types: Seiko 2430.",
       " --------------------------------------------------------------",
    "    Bus: MicroWire. Featutes: Seiko",
    "    Size: 8 bytes",
       " --------------------------------------------------------------",
    "     Author: fomalex@biz.by (C) 2007";
Buffer1 = "EEPROM",16;
Picture = "S2430.bmp";
VCC = 8;
GND = 5;
Voltage = 5.0;

[Menu]

Read    = "&Read",ReadButton,"Read chip",F2;
Div10   = "-";
Write   = "&Write",WriteButton,"Write chip",F5;
WriteChanges= "Write &changes",WriteChangesButton,"Write changes",F6;
Div1    = "-";
Compare = "&Vertify",CheckButton,"Compare with buffer",F3;
Div2    = "-";
Info    = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]


// ==================================================
//                  Host section
// ==================================================

[Host]

define  END_ADR 8

Proc Read {
    
    var Address,Error,Value;
    print("");
    print("  Reading chip...");
    Run(Read);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    while(Address != END_ADR)
    {
            Value = USB;
            FBUF[Address] = Value;
            Address++;
            PBPos++;
    }
    if(Error = 0x8000)
    {
        print("  Reading error: Time out");
    }
    if(Error = 0)
    {
        if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF); }
        print("   Reading is done. %i bytes were read.",Address);
    }
    if(Error = 0xff)
    {
        print("  Reading error. Chip not response. Address %i",Address);
    }
}


Proc Write  {

    var Address,Error,Value;
    print("");
    print("  Writing chip...");
    Run(Write);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    while(Address != END_ADR)
    {
        Value = FBUF[Address];USB = Value;Address++;
        Error = USB;
        if(Error != 0) { break; }
        PBPos++;
    }
    if(Error = 0x8000)
    {
        print("  Writing error: Time out");
    }
    if(Error = 0xFF)
    {
        print("  Wrintig error. Chip not response. Address %i", Address);
    }
    if(Error = 0)
    {
        Error = USB;
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return (0x7FFFFFFF); }
        print("   Writing is done. %i bytes were write.",Address);
    }
}

Proc Compare {

    var Address,Error,Value,Value2,Mismatch;
    print("");
    print("  Verifying chip...");
    Run(Read);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    Mismatch = 0;
    while(Address != END_ADR)
    {
            Value = USB;
            Value2 = FBUF[Address];
            if(Value != Value2)
            {
                Mismatch++;
                FSEL[Address] = 0;    
            }
            else
            {
                FSEL[Address] = 1;
            }
            Address++;
            PBPos++;
    }
    Address = Address;
    if(Error = 0x8000)
    {
        print("  Verifying error: Time out");
    }
    if(Error = 0)
    {
        if(ForceCloseAfterVerify != 0) { return(0x7FFFFFFF); }
        print("  Verifying is done. %i bytes were read. Number of mismatch: %i",Address,Mismatch);
    }
    if(Error = 0xFF)
    {
        print("  Verifying error. Chip not response. Address %i", Address);
    }
}

Proc WriteChanges
{
    var Address,Temp,Value,Mismatch,Error,Writed;
    Address = 0;Temp = 0;
    print("");
    while(Address != END_ADR)
    {
        Value = FSEL[Address];
        if(Value = 0) {Temp++;}
        Address++;    
    }
    if(Temp = 0)
    {        
        print("   Any mismatch is not found. Writing is canceled.");
        return;
    }
    else
    {
        print("   Writing changes...");
        PBpos = 0; PBMax = END_ADR;Address = 0;Error = 0;
        Writed = 0;
        while(Address != END_ADR)
        {
            Mismatch = FSEL[Address];
            Value = FBUF[Address];                    
            if(Mismatch = 0)
            {
                Run(WriteWord);
                USB = Address;
                USB = Value;
                Error = USB;
                if(Error != 0) { break;}
                Writed++;
            }
            Address++;
            PBPos++;        
        }
    }
    if(Error = 0x8000)
    {
        print("  Writing error: Time out");
    }
    if(Error = 0xFF)
    {
        print("  Wrintig error. Chip not response. Address %i", Address);
    }
    if(Error = 0)
    {
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }
        print("   Writing is done. %i bytes were write.",Writed);
    }
}

Proc Info
{
  print("");
  print("  Module for programming 3-Wire eeprom.");
  print("  Memory types Seiko S2430.");
  print("");
  print("  Copyright (C) 2006 fomalex@biz.by");
}

// ========================================================
//                    Device section
// ========================================================

[Device]

define      CE     POUT.0
define      SK     POUT.1
define      DI     POUT.2
define      DO     POUT.3
define      DOIN   PIN.3
define      RECALL POUT.5
define      STORE  POUT.6

define      END_ADR    8

define      WRDS        0b10000000
define      STO         0b10000001
define      WRITEINS    0b10000011
define      WREN        0b10000100
define      RCL         0b10000101
define      READINS     0b10000111

// Variables

var Address,Error,Temp,DataWord,Counter,DataL;

//***********************************************************
//*       Read                                             **
//***********************************************************
Proc Read {
 
    Error = 0;Address = 0;

    POUT = 0b01101100;                  // init
                                        // CE=0, SK=1, DI=1, DO=1, STORE=1
                                        // RECALL = 1;

    CE = 1;
    DataWord = RCL;
    ShiftOutByte;                       // Copy EEPROM to RAM
    CE = 0;


    while(Address != END_ADR)
    {
        CE = 1;                         // Select chip
        DataWord = Address << 4;        // composing address field
        DataWord = DataWord | READINS;  // and READ instruction
        ShiftOutByte;                   // Shift result into chip
        DO = 1;DI = 1;                  // release bus
        ShiftInByte;                    // get low data byte from chip
        USB = DataWord;                // send to host
        CE = 0;                         // release chip
        Address++;                      // next address
    }
}

//************************************************************
//*      Write                                              **
//************************************************************

Proc Write
{
    Error = 0;Address = 0;

    POUT = 0b01101100;                  // init
                                        // CE=0, SK=0, DI=1, DO=1, STORE=1
                                        // RECALL = 1;

    CE = 1;
    DataWord = RCL;                     // Copy EEPROM to RAM
    ShiftOutByte;
    CE = 0;

    CE = 1;
    DataWord = WREN;                    // Send to chip WRITE ENABLE command 
    ShiftOutByte;                 
    CE = 0;

    // 

    CE = 1;
    while(Address != END_ADR)
    {
        DataL = USB;                   // Get data word from host
        CE = 1;
        DataWord = Address << 4;        // composing address field
        DataWord = DataWord | WRITEINS; // and WRITE instruction
        ShiftOutByte;                   // Shift result into chip
        DataWord = DataL;               // Composing Data Word
        ShiftOutByte;                   // Shift result into chip
        CE = 0;

        USB = 0;
        Address++;
    }
    CE = 1;
    DataWord = STO;                 // copy RAM to EEPROM
    ShiftOutByte;
    CE = 0;
    //   wait until chip ready
    Counter = 5000;
    while(Counter != 0)
    {
      Counter--;
    }
    USB = 0;
}


Proc ShiftOutByte
{
    Counter = 8;                    // bit counter
    while(Counter != 0)
    {
        DI = DataWord.7;            // msb on the input data pin
        SK = 1; SK = 0;             // rising edge SK
        DataWord = DataWord << 1;   // next bit
        Counter--;          
    }    
}

Proc ShiftInByte
{
    Counter = 8;                   // bit counter
    DataWord = 0;
    while(Counter != 0)
    {
        DataWord = DataWord << 1;   // next bit
        SK = 0;                             
        DataWord.0 = DOIN;  // next bit from bus into lowest bit DataWord
        SK = 1;
        Counter--;
    }
    SK = 0;
}

Proc WriteWord
{

    POUT = 0b01101100;                  // init
                                        // CE=0, SK=0, DI=1, DO=1, STORE=1
                                        // RECALL = 1;

    Address = USB;
    DataL = USB;                        // Get data word from host

    CE = 1;
    DataWord = RCL;                     // Copy EEPROM to RAM
    ShiftOutByte;
    CE = 0;

    CE = 1;
    DataWord = WREN;                    // Send to chip WRITE ENABLE command 
    ShiftOutByte;                 
    CE = 0;


        CE = 1;
        DataWord = Address << 4;        // composing address field
		DataWord = DataWord & 0x70;
        DataWord = DataWord | WRITEINS; // and WRITE instruction
        ShiftOutByte;                   // Shift result into chip
        DataWord = DataL;               // Composing Data Word
        ShiftOutByte;                   // Shift result into chip
        CE = 0;

    CE = 1;
    DataWord = STO;                 // copy RAM to EEPROM
    ShiftOutByte;
    CE = 0;
    //   wait until chip ready
    Delay(10000);
    USB = 0;

}