@@ -36,7 +36,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, IRQn_Type IRQn, uint8_t pinSDA, uint8_t p
36
36
this ->_IRQn = IRQn;
37
37
this ->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
38
38
this ->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
39
- transmissionBegun = false ;
39
+ this -> transmissionBegun = false ;
40
40
}
41
41
42
42
void TwoWire::begin (void ) {
@@ -55,7 +55,7 @@ void TwoWire::begin(void) {
55
55
| ((uint32_t )GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
56
56
| ((uint32_t )GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
57
57
58
- _p_twi->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100;
58
+ _p_twi->FREQUENCY = ( TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos) ;
59
59
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
60
60
_p_twi->PSELSCL = _uc_pinSCL;
61
61
_p_twi->PSELSDA = _uc_pinSDA;
@@ -83,7 +83,7 @@ void TwoWire::setClock(uint32_t baudrate) {
83
83
frequency = TWI_FREQUENCY_FREQUENCY_K400;
84
84
}
85
85
86
- _p_twi->FREQUENCY = frequency;
86
+ _p_twi->FREQUENCY = ( frequency << TWI_FREQUENCY_FREQUENCY_Pos) ;
87
87
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
88
88
}
89
89
@@ -93,22 +93,34 @@ void TwoWire::end() {
93
93
94
94
uint8_t TwoWire::requestFrom (uint8_t address, size_t quantity, bool stopBit)
95
95
{
96
- if (quantity == 0 )
96
+ if (quantity == 0 )
97
97
{
98
98
return 0 ;
99
99
}
100
+ if (quantity > SERIAL_BUFFER_SIZE)
101
+ {
102
+ quantity = SERIAL_BUFFER_SIZE;
103
+ }
100
104
101
105
size_t byteRead = 0 ;
102
106
rxBuffer.clear ();
103
107
104
108
_p_twi->ADDRESS = address;
105
-
109
+ _p_twi-> SHORTS = 0x1UL ; // To trigger suspend task when a byte is received
106
110
_p_twi->TASKS_RESUME = 0x1UL ;
107
111
_p_twi->TASKS_STARTRX = 0x1UL ;
108
112
109
- for (size_t i = 0 ; i < quantity; i ++)
113
+ for (byteRead = 0 ; byteRead < quantity; byteRead ++)
110
114
{
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 );
112
124
113
125
if (_p_twi->EVENTS_ERROR )
114
126
{
@@ -118,8 +130,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
118
130
_p_twi->EVENTS_RXDREADY = 0x0UL ;
119
131
120
132
rxBuffer.store_char (_p_twi->RXD );
121
-
122
- _p_twi->TASKS_RESUME = 0x1UL ;
123
133
}
124
134
125
135
if (stopBit || _p_twi->EVENTS_ERROR )
@@ -164,11 +174,11 @@ void TwoWire::beginTransmission(uint8_t address) {
164
174
// 4 : Other error
165
175
uint8_t TwoWire::endTransmission (bool stopBit)
166
176
{
167
- transmissionBegun = false ;
177
+ transmissionBegun = false ;
168
178
169
179
// Start I2C transmission
170
180
_p_twi->ADDRESS = txAddress;
171
-
181
+ _p_twi-> SHORTS = 0x0UL ;
172
182
_p_twi->TASKS_RESUME = 0x1UL ;
173
183
_p_twi->TASKS_STARTTX = 0x1UL ;
174
184
0 commit comments