From 7281acea0a42ecc0dbcfbd649ce3f7c1999c2625 Mon Sep 17 00:00:00 2001 From: Andrej Pala <78907070+xpalaa00@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:31:58 +0100 Subject: [PATCH 1/4] A clock phase correction in write_start(void) Some devices require the data wire SDA to be held down at the moment while the clock wire is pulled down too to execute the start condition (e.g. devices using "TinyWires.h" library). This change follows a behaviour of Arduino Wire.h library, where the SCL signal is pulled down in half of period of start condition. --- cores/esp8266/core_esp8266_si2c.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index e4bb1c2b07..10936ecd02 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -248,7 +248,9 @@ bool Twi::write_start(void) } busywait(twi_dcount); SDA_LOW(twi_sda); - busywait(twi_dcount); + busywait(twi_dcount/2); + SCL_LOW(twi_scl); + busywait((twi_dcount/2) + (twi_dcount % 2)); return true; } From 1ea6a57657fda5c8888ef8e152612df728d2f825 Mon Sep 17 00:00:00 2001 From: Andrej Pala <78907070+xpalaa00@users.noreply.github.com> Date: Wed, 24 Nov 2021 11:22:06 +0100 Subject: [PATCH 2/4] Formatting of modification restyled --- cores/esp8266/core_esp8266_si2c.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 10936ecd02..e9e9ec75ea 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -248,9 +248,9 @@ bool Twi::write_start(void) } busywait(twi_dcount); SDA_LOW(twi_sda); - busywait(twi_dcount/2); + busywait(twi_dcount / 2); SCL_LOW(twi_scl); - busywait((twi_dcount/2) + (twi_dcount % 2)); + busywait((twi_dcount / 2) + (twi_dcount % 2)); return true; } From 189b931e658e1f223911b45455fdbcf06b86fad8 Mon Sep 17 00:00:00 2001 From: Andrej Pala <78907070+xpalaa00@users.noreply.github.com> Date: Sat, 27 Nov 2021 00:54:24 +0100 Subject: [PATCH 3/4] Removing mathematical operations from delay --- cores/esp8266/core_esp8266_si2c.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index e9e9ec75ea..11d4a741c2 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -248,9 +248,9 @@ bool Twi::write_start(void) } busywait(twi_dcount); SDA_LOW(twi_sda); - busywait(twi_dcount / 2); + busywait(twi_dcount); SCL_LOW(twi_scl); - busywait((twi_dcount / 2) + (twi_dcount % 2)); + busywait(twi_dcount); return true; } From a813c527361ebe4104a7e58f7cf401a5068f25af Mon Sep 17 00:00:00 2001 From: Andrej Pala <78907070+xpalaa00@users.noreply.github.com> Date: Sat, 27 Nov 2021 01:19:44 +0100 Subject: [PATCH 4/4] Comments added --- cores/esp8266/core_esp8266_si2c.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 11d4a741c2..e01dffe080 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -247,8 +247,10 @@ bool Twi::write_start(void) return false; } busywait(twi_dcount); + // A high-to-low transition on the SDA line while the SCL is high defines a START condition. SDA_LOW(twi_sda); busywait(twi_dcount); + // An additional delay between the SCL line high-to-low transition and setting up the SDA line to prevent a STOP condition execute. SCL_LOW(twi_scl); busywait(twi_dcount); return true; @@ -262,6 +264,7 @@ bool Twi::write_stop(void) SCL_HIGH(twi_scl); WAIT_CLOCK_STRETCH(); busywait(twi_dcount); + // A low-to-high transition on the SDA line while the SCL is high defines a STOP condition. SDA_HIGH(twi_sda); busywait(twi_dcount); return true;