Skip to content

Commit c9893bb

Browse files
Merge pull request #12001 from MrScothh/feature/uart-rx-buffer
gps: satellite details twice a second, and a 512 byte receive buffer for the GPS port
2 parents 2671b7c + fd12836 commit c9893bb

7 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎src/main/drivers/serial.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
#include "platform.h"
2222

23+
#include "build/atomic.h"
24+
25+
#include "drivers/nvic.h"
26+
2327
#include "serial.h"
2428

2529
void serialPrint(serialPort_t *instance, const char *str)
@@ -133,3 +137,19 @@ bool serialIsIdle(serialPort_t *instance)
133137
else
134138
return false;
135139
}
140+
141+
// Swaps in a larger receive buffer; what the old one held is dropped
142+
void serialSetRxBuffer(serialPort_t *instance, volatile uint8_t *buffer, uint32_t size)
143+
{
144+
if (size <= instance->rxBufferSize) {
145+
return;
146+
}
147+
148+
// The receive interrupt must never see the new buffer with the old size or indices
149+
ATOMIC_BLOCK(NVIC_PRIO_MAX) {
150+
instance->rxBuffer = buffer;
151+
instance->rxBufferSize = size;
152+
instance->rxBufferHead = 0;
153+
instance->rxBufferTail = 0;
154+
}
155+
}

‎src/main/drivers/serial.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void serialPrint(serialPort_t *instance, const char *str);
122122
uint32_t serialGetBaudRate(serialPort_t *instance);
123123
bool serialIsConnected(const serialPort_t *instance);
124124
bool serialIsIdle(serialPort_t *instance);
125+
void serialSetRxBuffer(serialPort_t *instance, volatile uint8_t *buffer, uint32_t size);
125126

126127
// A shim that adapts the bufWriter API to the serialWriteBuf() API.
127128
void serialWriteBufShim(void *instance, const uint8_t *data, int count);

‎src/main/drivers/serial_uart.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
#define UART_AF(uart, af) CONCAT3(GPIO_AF, af, _ ## uart)
2121

2222
// Since serial ports can be used for any function these buffer sizes should be equal
23-
// The two largest things that need to be sent are: 1, MSP responses, 2, UBLOX SVINFO packet.
23+
// The largest thing that needs to be sent is an MSP response.
2424

25-
// Size must be a power of two due to various optimizations which use 'and' instead of 'mod'
26-
// Various serial routines return the buffer occupied size as uint8_t which would need to be extended in order to
27-
// increase size further.
25+
// Size must be a power of two due to various optimizations which use 'and' instead of 'mod'.
26+
// The GPS port gets a larger receive buffer of its own, see gps.c.
2827
#define UART1_RX_BUFFER_SIZE 256
2928
#define UART1_TX_BUFFER_SIZE 256
3029
#define UART2_RX_BUFFER_SIZE 256

‎src/main/io/gps.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ gpsStatistics_t gpsStats;
9191
gpsSolutionData_t gpsSolDRV; //filled by driver
9292
gpsSolutionData_t gpsSol; //used in the rest of the code
9393

94+
#ifdef USE_GPS_PROTO_UBLOX
95+
// Covers 230400; at 460800 and above a NAV-PVT plus a large NAV-SIG can still overflow it
96+
#define GPS_RX_BUFFER_SIZE 512
97+
STATIC_ASSERT((GPS_RX_BUFFER_SIZE & (GPS_RX_BUFFER_SIZE - 1)) == 0, gps_rx_buffer_size_not_power_of_2);
98+
STATIC_FASTRAM volatile uint8_t gpsRxBuffer[GPS_RX_BUFFER_SIZE];
99+
#endif
100+
94101
// Map gpsBaudRate_e index to baudRate_e
95102
baudRate_e gpsToSerialBaudRate[GPS_BAUDRATE_COUNT] = { BAUD_115200, BAUD_57600, BAUD_38400, BAUD_19200, BAUD_9600, BAUD_230400, BAUD_460800, BAUD_921600 };
96103

