Skip to content

Commit f3f2b0b

Browse files
committed
Improve Wire library
still unreliable though
1 parent a33835c commit f3f2b0b

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

cores/arduino/i2c.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "i2c.h"
2424
#include "variant.h"
2525

26-
#define TIMEOUT 0xFFFFF
26+
#define TIMEOUT 16
2727

2828
int i2c_getadapter(uint32_t i2c_bus_address)
2929
{
@@ -34,7 +34,7 @@ static volatile uint8_t i2c_tx_complete;
3434
static volatile uint8_t i2c_rx_complete;
3535
static volatile uint8_t i2c_err_detect;
3636

37-
uint8_t i2c_slave = 0;
37+
static volatile uint8_t i2c_slave = 0;
3838

3939
static void ss_i2c_rx(uint32_t dev_id)
4040
{
@@ -53,27 +53,29 @@ static void ss_i2c_err(uint32_t dev_id)
5353

5454
static int wait_rx_or_err(){
5555
uint64_t timeout = TIMEOUT;
56-
while(timeout--)
57-
if ((i2c_rx_complete) || (i2c_err_detect)) {
58-
if (i2c_rx_complete)
59-
return I2C_OK;
60-
else if (i2c_err_detect)
61-
return I2C_ERROR;
62-
break;
56+
while(timeout--) {
57+
if (i2c_err_detect) {
58+
return I2C_ERROR;
6359
}
60+
if (i2c_rx_complete) {
61+
return I2C_OK;
62+
}
63+
delay(1);
64+
}
6465
return I2C_TIMEOUT;
6566
}
6667

6768
static int wait_tx_or_err(){
6869
uint64_t timeout = TIMEOUT;
69-
while(timeout--)
70-
if ((i2c_tx_complete) || (i2c_err_detect)) {
71-
if (i2c_rx_complete)
72-
return I2C_OK;
73-
else if (i2c_err_detect)
74-
return I2C_ERROR;
75-
break;
70+
while(timeout--) {
71+
if (i2c_err_detect) {
72+
return I2C_ERROR;
73+
}
74+
if (i2c_tx_complete) {
75+
return I2C_OK;
7676
}
77+
delay(1);
78+
}
7779
return I2C_TIMEOUT;
7880
}
7981

@@ -104,7 +106,7 @@ int i2c_openadapter(uint8_t i2c_adapter_nr)
104106

105107
i2c_cfg_data_t i2c_cfg;
106108

107-
i2c_cfg.speed = I2C_SLOW;
109+
i2c_cfg.speed = I2C_FAST;
108110
i2c_cfg.addressing_mode = I2C_7_Bit;
109111
i2c_cfg.mode_type = I2C_MASTER;
110112
i2c_cfg.cb_tx = ss_i2c_tx;
@@ -136,19 +138,20 @@ int i2c_writebyte(uint8_t byte)
136138
i2c_tx_complete = 0;
137139
i2c_err_detect = 0;
138140
ss_i2c_write(I2C_SENSING_0, ch, 1, i2c_slave);
141+
139142
ret = wait_tx_or_err();
140143
if (ret)
141144
return ret;
142145

143146
ret = wait_dev_ready(I2C_SENSING_0);
144147
if (ret)
145-
return I2C_ERROR;
148+
return ret;
146149
return 1; /* number of bytes */
147150
}
148151

149152
int i2c_writebytes(uint8_t *bytes, uint8_t length)
150153
{
151-
uint8_t ret;
154+
int ret;
152155

153156
i2c_tx_complete = 0;
154157
i2c_err_detect = 0;
@@ -165,7 +168,7 @@ int i2c_writebytes(uint8_t *bytes, uint8_t length)
165168
int i2c_readbyte()
166169
{
167170
unsigned char byte;
168-
uint8_t ret;
171+
int ret;
169172

170173
i2c_rx_complete = 0;
171174
i2c_err_detect = 0;
@@ -181,7 +184,7 @@ int i2c_readbyte()
181184

182185
int i2c_readbytes(uint8_t *buf, int length)
183186
{
184-
uint8_t ret;
187+
int ret;
185188

186189
i2c_rx_complete = 0;
187190
i2c_err_detect = 0;
@@ -197,7 +200,7 @@ int i2c_readbytes(uint8_t *buf, int length)
197200

198201
int i2c_transferbytes(uint8_t *tx_buf, int tx_length, uint8_t *rx_buf, int rx_length)
199202
{
200-
uint8_t ret;
203+
int ret;
201204

202205
i2c_tx_complete = 0;
203206
i2c_rx_complete = 0;

libraries/Wire/Wire.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void TwoWire::begin(int address)
6464

6565
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
6666
{
67+
int ret;
6768
if (quantity > BUFFER_LENGTH)
6869
quantity = BUFFER_LENGTH;
6970

@@ -89,8 +90,10 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop
8990
} else
9091
return 0;
9192
}
92-
if (i2c_readbytes(rxBuffer, quantity) < 0)
93+
ret = i2c_readbytes(rxBuffer, quantity);
94+
if (ret < 0) {
9395
return 0;
96+
}
9497
// set rx buffer iterator vars
9598
rxBufferIndex = 0;
9699
rxBufferLength = quantity;
@@ -154,16 +157,18 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
154157
err = i2c_writebytes(txBuffer, txBufferLength);
155158
else if (txBufferLength == 1)
156159
err = i2c_writebyte(*txBuffer);
157-
else
160+
else {
158161
/* FIXME: A zero byte transmit is typically used to check for an
159162
* ACK from the slave device. I'm not sure if this is the
160163
* correct way to do this.
161164
*/
162165
err = i2c_readbyte();
166+
}
163167
// empty buffer
164168
txBufferLength = 0;
165-
if (err < 0)
169+
if (err < 0) {
166170
return 2;
171+
}
167172
return 0;
168173
} else {
169174
/* sendStop = false */

0 commit comments

Comments
 (0)