add main file and Makefile

This commit is contained in:
Manticore 2024-12-31 09:53:17 +05:30
parent 7659c89e72
commit 120860328f
2 changed files with 58 additions and 0 deletions

12
Makefile Normal file
View File

@ -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

46
main.c Normal file
View File

@ -0,0 +1,46 @@
/* Autor : Siva Prakasam //
Date : 30/12/24 //
Title : I2C scan */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#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;
}