diff --git a/main.c b/main.c index 8f3d154..f6005e7 100644 --- a/main.c +++ b/main.c @@ -173,6 +173,9 @@ static bool save_edid(void) return ram_eeprom_blockprog(edid, edid_length()); } +// size in byte of a page in the I²C EEPROM (for faster page write) +#define I2C_EEPROM_PAGE_SIZE 16U + // read EDID from I²C memory // return if succeeded static bool read_edid(void) @@ -192,12 +195,25 @@ static bool read_edid(void) // return if succeeded static bool write_edid(void) { - const uint8_t address[] = {0}; - if (!i2c_master_setup(false)) { + if (!i2c_master_setup(5)) { + puts("I²C setup failed"); return false; } - if (I2C_MASTER_RC_NONE != i2c_master_address_write(I2C_SLAVE, false, address, ARRAY_LENGTH(address), edid, edid_length())) { - return false; + // write all page + const uint16_t length = edid_length(); + for (uint16_t i = 0; i < length; i += I2C_EEPROM_PAGE_SIZE) { + const uint8_t address[] = {i}; + const enum i2c_master_rc rc = i2c_master_address_write(I2C_SLAVE, false, address, ARRAY_LENGTH(address), &edid[i], I2C_EEPROM_PAGE_SIZE); // write I²C EEPROM page + if (I2C_MASTER_RC_NONE != rc) { + puts("I²C write failed ("); + putc('0' + rc); + putc(')'); + if (I2C_MASTER_RC_BUS_ERROR == rc) { + i2c_master_reset(); // try to clear any I²C issue + } + return false; + } + wait_10us((5 + 1) * 100); // wait 5 ms for the page write to complete } i2c_master_release(); // release I²C again return true;