Skip to content

Commit b7e90c6

Browse files
committed
Commits as suggested by Paul
1 parent bcc68da commit b7e90c6

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ disableUBX7Fcheck KEYWORD2
6666
checkUblox KEYWORD2
6767
checkUbloxI2C KEYWORD2
6868
checkUbloxSerial KEYWORD2
69+
checkUbloxSPI KEYWORD2
6970

7071
process KEYWORD2
7172
processNMEA KEYWORD2

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,15 @@ boolean SFE_UBLOX_GNSS::begin(Stream &serialPort)
452452
}
453453

454454
// 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)
456456
{
457457
commType = COMM_TYPE_SPI;
458458
_spiPort = &spiPort;
459-
_ssPin = ssPin;
459+
_csPin = csPin;
460460
_spiSpeed = spiSpeed;
461+
// Initialize the chip select pin
462+
pinMode(_csPin, OUTPUT);
463+
digitalWrite(_csPin, HIGH);
461464
//New in v2.0: allocate memory for the packetCfg payload here - if required. (The user may have called setPacketCfgPayloadSize already)
462465
if (packetCfgPayloadSize == 0)
463466
setPacketCfgPayloadSize(MAX_PAYLOAD_SIZE);
@@ -790,32 +793,22 @@ boolean SFE_UBLOX_GNSS::checkUbloxSerial(ubxPacket *incomingUBX, uint8_t request
790793
//Checks SPI for data, passing any new bytes to process()
791794
boolean SFE_UBLOX_GNSS::checkUbloxSpi(ubxPacket *incomingUBX, uint8_t requestedClass, uint8_t requestedID)
792795
{
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);
801799
}
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+
809802
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0);
810803
_spiPort->beginTransaction(settingsA);
811-
digitalWrite(_ssPin, LOW);
804+
digitalWrite(_csPin, LOW);
812805
uint8_t byteReturned = _spiPort->transfer(0x0A);
813806
while (byteReturned != 0xFF || currentSentence != NONE)
814807
{
815808
process(byteReturned, incomingUBX, requestedClass, requestedID);
816809
byteReturned = _spiPort->transfer(0x0A);
817810
}
818-
digitalWrite(_ssPin, HIGH);
811+
digitalWrite(_csPin, HIGH);
819812
_spiPort->endTransaction();
820813
return (true);
821814

@@ -2858,7 +2851,7 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX)
28582851
void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
28592852
{
28602853
uint8_t returnedByte = _spiPort->transfer(byteToTransfer);
2861-
if (returnedByte != 0xFF)
2854+
if (returnedByte != 0xFF || currentSentence != NONE)
28622855
{
28632856
spiBuffer[spiBufferIndex] = returnedByte;
28642857
spiBufferIndex++;
@@ -2868,9 +2861,24 @@ void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
28682861
// Send a command via SPI
28692862
void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
28702863
{
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+
28712879
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0);
28722880
_spiPort->beginTransaction(settingsA);
2873-
digitalWrite(_ssPin, LOW);
2881+
digitalWrite(_csPin, LOW);
28742882
//Write header bytes
28752883
spiTransfer(UBX_SYNCH_1); //μ - oh ublox, you're funny. I will call you micro-blox from now on.
28762884
if (_printDebug) _debugSerial->printf("%x ", UBX_SYNCH_1);
@@ -2898,7 +2906,7 @@ void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
28982906
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->checksumA);
28992907
spiTransfer(outgoingUBX->checksumB);
29002908
if (_printDebug) _debugSerial->printf("%x \n", outgoingUBX->checksumB);
2901-
digitalWrite(_ssPin, HIGH);
2909+
digitalWrite(_csPin, HIGH);
29022910
_spiPort->endTransaction();
29032911
}
29042912

src/SparkFun_u-blox_GNSS_Arduino_Library.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ enum sfe_ublox_ls_src_e
494494
//#define MAX_PAYLOAD_SIZE 768 //Worst case: UBX_CFG_VALSET packet with 64 keyIDs each with 64 bit values
495495
#endif
496496

497+
// For storing SPI bytes received during sendSpiCommand
498+
#define SPI_BUFFER_SIZE 128
499+
497500
//-=-=-=-=- UBX binary specific variables
498501
struct ubxPacket
499502
{
@@ -562,8 +565,8 @@ class SFE_UBLOX_GNSS
562565
boolean begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = 0x42); //Returns true if module is detected
563566
//serialPort needs to be perviously initialized to correct baud rate
564567
boolean begin(Stream &serialPort); //Returns true if module is detected
565-
//SPI - supply instance of SPIClass, slave select pin and SPI speed (in Hz)
566-
boolean begin(SPIClass &spiPort, uint8_t ssPin, int spiSpeed);
568+
//SPI - supply instance of SPIClass, chip select pin and SPI speed (in Hz)
569+
boolean begin(SPIClass &spiPort, uint8_t csPin, uint32_t spiSpeed);
567570

568571
void end(void); //Stop all automatic message processing. Free all used RAM
569572

@@ -1283,7 +1286,7 @@ class SFE_UBLOX_GNSS
12831286
Stream *_debugSerial; //The stream to send debug messages to if enabled
12841287

12851288
SPIClass *_spiPort; //The instance of SPIClass
1286-
uint8_t _ssPin; //The slave select pin
1289+
uint8_t _csPin; //The chip select pin
12871290
int _spiSpeed; //The speed to use for SPI (Hz)
12881291

12891292
uint8_t _gpsI2Caddress = 0x42; //Default 7-bit unshifted address of the ublox 6/7/8/M8/F9 series
@@ -1305,8 +1308,8 @@ class SFE_UBLOX_GNSS
13051308
uint8_t *payloadCfg = NULL;
13061309
uint8_t *payloadAuto = NULL;
13071310

1308-
uint8_t spiBuffer[20]; // A small buffer to store any bytes being recieved back from the device while we are sending via SPI
1309-
uint8_t spiBufferIndex = 0; // The index into the SPI buffer
1311+
uint8_t *spiBuffer = NULL; // A buffer to store any bytes being recieved back from the device while we are sending via SPI
1312+
uint8_t spiBufferIndex = 0; // Index into the SPI buffer
13101313

13111314
//Init the packet structures and init them with pointers to the payloadAck, payloadCfg, payloadBuf and payloadAuto arrays
13121315
ubxPacket packetAck = {0, 0, 0, 0, 0, payloadAck, 0, 0, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED, SFE_UBLOX_PACKET_VALIDITY_NOT_DEFINED};

0 commit comments

Comments
 (0)