Skip to content

Commit

Permalink
Implement sequential read/write for 16-bit operations (#43)
Browse files Browse the repository at this point in the history
* Sequential read/write for 16-bit operations ==> ~30% performance improvement
  • Loading branch information
olkal authored Jun 11, 2024
1 parent fd3c861 commit 7de0753
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions MCP23S17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ bool MCP23S17::begin(bool pullup)
if (! isConnected()) return false;

// disable address increment - datasheet P20
// note that address increment must be enabled for readReg16() and writeReg16() to work.
// SEQOP: Sequential Operation mode bit
// 1 = Sequential operation disabled, address pointer does not increment.
// 0 = Sequential operation enabled, address pointer increments.
// if (! writeReg(MCP23017_IOCR, MCP23017_IOCR_SEQOP)) return false;
uint8_t reg = readReg(MCP23x17_IOCR);
if (reg & MCP23x17_IOCR_SEQOP) // check if already zero
{
reg &= ~MCP23x17_IOCR_SEQOP; // clear SEQOP bit for sequential read/write
if (! writeReg(MCP23x17_IOCR, reg)) return false;
}

if (pullup)
{
Expand Down Expand Up @@ -833,11 +839,6 @@ bool MCP23S17::writeReg16(uint8_t reg, uint16_t value)
_mySPI->transfer(MCP23S17_WRITE_REG | _address );
_mySPI->transfer(reg);
_mySPI->transfer(value >> 8);
::digitalWrite(_select, HIGH);

::digitalWrite(_select, LOW);
_mySPI->transfer(MCP23S17_WRITE_REG | _address );
_mySPI->transfer(reg + 1);
_mySPI->transfer(value & 0xFF);
_mySPI->endTransaction();
}
Expand All @@ -847,11 +848,6 @@ bool MCP23S17::writeReg16(uint8_t reg, uint16_t value)
swSPI_transfer(MCP23S17_WRITE_REG | _address );
swSPI_transfer(reg);
swSPI_transfer(value >> 8);
::digitalWrite(_select, HIGH);

::digitalWrite(_select, LOW);
swSPI_transfer(MCP23S17_WRITE_REG | _address );
swSPI_transfer(reg + 1);
swSPI_transfer(value & 0xFF);
}
::digitalWrite(_select, HIGH);
Expand Down Expand Up @@ -880,11 +876,6 @@ uint16_t MCP23S17::readReg16(uint8_t reg)
_mySPI->transfer(MCP23S17_READ_REG | _address );
_mySPI->transfer(reg);
rv = _mySPI->transfer(0xFF) << 8;
::digitalWrite(_select, HIGH);

::digitalWrite(_select, LOW);
_mySPI->transfer(MCP23S17_READ_REG | _address );
_mySPI->transfer(reg + 1);
rv += _mySPI->transfer(0xFF);
_mySPI->endTransaction();
}
Expand All @@ -894,11 +885,6 @@ uint16_t MCP23S17::readReg16(uint8_t reg)
swSPI_transfer(MCP23S17_READ_REG | _address );
swSPI_transfer(reg);
rv = swSPI_transfer(0xFF) << 8;
::digitalWrite(_select, HIGH);

::digitalWrite(_select, LOW);
swSPI_transfer(MCP23S17_READ_REG | _address );
swSPI_transfer(reg + 1);
rv += swSPI_transfer(0xFF);
}
::digitalWrite(_select, HIGH);
Expand Down

0 comments on commit 7de0753

Please sign in to comment.