Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 73e2308

Browse files
committed
address review comments for serial protocol support; add reset example sketch
1 parent 7a05c41 commit 73e2308

File tree

3 files changed

+115
-10
lines changed

3 files changed

+115
-10
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Test baud rate changes on serial, factory reset, and hard reset.
3+
By: Thorsten von Eicken
4+
Date: January 29rd, 2019
5+
License: MIT. See license file for more information but you can
6+
basically do whatever you want with this code.
7+
8+
This example shows how to configure the library and U-Blox for serial port use as well as
9+
switching the module from the default 9600 baud to 38400.
10+
11+
Note: Long/lat are large numbers because they are * 10^7. To convert lat/long
12+
to something google maps understands simply divide the numbers by 10,000,000. We
13+
do this so that we don't have to use floating point numbers.
14+
15+
Leave NMEA parsing behind. Now you can simply ask the module for the datums you want!
16+
17+
Feel like supporting open source hardware?
18+
Buy a board from SparkFun!
19+
ZED-F9P RTK2: https://www.sparkfun.com/products/15136
20+
NEO-M8P RTK: https://www.sparkfun.com/products/15005
21+
SAM-M8Q: https://www.sparkfun.com/products/15106
22+
23+
Hardware Connections:
24+
Connect the U-Blox serial port to Serial1
25+
Open the serial monitor at 115200 baud to see the output
26+
*/
27+
28+
#include <SparkFun_Ublox_Arduino_Library.h> //http://librarymanager/All#SparkFun_Ublox_GPS
29+
SFE_UBLOX_GPS myGPS;
30+
31+
int state = 0; // steps through auto-baud, reset, etc states
32+
33+
void setup()
34+
{
35+
Serial.begin(115200);
36+
while (!Serial); //Wait for user to open terminal
37+
Serial.println("SparkFun Ublox Example");
38+
}
39+
40+
void loop()
41+
{
42+
Serial.print("===== STATE ");
43+
Serial.println(state);
44+
switch (state) {
45+
case 0: // auto-baud connection, then switch to 38400 and save config
46+
do {
47+
Serial.println("GPS: trying 38400 baud");
48+
Serial1.begin(38400);
49+
if (myGPS.begin(Serial1)) break;
50+
51+
delay(100);
52+
Serial.println("GPS: trying 9600 baud");
53+
Serial1.begin(9600);
54+
if (myGPS.begin(Serial1)) {
55+
Serial.println("GPS: connected at 9600 baud, switching to 38400");
56+
myGPS.setSerialRate(38400);
57+
delay(100);
58+
} else {
59+
delay(2000); //Wait a bit before trying again to limit the Serial output flood
60+
}
61+
} while(1);
62+
myGPS.setUART1Output(COM_TYPE_UBX); //Set the UART port to output UBX only
63+
myGPS.saveConfiguration(); //Save the current settings to flash and BBR
64+
Serial.println("GPS serial connected, saved config");
65+
state++;
66+
break;
67+
case 1: // hardReset, expect to see GPS back at 38400 baud
68+
Serial.println("Issuing hardReset (cold start)");
69+
myGPS.hardReset();
70+
delay(1000);
71+
Serial1.begin(38400);
72+
if (myGPS.begin(Serial1)) {
73+
Serial.println("Success.");
74+
state++;
75+
} else {
76+
Serial.println("*** GPS did not respond at 38400 baud, starting over.");
77+
state = 0;
78+
}
79+
break;
80+
case 2: // factoryReset, expect to see GPS back at 9600 baud
81+
Serial.println("Issuing factoryReset");
82+
myGPS.factoryReset();
83+
delay(2000); // takes more than one second... a loop to resync would be best
84+
Serial1.begin(9600);
85+
if (myGPS.begin(Serial1)) {
86+
Serial.println("Success.");
87+
state++;
88+
} else {
89+
Serial.println("*** GPS did not come back at 9600 baud, starting over.");
90+
state = 0;
91+
}
92+
break;
93+
case 3: // print version info
94+
Serial.print("GPS protocol version: ");
95+
Serial.print(myGPS.getProtocolVersionHigh());
96+
Serial.print('.');
97+
Serial.print(myGPS.getProtocolVersionLow());
98+
state = 0;
99+
}
100+
delay(1000);
101+
}

