forked from SequentMicrosystems/megaind-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.c
119 lines (96 loc) · 2.37 KB
/
comm.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* comm.c:
* Communication routines "platform specific" for Raspberry Pi
*
* Copyright (c) 2016-2020 Sequent Microsystem
* <http://www.sequentmicrosystem.com>
***********************************************************************
* Author: Alexandru Burcea
***********************************************************************
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include "comm.h"
#define I2C_SLAVE 0x0703
#define I2C_SMBUS 0x0720 /* SMBus-level access */
#define I2C_SMBUS_READ 1
#define I2C_SMBUS_WRITE 0
// SMBus transaction types
#define I2C_SMBUS_QUICK 0
#define I2C_SMBUS_BYTE 1
#define I2C_SMBUS_BYTE_DATA 2
#define I2C_SMBUS_WORD_DATA 3
#define I2C_SMBUS_PROC_CALL 4
#define I2C_SMBUS_BLOCK_DATA 5
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
#define I2C_SMBUS_I2C_BLOCK_DATA 8
// SMBus messages
#define I2C_SMBUS_BLOCK_MAX 512 /* As specified in SMBus standard */
#define I2C_SMBUS_I2C_BLOCK_MAX 512 /* Not specified but we use same structure */
int i2cSetup(int addr)
{
int file;
char filename[40];
sprintf(filename, "/dev/i2c-1");
if ( (file = open(filename, O_RDWR)) < 0)
{
printf("Failed to open the bus.");
return -1;
}
if (ioctl(file, I2C_SLAVE, addr) < 0)
{
printf("Failed to acquire bus access and/or talk to slave.\n");
return -1;
}
return file;
}
int i2cMem8Read(int dev, int add, uint8_t* buff, int size)
{
uint8_t intBuff[I2C_SMBUS_BLOCK_MAX];
if (NULL == buff)
{
return -1;
}
if (size > I2C_SMBUS_BLOCK_MAX)
{
return -1;
}
intBuff[0] = 0xff & add;
if (write(dev, intBuff, 1) != 1)
{
//printf("Fail to select mem add!\n");
return -1;
}
if (read(dev, buff, size) != size)
{
//printf("Fail to read memory!\n");
return -1;
}
return 0; //OK
}
int i2cMem8Write(int dev, int add, uint8_t* buff, int size)
{
uint8_t intBuff[I2C_SMBUS_BLOCK_MAX];
if (NULL == buff)
{
return -1;
}
if (size > I2C_SMBUS_BLOCK_MAX - 1)
{
return -1;
}
intBuff[0] = 0xff & add;
memcpy(&intBuff[1], buff, size);
if (write(dev, intBuff, size + 1) != size + 1)
{
//printf("Fail to write memory!\n");
return -1;
}
return 0;
}