1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

LP-252 correct buffer handling

This commit is contained in:
Harold Hankins 2016-03-02 22:38:51 -05:00
parent 352d2c27f0
commit fd235d8aa5
2 changed files with 21 additions and 19 deletions

View File

@ -34,40 +34,41 @@
#include "gps9protocol.h" #include "gps9protocol.h"
uint32_t lastUnsentData = 0; uint32_t lastUnsentData = 0;
uint8_t gpsBuffer[BUFFER_SIZE]; uint8_t gpsBuffer[GPS_BUFFER_SIZE];
uint8_t fcBuffer[BUFFER_SIZE]; uint8_t fcBuffer[FC_BUFFER_SIZE];
void handleGPS() void handleGPS()
{ {
bool completeSentenceSent = false; bool completeSentenceInBuffer = false;
int8_t maxCount = 2; int8_t maxCount = 2;
do { do {
int32_t datacounter = PIOS_UBX_DDC_GetAvailableBytes(PIOS_I2C_GPS); int32_t datacounter = PIOS_UBX_DDC_GetAvailableBytes(PIOS_I2C_GPS);
if (datacounter > 0) { if (datacounter > 0) {
uint8_t toRead = (uint32_t)datacounter > BUFFER_SIZE - lastUnsentData ? BUFFER_SIZE - lastUnsentData : (uint8_t)datacounter; uint8_t toRead = (uint32_t)datacounter > GPS_BUFFER_SIZE - lastUnsentData ? GPS_BUFFER_SIZE - lastUnsentData : (uint8_t)datacounter;
uint8_t toSend = toRead; uint8_t toSend = toRead + lastUnsentData;
PIOS_UBX_DDC_ReadData(PIOS_I2C_GPS, gpsBuffer, toRead); PIOS_UBX_DDC_ReadData(PIOS_I2C_GPS, gpsBuffer + lastUnsentData, toRead);
uint8_t *lastSentence; uint8_t *lastSentence;
uint16_t lastSentenceLength; uint16_t lastSentenceLength;
completeSentenceSent = ubx_getLastSentence(gpsBuffer, toRead, &lastSentence, &lastSentenceLength); completeSentenceInBuffer = ubx_getLastSentence(gpsBuffer, toSend, &lastSentence, &lastSentenceLength);
if (completeSentenceSent) { if (completeSentenceInBuffer) {
toSend = (uint8_t)(lastSentence - gpsBuffer + lastSentenceLength); toSend = (uint8_t)(lastSentence - gpsBuffer + lastSentenceLength);
} else {
lastUnsentData = 0;
}
PIOS_COM_SendBuffer(pios_com_main_id, gpsBuffer, toSend); PIOS_COM_SendBuffer(pios_com_main_id, gpsBuffer, toSend);
// move unsent data to the beginning of gpsBuffer to be sent next time
if (toRead > toSend) { lastUnsentData = lastUnsentData > toSend ? lastUnsentData - toSend : 0;
// move unsent data at the beginning of gpsBuffer to be sent next time if (lastUnsentData > 0) {
lastUnsentData = toRead - toSend;
memcpy(gpsBuffer, (gpsBuffer + toSend), lastUnsentData); memcpy(gpsBuffer, (gpsBuffer + toSend), lastUnsentData);
} }
} else {
// toss the buffer contents if it's full and there are no complete
// ublox sentences in it. This happens if the module is sending NMEA
lastUnsentData = toSend < GPS_BUFFER_SIZE ? toSend : 0;
PIOS_COM_SendBuffer(pios_com_main_id, gpsBuffer, toSend);
}
} }
datacounter = PIOS_COM_ReceiveBuffer(pios_com_main_id, fcBuffer, BUFFER_SIZE, 0); datacounter = PIOS_COM_ReceiveBuffer(pios_com_main_id, fcBuffer, FC_BUFFER_SIZE, 0);
if (datacounter > 0) { if (datacounter > 0) {
PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, fcBuffer, datacounter); PIOS_UBX_DDC_WriteData(PIOS_I2C_GPS, fcBuffer, datacounter);
} }

View File

@ -26,7 +26,8 @@
#ifndef GPS9GPSHANDLER_H_ #ifndef GPS9GPSHANDLER_H_
#define GPS9GPSHANDLER_H_ #define GPS9GPSHANDLER_H_
#define BUFFER_SIZE 200 #define GPS_BUFFER_SIZE 250
#define FC_BUFFER_SIZE 120
void handleGPS(); void handleGPS();
void setupGPS(); void setupGPS();