Skip to content

Commit

Permalink
Add support for non-default SPI class
Browse files Browse the repository at this point in the history
This implementation looks quite similar to @tyllmoritz previous changes,
but it doesn't copy the SPI class. Instead, the class member is now a
reference, which is initialized in the constructor.

Some other members have also been moved into the initializer of the
constructor.
  • Loading branch information
mwick83 committed May 8, 2021
1 parent b8408b8 commit b99b54d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
71 changes: 36 additions & 35 deletions PN5180.cpp
Expand Up @@ -39,11 +39,12 @@

uint8_t PN5180::readBuffer[508];

PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin) {
PN5180_NSS = SSpin;
PN5180_BUSY = BUSYpin;
PN5180_RST = RSTpin;

PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi) :
PN5180_NSS(SSpin),
PN5180_BUSY(BUSYpin),
PN5180_RST(RSTpin),
PN5180_SPI(spi)
{
/*
* 11.4.1 Physical Host Interface
* The interface of the PN5180 to a host microcontroller is based on a SPI interface,
Expand All @@ -63,7 +64,7 @@ void PN5180::begin() {
digitalWrite(PN5180_NSS, HIGH); // disable
digitalWrite(PN5180_RST, HIGH); // no reset

SPI.begin();
PN5180_SPI.begin();
PN5180DEBUG(F("SPI pinout: "));
PN5180DEBUG(F("SS=")); PN5180DEBUG(SS);
PN5180DEBUG(F(", MOSI=")); PN5180DEBUG(MOSI);
Expand All @@ -74,7 +75,7 @@ void PN5180::begin() {

void PN5180::end() {
digitalWrite(PN5180_NSS, HIGH); // disable
SPI.end();
PN5180_SPI.end();
}

/*
Expand Down Expand Up @@ -102,9 +103,9 @@ bool PN5180::writeRegister(uint8_t reg, uint32_t value) {
*/
uint8_t buf[6] = { PN5180_WRITE_REGISTER, reg, p[0], p[1], p[2], p[3] };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(buf, 6);
SPI.endTransaction();
PN5180_SPI.endTransaction();

return true;
}
Expand Down Expand Up @@ -132,9 +133,9 @@ bool PN5180::writeRegisterWithOrMask(uint8_t reg, uint32_t mask) {

uint8_t buf[6] = { PN5180_WRITE_REGISTER_OR_MASK, reg, p[0], p[1], p[2], p[3] };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(buf, 6);
SPI.endTransaction();
PN5180_SPI.endTransaction();

return true;
}
Expand Down Expand Up @@ -162,9 +163,9 @@ bool PN5180::writeRegisterWithAndMask(uint8_t reg, uint32_t mask) {

uint8_t buf[6] = { PN5180_WRITE_REGISTER_AND_MASK, reg, p[0], p[1], p[2], p[3] };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(buf, 6);
SPI.endTransaction();
PN5180_SPI.endTransaction();

return true;
}
Expand All @@ -183,9 +184,9 @@ bool PN5180::readRegister(uint8_t reg, uint32_t *value) {

uint8_t cmd[2] = { PN5180_READ_REGISTER, reg };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 2, (uint8_t*)value, 4);
SPI.endTransaction();
PN5180_SPI.endTransaction();

PN5180DEBUG(F("Register value=0x"));
PN5180DEBUG(formatHex(*value));
Expand All @@ -202,9 +203,9 @@ bool PN5180::writeEEprom(uint8_t addr, uint8_t *buffer, uint8_t len) {
cmd[0] = PN5180_WRITE_EEPROM;
cmd[1] = addr;
for (int i = 0; i < len; i++) cmd[2 + i] = buffer[i];
SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, len + 2);
SPI.endTransaction();
PN5180_SPI.endTransaction();
return true;
}

Expand Down Expand Up @@ -233,9 +234,9 @@ bool PN5180::readEEprom(uint8_t addr, uint8_t *buffer, int len) {

uint8_t cmd[3] = { PN5180_READ_EEPROM, addr, uint8_t(len) };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 3, buffer, len);
SPI.endTransaction();
PN5180_SPI.endTransaction();

#ifdef DEBUG
PN5180DEBUG(F("EEPROM values: "));
Expand Down Expand Up @@ -306,9 +307,9 @@ bool PN5180::sendData(uint8_t *data, int len, uint8_t validBits) {
return false;
}

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
bool success = transceiveCommand(buffer, len+2);
SPI.endTransaction();
PN5180_SPI.endTransaction();

return success;
}
Expand All @@ -335,9 +336,9 @@ uint8_t * PN5180::readData(int len) {

uint8_t cmd[2] = { PN5180_READ_DATA, 0x00 };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 2, readBuffer, len);
SPI.endTransaction();
PN5180_SPI.endTransaction();

#ifdef DEBUG
PN5180DEBUG(F("Data read: "));
Expand All @@ -356,9 +357,9 @@ bool PN5180::readData(uint8_t len, uint8_t *buffer) {
return false;
}
uint8_t cmd[2] = { PN5180_READ_DATA, 0x00 };
SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
bool success = transceiveCommand(cmd, 2, buffer, len);
SPI.endTransaction();
PN5180_SPI.endTransaction();
return success;
}

