Skip to content

Commit 16d570b

Browse files
committed
Linux i2c rework for better compatibility
1 parent bebaf2d commit 16d570b

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

cores/portduino/linux/LinuxHardwareI2C.cpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212

1313
extern "C"
1414
{
15-
#include<linux/i2c-dev.h>
15+
#include <linux/i2c-dev.h>
1616
#include <i2c/smbus.h>
1717
}
1818

1919

2020
namespace arduino {
21-
char buf[100] = {0};
21+
char TXbuf[1000] = {0};
22+
char RXbuf[1000] = {0};
23+
int RXlen = 0;
24+
size_t RXindex = 0;
2225
int requestedBytes = 0;
2326
int sessionstatus;
2427
LinuxHardwareI2C Wire;
@@ -43,12 +46,17 @@ namespace arduino {
4346

4447
void LinuxHardwareI2C::beginTransmission(uint8_t address) {
4548
sessionstatus = ioctl(i2c_file, I2C_SLAVE, address);
49+
requestedBytes = 0;
4650
}
4751
uint8_t LinuxHardwareI2C::endTransmission(bool stopBit) {
4852
if (requestedBytes) {
49-
int resp = ::write(i2c_file, buf, requestedBytes);
53+
int resp = ::write(i2c_file, TXbuf, requestedBytes);
54+
bool success = resp == requestedBytes;
5055
requestedBytes = 0;
51-
return resp;
56+
if (success)
57+
return 0;
58+
else
59+
return 4;
5260
}
5361
return 0;
5462

@@ -60,15 +68,28 @@ namespace arduino {
6068
}
6169

6270
size_t LinuxHardwareI2C::write(uint8_t toWrite) {
63-
buf[requestedBytes] = toWrite; // todo: alloc and copy
71+
TXbuf[requestedBytes] = toWrite;
6472
requestedBytes++;
6573
return 0;
6674
}
6775
size_t LinuxHardwareI2C::write(const uint8_t *buffer, size_t size) {
68-
return ::write(i2c_file, buffer, size); // Should this defer actual writing til transaction close?
76+
for (int i = 0; i < size; i++) {
77+
TXbuf[requestedBytes] = buffer[i];
78+
requestedBytes++;
79+
}
80+
return size;
6981
}
7082

7183
int LinuxHardwareI2C::read() {
84+
if (RXlen - RXindex != 0) {
85+
int tmpVal = RXbuf[RXindex];
86+
RXindex++;
87+
if (RXindex == RXlen || RXindex > 999) {
88+
RXindex = 0;
89+
RXlen = 0;
90+
}
91+
return tmpVal;
92+
}
7293
int tmpBuf = 0;
7394
if (::read(i2c_file, &tmpBuf, 1) == -1)
7495
return -1;
@@ -82,18 +103,27 @@ namespace arduino {
82103
return length;
83104
} else {
84105
bytes_read = ::read(i2c_file, buffer, length);
85-
if ( bytes_read < 0) bytes_read = 0;
106+
if ( bytes_read < 0)
107+
bytes_read = 0;
86108
}
87109
return bytes_read;
88110
}
89111

90112
int LinuxHardwareI2C::available() {
91-
int numBytes;
113+
if (RXlen - RXindex != 0)
114+
return RXlen - RXindex;
115+
int numBytes = 0;
92116
ioctl(i2c_file, FIONREAD, &numBytes);
93117
return numBytes;
94118
}
95119

96-
uint8_t LinuxHardwareI2C::requestFrom(uint8_t address, size_t) {
97-
return ioctl(i2c_file, I2C_SLAVE, address);
120+
uint8_t LinuxHardwareI2C::requestFrom(uint8_t address, size_t count, bool stop) {
121+
ioctl(i2c_file, I2C_SLAVE, address);
122+
RXlen = ::read(i2c_file, RXbuf, count);
123+
if (RXlen < 1) {
124+
RXlen = 0;
125+
}
126+
RXindex = 0;
127+
return RXlen;
98128
}
99129
}

cores/portduino/linux/LinuxHardwareI2C.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum ResultI2c {
2020
};
2121

2222
class LinuxHardwareI2C : public HardwareI2C {
23-
int i2c_file;
23+
int i2c_file = 0;
2424

2525
public:
2626
void begin(const char* device);
@@ -39,12 +39,11 @@ class LinuxHardwareI2C : public HardwareI2C {
3939

4040
virtual uint8_t endTransmission(void) { return endTransmission(true); }
4141

42-
virtual uint8_t requestFrom(uint8_t address, size_t len, bool stopBit) {
43-
notImplemented("requestFrom");
44-
return 0;
45-
}
42+
virtual uint8_t requestFrom(uint8_t address, size_t len, bool stopBit);
4643

47-
virtual uint8_t requestFrom(uint8_t address, size_t len);
44+
virtual uint8_t requestFrom(uint8_t address, size_t len) {
45+
return requestFrom(address, len, true);
46+
}
4847

4948
virtual void onReceive(void (*)(int)) NOT_IMPLEMENTED("onReceive");
5049

@@ -65,7 +64,7 @@ class LinuxHardwareI2C : public HardwareI2C {
6564
virtual int read();
6665

6766
virtual uint8_t readBytes(uint8_t address, size_t len, bool stopBit) {
68-
notImplemented("requestFrom");
67+
notImplemented("readBytes");
6968
return 0;
7069
}
7170

0 commit comments

Comments
 (0)