Skip to content

Instantly share code, notes, and snippets.

@murilopontes
Created July 5, 2014 21:17
Show Gist options
  • Save murilopontes/3127df6abe6d46a25724 to your computer and use it in GitHub Desktop.
Save murilopontes/3127df6abe6d46a25724 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char** argv) {
int file;
char filename[40];
uint8_t buf[10];
uint16_t* p16;
uint32_t* p32;
p16=&buf[0];
p32=&buf[0];
int addr = 0x77;
sprintf(filename,"/dev/i2c-2");
if ((file = open(filename,O_RDWR)) < 0) {
printf("Failed to open the bus.");
exit(1);
}
if (ioctl(file,I2C_SLAVE,addr) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
//RESET
buf[0]=0x1e;
write(file,buf,1);
usleep(2000);
//PROM
for(int i=0xa0;i<=0xae;i+=2){
buf[0]=i;
write(file,buf,1);
read(file,buf,2);
printf("reg=%02x value=%04x (%d)\r\n",i,p16[0],p16[0]);
}
//D1
buf[0]=0x40;
write(file,buf,1);
usleep(1000);
buf[0]=0x0;
write(file,buf,1);
read(file,buf,3);
printf("0x40=%02x%02x%02x (%d)\r\n",buf[0],buf[1],buf[2],p32[0]);
//D2
buf[0]=0x50;
write(file,buf,1);
usleep(1000);
buf[0]=0x0;
write(file,buf,1);
read(file,buf,3);
printf("0x50=%02x%02x%02x (%d)\r\n",buf[0],buf[1],buf[2],p32[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment