Commit 3196bfb9 authored by carlosperate's avatar carlosperate

Fix nRF51 I2C read operation

parent 8747fa2d
...@@ -28,9 +28,7 @@ ...@@ -28,9 +28,7 @@
#include "RingBuffer.h" #include "RingBuffer.h"
#define BUFFER_LENGTH 32 // WIRE_HAS_END means Wire has end()
// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END 1 #define WIRE_HAS_END 1
class TwoWire : public Stream class TwoWire : public Stream
...@@ -91,7 +89,7 @@ class TwoWire : public Stream ...@@ -91,7 +89,7 @@ class TwoWire : public Stream
// RX Buffer // RX Buffer
RingBuffer rxBuffer; RingBuffer rxBuffer;
//TX buffer // TX buffer
RingBuffer txBuffer; RingBuffer txBuffer;
uint8_t txAddress; uint8_t txAddress;
......
...@@ -36,7 +36,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, IRQn_Type IRQn, uint8_t pinSDA, uint8_t p ...@@ -36,7 +36,7 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, IRQn_Type IRQn, uint8_t pinSDA, uint8_t p
this->_IRQn = IRQn; this->_IRQn = IRQn;
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
transmissionBegun = false; this->transmissionBegun = false;
} }
void TwoWire::begin(void) { void TwoWire::begin(void) {
...@@ -55,7 +55,7 @@ void TwoWire::begin(void) { ...@@ -55,7 +55,7 @@ void TwoWire::begin(void) {
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
_p_twi->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100; _p_twi->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos); _p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
_p_twi->PSELSCL = _uc_pinSCL; _p_twi->PSELSCL = _uc_pinSCL;
_p_twi->PSELSDA = _uc_pinSDA; _p_twi->PSELSDA = _uc_pinSDA;
...@@ -83,7 +83,7 @@ void TwoWire::setClock(uint32_t baudrate) { ...@@ -83,7 +83,7 @@ void TwoWire::setClock(uint32_t baudrate) {
frequency = TWI_FREQUENCY_FREQUENCY_K400; frequency = TWI_FREQUENCY_FREQUENCY_K400;
} }
_p_twi->FREQUENCY = frequency; _p_twi->FREQUENCY = (frequency << TWI_FREQUENCY_FREQUENCY_Pos);
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos); _p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
} }
...@@ -93,22 +93,34 @@ void TwoWire::end() { ...@@ -93,22 +93,34 @@ void TwoWire::end() {
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
{ {
if(quantity == 0) if (quantity == 0)
{ {
return 0; return 0;
} }
if (quantity > SERIAL_BUFFER_SIZE)
{
quantity = SERIAL_BUFFER_SIZE;
}
size_t byteRead = 0; size_t byteRead = 0;
rxBuffer.clear(); rxBuffer.clear();
_p_twi->ADDRESS = address; _p_twi->ADDRESS = address;
_p_twi->SHORTS = 0x1UL; // To trigger suspend task when a byte is received
_p_twi->TASKS_RESUME = 0x1UL; _p_twi->TASKS_RESUME = 0x1UL;
_p_twi->TASKS_STARTRX = 0x1UL; _p_twi->TASKS_STARTRX = 0x1UL;
for (size_t i = 0; i < quantity; i++) for (byteRead = 0; byteRead < quantity; byteRead++)
{ {
while(!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR); if (byteRead == quantity - 1)
{
// To trigger stop task when last byte is received, set before resume task.
_p_twi->SHORTS = 0x2UL;
}
_p_twi->TASKS_RESUME = 0x1UL;
while (!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
if (_p_twi->EVENTS_ERROR) if (_p_twi->EVENTS_ERROR)
{ {
...@@ -118,8 +130,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit) ...@@ -118,8 +130,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
_p_twi->EVENTS_RXDREADY = 0x0UL; _p_twi->EVENTS_RXDREADY = 0x0UL;
rxBuffer.store_char(_p_twi->RXD); rxBuffer.store_char(_p_twi->RXD);
_p_twi->TASKS_RESUME = 0x1UL;
} }
if (stopBit || _p_twi->EVENTS_ERROR) if (stopBit || _p_twi->EVENTS_ERROR)
...@@ -164,11 +174,11 @@ void TwoWire::beginTransmission(uint8_t address) { ...@@ -164,11 +174,11 @@ void TwoWire::beginTransmission(uint8_t address) {
// 4 : Other error // 4 : Other error
uint8_t TwoWire::endTransmission(bool stopBit) uint8_t TwoWire::endTransmission(bool stopBit)
{ {
transmissionBegun = false ; transmissionBegun = false;
// Start I2C transmission // Start I2C transmission
_p_twi->ADDRESS = txAddress; _p_twi->ADDRESS = txAddress;
_p_twi->SHORTS = 0x0UL;
_p_twi->TASKS_RESUME = 0x1UL; _p_twi->TASKS_RESUME = 0x1UL;
_p_twi->TASKS_STARTTX = 0x1UL; _p_twi->TASKS_STARTTX = 0x1UL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment