forked from raspberrypi/pico-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c_bus_scan.c
42 lines (34 loc) · 1.08 KB
/
i2c_bus_scan.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
#include <stdio.h>
#include "pico/stdlib.h"
#include "pio_i2c.h"
#define PIN_SDA 2
#define PIN_SCL 3
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
int main() {
stdio_init_all();
PIO pio = pio0;
uint sm = 0;
uint offset = pio_add_program(pio, &i2c_program);
i2c_program_init(pio, sm, offset, PIN_SDA, PIN_SCL);
printf("\nPIO I2C Bus Scan\n");
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
for (int addr = 0; addr < (1 << 7); ++addr) {
if (addr % 16 == 0) {
printf("%02x ", addr);
}
// Perform a 0-byte read from the probe address. The read function
// returns a negative result NAK'd any time other than the last data
// byte. Skip over reserved addresses.
int result;
if (reserved_addr(addr))
result = -1;
else
result = pio_i2c_read_blocking(pio, sm, addr, NULL, 0);
printf(result < 0 ? "." : "@");
printf(addr % 16 == 15 ? "\n" : " ");
}
printf("Done.\n");
return 0;
}