Skip to content

Commit aa4af6f

Browse files
committed
Merge branch 'add-spi-aberridg' into modify-state-management-aberridg
2 parents 87b641c + b7e90c6 commit aa4af6f

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];
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);
807799
}
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

@@ -2791,7 +2784,7 @@ void SFE_UBLOX_GNSS::sendSerialCommand(ubxPacket *outgoingUBX)
27912784
void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
27922785
{
27932786
uint8_t returnedByte = _spiPort->transfer(byteToTransfer);
2794-
if (returnedByte != 0xFF)
2787+
if (returnedByte != 0xFF || currentSentence != NONE)
27952788
{
27962789
spiBuffer[spiBufferIndex] = returnedByte;
27972790
spiBufferIndex++;
@@ -2801,9 +2794,24 @@ void SFE_UBLOX_GNSS::spiTransfer(uint8_t byteToTransfer)
28012794
// Send a command via SPI
28022795
void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
28032796
{
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+
28042812
SPISettings settingsA(_spiSpeed, MSBFIRST, SPI_MODE0);
28052813
_spiPort->beginTransaction(settingsA);
2806-
digitalWrite(_ssPin, LOW);
2814+
digitalWrite(_csPin, LOW);
28072815
//Write header bytes
28082816
spiTransfer(UBX_SYNCH_1); //μ - oh ublox, you're funny. I will call you micro-blox from now on.
28092817
if (_printDebug) _debugSerial->printf("%x ", UBX_SYNCH_1);
@@ -2831,7 +2839,7 @@ void SFE_UBLOX_GNSS::sendSpiCommand(ubxPacket *outgoingUBX)
28312839
if (_printDebug) _debugSerial->printf("%x ", outgoingUBX->checksumA);
28322840
spiTransfer(outgoingUBX->checksumB);
28332841
if (_printDebug) _debugSerial->printf("%x \n", outgoingUBX->checksumB);
2834-
digitalWrite(_ssPin, HIGH);
2842+
digitalWrite(_csPin, HIGH);
28352843
_spiPort->endTransaction();
28362844
}
28372845

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
{
@@ -570,8 +573,8 @@ class SFE_UBLOX_GNSS
570573
boolean begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = 0x42); //Returns true if module is detected
571574
//serialPort needs to be perviously initialized to correct baud rate
572575
boolean begin(Stream &serialPort); //Returns true if module is detected
573-
//SPI - supply instance of SPIClass, slave select pin and SPI speed (in Hz)
574-
boolean begin(SPIClass &spiPort, uint8_t ssPin, int spiSpeed);
576+
//SPI - supply instance of SPIClass, chip select pin and SPI speed (in Hz)
577+
boolean begin(SPIClass &spiPort, uint8_t csPin, uint32_t spiSpeed);
575578

576579
void end(void); //Stop all automatic message processing. Free all used RAM
577580

@@ -1291,7 +1294,7 @@ class SFE_UBLOX_GNSS
12911294
Stream *_debugSerial; //The stream to send debug messages to if enabled
12921295

12931296
SPIClass *_spiPort; //The instance of SPIClass
1294-
uint8_t _ssPin; //The slave select pin
1297+
uint8_t _csPin; //The chip select pin
12951298
int _spiSpeed; //The speed to use for SPI (Hz)
12961299

12971300
uint8_t _gpsI2Caddress = 0x42; //Default 7-bit unshifted address of the ublox 6/7/8/M8/F9 series
@@ -1313,8 +1316,8 @@ class SFE_UBLOX_GNSS
13131316
uint8_t *payloadCfg = NULL;
13141317
uint8_t *payloadAuto = NULL;
13151318

1316-
uint8_t spiBuffer[20]; // A small buffer to store any bytes being recieved back from the device while we are sending via SPI
1317-
uint8_t spiBufferIndex = 0; // The index into the SPI buffer
1319+
uint8_t *spiBuffer = NULL; // A buffer to store any bytes being recieved back from the device while we are sending via SPI
1320+
uint8_t spiBufferIndex = 0; // Index into the SPI buffer
13181321

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

0 commit comments

Comments
 (0)