forked from NachiketKelkar/AESD-Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myi2c.c
72 lines (64 loc) · 1.86 KB
/
myi2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/************************************************************************************************
* File name : myi2c.c *
* Authors : Puneet Bansal and Nachiket Kelkar *
* Description : The function definition for reading and writing to i2c device *
* Tools used : GNU make, gcc, arm-linux-gcc *
************************************************************************************************/
// ref:https://elinux.org/Interfacing_with_I2C_Devices
#include<linux/i2c.h>
#include<linux/i2c-dev.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "myi2c.h"
int myi2cInit(uint8_t slaveAdd)
{
char fileName[50];
int fileDescrip;
sprintf(fileName,"/dev/i2c-2");
fileDescrip=open(fileName,O_RDWR);
if(fileDescrip < 0)
{
perror("Failed to open i2c file");
exit(1);
}
if(ioctl(fileDescrip, I2C_SLAVE, slaveAdd) < 0)
{
perror("Failed to communicate with slave"); //or use errno
exit(1);
}
return fileDescrip;
}
uint8_t* myi2cRead(int fileDescrip,uint8_t len)
{
static uint8_t readBuffer[2];
pthread_mutex_lock(&lock);
if (read(fileDescrip,readBuffer,len) != len)
{
perror("Read failed");
pthread_mutex_unlock(&lock);
return NULL;
//check whether we need to return or not.
}
pthread_mutex_unlock(&lock);
//printf("Value of buffer is %x-%x\n",readBuffer[0],readBuffer[1]);
return readBuffer;
}
int myi2cWrite(int fileDescrip, uint8_t writeBuffer[], uint8_t len)
{
pthread_mutex_lock(&lock);
if (write(fileDescrip,writeBuffer,len) != len)
{
pthread_mutex_unlock(&lock);
perror("Write failed");
return -1;
}
pthread_mutex_unlock(&lock);
return 0;
}