#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
const int SDA_PIN = 8;
const int SCL_PIN = 9;
// I2C reserves some addresses for special purposes. We exclude these from the scan.
// These are any addresses of the form 000 0xxx or 111 1xxx
bool reserved_addr(uint8_t addr) {
return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
}
int main() {
// Enable UART so we can print status output
stdio_init_all();
// This example will use I2C0 on the default SDA and SCL pins (GP4, GP5 on a Pico)
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(SDA_PIN);
gpio_pull_up(SCL_PIN);
// Make the I2C pins available to picotool
bi_decl(bi_2pins_with_func(SDA_PIN, SCL_PIN, GPIO_FUNC_I2C));
printf("\nI2C Bus Scan\n");
for (int addr = 0; addr < (1 << 7); ++addr) {
// if 1-byte dummy read is possible on the address,
// the function returns the number of bytes available (>0)
// If the address byte is ignored, the function returns -1
int ret;
uint8_t rxdata;
// Skip over any reserved addresses.
if (reserved_addr(addr))
ret = PICO_ERROR_GENERIC;
else
ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
if (ret > 0 ) printf("00x%x\n", addr);
}
printf("Done.\n");
return 0;
}