Skip to content

Commit 9383f4f

Browse files
committed
test(i2c): Add test to scan bus
1 parent 7575fa0 commit 9383f4f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: tests/validation/i2c_master/i2c_master.ino

+24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <Arduino.h>
66
#include <unity.h>
77
#include <Wire.h>
8+
#include <vector>
9+
#include <algorithm>
810

911
/* DS1307 functions */
1012

@@ -245,6 +247,27 @@ void test_api() {
245247
Wire.flush();
246248
}
247249

250+
void scan_bus() {
251+
std::vector<uint8_t> found_addresses;
252+
uint8_t err;
253+
254+
for (uint8_t address = 1; address < 127; ++address) {
255+
Wire.beginTransmission(address);
256+
err = Wire.endTransmission();
257+
log_d("Address: 0x%02X, Error: %d", address, err);
258+
if (err == 0) {
259+
log_i("Found device at address: 0x%02X", address);
260+
found_addresses.push_back(address);
261+
} else if (address == DS1307_ADDR) {
262+
TEST_FAIL_MESSAGE("Failed to find DS1307");
263+
}
264+
}
265+
266+
// Find DS1307_ADDR in found_addresses
267+
auto it = std::find(found_addresses.begin(), found_addresses.end(), DS1307_ADDR);
268+
TEST_ASSERT_TRUE(it != found_addresses.end());
269+
}
270+
248271
/* Main */
249272

250273
void setup() {
@@ -258,6 +281,7 @@ void setup() {
258281

259282
log_d("Starting tests");
260283
UNITY_BEGIN();
284+
RUN_TEST(scan_bus);
261285
RUN_TEST(rtc_set_time);
262286
RUN_TEST(rtc_run_clock);
263287
RUN_TEST(change_clock);

0 commit comments

Comments
 (0)