Skip to content

Commit 3196bfb

Browse files
committed
Fix nRF51 I2C read operation
1 parent 8747fa2 commit 3196bfb

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
@@ -91,7 +89,7 @@ class TwoWire : public Stream
9189
// RX Buffer
9290
RingBuffer rxBuffer;
9391

94-
//TX buffer
92+
// TX buffer
9593
RingBuffer txBuffer;
9694
uint8_t txAddress;
9795

libraries/Wire/Wire_nRF51.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, IRQn_Type IRQn, uint8_t pinSDA, uint8_t p
3636
this->_IRQn = IRQn;
3737
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
3838
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
39-
transmissionBegun = false;
39+
this->transmissionBegun = false;
4040
}
4141

4242
void TwoWire::begin(void) {
@@ -55,7 +55,7 @@ void TwoWire::begin(void) {
5555
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
5656
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
5757

58-
_p_twi->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100;
58+
_p_twi->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
5959
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
6060
_p_twi->PSELSCL = _uc_pinSCL;
6161
_p_twi->PSELSDA = _uc_pinSDA;
@@ -83,7 +83,7 @@ void TwoWire::setClock(uint32_t baudrate) {
8383
frequency = TWI_FREQUENCY_FREQUENCY_K400;
8484
}
8585

86-
_p_twi->FREQUENCY = frequency;
86+
_p_twi->FREQUENCY = (frequency << TWI_FREQUENCY_FREQUENCY_Pos);
8787
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
8888
}
8989

@@ -93,22 +93,34 @@ void TwoWire::end() {
9393

9494
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
9595
{
96-
if(quantity == 0)
96+
if (quantity == 0)
9797
{
9898
return 0;
9999
}
100+
if (quantity > SERIAL_BUFFER_SIZE)
101+
{
102+
quantity = SERIAL_BUFFER_SIZE;
103+
}
100104

101105
size_t byteRead = 0;
102106
rxBuffer.clear();
103107

104108
_p_twi->ADDRESS = address;
105-
109+
_p_twi->SHORTS = 0x1UL; // To trigger suspend task when a byte is received
106110
_p_twi->TASKS_RESUME = 0x1UL;
107111
_p_twi->TASKS_STARTRX = 0x1UL;
108112

109-
for (size_t i = 0; i < quantity; i++)
113+
for (byteRead = 0; byteRead < quantity; byteRead++)
110114
{
111-
while(!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
115+
if (byteRead == quantity - 1)
116+
{
117+
// To trigger stop task when last byte is received, set before resume task.
118+
_p_twi->SHORTS = 0x2UL;
119+
}
120+
121+
_p_twi->TASKS_RESUME = 0x1UL;
122+
123+
while (!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
112124

113125
if (_p_twi->EVENTS_ERROR)
114126
{
@@ -118,8 +130,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
118130
_p_twi->EVENTS_RXDREADY = 0x0UL;
119131

120132
rxBuffer.store_char(_p_twi->RXD);
121-
122-
_p_twi->TASKS_RESUME = 0x1UL;
123133
}
124134

125135
if (stopBit || _p_twi->EVENTS_ERROR)
@@ -164,11 +174,11 @@ void TwoWire::beginTransmission(uint8_t address) {
164174
// 4 : Other error
165175
uint8_t TwoWire::endTransmission(bool stopBit)
166176
{
167-
transmissionBegun = false ;
177+
transmissionBegun = false;
168178

169179
// Start I2C transmission
170180
_p_twi->ADDRESS = txAddress;
171-
181+
_p_twi->SHORTS = 0x0UL;
172182
_p_twi->TASKS_RESUME = 0x1UL;
173183
_p_twi->TASKS_STARTTX = 0x1UL;
174184

0 commit comments

Comments
 (0)