@@ -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];
801
- }
802
-
803
- // reset the contents of the SPI buffer
804
- for (uint8_t i = 0 ; i < bufferIndex; i++)
805
- {
806
- spiBuffer[i] = 0xFF ;
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);
807
799
}
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
@@ -2791,7 +2784,7 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX)
2791
2784
void SFE_UBLOX_GNSS::spiTransfer (uint8_t byteToTransfer)
2792
2785
{
2793
2786
uint8_t returnedByte = _spiPort->transfer (byteToTransfer);
2794
- if (returnedByte != 0xFF )
2787
+ if (returnedByte != 0xFF || currentSentence != NONE )
2795
2788
{
2796
2789
spiBuffer[spiBufferIndex] = returnedByte;
2797
2790
spiBufferIndex++;
@@ -2801,9 +2794,24 @@ void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
2801
2794
// Send a command via SPI
2802
2795
void SFE_UBLOX_GNSS::sendSpiCommand (ubxPacket *outgoingUBX)
2803
2796
{
2797
+ if (spiBuffer == NULL ) // Memory has not yet been allocated - so use new
2798
+ {
2799
+ spiBuffer = new uint8_t [SPI_BUFFER_SIZE];
2800
+ }
2801
+
2802
+ if (spiBuffer == NULL ) {
2803
+ if ((_printDebug == true ) || (_printLimitedDebug == true )) // This is important. Print this if doing limited debugging
2804
+ {
2805
+ _debugSerial->print (F (" process: memory allocation failed for SPI Buffer!" ));
2806
+ }
2807
+ }
2808
+
2809
+ // Start at the beginning of the SPI buffer
2810
+ spiBufferIndex = 0 ;
2811
+
2804
2812
SPISettings settingsA (_spiSpeed, MSBFIRST, SPI_MODE0);
2805
2813
_spiPort->beginTransaction (settingsA);
2806
- digitalWrite (_ssPin , LOW);
2814
+ digitalWrite (_csPin , LOW);
2807
2815
// Write header bytes
2808
2816
spiTransfer (UBX_SYNCH_1); // μ - oh ublox, you're funny. I will call you micro-blox from now on.
2809
2817
if (_printDebug) _debugSerial->printf (" %x " , UBX_SYNCH_1);
@@ -2831,7 +2839,7 @@ void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
2831
2839
if (_printDebug) _debugSerial->printf (" %x " , outgoingUBX->checksumA );
2832
2840
spiTransfer (outgoingUBX->checksumB );
2833
2841
if (_printDebug) _debugSerial->printf (" %x \n " , outgoingUBX->checksumB );
2834
- digitalWrite (_ssPin , HIGH);
2842
+ digitalWrite (_csPin , HIGH);
2835
2843
_spiPort->endTransaction ();
2836
2844
}
2837
2845
0 commit comments