-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2cport.cpp
174 lines (146 loc) · 4.96 KB
/
i2cport.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2018 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of nymea. *
* *
* This library 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; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "i2cport.h"
#include "i2cport_p.h"
#include "loggingcategories.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <QDir>
I2CPort::I2CPort(const QString &portName, QObject *parent) :
QObject(parent),
d_ptr(new I2CPortPrivate(this))
{
d_ptr->portDeviceName = "/dev/" + portName;
d_ptr->fileDescriptor.setFileName(d_ptr->portDeviceName);
}
QStringList I2CPort::availablePorts()
{
return QDir("/sys/class/i2c-adapter/").entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
}
QList<int> I2CPort::scanRegirsters()
{
return d_ptr->scanRegirsters();
}
int I2CPort::deviceDescriptor() const
{
return d_ptr->deviceDescriptor;
}
int I2CPort::address() const
{
return d_ptr->address;
}
QString I2CPort::portName() const
{
return d_ptr->portName;
}
QString I2CPort::portDeviceName() const
{
return d_ptr->portDeviceName;
}
bool I2CPort::isOpen() const
{
return d_ptr->isOpen();
}
bool I2CPort::isValid() const
{
return d_ptr->isValid();
}
bool I2CPort::openPort(int i2cAddress)
{
return d_ptr->openPort(i2cAddress);
}
void I2CPort::closePort()
{
return d_ptr->closePort();
}
I2CPortPrivate::I2CPortPrivate(I2CPort *q) :
QObject(q),
q_ptr(q)
{
}
QList<int> I2CPortPrivate::scanRegirsters()
{
qCDebug(dcHardware()) << "Scanning I2C device" << portDeviceName;
// Note: these methods are only available on arm since libi2c-dev package is different on amd64 and i386
QList<int> addressList;
#ifdef __arm__
for (int address = 0x3; address <= 0x77; address++) {
// Check if ioctl possible
if (ioctl(deviceDescriptor, I2C_SLAVE, address) >= 0) {
i2c_smbus_write_byte(deviceDescriptor, 0x00);
int id = i2c_smbus_read_byte(deviceDescriptor);
if (id >= 0) {
qCDebug(dcHardware()) << QString(" --> found address = 0x%1").arg(address, 0, 16);
addressList.append(address);
}
}
}
#else
qCWarning(dcHardware()) << "This hardware architecture does not support I2C.";
#endif
return addressList;
}
bool I2CPortPrivate::isOpen() const
{
return fileDescriptor.isOpen();
}
bool I2CPortPrivate::isValid() const
{
return valid;
}
bool I2CPortPrivate::openPort(int i2cAddress)
{
if (fileDescriptor.isOpen()) {
qCWarning(dcHardware()) << "The given I2C file descriptor is already open:" << fileDescriptor.fileName();
return false;
}
if (!fileDescriptor.exists()) {
qCWarning(dcHardware()) << "The given I2C file descriptor does not exist:" << fileDescriptor.fileName();
return false;
}
if (!fileDescriptor.open(QFile::ReadWrite)) {
qCWarning(dcHardware()) << "Could not open the given I2C file descriptor:" << fileDescriptor.fileName();
return false;
}
deviceDescriptor = fileDescriptor.handle();
// if (ioctl(deviceDescriptor, I2C_SLAVE, i2cAddress) < 0) {
// qCWarning(dcHardware()) << "Could not set I2C into slave mode" << portDeviceName << QString("0x%1").arg(i2cAddress, 0, 16);
// closePort();
// return false;
// }
address = i2cAddress;
valid = true;
return true;
}
void I2CPortPrivate::closePort()
{
if (fileDescriptor.isOpen()) {
fileDescriptor.close();
}
deviceDescriptor = -1;
valid = false;
}