main: replace I²C library with software implementation
This commit is contained in:
parent
e988151465
commit
3c1b0ca7ea
30
main.c
30
main.c
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include "stm8s.h"
|
||||
#include "main.h"
|
||||
#include "i2c_master.h"
|
||||
#include "softi2c_master.h"
|
||||
#include "eeprom_blockprog.h"
|
||||
|
||||
// blink RUN LED to show error
|
||||
|
@ -180,7 +180,7 @@ static bool save_edid(void)
|
|||
// return if succeeded
|
||||
static bool read_edid(void)
|
||||
{
|
||||
if (!i2c_master_setup(5)) {
|
||||
if (!softi2c_master_setup(10)) {
|
||||
puts("I²C setup failed");
|
||||
return false;
|
||||
}
|
||||
|
@ -191,12 +191,9 @@ static bool read_edid(void)
|
|||
uint8_t data[I2C_EEPROM_PAGE_SIZE]; // data from page
|
||||
bool same = false; // if the data read 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
|
||||
if (I2C_MASTER_RC_NONE != rc) {
|
||||
puts("I²C read failed (");
|
||||
putc('0' + rc);
|
||||
putc(')');
|
||||
i2c_master_reset(); // try to clear any I²C issue
|
||||
const bool rc = softi2c_master_address_read(I2C_SLAVE, address, ARRAY_LENGTH(address), data, ARRAY_LENGTH(data)); // read I²C EEPROM data
|
||||
if (!rc) {
|
||||
puts("I²C read failed");
|
||||
return false;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
@ -218,7 +215,7 @@ static bool read_edid(void)
|
|||
// return if succeeded
|
||||
static bool write_edid(void)
|
||||
{
|
||||
if (!i2c_master_setup(5)) {
|
||||
if (!softi2c_master_setup(10)) {
|
||||
puts("I²C setup failed");
|
||||
return false;
|
||||
}
|
||||
|
@ -226,19 +223,14 @@ static bool write_edid(void)
|
|||
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
|
||||
}
|
||||
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 (!rc) {
|
||||
puts("I²C write failed");
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue