2828#define GPS_RESET_MODE HIGH
2929#endif
3030
31+ // Not all platforms have std::size().
32+ template <typename T, std::size_t N> std::size_t array_count (const T (&)[N])
33+ {
34+ return N;
35+ }
36+
3137#if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(ARCH_ESP32) || defined(ARCH_PORTDUINO)
3238HardwareSerial *GPS::_serial_gps = &Serial1;
3339#elif defined(ARCH_RP2040)
@@ -46,8 +52,14 @@ static GPSUpdateScheduling scheduling;
4652// / only init that port once.
4753static bool didSerialInit;
4854
49- static struct uBloxGnssModelInfo info;
50- static uint8_t uBloxProtocolVersion;
55+ static struct uBloxGnssModelInfo {
56+ char swVersion[30 ];
57+ char hwVersion[10 ];
58+ uint8_t extensionNo;
59+ char extension[10 ][30 ];
60+ uint8_t protocol_version;
61+ } ublox_info;
62+
5163#define GPS_SOL_EXPIRY_MS 5000 // in millis. give 1 second time to combine different sentences. NMEA Frequency isn't higher anyway
5264#define NMEA_MSG_GXGSA " GNGSA" // GSA message (GPGSA, GNGSA etc)
5365
@@ -412,6 +424,15 @@ int GPS::getACK(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
412424 return 0 ;
413425}
414426
427+ #if GPS_BAUDRATE_FIXED
428+ // if GPS_BAUDRATE is specified in variant, only try that.
429+ static const int serialSpeeds[1 ] = {GPS_BAUDRATE};
430+ static const int rareSerialSpeeds[1 ] = {GPS_BAUDRATE};
431+ #else
432+ static const int serialSpeeds[3 ] = {9600 , 115200 , 38400 };
433+ static const int rareSerialSpeeds[3 ] = {4800 , 57600 , GPS_BAUDRATE};
434+ #endif
435+
415436/* *
416437 * @brief Setup the GPS based on the model detected.
417438 * We detect the GPS by cycling through a set of baud rates, first common then rare.
@@ -428,7 +449,7 @@ bool GPS::setup()
428449 LOG_DEBUG (" Probe for GPS at %d" , serialSpeeds[speedSelect]);
429450 gnssModel = probe (serialSpeeds[speedSelect]);
430451 if (gnssModel == GNSS_MODEL_UNKNOWN) {
431- if (++speedSelect == sizeof (serialSpeeds) / sizeof ( int )) {
452+ if (++speedSelect == array_count (serialSpeeds)) {
432453 speedSelect = 0 ;
433454 ++probeTries;
434455 }
@@ -439,7 +460,7 @@ bool GPS::setup()
439460 LOG_DEBUG (" Probe for GPS at %d" , rareSerialSpeeds[speedSelect]);
440461 gnssModel = probe (rareSerialSpeeds[speedSelect]);
441462 if (gnssModel == GNSS_MODEL_UNKNOWN) {
442- if (++speedSelect == sizeof (rareSerialSpeeds) / sizeof ( int )) {
463+ if (++speedSelect == array_count (rareSerialSpeeds)) {
443464 LOG_WARN (" Give up on GPS probe and set to %d" , GPS_BAUDRATE);
444465 return true ;
445466 }
@@ -635,7 +656,7 @@ bool GPS::setup()
635656 SEND_UBX_PACKET (0x06 , 0x01 , _message_RMC, " enable NMEA RMC" , 500 );
636657 SEND_UBX_PACKET (0x06 , 0x01 , _message_GGA, " enable NMEA GGA" , 500 );
637658
638- if (uBloxProtocolVersion >= 18 ) {
659+ if (ublox_info. protocol_version >= 18 ) {
639660 clearBuffer ();
640661 SEND_UBX_PACKET (0x06 , 0x86 , _message_PMS, " enable powersave for GPS" , 500 );
641662 SEND_UBX_PACKET (0x06 , 0x3B , _message_CFG_PM2, " enable powersave details for GPS" , 500 );
@@ -1131,7 +1152,7 @@ GnssModel_t GPS::probe(int serialSpeed)
11311152 }
11321153#endif
11331154
1134- memset (&info , 0 , sizeof (struct uBloxGnssModelInfo ));
1155+ memset (&ublox_info , 0 , sizeof (ublox_info ));
11351156 uint8_t buffer[768 ] = {0 };
11361157 delay (100 );
11371158
@@ -1198,64 +1219,64 @@ GnssModel_t GPS::probe(int serialSpeed)
11981219 if (len) {
11991220 uint16_t position = 0 ;
12001221 for (int i = 0 ; i < 30 ; i++) {
1201- info .swVersion [i] = buffer[position];
1222+ ublox_info .swVersion [i] = buffer[position];
12021223 position++;
12031224 }
12041225 for (int i = 0 ; i < 10 ; i++) {
1205- info .hwVersion [i] = buffer[position];
1226+ ublox_info .hwVersion [i] = buffer[position];
12061227 position++;
12071228 }
12081229
12091230 while (len >= position + 30 ) {
12101231 for (int i = 0 ; i < 30 ; i++) {
1211- info .extension [info .extensionNo ][i] = buffer[position];
1232+ ublox_info .extension [ublox_info .extensionNo ][i] = buffer[position];
12121233 position++;
12131234 }
1214- info .extensionNo ++;
1215- if (info .extensionNo > 9 )
1235+ ublox_info .extensionNo ++;
1236+ if (ublox_info .extensionNo > 9 )
12161237 break ;
12171238 }
12181239
12191240 LOG_DEBUG (" Module Info : " );
1220- LOG_DEBUG (" Soft version: %s" , info .swVersion );
1221- LOG_DEBUG (" Hard version: %s" , info .hwVersion );
1222- LOG_DEBUG (" Extensions:%d" , info .extensionNo );
1223- for (int i = 0 ; i < info .extensionNo ; i++) {
1224- LOG_DEBUG (" %s" , info .extension [i]);
1241+ LOG_DEBUG (" Soft version: %s" , ublox_info .swVersion );
1242+ LOG_DEBUG (" Hard version: %s" , ublox_info .hwVersion );
1243+ LOG_DEBUG (" Extensions:%d" , ublox_info .extensionNo );
1244+ for (int i = 0 ; i < ublox_info .extensionNo ; i++) {
1245+ LOG_DEBUG (" %s" , ublox_info .extension [i]);
12251246 }
12261247
12271248 memset (buffer, 0 , sizeof (buffer));
12281249
12291250 // tips: extensionNo field is 0 on some 6M GNSS modules
1230- for (int i = 0 ; i < info .extensionNo ; ++i) {
1231- if (!strncmp (info .extension [i], " MOD=" , 4 )) {
1232- strncpy ((char *)buffer, &(info .extension [i][4 ]), sizeof (buffer));
1233- } else if (!strncmp (info .extension [i], " PROTVER" , 7 )) {
1251+ for (int i = 0 ; i < ublox_info .extensionNo ; ++i) {
1252+ if (!strncmp (ublox_info .extension [i], " MOD=" , 4 )) {
1253+ strncpy ((char *)buffer, &(ublox_info .extension [i][4 ]), sizeof (buffer));
1254+ } else if (!strncmp (ublox_info .extension [i], " PROTVER" , 7 )) {
12341255 char *ptr = nullptr ;
12351256 memset (buffer, 0 , sizeof (buffer));
1236- strncpy ((char *)buffer, &(info .extension [i][8 ]), sizeof (buffer));
1257+ strncpy ((char *)buffer, &(ublox_info .extension [i][8 ]), sizeof (buffer));
12371258 LOG_DEBUG (" Protocol Version:%s" , (char *)buffer);
12381259 if (strlen ((char *)buffer)) {
1239- uBloxProtocolVersion = strtoul ((char *)buffer, &ptr, 10 );
1240- LOG_DEBUG (" ProtVer=%d" , uBloxProtocolVersion );
1260+ ublox_info. protocol_version = strtoul ((char *)buffer, &ptr, 10 );
1261+ LOG_DEBUG (" ProtVer=%d" , ublox_info. protocol_version );
12411262 } else {
1242- uBloxProtocolVersion = 0 ;
1263+ ublox_info. protocol_version = 0 ;
12431264 }
12441265 }
12451266 }
1246- if (strncmp (info .hwVersion , " 00040007" , 8 ) == 0 ) {
1267+ if (strncmp (ublox_info .hwVersion , " 00040007" , 8 ) == 0 ) {
12471268 LOG_INFO (DETECTED_MESSAGE, " U-blox 6" , " 6" );
12481269 return GNSS_MODEL_UBLOX6;
1249- } else if (strncmp (info .hwVersion , " 00070000" , 8 ) == 0 ) {
1270+ } else if (strncmp (ublox_info .hwVersion , " 00070000" , 8 ) == 0 ) {
12501271 LOG_INFO (DETECTED_MESSAGE, " U-blox 7" , " 7" );
12511272 return GNSS_MODEL_UBLOX7;
1252- } else if (strncmp (info .hwVersion , " 00080000" , 8 ) == 0 ) {
1273+ } else if (strncmp (ublox_info .hwVersion , " 00080000" , 8 ) == 0 ) {
12531274 LOG_INFO (DETECTED_MESSAGE, " U-blox 8" , " 8" );
12541275 return GNSS_MODEL_UBLOX8;
1255- } else if (strncmp (info .hwVersion , " 00190000" , 8 ) == 0 ) {
1276+ } else if (strncmp (ublox_info .hwVersion , " 00190000" , 8 ) == 0 ) {
12561277 LOG_INFO (DETECTED_MESSAGE, " U-blox 9" , " 9" );
12571278 return GNSS_MODEL_UBLOX9;
1258- } else if (strncmp (info .hwVersion , " 000A0000" , 8 ) == 0 ) {
1279+ } else if (strncmp (ublox_info .hwVersion , " 000A0000" , 8 ) == 0 ) {
12591280 LOG_INFO (DETECTED_MESSAGE, " U-blox 10" , " 10" );
12601281 return GNSS_MODEL_UBLOX10;
12611282 }
@@ -1729,4 +1750,4 @@ void GPS::toggleGpsMode()
17291750 enable ();
17301751 }
17311752}
1732- #endif // Exclude GPS
1753+ #endif // Exclude GPS
0 commit comments