Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(i2c): Add test to scan bus #11022

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/validation/i2c_master/i2c_master.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <Arduino.h>
#include <unity.h>
#include <Wire.h>
#include <vector>
#include <algorithm>
#include <WiFi.h>

#include "sdkconfig.h"

/* DS1307 functions */

Expand All @@ -24,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));
};
Expand Down Expand Up @@ -245,6 +253,42 @@ void test_api() {
Wire.flush();
}

bool device_found() {
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);
} else if (address == DS1307_ADDR) {
log_e("Failed to find DS1307");
return false;
}
}

return true;
}

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 */

void setup() {
Expand All @@ -258,6 +302,10 @@ 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);
Expand Down
Loading