Skip to content

Commit

Permalink
call spi.beginTransaction every time we do an SPI transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
danverstom committed Dec 18, 2022
1 parent 3d91a66 commit 7c714d3
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SPIProtocol::SPIProtocol(byte chipSelect, SPIClass spiChannel, SPISettings setti

uint8_t SPIProtocol::protocol_begin() {
pinMode(CS, OUTPUT); // Other pins are configured on spi.begin().
_spi.beginTransaction(_settings);
digitalWrite(CS, HIGH);
_spi.begin(); // you have to begin the SPI bus before you can use it --_--
return 0; // 0 for success - not applicable to all protocols
}
Expand All @@ -110,6 +110,7 @@ byte SPIProtocol::read_reg(byte regAddress) {
regAddress = READ_BYTE | regAddress; // puts read bit into the 8th bit.
byte inByte = 0;
digitalWrite(CS, LOW); // pulls CS low, which begins the transfer
_spi.beginTransaction(_settings);
_spi.transfer(regAddress); // transfers address over MOSI line
inByte = _spi.transfer(0x00);
digitalWrite(CS, HIGH); // pulls CS high, which ends the transfer
Expand All @@ -121,6 +122,7 @@ void SPIProtocol::read_regs(byte regAddress, byte *outputPointer, uint length) {
regAddress = READ_BYTE | regAddress; // puts read bit into the 8th bit.
byte inByte = 0;
digitalWrite(CS, LOW); // pulls CS low, which begins the transfer
_spi.beginTransaction(_settings);
_spi.transfer(regAddress); // transfer address of desired register
for (uint i=0; i<length; i++) {
inByte = _spi.transfer(0x00); // transfers 0x00 over MOSI line, recieves a byte over MISO line.
Expand All @@ -133,6 +135,7 @@ void SPIProtocol::read_regs(byte regAddress, byte *outputPointer, uint length) {
uint8_t SPIProtocol::write_reg(byte regAddress, byte data) {
regAddress = WRITE_BYTE | regAddress; //
digitalWrite(CS, LOW); // pulls CS low, which begins the transfer
_spi.beginTransaction(_settings);
_spi.transfer(regAddress);
_spi.transfer(data);
digitalWrite(CS, HIGH); // pulls CS high, which ends the transfer
Expand All @@ -142,6 +145,7 @@ uint8_t SPIProtocol::write_reg(byte regAddress, byte data) {
uint8_t SPIProtocol::write_regs(byte regAddress, byte *writeBuffer, uint length) {
regAddress = WRITE_BYTE | regAddress;
digitalWrite(CS, LOW); // pulls CS low, which begins the transfer
_spi.beginTransaction(_settings);
_spi.transfer(regAddress);
for (uint i=0; i<length; i++) {
_spi.transfer(*writeBuffer);
Expand Down

0 comments on commit 7c714d3

Please sign in to comment.