From 9383f4fcadbce09926575938f062b37bfa755203 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:30:30 -0300 Subject: [PATCH 1/3] test(i2c): Add test to scan bus --- tests/validation/i2c_master/i2c_master.ino | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/validation/i2c_master/i2c_master.ino b/tests/validation/i2c_master/i2c_master.ino index 3c7b2d9824a..85b84b017be 100644 --- a/tests/validation/i2c_master/i2c_master.ino +++ b/tests/validation/i2c_master/i2c_master.ino @@ -5,6 +5,8 @@ #include #include #include +#include +#include /* DS1307 functions */ @@ -245,6 +247,27 @@ void test_api() { Wire.flush(); } +void scan_bus() { + std::vector found_addresses; + uint8_t err; + + for (uint8_t address = 1; address < 127; ++address) { + Wire.beginTransmission(address); + err = Wire.endTransmission(); + log_d("Address: 0x%02X, Error: %d", address, err); + if (err == 0) { + log_i("Found device at address: 0x%02X", address); + found_addresses.push_back(address); + } else if (address == DS1307_ADDR) { + TEST_FAIL_MESSAGE("Failed to find DS1307"); + } + } + + // Find DS1307_ADDR in found_addresses + auto it = std::find(found_addresses.begin(), found_addresses.end(), DS1307_ADDR); + TEST_ASSERT_TRUE(it != found_addresses.end()); +} + /* Main */ void setup() { @@ -258,6 +281,7 @@ void setup() { log_d("Starting tests"); UNITY_BEGIN(); + RUN_TEST(scan_bus); RUN_TEST(rtc_set_time); RUN_TEST(rtc_run_clock); RUN_TEST(change_clock); From 8772e1d4421ff9c9201afc51c84ee36ab9acb5d3 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 28 Feb 2025 12:38:10 -0300 Subject: [PATCH 2/3] test(i2c): Add scan test with wifi running --- tests/validation/i2c_master/i2c_master.ino | 34 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tests/validation/i2c_master/i2c_master.ino b/tests/validation/i2c_master/i2c_master.ino index 85b84b017be..aea8837e529 100644 --- a/tests/validation/i2c_master/i2c_master.ino +++ b/tests/validation/i2c_master/i2c_master.ino @@ -7,6 +7,9 @@ #include #include #include +#include + +#include "sdkconfig.h" /* DS1307 functions */ @@ -26,6 +29,9 @@ static uint8_t read_month = 0; static uint16_t read_year = 0; static int peek_data = -1; +const char *ssid = "Wokwi-GUEST"; +const char *password = ""; + const auto BCD2DEC = [](uint8_t num) -> uint8_t { return ((num / 16 * 10) + (num % 16)); }; @@ -247,7 +253,7 @@ void test_api() { Wire.flush(); } -void scan_bus() { +bool device_found() { std::vector found_addresses; uint8_t err; @@ -259,14 +265,33 @@ void scan_bus() { log_i("Found device at address: 0x%02X", address); found_addresses.push_back(address); } else if (address == DS1307_ADDR) { - TEST_FAIL_MESSAGE("Failed to find DS1307"); + log_e("Failed to find DS1307"); + return false; } } // Find DS1307_ADDR in found_addresses auto it = std::find(found_addresses.begin(), found_addresses.end(), DS1307_ADDR); - TEST_ASSERT_TRUE(it != found_addresses.end()); + return it != found_addresses.end(); +} + +void scan_bus() { + TEST_ASSERT_TRUE(device_found()); +} + +#if SOC_WIFI_SUPPORTED +void scan_bus_with_wifi() { + // delete old config + WiFi.disconnect(true, true, 1000); + delay(1000); + WiFi.begin(ssid, password); + delay(5000); + bool found = device_found(); + WiFi.disconnect(true, true, 1000); + + TEST_ASSERT_TRUE(found); } +#endif /* Main */ @@ -282,6 +307,9 @@ void setup() { log_d("Starting tests"); UNITY_BEGIN(); RUN_TEST(scan_bus); +#if SOC_WIFI_SUPPORTED + RUN_TEST(scan_bus_with_wifi); +#endif RUN_TEST(rtc_set_time); RUN_TEST(rtc_run_clock); RUN_TEST(change_clock); From 62cf432ed63fd50566290a1784768fc07802268a Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 28 Feb 2025 13:52:55 -0300 Subject: [PATCH 3/3] fix(i2c): Simplify test --- tests/validation/i2c_master/i2c_master.ino | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/validation/i2c_master/i2c_master.ino b/tests/validation/i2c_master/i2c_master.ino index aea8837e529..9b4cc508a35 100644 --- a/tests/validation/i2c_master/i2c_master.ino +++ b/tests/validation/i2c_master/i2c_master.ino @@ -254,7 +254,6 @@ void test_api() { } bool device_found() { - std::vector found_addresses; uint8_t err; for (uint8_t address = 1; address < 127; ++address) { @@ -263,16 +262,13 @@ bool device_found() { log_d("Address: 0x%02X, Error: %d", address, err); if (err == 0) { log_i("Found device at address: 0x%02X", address); - found_addresses.push_back(address); } else if (address == DS1307_ADDR) { log_e("Failed to find DS1307"); return false; } } - // Find DS1307_ADDR in found_addresses - auto it = std::find(found_addresses.begin(), found_addresses.end(), DS1307_ADDR); - return it != found_addresses.end(); + return true; } void scan_bus() {