-
Notifications
You must be signed in to change notification settings - Fork 2
/
i2c_smbus.cpp
130 lines (118 loc) · 3.67 KB
/
i2c_smbus.cpp
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
120
121
122
123
124
125
126
127
128
129
130
/******************************************************************************************\
* *
* i2c_smbus.cpp *
* *
* Device-independent i2c/SMBus communication functions *
* *
\******************************************************************************************/
#include "i2c_smbus.h"
#include <string.h>
s32 i2c_smbus_interface::i2c_smbus_write_quick(u8 addr, u8 value)
{
return i2c_smbus_xfer(addr, value, 0, I2C_SMBUS_QUICK, NULL);
}
s32 i2c_smbus_interface::i2c_smbus_read_byte(u8 addr)
{
i2c_smbus_data data;
if (i2c_smbus_xfer(addr, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
{
return -1;
}
else
{
return data.byte;
}
}
s32 i2c_smbus_interface::i2c_smbus_write_byte(u8 addr, u8 value)
{
return i2c_smbus_xfer(addr, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
}
s32 i2c_smbus_interface::i2c_smbus_read_byte_data(u8 addr, u8 command)
{
i2c_smbus_data data;
if (i2c_smbus_xfer(addr, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &data))
{
return -1;
}
else
{
return data.byte;
}
}
s32 i2c_smbus_interface::i2c_smbus_write_byte_data(u8 addr, u8 command, u8 value)
{
i2c_smbus_data data;
data.byte = value;
return i2c_smbus_xfer(addr, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &data);
}
s32 i2c_smbus_interface::i2c_smbus_read_word_data(u8 addr, u8 command)
{
i2c_smbus_data data;
if (i2c_smbus_xfer(addr, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &data))
{
return -1;
}
else
{
return data.word;
}
}
s32 i2c_smbus_interface::i2c_smbus_write_word_data(u8 addr, u8 command, u16 value)
{
i2c_smbus_data data;
data.word = value;
return i2c_smbus_xfer(addr, I2C_SMBUS_WRITE, command, I2C_SMBUS_WORD_DATA, &data);
}
s32 i2c_smbus_interface::i2c_smbus_read_block_data(u8 addr, u8 command, u8 *values)
{
i2c_smbus_data data;
if (i2c_smbus_xfer(addr, I2C_SMBUS_READ, command, I2C_SMBUS_BLOCK_DATA, &data))
{
return -1;
}
else
{
memcpy(values, &data.block[1], data.block[0]);
return data.block[0];
}
}
s32 i2c_smbus_interface::i2c_smbus_write_block_data(u8 addr, u8 command, u8 length, const u8 *values)
{
i2c_smbus_data data;
if (length > I2C_SMBUS_BLOCK_MAX)
{
length = I2C_SMBUS_BLOCK_MAX;
}
data.block[0] = length;
memcpy(&data.block[1], values, length);
return i2c_smbus_xfer(addr, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_DATA, &data);
}
s32 i2c_smbus_interface::i2c_smbus_read_i2c_block_data(u8 addr, u8 command, u8 length, u8 *values)
{
i2c_smbus_data data;
if (length > I2C_SMBUS_BLOCK_MAX)
{
length = I2C_SMBUS_BLOCK_MAX;
}
data.block[0] = length;
if (i2c_smbus_xfer(addr, I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA, &data))
{
return -1;
}
else
{
memcpy(values, &data.block[1], data.block[0]);
return data.block[0];
}
}
s32 i2c_smbus_interface::i2c_smbus_write_i2c_block_data(u8 addr, u8 command, u8 length, const u8 *values)
{
i2c_smbus_data data;
if (length > I2C_SMBUS_BLOCK_MAX)
{
length = I2C_SMBUS_BLOCK_MAX;
}
data.block[0] = length;
memcpy(&data.block[1], values, length);
return i2c_smbus_xfer(addr, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &data);
}