Skip to content

Commit 79b9a11

Browse files
committed
proposing hooks for adding mutex that allow multiple tasks to write directly the gnss with different priorities
- also propose to align the ping return type.
1 parent 9df5ba2 commit 79b9a11

File tree

2 files changed

+61
-48
lines changed

2 files changed

+61
-48
lines changed

src/u-blox_GNSS.cpp

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,12 @@ uint16_t DevUBLOXGNSS::available()
665665
}
666666
// For I2C, ping the _address
667667
// Not Applicable for SPI and Serial
668-
uint16_t DevUBLOXGNSS::ping()
668+
bool DevUBLOXGNSS::ping()
669669
{
670-
return _sfeBus->ping();
670+
if (!lock()) return 0;
671+
bool ok = _sfeBus->ping();
672+
unlock();
673+
return ok;
671674
}
672675
// For Serial, do Serial.write
673676
// For I2C, push data to register 0xFF. Chunkify if necessary. Prevent single byte writes as these are illegal
@@ -1014,12 +1017,15 @@ bool DevUBLOXGNSS::checkUblox(uint8_t requestedClass, uint8_t requestedID)
10141017
// PRIVATE: Called regularly to check for available bytes on the user' specified port
10151018
bool DevUBLOXGNSS::checkUbloxInternal(ubxPacket *incomingUBX, uint8_t requestedClass, uint8_t requestedID)
10161019
{
1020+
if (!lock()) return false;
1021+
bool ok = false;
10171022
if (_commType == COMM_TYPE_I2C)
1018-
return (checkUbloxI2C(incomingUBX, requestedClass, requestedID));
1023+
ok = (checkUbloxI2C(incomingUBX, requestedClass, requestedID));
10191024
else if (_commType == COMM_TYPE_SERIAL)
1020-
return (checkUbloxSerial(incomingUBX, requestedClass, requestedID));
1025+
ok = (checkUbloxSerial(incomingUBX, requestedClass, requestedID));
10211026
else if (_commType == COMM_TYPE_SPI)
1022-
return (checkUbloxSpi(incomingUBX, requestedClass, requestedID));
1027+
ok = (checkUbloxSpi(incomingUBX, requestedClass, requestedID));
1028+
unlock();
10231029
return false;
10241030
}
10251031

@@ -4162,7 +4168,6 @@ void DevUBLOXGNSS::addToChecksum(uint8_t incoming)
41624168
sfe_ublox_status_e DevUBLOXGNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t maxWait, bool expectACKonly)
41634169
{
41644170
sfe_ublox_status_e retVal = SFE_UBLOX_STATUS_SUCCESS;
4165-
41664171
calcChecksum(outgoingUBX); // Sets checksum A and B bytes of the packet
41674172

41684173
#ifndef SFE_UBLOX_REDUCED_PROG_MEM
@@ -4173,6 +4178,7 @@ sfe_ublox_status_e DevUBLOXGNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t ma
41734178
}
41744179
#endif
41754180

