diff --git a/examples/TFTPServer/CustomTFTPServer.h b/examples/TFTPServer/CustomTFTPServer.h index 54c933e..5e32689 100644 --- a/examples/TFTPServer/CustomTFTPServer.h +++ b/examples/TFTPServer/CustomTFTPServer.h @@ -16,8 +16,9 @@ */ class CustomTFTPServer: public TFTPServer { - const uint16_t AT24C128_LENGTH = 16384; - const uint8_t AT24C128_ADDRESS = 0x50; + const uint16_t AT24C128_LENGTH = 16384; + const uint8_t AT24C128_ADDRESS = 0x50; + const uint8_t AT24C128_BUF_SIZE = 32; // This is the size of the Arduino buffer public: /** @@ -46,12 +47,13 @@ class CustomTFTPServer: public TFTPServer { void writeBytes(int8_t fileno, uint16_t block, const uint8_t* data, uint16_t len) { + uint16_t offset = (block-1) * TFTP_BLOCK_SIZE; + if (fileno == 1) { // Write straight to serial port Serial.write(data, len); } else if (fileno == 2) { // Handle writing to internal EEPROM - uint16_t offset = (block-1) * TFTP_BLOCK_SIZE; for(uint16_t i=0; i TFTP_BLOCK_SIZE) len = TFTP_BLOCK_SIZE; @@ -84,14 +81,7 @@ class CustomTFTPServer: public TFTPServer { return len; } else if (fileno == 3) { // Handle reading from external i2c EEPROM - uint16_t offset = (block-1) * TFTP_BLOCK_SIZE; - uint16_t len = AT24C128_LENGTH - offset; - if (len > TFTP_BLOCK_SIZE) - len = TFTP_BLOCK_SIZE; - for(uint16_t i=0; i> 8)); // MSB - Wire.write((int)(eeaddress & 0xFF)); // LSB - Wire.write(data); - Wire.endTransmission(); - - delay(5); + void writeToI2c(uint16_t offset, const uint8_t* data, uint16_t len) + { + // Handle writing to external i2c EEPROM + for(uint16_t i=0; i= AT24C128_LENGTH) + return; + + Wire.beginTransmission(AT24C128_ADDRESS); + Wire.write((int)(eeaddress >> 8)); // MSB + Wire.write((int)(eeaddress & 0xFF)); // LSB + + // WARNING: this page write only works because the TFTP block size (512) + // is divisible by the EEPROM page size (64 bytes) + // But it must also fit within the Arduino 32 byte Wire buffer + uint16_t end = i + (AT24C128_BUF_SIZE/2); + while(i> 8)); // MSB - Wire.write((int)(eeaddress & 0xFF)); // LSB - Wire.endTransmission(); - Wire.requestFrom(deviceaddress, 1); + int16_t readFromI2c(uint16_t offset, uint8_t* data) + { + uint16_t len = AT24C128_LENGTH - offset; + if (len > TFTP_BLOCK_SIZE) + len = TFTP_BLOCK_SIZE; + + for(uint16_t i=0; i> 8)); // MSB + Wire.write((int)(eeaddress & 0xFF)); // LSB + Wire.endTransmission(); - if (Wire.available()) rdata = Wire.read(); + Wire.requestFrom(AT24C128_ADDRESS, AT24C128_BUF_SIZE); - return rdata; + uint16_t end = i + AT24C128_BUF_SIZE; + while(i