Expand Down Expand Up @@ -430,9 +431,9 @@ bool PN5180::switchToLPCD(uint16_t wakeupCounterInMs) {
writeRegister(IRQ_ENABLE, LPCD_IRQ_STAT | GENERAL_ERROR_IRQ_STAT);
// switch mode to LPCD
uint8_t cmd[4] = { PN5180_SWITCH_MODE, 0x01, (uint8_t)(wakeupCounterInMs & 0xFF), (uint8_t)((wakeupCounterInMs >> 8U) & 0xFF) };
SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
bool success = transceiveCommand(cmd, sizeof(cmd));
SPI.endTransaction();
PN5180_SPI.endTransaction();
return success;
}

Expand Down Expand Up @@ -463,9 +464,9 @@ bool PN5180::loadRFConfig(uint8_t txConf, uint8_t rxConf) {

uint8_t cmd[3] = { PN5180_LOAD_RF_CONFIG, txConf, rxConf };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 3);
SPI.endTransaction();
PN5180_SPI.endTransaction();

return true;
}
Expand All @@ -480,9 +481,9 @@ bool PN5180::setRF_on() {

uint8_t cmd[2] = { PN5180_RF_ON, 0x00 };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 2);
SPI.endTransaction();
PN5180_SPI.endTransaction();

while (0 == (TX_RFON_IRQ_STAT & getIRQStatus())); // wait for RF field to set up
clearIRQStatus(TX_RFON_IRQ_STAT);
Expand All @@ -499,9 +500,9 @@ bool PN5180::setRF_off() {

uint8_t cmd[2] { PN5180_RF_OFF, 0x00 };

SPI.beginTransaction(SPI_SETTINGS);
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, 2);
SPI.endTransaction();
PN5180_SPI.endTransaction();

while (0 == (TX_RFOFF_IRQ_STAT & getIRQStatus())); // wait for RF field to shut down
clearIRQStatus(TX_RFOFF_IRQ_STAT);
Expand Down Expand Up @@ -565,7 +566,7 @@ bool PN5180::transceiveCommand(uint8_t *sendBuffer, size_t sendBufferLen, uint8_
digitalWrite(PN5180_NSS, LOW); delay(1);
// 2.
for (uint8_t i=0; i<sendBufferLen; i++) {
SPI.transfer(sendBuffer[i]);
PN5180_SPI.transfer(sendBuffer[i]);
}
// 3.
startedWaiting = millis();
Expand All @@ -589,7 +590,7 @@ bool PN5180::transceiveCommand(uint8_t *sendBuffer, size_t sendBufferLen, uint8_
digitalWrite(PN5180_NSS, LOW); delay(1);
// 2.
for (uint8_t i=0; i<recvBufferLen; i++) {
recvBuffer[i] = SPI.transfer(0xff);
recvBuffer[i] = PN5180_SPI.transfer(0xff);
}
// 3.
startedWaiting = millis();
Expand Down
3 changes: 2 additions & 1 deletion PN5180.h
Expand Up @@ -76,12 +76,13 @@ class PN5180 {
uint8_t PN5180_NSS; // active low
uint8_t PN5180_BUSY;
uint8_t PN5180_RST;
SPIClass& PN5180_SPI;

SPISettings SPI_SETTINGS;
static uint8_t readBuffer[508];

public:
PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin);
PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi=SPI);

void begin();
void end();
Expand Down
4 changes: 2 additions & 2 deletions PN5180ISO14443.cpp
Expand Up @@ -23,8 +23,8 @@
#include <PN5180.h>
#include "Debug.h"

PN5180ISO14443::PN5180ISO14443(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin)
: PN5180(SSpin, BUSYpin, RSTpin) {
PN5180ISO14443::PN5180ISO14443(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi)
: PN5180(SSpin, BUSYpin, RSTpin, spi) {
}

bool PN5180ISO14443::setupRF() {
Expand Down
2 changes: 1 addition & 1 deletion PN5180ISO14443.h
Expand Up @@ -24,7 +24,7 @@
class PN5180ISO14443 : public PN5180 {

public:
PN5180ISO14443(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin);
PN5180ISO14443(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi=SPI);

private:
uint16_t rxBytesReceived();
Expand Down
4 changes: 2 additions & 2 deletions PN5180ISO15693.cpp
Expand Up @@ -22,8 +22,8 @@
#include "PN5180ISO15693.h"
#include "Debug.h"

PN5180ISO15693::PN5180ISO15693(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin)
: PN5180(SSpin, BUSYpin, RSTpin) {
PN5180ISO15693::PN5180ISO15693(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi)
: PN5180(SSpin, BUSYpin, RSTpin, spi) {
}

/*
Expand Down
2 changes: 1 addition & 1 deletion PN5180ISO15693.h
Expand Up @@ -39,7 +39,7 @@ enum ISO15693ErrorCode {
class PN5180ISO15693 : public PN5180 {

public:
PN5180ISO15693(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin);
PN5180ISO15693(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi=SPI);

private:
ISO15693ErrorCode issueISO15693Command(uint8_t *cmd, uint8_t cmdLen, uint8_t **resultPtr);
Expand Down

1 comment on commit b99b54d

@tyllmoritz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for fixing my incorrect attempt to implement this change!

Please sign in to comment.