-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.cpp
119 lines (82 loc) · 2.46 KB
/
i2c.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
/*
Title --- driver/i2c.cpp
Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
This file is part of Rpi-hw.
Rpi-hw is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation version 3 of the License.
Rpi-hw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
*/
#include "i2c.h"
namespace rpihw { // Begin main namespace
namespace driver { // Begin drivers namespace
i2c::i2c( const std::string &dev_path)
: m_dev_path ( dev_path )
{
// Open device file
m_dev_fd = open( m_dev_path.c_str(), O_RDWR );
if ( m_dev_fd < 0 )
throw "(Fatal) `i2c`: can't open I2C device \n" ;
}
void i2c::addr(uint8_t addr)
{
m_addr = addr;
// Select slave device
if(ioctl(m_dev_fd, I2C_SLAVE, addr) < 0)
throw "(Fatal) `i2c`: can't select slave device \n";
}
i2c::~i2c() {
// Close the device file
close( m_dev_fd );
}
void
i2c::write( uint8_t *data, uint8_t size ) {
if ( ::write( m_dev_fd, data, size ) != size )
throw "(Fatal) `i2c`: failed to write to the bus\n";
}
void
i2c::read( uint8_t *data, uint8_t size ) {
if ( ::read( m_dev_fd, data, size ) != size )
throw "(Fatal) `i2c`: failed to read from the bus\n";
}
void
i2c::writeReg8( uint8_t reg, uint8_t data ) {
// Build the buffer to send
m_buffer[0] = reg;
m_buffer[1] = data;
// Write the data on the device
write( m_buffer, 2 );
}
uint8_t
i2c::readReg8( uint8_t reg ) {
// Select the register on the device
write( ®, 1 );
// Read the data from the device
read( m_buffer, 1 );
return m_buffer[0];
}
void
i2c::writeReg16( uint8_t reg, uint16_t data ) {
// Build the buffer to send
m_buffer[0] = reg;
m_buffer[1] = data & 0xFF;
m_buffer[2] = (data >> 8) & 0xFF;
// Write the data on the device
write( m_buffer, 2 );
}
uint16_t
i2c::readReg16( uint8_t reg ) {
// Select the register on the device
write( ®, 1 );
// Read the data from the device
read( m_buffer, 2 );
// Merge the 16 bit data
return (uint16_t) m_buffer[0] | ( (uint16_t) m_buffer[1] << 8 );
}
} // End of drivers namespace
} // End of main namespace