Skip to content

Commit a2b67bc

Browse files
committed
Buffer incoming BT SPP data for bulk I2C sends
Fix for issue #469
1 parent 9b8a78b commit a2b67bc

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
volatile static uint16_t dataHead = 0; // Head advances as data comes in from GNSS's UART
55
volatile int availableHandlerSpace = 0; // settings.gnssHandlerBufferSize - usedSpace
66

7+
// Buffer the incoming Bluetooth stream so that it can be passed in bulk over I2C
8+
uint8_t bluetoothOutgoingToZed[100];
9+
uint16_t bluetoothOutgoingToZedHead = 0;
10+
unsigned long lastZedI2CSend = 0; // Timestamp of the last time we sent RTCM ZED over I2C
11+
712
// If the phone has any new data (NTRIP RTCM, etc), read it in over Bluetooth and pass along to ZED
813
// Scan for escape characters to enter config menu
914
void btReadTask(void *e)
@@ -41,7 +46,7 @@ void btReadTask(void *e)
4146
if (USE_I2C_GNSS)
4247
{
4348
// serialGNSS.write(incoming);
44-
theGNSS.pushRawData(&incoming, 1);
49+
addToZedI2CBuffer(btEscapeCharacter);
4550
}
4651
else
4752
theGNSS.pushRawData(&incoming, 1);
@@ -55,8 +60,7 @@ void btReadTask(void *e)
5560
if (USE_I2C_GNSS)
5661
{
5762
// serialGNSS.write(btEscapeCharacter);
58-
uint8_t escChar = btEscapeCharacter;
59-
theGNSS.pushRawData(&escChar, 1);
63+
addToZedI2CBuffer(btEscapeCharacter);
6064
}
6165
else
6266
{
@@ -72,7 +76,7 @@ void btReadTask(void *e)
7276
// UART RX can be corrupted by UART TX
7377
// See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/469
7478
// serialGNSS.write(incoming);
75-
theGNSS.pushRawData(&incoming, 1);
79+
addToZedI2CBuffer(incoming);
7680
}
7781
else
7882
theGNSS.pushRawData(&incoming, 1);
@@ -87,6 +91,11 @@ void btReadTask(void *e)
8791

8892
} // End bluetoothGetState() == BT_CONNECTED
8993

94+
if (bluetoothOutgoingToZedHead > 0 && ((millis() - lastZedI2CSend) > 100))
95+
{
96+
sendZedI2CBuffer(); // Send any outstanding RTCM
97+
}
98+
9099
if (settings.enableTaskReports == true)
91100
systemPrintf("SerialWriteTask High watermark: %d\r\n", uxTaskGetStackHighWaterMark(nullptr));
92101

@@ -95,6 +104,35 @@ void btReadTask(void *e)
95104
} // End while(true)
96105
}
97106

107+
// Add byte to buffer that will be sent to ZED
108+
// We cannot write single characters to the ZED over I2C (as this will change the address pointer)
109+
void addToZedI2CBuffer(uint8_t incoming)
110+
{
111+
bluetoothOutgoingToZed[bluetoothOutgoingToZedHead] = incoming;
112+
113+
bluetoothOutgoingToZedHead++;
114+
if (bluetoothOutgoingToZedHead == sizeof(bluetoothOutgoingToZed))
115+
{
116+
sendZedI2CBuffer();
117+
}
118+
}
119+
120+
// Push the buffered data in bulk to the GNSS over I2C
121+
bool sendZedI2CBuffer()
122+
{
123+
bool response = theGNSS.pushRawData(bluetoothOutgoingToZed, bluetoothOutgoingToZedHead);
124+
125+
if (response == true)
126+
{
127+
// log_d("Pushed %d bytes RTCM to ZED", bluetoothOutgoingToZedHead);
128+
}
129+
130+
// No matter the response, wrap the head and reset the timer
131+
bluetoothOutgoingToZedHead = 0;
132+
lastZedI2CSend = millis();
133+
return (response);
134+
}
135+
98136
// Normally a delay(1) will feed the WDT but if we don't want to wait that long, this feeds the WDT without delay
99137
void feedWdt()
100138
{
@@ -1247,7 +1285,7 @@ void sdSizeCheckTask(void *e)
12471285
sdCardSize = SD_MMC.cardSize();
12481286
sdFreeSpace = SD_MMC.totalBytes() - SD_MMC.usedBytes();
12491287
}
1250-
#endif // COMPILE_SD_MMC
1288+
#endif // COMPILE_SD_MMC
12511289

12521290
xSemaphoreGive(sdCardSemaphore);
12531291

0 commit comments

Comments
 (0)