From c68bf6a3244ec3fc31415621b25ff5e77726783f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Tue, 17 Aug 2021 08:54:17 +0200 Subject: [PATCH] =?UTF-8?q?main:=20fix,=20write=20I=C2=B2C=20EEPROM=20in?= =?UTF-8?q?=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) 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;