Hello, I am implementing a client in python to send and read data from an EEPROM, however, I am having trouble synchronizing.

The EEPROM is SST28SF040A, datasheet: SST28SF040A.pdf.

My code works when I call the functions one by one, but here's the actual code:
Code:
/* Teensy++ 2 / Teensyduino
** EEPROM SST28SF040A Reader/Writer
** 4 Mbit (512 Kbit x 8)
** 512 Kylobytes total (8 sectors of 64 Kylobytes) */

#include <stdio.h>

#define DATA_DDR DDRC
#define DATA_PORT PORTC
#define DATA_PIN PINC

#define COL_ADDR_DDR DDRF
#define COL_ADDR_PORT PORTF
#define COL_ADDR_PIN PINF

#define ROW_ADDR_DDR DDRB
#define ROW_ADDR_PORT PORTB
#define ROW_ADDR_PIN PINB

#define SEC_ADDR_DDR DDRD
#define SEC_ADDR_PORT PORTD
#define SEC_ADDR_PIN PIND

#define CHIP_SIZE 0x80000 //bytes

const byte ledPin = 6;
const byte oePin = 8;
const byte cePin = 9;
const byte wePin = 18;

byte command;

enum {REBOOT = 0, READ, WRITE, ERASE};

void setup() {
	Serial.begin(115200);

        pinMode(oePin, OUTPUT);
        pinMode(cePin, OUTPUT);
        pinMode(wePin, OUTPUT);

         // Address must be outputs
        COL_ADDR_DDR = 0xFF;
        ROW_ADDR_DDR = 0xFF;
        SEC_ADDR_DDR = 0xFF;

   	digitalWrite(wePin, HIGH);
	digitalWrite(cePin, HIGH);
	digitalWrite(oePin, HIGH);
}

// Unprotect the chip to allow certain operations
void unprotect() {
	digitalWrite(wePin, HIGH); digitalWrite(cePin, LOW);
	digitalWrite(oePin, LOW); readFrom(0x1823); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x1820); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x1822); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x0418); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x041B); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x0419); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x041A); digitalWrite(oePin, HIGH);
}

// Protect the chip to prevent certain operations
void protect() {
	digitalWrite(wePin, HIGH); digitalWrite(cePin, LOW);
	digitalWrite(oePin, LOW); readFrom(0x1823); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x1820); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x1822); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x0418); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x041B); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x0419); digitalWrite(oePin, HIGH);
	digitalWrite(oePin, LOW); readFrom(0x040A); digitalWrite(oePin, HIGH);
}

// Configure the address on correct pins
void setAddress(unsigned long address) {
	COL_ADDR_PORT = address & 0xFF;
	ROW_ADDR_PORT = (address >> 8) & 0xFF;
	SEC_ADDR_PORT = address >> 16;
}

// Turn on the Teensy ++ 2 led
void ledOn(int ms) {
    digitalWrite(ledPin, HIGH);
    delay(ms);
}

// Turn off the Teensy ++ 2 led
void ledOff(int ms) {
    digitalWrite(ledPin, LOW);
    delay(ms);
}

// Erase the chip, the chip must be unprotected
void chipErase() {
	DATA_DDR = 0xFF;

	digitalWrite(cePin, LOW);
	digitalWrite(oePin, HIGH);
	digitalWrite(wePin, LOW);

	DATA_PORT = 0x30;

	digitalWrite(wePin, HIGH);
	digitalWrite(cePin, HIGH);
	digitalWrite(oePin, LOW);

	digitalWrite(oePin, HIGH);
	digitalWrite(cePin, LOW);
	digitalWrite(wePin, LOW);

	DATA_PORT = 0x30;

	digitalWrite(cePin, HIGH);
	digitalWrite(oePin, LOW);
	digitalWrite(wePin, HIGH);

	delayMicroseconds(1);
}

