Skip to content

Commit 79f4412

Browse files
committed
Add spiPollingWait and setSPIpollingWait. Change delayMicroseconds(500) to delay(1)
1 parent c1db2f5 commit 79f4412

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

examples/SPI/Example1_GetPosition/Example1_GetPosition.ino

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ void setup()
8686
while (1);
8787
}
8888

89+
//myGNSS.factoryDefault(); delay(5000); // Uncomment this line to reset the module back to its factory defaults
90+
8991
myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise)
9092
myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR
9193
}

examples/SPI/Example2_AutoPVT/Example2_AutoPVT.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ void setup()
7979
while (1);
8080
}
8181

82+
//myGNSS.factoryDefault(); delay(5000); // Uncomment this line to reset the module back to its factory defaults
83+
8284
myGNSS.setPortOutput(COM_PORT_SPI, COM_TYPE_UBX); //Set the SPI port to output UBX only (turn off NMEA noise)
8385
myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR
8486

8587
myGNSS.setNavigationFrequency(2); //Produce two solutions per second
8688
myGNSS.setAutoPVT(true); //Tell the GNSS to "send" each solution
87-
//myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
89+
//myGNSS.saveConfiguration(); //Optional: Save _all_ the current settings to flash and BBR
8890
}
8991

9092
void loop()

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ setPacketCfgPayloadSize KEYWORD2
5353
begin KEYWORD2
5454
end KEYWORD2
5555
setI2CpollingWait KEYWORD2
56+
setSPIpollingWait KEYWORD2
5657
setI2CTransactionSize KEYWORD2
5758
getI2CTransactionSize KEYWORD2
5859
setSpiTransactionSize KEYWORD2

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,13 @@ void SFE_UBLOX_GNSS::setI2CpollingWait(uint8_t newPollingWait_ms)
514514
i2cPollingWait = newPollingWait_ms;
515515
}
516516

517+
// Allow the user to change SPI polling wait
518+
// (the minimum interval between SPI data requests when no data is available - to avoid pounding the bus)
519+
void SFE_UBLOX_GNSS::setSPIpollingWait(uint8_t newPollingWait_ms)
520+
{
521+
spiPollingWait = newPollingWait_ms;
522+
}
523+
517524
//Sets the global size for I2C transactions
518525
//Most platforms use 32 bytes (the default) but this allows users to increase the transaction
519526
//size if the platform supports it
@@ -664,7 +671,7 @@ const char *SFE_UBLOX_GNSS::statusString(sfe_ublox_status_e stat)
664671
return "None";
665672
}
666673

667-
// Check for the arrival of new I2C/Serial data
674+
// Check for the arrival of new I2C/Serial/SPI data
668675

669676
//Allow the user to disable the "7F" check (e.g.) when logging RAWX data
670677
void SFE_UBLOX_GNSS::disableUBX7Fcheck(boolean disabled)
@@ -860,8 +867,20 @@ boolean SFE_UBLOX_GNSS::checkUbloxSpi(ubxPacket *incomingUBX, uint8_t requestedC
860867
_spiPort->beginTransaction(SPISettings(_spiSpeed, MSBFIRST, SPI_MODE0));
861868
digitalWrite(_csPin, LOW);
862869
uint8_t byteReturned = _spiPort->transfer(0xFF);
863-
// Note to future self: I think the 0xFF check will cause problems when attempting to process (e.g.) RAWX data
864-
// which could legitimately contain 0xFF within the data stream
870+
871+
// Note to future self: I think the 0xFF check might cause problems when attempting to process (e.g.) RAWX data
872+
// which could legitimately contain 0xFF within the data stream. But the currentSentence check will certainly help!
873+
874+
// If we are not receiving a sentence (currentSentence == NONE) and the byteReturned is 0xFF,
875+
// i.e. the module has no data for us, then delay for
876+
if ((byteReturned == 0xFF) && (currentSentence == NONE))
877+
{
878+
digitalWrite(_csPin, HIGH);
879+
_spiPort->endTransaction();
880+
delay(spiPollingWait);
881+
return (true);
882+
}
883+
865884
while (byteReturned != 0xFF || currentSentence != NONE)
866885
{
867886
process(byteReturned, incomingUBX, requestedClass, requestedID);
@@ -3208,7 +3227,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForACKResponse(ubxPacket *outgoingUBX, ui
32083227

32093228
} //checkUbloxInternal == true
32103229

3211-
delayMicroseconds(500);
3230+
delay(1); // Allow an RTOS to get an elbow in (#11)
32123231
} //while (millis() - startTime < maxTime)
32133232

32143233
// We have timed out...
@@ -3318,7 +3337,7 @@ sfe_ublox_status_e SFE_UBLOX_GNSS::waitForNoACKResponse(ubxPacket *outgoingUBX,
33183337
}
33193338
}
33203339

3321-
delayMicroseconds(500);
3340+
delay(1); // Allow an RTOS to get an elbow in (#11)
33223341
}
33233342

33243343
if (_printDebug == true)

src/SparkFun_u-blox_GNSS_Arduino_Library.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ class SFE_UBLOX_GNSS
577577
void end(void); //Stop all automatic message processing. Free all used RAM
578578

579579
void setI2CpollingWait(uint8_t newPollingWait_ms); // Allow the user to change the I2C polling wait if required
580+
void setSPIpollingWait(uint8_t newPollingWait_ms); // Allow the user to change the SPI polling wait if required
580581

581582
//Set the max number of bytes set in a given I2C transaction
582583
uint8_t i2cTransactionSize = 32; //Default to ATmega328 limit
@@ -1344,10 +1345,14 @@ class SFE_UBLOX_GNSS
13441345
sfe_ublox_packet_buffer_e activePacketBuffer = SFE_UBLOX_PACKET_PACKETBUF;
13451346

13461347
//Limit checking of new data to every X ms
1347-
//If we are expecting an update every X Hz then we should check every half that amount of time
1348+
//If we are expecting an update every X Hz then we should check every quarter that amount of time
13481349
//Otherwise we may block ourselves from seeing new data
13491350
uint8_t i2cPollingWait = 100; //Default to 100ms. Adjusted when user calls setNavigationFrequency() or setHNRNavigationRate() or setMeasurementRate()
13501351

1352+
//The SPI polling wait is a little different. checkUbloxSpi will delay for this amount before returning if
1353+
//there is no data waiting to be read. This prevents waitForACKResponse from pounding the SPI bus too hard.
1354+
uint8_t spiPollingWait = 9; //Default to 9ms; waitForACKResponse delays for 1ms on top of this. User can adjust with setSPIPollingWait.
1355+
13511356
unsigned long lastCheck = 0;
13521357

13531358
uint16_t ubxFrameCounter; //Count all UBX frame bytes. [Fixed header(2bytes), CLS(1byte), ID(1byte), length(2bytes), payload(x bytes), checksums(2bytes)]

0 commit comments

Comments
 (0)