Skip to content

Commit 9c0406a

Browse files
committed
Merge branch 'i2c_read' into hyperfirmata
2 parents fd4c781 + 3196bfb commit 9c0406a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

libraries/Wire/Wire.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828

2929
#include "RingBuffer.h"
3030

31-
#define BUFFER_LENGTH 32
32-
33-
// WIRE_HAS_END means Wire has end()
31+
// WIRE_HAS_END means Wire has end()
3432
#define WIRE_HAS_END 1
3533

3634
class TwoWire : public Stream
@@ -90,7 +88,7 @@ class TwoWire : public Stream
9088
// RX Buffer
9189
RingBuffer rxBuffer;
9290

93-
//TX buffer
91+
// TX buffer
9492
RingBuffer txBuffer;
9593
uint8_t txAddress;
9694

libraries/Wire/Wire_nRF51.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
3535
this->_p_twi = p_twi;
3636
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
3737
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
38-
transmissionBegun = false;
38+
this->transmissionBegun = false;
3939
}
4040

4141
void TwoWire::begin(void) {
@@ -54,7 +54,7 @@ void TwoWire::begin(void) {
5454
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
5555
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
5656

57-
_p_twi->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100;
57+
_p_twi->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
5858
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
5959
_p_twi->PSELSCL = _uc_pinSCL;
6060
_p_twi->PSELSDA = _uc_pinSDA;
@@ -78,7 +78,7 @@ void TwoWire::setClock(uint32_t baudrate) {
7878
frequency = TWI_FREQUENCY_FREQUENCY_K400;
7979
}
8080

81-
_p_twi->FREQUENCY = frequency;
81+
_p_twi->FREQUENCY = (frequency << TWI_FREQUENCY_FREQUENCY_Pos);
8282
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
8383
}
8484

@@ -88,22 +88,34 @@ void TwoWire::end() {
8888

8989
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
9090
{
91-
if(quantity == 0)
91+
if (quantity == 0)
9292
{
9393
return 0;
9494
}
95+
if (quantity > SERIAL_BUFFER_SIZE)
96+
{
97+
quantity = SERIAL_BUFFER_SIZE;
98+
}
9599

96100
size_t byteRead = 0;
97101
rxBuffer.clear();
98102

99103
_p_twi->ADDRESS = address;
100-
104+
_p_twi->SHORTS = 0x1UL; // To trigger suspend task when a byte is received
101105
_p_twi->TASKS_RESUME = 0x1UL;
102106
_p_twi->TASKS_STARTRX = 0x1UL;
103107

104-
for (size_t i = 0; i < quantity; i++)
108+
for (byteRead = 0; byteRead < quantity; byteRead++)
105109
{
106-
while(!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
110+
if (byteRead == quantity - 1)
111+
{
112+
// To trigger stop task when last byte is received, set before resume task.
113+
_p_twi->SHORTS = 0x2UL;
114+
}
115+
116+
_p_twi->TASKS_RESUME = 0x1UL;
117+
118+
while (!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
107119

108120
if (_p_twi->EVENTS_ERROR)
109121
{
@@ -113,8 +125,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
113125
_p_twi->EVENTS_RXDREADY = 0x0UL;
114126

115127
rxBuffer.store_char(_p_twi->RXD);
116-
117-
_p_twi->TASKS_RESUME = 0x1UL;
118128
}
119129

120130
if (stopBit || _p_twi->EVENTS_ERROR)
@@ -159,11 +169,11 @@ void TwoWire::beginTransmission(uint8_t address) {
159169
// 4 : Other error
160170
uint8_t TwoWire::endTransmission(bool stopBit)
161171
{
162-
transmissionBegun = false ;
172+
transmissionBegun = false;
163173

164174
// Start I2C transmission
165175
_p_twi->ADDRESS = txAddress;
166-
176+
_p_twi->SHORTS = 0x0UL;
167177
_p_twi->TASKS_RESUME = 0x1UL;
168178
_p_twi->TASKS_STARTTX = 0x1UL;
169179

0 commit comments

Comments
 (0)