@@ -526,6 +533,10 @@ void gpsInit(void)
526533
return;
527534
}
528535

536+
#ifdef USE_GPS_PROTO_UBLOX
537+
serialSetRxBuffer(gpsState.gpsPort, gpsRxBuffer, sizeof(gpsRxBuffer));
538+
#endif
539+
529540
gpsSetState(GPS_INITIALIZING);
530541
}
531542

‎src/main/io/gps_ublox.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ static uint16_t hz2rate(uint8_t hz)
956956
return 1000 / hz;
957957
}
958958

959+
// Satellite details only feed gpssats and the OSD's GPS extra stats: about 2 Hz, in epochs
960+
static uint8_t ubloxSatInfoRate(void)
961+
{
962+
return MAX(1, (gpsState.gpsConfig->ubloxNavHz + 1) / 2);
963+
}
964+
959965
STATIC_PROTOTHREAD(gpsConfigure)
960966
{
961967
ptBegin(gpsConfigure);
@@ -1074,7 +1080,7 @@ STATIC_PROTOTHREAD(gpsConfigure)
10741080
{UBLOX_CFG_MSGOUT_NAV_VELNED_UART1, 0}, // 2
10751081
{UBLOX_CFG_MSGOUT_NAV_TIMEUTC_UART1, 0}, // 3
10761082
{UBLOX_CFG_MSGOUT_NAV_PVT_UART1, 1}, // 4
1077-
{UBLOX_CFG_MSGOUT_NAV_SIG_UART1, 1}, // 5
1083+
{UBLOX_CFG_MSGOUT_NAV_SIG_UART1, ubloxSatInfoRate()}, // 5
10781084
{UBLOX_CFG_MSGOUT_NAV_SAT_UART1, 0} // 6
10791085
};
10801086

@@ -1102,7 +1108,7 @@ STATIC_PROTOTHREAD(gpsConfigure)
11021108
configureMSG(MSG_CLASS_UBX, MSG_PVT, 1);
11031109
ptWait(_ack_state == UBX_ACK_GOT_ACK || _ack_state == UBX_ACK_GOT_NAK);
11041110

1105-
configureMSG(MSG_CLASS_UBX, MSG_NAV_SAT, 1);
1111+
configureMSG(MSG_CLASS_UBX, MSG_NAV_SAT, ubloxSatInfoRate());
11061112
ptWait(_ack_state == UBX_ACK_GOT_ACK || _ack_state == UBX_ACK_GOT_NAK);
11071113
} else { // Really old stuff, consider upgrading :), ols setting API, no PVT or NAV_SAT or NAV_SIG
11081114
// TODO: remove in INAV 9.0.0

‎src/test/unit/gps_heartbeat_unittest.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count) { UN
7272
void serialPrint(serialPort_t *instance, const char *str) { UNUSED(instance); UNUSED(str); }
7373
void serialSetMode(serialPort_t *instance, portMode_t mode) { UNUSED(instance); UNUSED(mode); }
7474
void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate) { UNUSED(instance); UNUSED(baudRate); }
75+
void serialSetRxBuffer(serialPort_t *instance, volatile uint8_t *buffer, uint32_t size) { UNUSED(instance); UNUSED(buffer); UNUSED(size); }
7576

7677
serialPort_t *openSerialPort(serialPortIdentifier_e identifier, serialPortFunction_e function,
7778
serialReceiveCallbackPtr rxCallback, void *rxCallbackData,

‎src/test/unit/gps_null_port_unittest.cc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ void serialSetBaudRate(serialPort_t *instance, uint32_t baudRate)
189189
UNUSED(baudRate);
190190
}
191191

192+
void serialSetRxBuffer(serialPort_t *instance, volatile uint8_t *buffer, uint32_t size)
193+
{
194+
UNUSED(instance);
195+
UNUSED(buffer);
196+
UNUSED(size);
197+
}
198+
192199
serialPort_t *openSerialPort(serialPortIdentifier_e identifier,
193200
serialPortFunction_e function,
194201
serialReceiveCallbackPtr rxCallback,

0 commit comments

Comments
 (0)