diff --git a/keywords.txt b/keywords.txt index 831b4c2..a1556c3 100644 --- a/keywords.txt +++ b/keywords.txt @@ -55,6 +55,8 @@ end KEYWORD2 setI2CpollingWait KEYWORD2 setI2CTransactionSize KEYWORD2 getI2CTransactionSize KEYWORD2 +setSpiTransactionSize KEYWORD2 +getSpiTransactionSize KEYWORD2 isConnected KEYWORD2 enableDebugging KEYWORD2 disableDebugging KEYWORD2 diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index c50645b..dda1e27 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -505,6 +505,19 @@ uint8_t SFE_UBLOX_GNSS::getI2CTransactionSize(void) return (i2cTransactionSize); } +//Sets the global size for the SPI buffer/transactions. +//Call this before begin()! +//Note: if the buffer size is too small, incoming characters may be lost if the message sent +//is larger than this buffer. If too big, you may run out of SRAM on constrained architectures! +void SFE_UBLOX_GNSS::setSpiTransactionSize(uint8_t transactionSize) +{ + spiTransactionSize = transactionSize; +} +uint8_t SFE_UBLOX_GNSS::getSpiTransactionSize(void) +{ + return (spiTransactionSize); +} + //Returns true if I2C device ack's boolean SFE_UBLOX_GNSS::isConnected(uint16_t maxWait) { @@ -2851,7 +2864,7 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX) void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer) { uint8_t returnedByte = _spiPort->transfer(byteToTransfer); - if (returnedByte != 0xFF || currentSentence != NONE) + if ((spiBufferIndex < getSpiTransactionSize()) && (returnedByte != 0xFF || currentSentence != NONE)) { spiBuffer[spiBufferIndex] = returnedByte; spiBufferIndex++; @@ -2863,13 +2876,13 @@ void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX) { if (spiBuffer == NULL) //Memory has not yet been allocated - so use new { - spiBuffer = new uint8_t[SPI_BUFFER_SIZE]; + 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("process: memory allocation failed for SPI Buffer!")); + _debugSerial->print(F("sendSpiCommand: memory allocation failed for SPI Buffer!")); } } diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.h b/src/SparkFun_u-blox_GNSS_Arduino_Library.h index 6f75034..f67510e 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.h +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.h @@ -576,6 +576,11 @@ public: void setI2CTransactionSize(uint8_t bufferSize); uint8_t getI2CTransactionSize(void); + //Control the size of the spi buffer. If the buffer isn't big enough, we'll start to lose bytes + //That we receive if the buffer is full! + void setSpiTransactionSize(uint8_t bufferSize); + uint8_t getSpiTransactionSize(void); + //Set the max number of bytes set in a given I2C transaction uint8_t i2cTransactionSize = 32; //Default to ATmega328 limit @@ -1310,6 +1315,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 //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};