-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_scanner.c
62 lines (48 loc) · 1.53 KB
/
i2c_scanner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2023 Christopher Courtney, aka Drashna Jael're (@drashna) <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "quantum.h"
ASSERT_COMMUNITY_MODULES_MIN_API_VERSION(1, 0, 0);
#include "i2c_master.h"
#include "debug.h"
static bool i2c_scanner_enable = true;
#ifndef I2C_SCANNER_TIMEOUT
# define I2C_SCANNER_TIMEOUT 50
#endif // I2C_SCANNER_TIMEOUT
static void do_scan(void) {
if (!i2c_scanner_enable) {
return;
}
uint8_t nDevices = 0;
xprintf("Scanning for I2C Devices...\n");
for (uint8_t address = 1; address < 127; address++) {
// The i2c_scanner uses the return value of
// i2c_ping_address to see if a device did acknowledge to the address.
i2c_status_t error = i2c_ping_address(address << 1, I2C_SCANNER_TIMEOUT);
if (error == I2C_STATUS_SUCCESS) {
xprintf(" I2C device found at address 0x%02X\n", address);
nDevices++;
} else {
// xprintf(" Unknown error (%u) at address 0x%02X\n", error, address);
}
}
if (nDevices == 0) {
xprintf("No I2C devices found\n");
}
}
uint16_t scan_timer = 0;
void housekeeping_task_i2c_scanner(void) {
if (timer_elapsed(scan_timer) > 5000) {
do_scan();
scan_timer = timer_read();
}
}
void keyboard_post_init_i2c_scanner(void) {
i2c_init();
scan_timer = timer_read();
}
bool i2c_scanner_get_enabled(void) {
return i2c_scanner_enable;
}
void i2c_scanner_set_enabled(bool enable) {
i2c_scanner_enable = enable;
}