From 79f44129d0ea309eb1b54beb053e6d3e94c780d0 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Wed, 30 Jun 2021 19:13:45 +0100 Subject: [PATCH] Add spiPollingWait and setSPIpollingWait. Change delayMicroseconds(500) to delay(1) --- .../Example1_GetPosition.ino | 2 ++ .../SPI/Example2_AutoPVT/Example2_AutoPVT.ino | 4 ++- keywords.txt | 1 + src/SparkFun_u-blox_GNSS_Arduino_Library.cpp | 29 +++++++++++++++---- src/SparkFun_u-blox_GNSS_Arduino_Library.h | 7 ++++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/SPI/Example1_GetPosition/Example1_GetPosition.ino b/examples/SPI/Example1_GetPosition/Example1_GetPosition.ino index 5c582e9..108a5ef 100644 --- a/examples/SPI/Example1_GetPosition/Example1_GetPosition.ino +++ b/examples/SPI/Example1_GetPosition/Example1_GetPosition.ino @@ -86,6 +86,8 @@ void setup() while (1); } + //myGNSS.factoryDefault(); delay(5000); // Uncomment this line to reset the module back to its factory defaults + myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise) myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR } diff --git a/examples/SPI/Example2_AutoPVT/Example2_AutoPVT.ino b/examples/SPI/Example2_AutoPVT/Example2_AutoPVT.ino index b6fc355..ff9d930 100644 --- a/examples/SPI/Example2_AutoPVT/Example2_AutoPVT.ino +++ b/examples/SPI/Example2_AutoPVT/Example2_AutoPVT.ino @@ -79,12 +79,14 @@ void setup() while (1); } + //myGNSS.factoryDefault(); delay(5000); // Uncomment this line to reset the module back to its factory defaults + myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise) myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR myGNSS.setNavigationFrequency(2); //Produce two solutions per second myGNSS.setAutoPVT(true); //Tell the GNSS to "send" each solution - //myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR + //myGNSS.saveConfiguration(); //Optional: Save _all_ the current settings to flash and BBR } void loop() diff --git a/keywords.txt b/keywords.txt index 387057a..9862fb0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -53,6 +53,7 @@ setPacketCfgPayloadSize KEYWORD2 begin KEYWORD2 end KEYWORD2 setI2CpollingWait KEYWORD2 +setSPIpollingWait KEYWORD2 setI2CTransactionSize KEYWORD2 getI2CTransactionSize KEYWORD2 setSpiTransactionSize KEYWORD2 diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index 2d17bff..4f623ee 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -514,6 +514,13 @@ void SFE_UBLOX_GNSS::setI2CpollingWait(uint8_t newPollingWait_ms) i2cPollingWait = newPollingWait_ms; } +// Allow the user to change SPI polling wait +// (the minimum interval between SPI data requests when no data is available - to avoid pounding the bus) +void SFE_UBLOX_GNSS::setSPIpollingWait(uint8_t newPollingWait_ms) +{ + spiPollingWait = newPollingWait_ms; +} + //Sets the global size for I2C transactions //Most platforms use 32 bytes (the default) but this allows users to increase the transaction //size if the platform supports it @@ -664,7 +671,7 @@ const char *SFE_UBLOX_GNSS::statusString(sfe_ublox_status_e stat) return "None"; } -// Check for the arrival of new I2C/Serial data +// Check for the arrival of new I2C/Serial/SPI data //Allow the user to disable the "7F" check (e.g.) when logging RAWX data void SFE_UBLOX_GNSS::disableUBX7Fcheck(boolean disabled) @@ -860,8 +867,20 @@ boolean SFE_UBLOX_GNSS::checkUbloxSpi(ubxPacket *incomingUBX, uint8_t requestedC _spiPort->beginTransaction(SPISettings(_spiSpeed, MSBFIRST, SPI_MODE0)); digitalWrite(_csPin, LOW); uint8_t byteReturned = _spiPort->transfer(0xFF); - // Note to future self: I think the 0xFF check will cause problems when attempting to process (e.g.) RAWX data - // which could legitimately contain 0xFF within the data stream + + // Note to future self: I think the 0xFF check might cause problems when attempting to process (e.g.) RAWX data + // which could legitimately contain 0xFF within the data stream. But the currentSentence check will certainly help! + + // If we are not receiving a sentence (currentSentence == NONE) and the byteReturned is 0xFF, + // i.e. the module has no data for us, then delay for + if ((byteReturned == 0xFF) && (currentSentence == NONE)) + { + digitalWrite(_csPin, HIGH); + _spiPort->endTransaction(); + delay(spiPollingWait); + return (true); + } + while (byteReturned != 0xFF || currentSentence != NONE) { process(byteReturned, incomingUBX, requestedClass, requestedID); @@ -3208,7 +3227,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui } //checkUbloxInternal == true - delayMicroseconds(500); + delay(1); // Allow an RTOS to get an elbow in (#11) } //while (millis() - startTime < maxTime) // We have timed out... @@ -3318,7 +3337,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForNoACKResponse(ubxPacket *outgoingUBX, } } - delayMicroseconds(500); + delay(1); // Allow an RTOS to get an elbow in (#11) } if (_printDebug == true) diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.h b/src/SparkFun_u-blox_GNSS_Arduino_Library.h index b274320..691b6a9 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.h +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.h @@ -577,6 +577,7 @@ public: void end(void); //Stop all automatic message processing. Free all used RAM void setI2CpollingWait(uint8_t newPollingWait_ms); // Allow the user to change the I2C polling wait if required + void setSPIpollingWait(uint8_t newPollingWait_ms); // Allow the user to change the SPI polling wait if required //Set the max number of bytes set in a given I2C transaction uint8_t i2cTransactionSize = 32; //Default to ATmega328 limit @@ -1344,10 +1345,14 @@ private: sfe_ublox_packet_buffer_e activePacketBuffer = SFE_UBLOX_PACKET_PACKETBUF; //Limit checking of new data to every X ms - //If we are expecting an update every X Hz then we should check every half that amount of time + //If we are expecting an update every X Hz then we should check every quarter that amount of time //Otherwise we may block ourselves from seeing new data uint8_t i2cPollingWait = 100; //Default to 100ms. Adjusted when user calls setNavigationFrequency() or setHNRNavigationRate() or setMeasurementRate() + //The SPI polling wait is a little different. checkUbloxSpi will delay for this amount before returning if + //there is no data waiting to be read. This prevents waitForACKResponse from pounding the SPI bus too hard. + uint8_t spiPollingWait = 9; //Default to 9ms; waitForACKResponse delays for 1ms on top of this. User can adjust with setSPIPollingWait. + unsigned long lastCheck = 0; uint16_t ubxFrameCounter; //Count all UBX frame bytes. [Fixed header(2bytes), CLS(1byte), ID(1byte), length(2bytes), payload(x bytes), checksums(2bytes)]