// Read a byte from the address
byte readFrom(unsigned long rAddress) {
	byte data = 0;
	DATA_DDR = 0x00; // Data must be input

	digitalWrite(wePin, HIGH);
	digitalWrite(cePin, LOW);
	digitalWrite(oePin, LOW);

	setAddress(rAddress);
	data = DATA_PIN;

	digitalWrite(wePin, LOW);
	digitalWrite(cePin, HIGH);
	digitalWrite(oePin, HIGH);

	return data;
}

// Write a byte to the address
void writeTo(unsigned long wAddress, byte data) {
	byte check = 0;
	
	DATA_DDR = 0xFF; // Data must be output

	digitalWrite(cePin, LOW);
	digitalWrite(oePin, HIGH);
	digitalWrite(wePin, LOW);

	DATA_PORT = 0x10; // Command to write

	digitalWrite(cePin, HIGH);
	digitalWrite(oePin, LOW);
	digitalWrite(wePin, HIGH);

	setAddress(wAddress);

	digitalWrite(cePin, LOW);
	digitalWrite(oePin, HIGH);
	digitalWrite(wePin, LOW);

	DATA_PORT = data;
	delayMicroseconds(1);

	/* Data check - Can't write 2 times at the same address */
	while (check != data) {
		check = readFrom(wAddress);
	}
}

// Do writes to an address range, the data is received from Serial
void doWrite(unsigned long int writeAddress, unsigned long int endAddress) {
    digitalWrite(ledPin, HIGH);
	if (endAddress >= CHIP_SIZE) {
		Serial.print("ERROR: Cannot write beyond 0x7FFFF.");
	} else {
		for (writeAddress = 0; writeAddress < endAddress; writeAddress++) {
			byte data = Serial.read();
			writeTo(writeAddress, data);
            Serial.write(">"); // feedback for write finish
		}
	}
    digitalWrite(ledPin, LOW);
}

void loop() {
	// While nothing is received, the led stay blinking
    while (!Serial) {
        ledOn(500); ledOff(500);
        continue;
    } 

    Serial.flush(); // Flush any previous data

    command = Serial.read();

    switch (command) {
        case REBOOT:
            _reboot_Teensyduino_(); // Builtin command
            break;

        case READ:
        	// Read the whole chip and print values in serial like an hexadecimal editor
        	char fAddr[5]; // formated address
        	char fData[2]; // formated data

        	Serial.println("_______________Data readed from SST28SF040A______________");
        	Serial.println("        [00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F]");

        	for (unsigned long int rAddr = 0; rAddr < CHIP_SIZE; rAddr += 0xF) {
        		sprintf(fAddr, "%05lX", rAddr); // pad formated address uint32_t with zeroes
        		Serial.print("[");
        		Serial.print(fAddr);
        		Serial.print("] "); // Space at end

        		for (byte count = 0; count <= 0xF; count++){
        			byte data = readFrom(rAddr);
        			sprintf(fData, "%02X", data);
        			if (count == 0){
        				Serial.print("[");
        			}

        			Serial.print(fData);
        			Serial.print(" ");

        			if (count == 0xF) {
        				Serial.println("]");
        			}
        		}
            }
            break;

        case WRITE:
        	unprotect();
        	doWrite(0x0, 0x7FFFF); // Writes 0x80000 bytes coming from serial
        	protect();
        	break;

        case ERASE:
        	unprotect();
        	chipErase();
        	protect();
    }
}
And here's the python client code (Uses PySerial)
Code:
import sys
from os.path import getsize

import serial
from serial.tools import list_ports
from serial.serialutil import SerialException

