@@ -452,12 +452,15 @@ boolean SFE_UBLOX_GNSS::begin(Stream &serialPort)
452
452
}
453
453
454
454
// Initialize for SPI
455
- boolean SFE_UBLOX_GNSS::begin(SPIClass &spiPort, uint8_t ssPin, int spiSpeed)
455
+ boolean SFE_UBLOX_GNSS::begin(SPIClass &spiPort, uint8_t csPin, uint32_t spiSpeed)
456
456
{
457
457
commType = COMM_TYPE_SPI;
458
458
_spiPort = &spiPort;
459
- _ssPin = ssPin ;
459
+ _csPin = csPin ;
460
460
_spiSpeed = spiSpeed;
461
+ // Initialize the chip select pin
462
+ pinMode(_csPin, OUTPUT);
463
+ digitalWrite(_csPin, HIGH);
461
464
//New in v2.0: allocate memory for the packetCfg payload here - if required. (The user may have called setPacketCfgPayloadSize already)
462
465
if (packetCfgPayloadSize == 0)
463
466
setPacketCfgPayloadSize(MAX_PAYLOAD_SIZE);
@@ -790,32 +793,22 @@ boolean SFE_UBLOX_GNSS::checkUbloxSerial(ubxPacket *incomingUBX, uint8_t request
790
793
//Checks SPI for data, passing any new bytes to process()
791
794
boolean SFE_UBLOX_GNSS::checkUbloxSpi(ubxPacket *incomingUBX, uint8_t requestedClass, uint8_t requestedID)
792
795
{
793
- // process the contents of the SPI buffer if not empty!
794
- uint8_t bufferByte = spiBuffer[0];
795
- uint8_t bufferIndex = 0;
796
-
797
- while (bufferByte != 0xFF) {
798
- process(bufferByte, incomingUBX, requestedClass, requestedID);
799
- bufferIndex++;
800
- bufferByte = spiBuffer[bufferIndex];
796
+ // Process the contents of the SPI buffer if not empty!
797
+ for (uint8_t i = 0; i < spiBufferIndex; i++) {
798
+ process(spiBuffer[i], incomingUBX, requestedClass, requestedID);
801
799
}
802
-
803
- // reset the contents of the SPI buffer
804
- for(uint8_t i = 0; i < bufferIndex; i++)
805
- {
806
- spiBuffer[i] = 0xFF;
807
- }
808
-
800
+ spiBufferIndex = 0;
801
+
809
802
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0);
810
803
_spiPort->beginTransaction(settingsA);
811
- digitalWrite(_ssPin , LOW);
804
+ digitalWrite(_csPin , LOW);
812
805
uint8_t byteReturned = _spiPort->transfer(0x0A);
813
806
while (byteReturned != 0xFF || currentSentence != NONE)
814
807
{
815
808
process(byteReturned, incomingUBX, requestedClass, requestedID);
816
809
byteReturned = _spiPort->transfer(0x0A);
817
810
}
818
- digitalWrite(_ssPin , HIGH);
811
+ digitalWrite(_csPin , HIGH);
819
812
_spiPort->endTransaction();
820
813
return (true);
821
814
@@ -2858,7 +2851,7 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX)
2858
2851
void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
2859
2852
{
2860
2853
uint8_t returnedByte = _spiPort->transfer(byteToTransfer);
2861
- if (returnedByte != 0xFF)
2854
+ if (returnedByte != 0xFF || currentSentence != NONE )
2862
2855
{
2863
2856
spiBuffer[spiBufferIndex] = returnedByte;
2864
2857
spiBufferIndex++;
@@ -2868,9 +2861,24 @@ void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
2868
2861
// Send a command via SPI
2869
2862
void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
2870
2863
{
2864
+ if (spiBuffer == NULL) //Memory has not yet been allocated - so use new
2865
+ {
2866
+ spiBuffer = new uint8_t[SPI_BUFFER_SIZE];
2867
+ }
2868
+
2869
+ if (spiBuffer == NULL) {
2870
+ if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
2871
+ {
2872
+ _debugSerial->print(F("process: memory allocation failed for SPI Buffer!"));
2873
+ }
2874
+ }
2875
+
2876
+ // Start at the beginning of the SPI buffer
2877
+ spiBufferIndex = 0;
2878
+
2871
2879
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0);
2872
2880
_spiPort->beginTransaction(settingsA);
2873
- digitalWrite(_ssPin , LOW);
2881
+ digitalWrite(_csPin , LOW);
2874
2882
//Write header bytes
2875
2883
spiTransfer(UBX_SYNCH_1); //μ - oh ublox, you're funny. I will call you micro-blox from now on.
2876
2884
if (_printDebug) _debugSerial->printf("%x ", UBX_SYNCH_1);
@@ -2898,7 +2906,7 @@ void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
2898
2906
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->checksumA);
2899
2907
spiTransfer(outgoingUBX->checksumB);
2900
2908
if (_printDebug) _debugSerial->printf("%x \n", outgoingUBX->checksumB);
2901
- digitalWrite(_ssPin , HIGH);
2909
+ digitalWrite(_csPin , HIGH);
2902
2910
_spiPort->endTransaction();
2903
2911
}
2904
2912
0 commit comments