@@ -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
4242void 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
9494uint8_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
165175uint8_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