class TeensySerial:
    def __init__(self, baudrate):
        self.teensy_port = self.getTeensyPort()
        self.teensy = serial.Serial(self.teensy_port[0], baudrate)

    def getTeensyPort(self):
        """Automatically discover the COM port where teensy is."""
        ports_avaiable = list(list_ports.comports())
        teensy_port = tuple()
        for port in ports_avaiable:
            if port[1].startswith("Teensy"):
                teensy_port = port
        if teensy_port:
            return teensy_port

    def send(self, data):
        """Send data to EEPROM."""
        self.teensy.write(data)

    def receive(self):
        """Receive data from EEPROM."""
        return self.teensy.read()

    def finish(self):
        if self.teensy.isOpen():
            print "Connection with %s was closed." % self.teensy_port[1]
            self.teensy.close()
            sys.exit()

    def writeFile(self, romFile):
        """Write the rom file to EEPROM"""
        try:
            rom = open(romFile, "rb")
            rom_size = getsize(rom.name)
        except IOError, error:
            print error
            sys.exit()

        if (rom_size > 524288):
            print "The file does not fit on the chip. Aborting..."
        else:
            for x in range(rom_size):
                data = rom.read(1)
                self.send(data)
                print self.receive()

def help():
    print "Help for Mega Monkey:\n" \
    "\t-rt or --restart: Restart Teensy.\n" \
    "\t-ra or --read-analisys: Read the EEPROM and write a information file.\n" \
    "\t-e or --erase: Completely erase the EEPROM.\n" \
    "\t-w or write: Write the EEPROM with contents of a file."

if __name__ == "__main__":
    print "Welcome to Mega Monkey!"
    try:
        teensy = TeensySerial(115200)
        print "Connected to: %s" % teensy.teensy_port[1]
    except TypeError:
        print "Cannot find a Teensy board connected..."
        sys.exit();

    if len(sys.argv) == 2:
        if sys.argv[1] == "--restart" or sys.argv[1] == "-rt":
            teensy.send(chr(0))
            print "%s was restarted." % teensy.teensy_port[1]
            teensy.finish()

        elif sys.argv[1] == "--read-analisys" or sys.argv[1] == "-ra":
            teensy.finish()

        elif sys.argv[1] == "--erase" or sys.argv[1] == "-e":
            teensy.send(chr(4))
            print "The EEPROM was erased."
            teensy.finish()

        else:
            help()
            teensy.finish()

    elif len(sys.argv) == 3:
        if sys.argv[1] == "--write" or sys.argv[1] == "-w":
            teensy.send(chr(3))
            teensy.writeFile(sys.argv[2])
            teensy.finish()

        else:
            help()
            teensy.finish()

    else:
        help()
        teensy.finish()
So, the important code snippets for that matter are these:

Teensy:
Code:
// Do writes to an address range, the data is received from Serial
void doWrite(unsigned long int writeAddress, unsigned long int endAddress) {
    digitalWrite(ledPin, HIGH);
	if (endAddress >= CHIP_SIZE) {
		Serial.print("ERROR: Cannot write beyond 0x7FFFF.");
	} else {
		for (writeAddress = 0; writeAddress < endAddress; writeAddress++) {
			byte data = Serial.read(); // Read data coming from client
			writeTo(writeAddress, data); // My function: Writes a byte to an EEPROM address
                        Serial.write(">"); // feedback for write finish
		}
	}
    digitalWrite(ledPin, LOW);
}
Python:
Code:
    def writeFile(self, romFile):
        """Write the rom file to EEPROM"""
        try:
            rom = open(romFile, "rb")
            rom_size = getsize(rom.name)
        except IOError, error:
            print error
            sys.exit()

        if (rom_size > 524288):
            print "This file does not fit on the chip. Aborting..."
        else:
            for x in range(rom_size):
                data = rom.read(1)
                self.send(data) // Send 1 byte readed from file
                print self.receive() // Check if the doWrite returns the ">" feedback
Don't know why (obvious, I'm asking ) but the write stops at random point without finish all the write and nothing is writed to EEPROM. I erase the chip after all the tries... So I thinking it's a problem with timing ou data format.

I don't understand completely the concept of HID, but you think I can achieve more easily the objective using a project like this: https://github.com/ihowson/Teensy-Raw-HID-in-Python

Project Image:
Click image for larger version. 

Name:	DSCN0163.jpg 
Views:	136 
Size:	72.2 KB 
ID:	1617

Thank you in advance.