src/SparkFun_Ublox_Arduino_Library.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@ void SFE_UBLOX_GPS::factoryReset()
6868
packetCfg.id = UBX_CFG_CFG;
6969
packetCfg.len = 13;
7070
packetCfg.startingSpot = 0;
71-
for (int i=0; i<4; i++) {
71+
for (uint8_t i=0; i<4; i++) {
7272
payloadCfg[0+i] = 0xff; // clear mask: copy default config to permanent config
7373
payloadCfg[4+i] = 0x00; // save mask: don't save current to permanent
7474
payloadCfg[8+i] = 0x00; // load mask: don't copy permanent config to current
7575
}
7676
payloadCfg[12] = 0xff; // all forms of permanent memory
7777
sendCommand(packetCfg, 0); // don't expect ACK
78+
hardReset(); // cause factory default config to actually be loaded and used cleanly
79+
}
7880

81+
void SFE_UBLOX_GPS::hardReset()
82+
{
7983
// Issue hard reset
8084
packetCfg.cls = UBX_CLASS_CFG;
8185
packetCfg.id = UBX_CFG_RST;
@@ -90,13 +94,13 @@ void SFE_UBLOX_GPS::factoryReset()
9094

9195
//Changes the serial baud rate of the Ublox module, can't return success/fail 'cause ACK from modem
9296
//is lost due to baud rate change
93-
void SFE_UBLOX_GPS::setSerialRate(uint32_t baudrate, uint16_t maxWait)
97+
void SFE_UBLOX_GPS::setSerialRate(uint32_t baudrate, uint8_t uartPort, uint16_t maxWait)
9498
{
9599
//Get the current config values for the UART port
96-
getPortSettings(COM_PORT_UART1, maxWait); //This will load the payloadCfg array with current port settings
100+
getPortSettings(uartPort, maxWait); //This will load the payloadCfg array with current port settings
97101
#ifdef DEBUG
98-
Serial.printf("Current baud rate: %d\n",
99-
((uint32_t)payloadCfg[10]<<16) | ((uint32_t)payloadCfg[9]<<8) | payloadCfg[8]);
102+
debug.print("Current baud rate: ");
103+
debug.println(((uint32_t)payloadCfg[10]<<16) | ((uint32_t)payloadCfg[9]<<8) | payloadCfg[8]);
100104
#endif
101105

102106
packetCfg.cls = UBX_CLASS_CFG;
@@ -110,9 +114,8 @@ void SFE_UBLOX_GPS::setSerialRate(uint32_t baudrate, uint16_t maxWait)
110114
payloadCfg[10] = baudrate>>16;
111115
payloadCfg[11] = baudrate>>24;
112116
#ifdef DEBUG
113-
Serial.printf("Next baud rate: %d=0x%x\n",
114-
((uint32_t)payloadCfg[10]<<16) | ((uint32_t)payloadCfg[9]<<8) | payloadCfg[8],
115-
((uint32_t)payloadCfg[10]<<16) | ((uint32_t)payloadCfg[9]<<8) | payloadCfg[8]);
117+
debug.print("New baud rate:");
118+
debug.println(((uint32_t)payloadCfg[10]<<16) | ((uint32_t)payloadCfg[9]<<8) | payloadCfg[8]);
116119
#endif
117120

118121
sendCommand(packetCfg);

src/SparkFun_Ublox_Arduino_Library.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ class SFE_UBLOX_GPS
206206

207207
void printPacket(ubxPacket *packet); //Useful for debugging
208208

209-
void factoryReset(); //Send factory reset sequence (i.e. load "default" configuration)
209+
void factoryReset(); //Send factory reset sequence (i.e. load "default" configuration and perform hardReset)
210+
void hardReset(); //Perform a reset leading to a cold start (zero info start-up)
210211

211212
boolean setI2CAddress(uint8_t deviceAddress, uint16_t maxTime = 250); //Changes the I2C address of the Ublox module
212-
void setSerialRate(uint32_t baudrate, uint16_t maxTime = 250); //Changes the serial baud rate of the Ublox module
213+
void setSerialRate(uint32_t baudrate, uint8_t uartPort = COM_PORT_UART1, uint16_t maxTime = 250); //Changes the serial baud rate of the Ublox module, uartPort should be COM_PORT_UART1/2
213214
void setNMEAOutputPort(Stream &nmeaOutputPort); //Sets the internal variable for the port to direct NMEA characters to
214215

215216
boolean setNavigationFrequency(uint8_t navFreq, uint16_t maxWait = 250); //Set the number of nav solutions sent per second

0 commit comments

Comments
 (0)