diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..90b82ad --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +SOURCE = main.c +CC = arm-linux-gnueabi-gcc +FLAG = --static -o +OUT = I2C_scan.elf +main:${SOURCE} + ${CC} ${SOURCE} ${FLAG} ${OUT} +clean: + rm -r ${OUT} +.PHONY:clean + + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..f312526 --- /dev/null +++ b/main.c @@ -0,0 +1,46 @@ +/* Autor : Siva Prakasam // + Date : 30/12/24 // + Title : I2C scan */ + + +#include +#include +#include +#include +#include +#include +#include + +#define I2C_BUS "/dev/i2c-1" // Modify according to your bus + +int main() { + int file; + int addr; + char *bus = I2C_BUS; + + // Open the I2C bus + if ((file = open(bus, O_RDWR)) < 0) { + perror("Failed to open I2C bus"); + exit(1); + } + + printf("Scanning I2C bus %s...\n", bus); + + // Loop through all possible I2C addresses (0x03 to 0x77) + for (addr = 0x03; addr < 0x78; addr++) { + // Try to communicate with the device at the address + if (ioctl(file, I2C_SLAVE, addr) < 0) { + // Device not found + continue; + } + + // Device found, print the address + printf("I2C device found at address 0x%02x\n", addr); + } + + // Close the I2C bus + close(file); + + return 0; +} +