main: replace I²C library with software implementation

This commit is contained in:
King Kévin 2021-08-17 11:31:42 +02:00
parent e988151465
commit 3c1b0ca7ea
1 changed files with 11 additions and 19 deletions

30
main.c
View File

@ -8,7 +8,7 @@
#include "stm8s.h" #include "stm8s.h"
#include "main.h" #include "main.h"
#include "i2c_master.h" #include "softi2c_master.h"
#include "eeprom_blockprog.h" #include "eeprom_blockprog.h"
// blink RUN LED to show error // blink RUN LED to show error
@ -180,7 +180,7 @@ static bool save_edid(void)
// return if succeeded // return if succeeded
static bool read_edid(void) static bool read_edid(void)
{ {
if (!i2c_master_setup(5)) { if (!softi2c_master_setup(10)) {
puts("I²C setup failed"); puts("I²C setup failed");
return false; return false;
} }
@ -191,12 +191,9 @@ static bool read_edid(void)
uint8_t data[I2C_EEPROM_PAGE_SIZE]; // data from page uint8_t data[I2C_EEPROM_PAGE_SIZE]; // data from page
bool same = false; // if the data read is the same bool same = false; // if the data read is the same
while (!same) { // read until the data is the same while (!same) { // read until the data is the same
const enum i2c_master_rc rc = i2c_master_address_read(I2C_SLAVE, false, address, ARRAY_LENGTH(address), data, ARRAY_LENGTH(data)); // read I²C EEPROM data const bool rc = softi2c_master_address_read(I2C_SLAVE, address, ARRAY_LENGTH(address), data, ARRAY_LENGTH(data)); // read I²C EEPROM data
if (I2C_MASTER_RC_NONE != rc) { if (!rc) {
puts("I²C read failed ("); puts("I²C read failed");
putc('0' + rc);
putc(')');
i2c_master_reset(); // try to clear any I²C issue
return false; return false;
} }
// check if the data is the same and copy it to EDID // check if the data is the same and copy it to EDID
@ -209,7 +206,7 @@ static bool read_edid(void)
} }
} }
} }
i2c_master_release(); // release I²C again softi2c_master_release(); // release I²C again
return true; return true;
} }
@ -218,7 +215,7 @@ static bool read_edid(void)
// return if succeeded // return if succeeded
static bool write_edid(void) static bool write_edid(void)
{ {
if (!i2c_master_setup(5)) { if (!softi2c_master_setup(10)) {
puts("I²C setup failed"); puts("I²C setup failed");
return false; return false;
} }
@ -226,19 +223,14 @@ static bool write_edid(void)
const uint16_t length = edid_length(); const uint16_t length = edid_length();
for (uint16_t i = 0; i < length; i += I2C_EEPROM_PAGE_SIZE) { for (uint16_t i = 0; i < length; i += I2C_EEPROM_PAGE_SIZE) {
const uint8_t address[] = {i}; 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 const bool rc = softi2c_master_address_write(I2C_SLAVE, address, ARRAY_LENGTH(address), &edid[i], I2C_EEPROM_PAGE_SIZE); // write I²C EEPROM page
if (I2C_MASTER_RC_NONE != rc) { if (!rc) {
puts("I²C write failed ("); 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; return false;
} }
wait_10us((5 + 1) * 100); // wait 5 ms for the page write to complete wait_10us((5 + 1) * 100); // wait 5 ms for the page write to complete
} }
i2c_master_release(); // release I²C again softi2c_master_release(); // release I²C again
return true; return true;
} }