// printf #include //O_RDWR #include // I2C_SLAVE #include // __u8 #include int main(void){ printf("Programme de test de l'i2c en mode maitre\n"); int file; int adapter_nr = 2; /* probably dynamically determined */ char filename[20]; int addr = 0x50; /* The I2C address */ __u8 registre = 0x0; /* Device register to access */ char buf[300]; int i,j,ok; for (adapter_nr=0;adapter_nr < 6; adapter_nr++){ snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); printf("\n%s\n",filename); file = open(filename, O_RDWR); if (file < 0) { printf("Echec à l'ouverture\n"); close(file); /* ERROR HANDLING; you can check errno to see what went wrong */ continue; } //printf("Ouverture Ok\n"); for (addr=80; addr < 81; addr++){ //printf("adresse = %d\n",addr); if (ioctl(file, I2C_SLAVE, addr) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ //printf("Erreur adresse\n"); continue; } //printf("Adresse Ok\n"); /* Using I2C Write, equivalent of i2c_smbus_write_word_data(file, register, 0x6543) */ for (registre = 0; registre< 1; registre++){ buf[0] = registre; buf[1] = 0x43; buf[2] = 0x65; if ( write(file, buf, 1) != 1 ) { /* ERROR HANDLING: i2c transaction failed */ //printf("erreur ecriture \n"); continue; } i = read(file, buf, 250); if (i != 250) { /* ERROR HANDLING: i2c transaction failed */ //printf("Erreur Lecture : %d\n",i); continue; } else { /* buf[0] contains the read byte */ ok = 0; for(j=0;j<250;j++){ if (buf[j] > 0){ ok=1; }else{ buf[j]=32; } printf("%d\t%c\t%d\n",j,buf[j],buf[j]); } /* if (ok == 1){ buf[100]='\0'; printf("\n%s\n",filename); printf("adresse = %d, ",addr); printf("registre = %d, ",registre); printf("## %s\n ##",buf); } */ } } } close(file); } return 0; }