Skip to content

Commit

Permalink
smaller memory footprint if reading UID only (save ~500 Bytes)
Browse files Browse the repository at this point in the history
  • Loading branch information
tueddy committed Jan 13, 2024
1 parent 0cbdf65 commit 2128fd7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 21 additions & 1 deletion PN5180.cpp
Expand Up @@ -38,7 +38,7 @@
#define PN5180_RF_ON (0x16)
#define PN5180_RF_OFF (0x17)

uint8_t PN5180::readBuffer[508];
uint8_t PN5180::readBufferStatic16[16];

PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi) :
PN5180_NSS(SSpin),
Expand All @@ -56,6 +56,11 @@ PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi) :
SPI_SETTINGS = SPISettings(7000000, MSBFIRST, SPI_MODE0);
}

PN5180::~PN5180() {
if (readBufferDynamic508) {
free(readBufferDynamic508);
}
}

void PN5180::begin() {
pinMode(PN5180_NSS, OUTPUT);
Expand Down Expand Up @@ -337,6 +342,21 @@ uint8_t * PN5180::readData(int len) {

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

uint8_t *readBuffer;
if (len <=16) {
// use a smaller static buffer, e.g. if reading the uid only
readBuffer = readBufferStatic16;
} else {
// allocate the max buffer length of 508 bytes
if (!readBufferDynamic508) {
readBufferDynamic508 = (uint8_t *) malloc(508);
if (!readBufferDynamic508) {
PN5180DEBUG(F("Cannot allocate the read buffer of 508 Bytes!"));
return NULL;
}
}
readBuffer = readBufferDynamic508;
}
PN5180_SPI.beginTransaction(SPI_SETTINGS);
transceiveCommand(cmd, sizeof(cmd), readBuffer, len);
PN5180_SPI.endTransaction();
Expand Down
5 changes: 3 additions & 2 deletions PN5180.h
Expand Up @@ -82,10 +82,11 @@ class PN5180 {
SPIClass& PN5180_SPI;

SPISettings SPI_SETTINGS;
static uint8_t readBuffer[508];

static uint8_t readBufferStatic16[16];
uint8_t* readBufferDynamic508 = NULL;
public:
PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass& spi=SPI);
~PN5180();

void begin();
void end();
Expand Down

0 comments on commit 2128fd7

Please sign in to comment.