From 1e479b9ad37b03576bc8dac126d82ab3042061f3 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Mon, 28 Jun 2021 10:44:02 +0100 Subject: [PATCH] Move SPI buffer new (memory allocation) into begin --- src/SparkFun_u-blox_GNSS_Arduino_Library.cpp | 31 ++++++++++++++------ src/SparkFun_u-blox_GNSS_Arduino_Library.h | 4 +-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index 34c510c..ebb119a 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -478,10 +478,26 @@ boolean SFE_UBLOX_GNSS::begin(SPIClass &spiPort, uint8_t csPin, uint32_t spiSpee if (!connected) connected = isConnected(); - // Initialize/clear the SPI buffer - fill it with 0xFF as this is what is received from the UBLOX module if there's no data to be processed - for (uint8_t i = 0; i < 20; i++) + //Create the SPI buffer + if (spiBuffer == NULL) //Memory has not yet been allocated - so use new { - spiBuffer[i] = 0xFF; + spiBuffer = new uint8_t[getSpiTransactionSize()]; + } + + if (spiBuffer == NULL) + { + if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging + { + _debugSerial->print(F("begin (SPI): memory allocation failed for SPI Buffer!")); + } + } + else + { + // Initialize/clear the SPI buffer - fill it with 0xFF as this is what is received from the UBLOX module if there's no data to be processed + for (uint8_t i = 0; i < getSpiTransactionSize(); i++) + { + spiBuffer[i] = 0xFF; + } } return (connected); @@ -2879,16 +2895,13 @@ void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer) // Send a command via SPI void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX) { - if (spiBuffer == NULL) //Memory has not yet been allocated - so use new - { - spiBuffer = new uint8_t[getSpiTransactionSize()]; - } - - if (spiBuffer == NULL) { + if (spiBuffer == NULL) + { if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging { _debugSerial->print(F("sendSpiCommand: memory allocation failed for SPI Buffer!")); } + return; } // Start at the beginning of the SPI buffer diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.h b/src/SparkFun_u-blox_GNSS_Arduino_Library.h index ddfeb5d..449850c 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.h +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.h @@ -495,7 +495,7 @@ enum sfe_ublox_ls_src_e #endif // For storing SPI bytes received during sendSpiCommand -#define SPI_BUFFER_SIZE 128 +#define SFE_UBLOX_SPI_BUFFER_SIZE 128 //-=-=-=-=- UBX binary specific variables struct ubxPacket @@ -1316,7 +1316,7 @@ private: uint8_t *spiBuffer = NULL; // A buffer to store any bytes being recieved back from the device while we are sending via SPI uint8_t spiBufferIndex = 0; // Index into the SPI buffer - uint8_t spiTransactionSize = SPI_BUFFER_SIZE; //Default size of the SPI buffer + uint8_t spiTransactionSize = SFE_UBLOX_SPI_BUFFER_SIZE; //Default size of the SPI buffer //Init the packet structures and init them with pointers to the payloadAck, payloadCfg, payloadBuf and payloadAuto arrays ubxPacket packetAck = {0, 0, 0, 0, 0, payloadAck, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};