Skip to content

Commit 9a5f701

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

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/validation/i2c_master/i2c_master.ino

+22
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,25 @@ void test_api() {
245247
Wire.flush();
246248
}
247249

250+
void scan_bus() {
251+
std::vector<uint8_t> found_addresses;
252+
std::vector<uint8_t> expected_addresses = {DS1307_ADDR};
253+
uint8_t err;
254+
255+
for (uint8_t address = 1; address < 127; ++address) {
256+
Wire.beginTransmission(address);
257+
err = Wire.endTransmission();
258+
log_d("Address: %d, Error: %d", address, err);
259+
if (err == 0) {
260+
found_addresses.push_back(address);
261+
} else if (address == DS1307_ADDR) {
262+
TEST_FAIL_MESSAGE("Failed to find DS1307");
263+
}
264+
}
265+
266+
TEST_ASSERT_TRUE(found_addresses == expected_addresses);
267+
}
268+
248269
/* Main */
249270

250271
void setup() {
@@ -258,6 +279,7 @@ void setup() {
258279

259280
log_d("Starting tests");
260281
UNITY_BEGIN();
282+
RUN_TEST(scan_bus);
261283
RUN_TEST(rtc_set_time);
262284
RUN_TEST(rtc_run_clock);
263285
RUN_TEST(change_clock);

0 commit comments

Comments
 (0)