4181+
if (!lock()) return SFE_UBLOX_STATUS_FAIL;
41764182
if (_commType == COMM_TYPE_I2C)
41774183
{
41784184
retVal = sendI2cCommand(outgoingUBX);
@@ -4195,7 +4201,8 @@ sfe_ublox_status_e DevUBLOXGNSS::sendCommand(ubxPacket *outgoingUBX, uint16_t ma
41954201
{
41964202
sendSpiCommand(outgoingUBX);
41974203
}
4198-
4204+
unlock();
4205+
41994206
if (maxWait > 0)
42004207
{
42014208
// Depending on what we just sent, either we need to look for an ACK or not
@@ -5341,12 +5348,14 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
53415348
{
53425349
// Return now if numDataBytes is zero
53435350
if (numDataBytes == 0)
5344-
return (false); // Indicate to the user that there was no data to push
5351+
return false; // Indicate to the user that there was no data to push
53455352

5353+
if (!lock()) return SFE_UBLOX_STATUS_FAIL;
5354+
bool ok = false;
53465355
if (_commType == COMM_TYPE_SERIAL)
53475356
{
53485357
// Serial: write all the bytes in one go
5349-
return writeBytes(dataBytes, numDataBytes) == numDataBytes;
5358+
ok = writeBytes(dataBytes, numDataBytes) == numDataBytes;
53505359
}
53515360
else if (_commType == COMM_TYPE_I2C)
53525361
{
@@ -5362,55 +5371,56 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
53625371
{
53635372
_pushThisSingleByte = *dataBytes;
53645373
_pushSingleByte = true;
5365-
return (false); // Indicate to the user that their data has not been pushed yet
5366-
}
5374+
ok = false; // Indicate to the user that their data has not been pushed yet
5375+
} else {
53675376

5368-
// I2C: split the data up into packets of i2cTransactionSize
5369-
size_t bytesLeftToWrite = numDataBytes;
5370-
size_t bytesWrittenTotal = 0;
5377+
// I2C: split the data up into packets of i2cTransactionSize
5378+
size_t bytesLeftToWrite = numDataBytes;
5379+
size_t bytesWrittenTotal = 0;
53715380

5372-
if (_pushSingleByte == true) // Increment bytesLeftToWrite if we have a single byte waiting to be pushed
5373-
bytesLeftToWrite++;
5381+
if (_pushSingleByte == true) // Increment bytesLeftToWrite if we have a single byte waiting to be pushed
5382+
bytesLeftToWrite++;
53745383

5375-
while (bytesLeftToWrite > 0)
5376-
{
5377-
size_t bytesToWrite; // Limit bytesToWrite to i2cTransactionSize
5384+
while (bytesLeftToWrite > 0)
5385+
{
5386+
size_t bytesToWrite; // Limit bytesToWrite to i2cTransactionSize
53785387

5379-
if (bytesLeftToWrite > i2cTransactionSize)
5380-
bytesToWrite = i2cTransactionSize;
5381-
else
5382-
bytesToWrite = bytesLeftToWrite;
5388+
if (bytesLeftToWrite > i2cTransactionSize)
5389+
bytesToWrite = i2cTransactionSize;
5390+
else
5391+
bytesToWrite = bytesLeftToWrite;
53835392

5384-
// If there would be one byte left to be written next time, send one byte less now
5385-
if ((bytesLeftToWrite - bytesToWrite) == 1)
5386-
bytesToWrite--;
5393+
// If there would be one byte left to be written next time, send one byte less now
5394+
if ((bytesLeftToWrite - bytesToWrite) == 1)
5395+
bytesToWrite--;
53875396

5388-
size_t bytesWritten = 0;
5397+
size_t bytesWritten = 0;
53895398

5390-
if (_pushSingleByte == true)
5391-
{
5392-
uint8_t buf[i2cTransactionSize];
5399+
if (_pushSingleByte == true)
5400+
{
5401+
uint8_t buf[i2cTransactionSize];
53935402

5394-
buf[0] = _pushThisSingleByte;
5403+
buf[0] = _pushThisSingleByte;
53955404

5396-
for (uint16_t x = 1; x < bytesToWrite; x++)
5397-
buf[x] = dataBytes[x - 1];
5405+
for (uint16_t x = 1; x < bytesToWrite; x++)
5406+
buf[x] = dataBytes[x - 1];
53985407

5399-
bytesWritten += writeBytes(buf, bytesToWrite); // Write the bytes
5400-
dataBytes += bytesToWrite - 1; // Point to fresh data
5401-
_pushSingleByte = false; // Clear the flag
5402-
}
5403-
else
5404-
{
5405-
bytesWritten += writeBytes(dataBytes, bytesToWrite); // Write the bytes
5406-
dataBytes += bytesToWrite; // Point to fresh data
5408+
bytesWritten += writeBytes(buf, bytesToWrite); // Write the bytes
5409+
dataBytes += bytesToWrite - 1; // Point to fresh data
5410+
_pushSingleByte = false; // Clear the flag
5411+
}
5412+
else
5413+
{
5414+
bytesWritten += writeBytes(dataBytes, bytesToWrite); // Write the bytes
5415+
dataBytes += bytesToWrite; // Point to fresh data
5416+
}
5417+
5418+
bytesWrittenTotal += bytesWritten; // Update the totals
5419+
bytesLeftToWrite -= bytesToWrite;
54075420
}
54085421

5409-
bytesWrittenTotal += bytesWritten; // Update the totals
5410-
bytesLeftToWrite -= bytesToWrite;
5422+
ok = (bytesWrittenTotal == numDataBytes); // Return true if the correct number of bytes were written
54115423
}
5412-
5413-
return (bytesWrittenTotal == numDataBytes); // Return true if the correct number of bytes were written
54145424
}
54155425
else if (_commType == COMM_TYPE_SPI)
54165426
{
@@ -5446,9 +5456,10 @@ bool DevUBLOXGNSS::pushRawData(uint8_t *dataBytes, size_t numDataBytes, bool cal
54465456
processSpiBuffer(&packetCfg, 0, 0); // This will hopefully prevent any lost data?
54475457
}
54485458

5449-
return (true);
5459+
ok = true;
54505460
}
5451-
return false;
5461+
unlock();
5462+
return ok;
54525463
}
54535464

54545465
// Push MGA AssistNow data to the module.

src/u-blox_GNSS.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class DevUBLOXGNSS
108108
void setCommunicationBus(SparkFun_UBLOX_GNSS::GNSSDeviceBus &theBus);
109109
// For I2C, ping the _address
110110
// Not Applicable for SPI and Serial
111-
uint16_t ping();
111+
bool ping();
112112
// For Serial, return Serial.available()
113113
// For I2C, read registers 0xFD and 0xFE. Return bytes available as uint16_t
114114
// Not Applicable for SPI
@@ -130,6 +130,8 @@ class DevUBLOXGNSS
130130
// Flag to indicate if we are connected to UART1 or UART2
131131
// Needed to select the correct config items when enabling a periodic message
132132
bool _UART2 = false; // Default to UART1
133+
virtual bool lock(void) { return true; }
134+
virtual void unlock(void) { }
133135
public:
134136
void connectedToUART2(bool connected = true) { _UART2 = connected; }
135137

0 commit comments

Comments
 (0)