@@ -35,7 +35,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
35
35
this ->_p_twi = p_twi;
36
36
this ->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
37
37
this ->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
38
- transmissionBegun = false ;
38
+ this -> transmissionBegun = false ;
39
39
}
40
40
41
41
void TwoWire::begin (void ) {
@@ -54,7 +54,7 @@ void TwoWire::begin(void) {
54
54
| ((uint32_t )GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
55
55
| ((uint32_t )GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
56
56
57
- _p_twi->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100;
57
+ _p_twi->FREQUENCY = ( TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos) ;
58
58
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
59
59
_p_twi->PSELSCL = _uc_pinSCL;
60
60
_p_twi->PSELSDA = _uc_pinSDA;
@@ -78,7 +78,7 @@ void TwoWire::setClock(uint32_t baudrate) {
78
78
frequency = TWI_FREQUENCY_FREQUENCY_K400;
79
79
}
80
80
81
- _p_twi->FREQUENCY = frequency;
81
+ _p_twi->FREQUENCY = ( frequency << TWI_FREQUENCY_FREQUENCY_Pos) ;
82
82
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
83
83
}
84
84
@@ -88,22 +88,34 @@ void TwoWire::end() {
88
88
89
89
uint8_t TwoWire::requestFrom (uint8_t address, size_t quantity, bool stopBit)
90
90
{
91
- if (quantity == 0 )
91
+ if (quantity == 0 )
92
92
{
93
93
return 0 ;
94
94
}
95
+ if (quantity > SERIAL_BUFFER_SIZE)
96
+ {
97
+ quantity = SERIAL_BUFFER_SIZE;
98
+ }
95
99
96
100
size_t byteRead = 0 ;
97
101
rxBuffer.clear ();
98
102
99
103
_p_twi->ADDRESS = address;
100
-
104
+ _p_twi-> SHORTS = 0x1UL ; // To trigger suspend task when a byte is received
101
105
_p_twi->TASKS_RESUME = 0x1UL ;
102
106
_p_twi->TASKS_STARTRX = 0x1UL ;
103
107
104
- for (size_t i = 0 ; i < quantity; i ++)
108
+ for (byteRead = 0 ; byteRead < quantity; byteRead ++)
105
109
{
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 );
107
119
108
120
if (_p_twi->EVENTS_ERROR )
109
121
{
@@ -113,8 +125,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
113
125
_p_twi->EVENTS_RXDREADY = 0x0UL ;
114
126
115
127
rxBuffer.store_char (_p_twi->RXD );
116
-
117
- _p_twi->TASKS_RESUME = 0x1UL ;
118
128
}
119
129
120
130
if (stopBit || _p_twi->EVENTS_ERROR )
@@ -159,11 +169,11 @@ void TwoWire::beginTransmission(uint8_t address) {
159
169
// 4 : Other error
160
170
uint8_t TwoWire::endTransmission (bool stopBit)
161
171
{
162
- transmissionBegun = false ;
172
+ transmissionBegun = false ;
163
173
164
174
// Start I2C transmission
165
175
_p_twi->ADDRESS = txAddress;
166
-
176
+ _p_twi-> SHORTS = 0x0UL ;
167
177
_p_twi->TASKS_RESUME = 0x1UL ;
168
178
_p_twi->TASKS_STARTTX = 0x1UL ;
169
179
0 commit comments