@@ -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
4141void 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
8989uint8_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
160170uint8_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