Skip to content

Commit 1c1ac09

Browse files
committed
Add firmware upgrade detection and menu. Change GPS to auto PVT and HPPOSLLH.
1 parent 8e7b7ad commit 1c1ac09

File tree

4 files changed

+181
-7
lines changed

4 files changed

+181
-7
lines changed

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
and communicates with the ZED-F9P.
88
99
Select the ESP32 Dev Module from the boards list. This maps the same pins to the ESP32-WROOM module.
10+
Select 'Minimal SPIFFS (1.9MB App)' from the partition list. This will enable SD firmware updates.
1011
1112
Special thanks to Avinab Malla for guidance on getting xTasks implemented.
1213
@@ -29,6 +30,9 @@
2930
(Done) Ports - Configure Radio and Data port baud rates
3031
(Done) Test menu
3132
Enable various debug outputs sent over BT
33+
34+
when user exits wifi mode, turn BT back on
35+
3236
*/
3337

3438
const int FIRMWARE_VERSION_MAJOR = 1;
@@ -170,6 +174,14 @@ TaskHandle_t F9PSerialWriteTaskHandle = NULL; //Store handles so that we can kil
170174
MicroOLED oled(PIN_RESET, DC_JUMPER);
171175
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
172176

177+
//Firmware binaries loaded from SD
178+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
179+
#include <Update.h>
180+
int binCount = 0;
181+
char binFileNames[10][50];
182+
const char* forceFirmwareFileName = "RTK_Surveyor_Firmware_Force.bin"; //File that will be loaded at startup regardless of user input
183+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
184+
173185
//Global variables
174186
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
175187
uint8_t unitMACAddress[6]; //Use MAC address in BT broadcast and display
@@ -206,10 +218,14 @@ void setup()
206218
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
207219
beginEEPROM();
208220

209-
delay(3000);
210221
//eepromErase();
211222

212223
beginSD(); //Test if SD is present
224+
if (online.microSD == true)
225+
{
226+
Serial.println(F("microSD online"));
227+
scanForFirmware(); //See if SD card contains new firmware that should be loaded at startup
228+
}
213229

214230
loadSettings(); //Attempt to load settings after SD is started so we can read the settings file if available
215231

@@ -223,9 +239,6 @@ void setup()
223239

224240
beginGNSS(); //Connect and configure ZED-F9P
225241

226-
if (online.microSD == true)
227-
Serial.println(F("microSD card online"));
228-
229242
Serial.flush(); //Complete any previous prints
230243

231244
danceLEDs(); //Turn on LEDs like a car dashboard

Firmware/RTK_Surveyor/System.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,8 @@ bool configureUbloxModule()
215215
//Make sure the appropriate NMEA sentences are enabled
216216
response &= enableNMEASentences(COM_PORT_UART1);
217217

218-
response &= myGPS.setAutoPVT(true); //Tell the GPS to "send" each solution
219-
//response &= myGPS.setAutoPVT(true, false); //Tell the GPS to "send" each solution and the lib not to update stale data implicitly
220-
//response &= myGPS.setAutoPVT(false); //Turn off PVT
218+
response &= myGPS.setAutoPVT(true, false); //Tell the GPS to "send" each solution, but do not update stale data when accessed
219+
response &= myGPS.setAutoHPPOSLLH(true, false); //Tell the GPS to "send" each high res solution, but do not update stale data when accessed
221220

222221
if (getSerialRate(COM_PORT_UART1) != settings.dataPortBaud)
223222
{
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//Update firmware if bin files found
2+
void menuFirmware()
3+
{
4+
if (online.microSD == false)
5+
{
6+
Serial.println(F("No SD card detected"));
7+
}
8+
9+
if (binCount == 0)
10+
{
11+
Serial.println("No valid binary files found.");
12+
delay(2000);
13+
return;
14+
}
15+
16+
while (1)
17+
{
18+
Serial.println();
19+
Serial.println(F("Menu: Update Firmware Menu"));
20+
21+
for (int x = 0 ; x < binCount ; x++)
22+
{
23+
Serial.printf("%d) Load %s\n", x + 1, binFileNames[x]);
24+
}
25+
26+
Serial.println(F("x) Exit"));
27+
28+
int incoming = getNumber(menuTimeout); //Timeout after x seconds
29+
30+
if (incoming > 0 && incoming < (binCount + 1))
31+
{
32+
//Adjust incoming to match array
33+
incoming--;
34+
updateFromSD(binFileNames[incoming]);
35+
}
36+
else if (incoming == STATUS_PRESSED_X)
37+
break;
38+
else if (incoming == STATUS_GETNUMBER_TIMEOUT)
39+
break;
40+
else
41+
Serial.printf("Bad value: %d\n", incoming);
42+
}
43+
44+
while (Serial.available()) Serial.read(); //Empty buffer of any newline chars
45+
}
46+
47+
//Looks for matching binary files in root
48+
//Loads a global called binCount
49+
void scanForFirmware()
50+
{
51+
if (online.microSD == true)
52+
{
53+
//Count available binaries
54+
SdFile tempFile;
55+
SdFile dir;
56+
const char* BIN_EXT = "bin";
57+
const char* BIN_HEADER = "RTK_Surveyor_Firmware";
58+
char fname[50]; //Handle long file names
59+
60+
dir.open("/"); //Open root
61+
62+
while (tempFile.openNext(&dir, O_READ))
63+
{
64+
if (tempFile.isFile())
65+
{
66+
tempFile.getName(fname, sizeof(fname));
67+
68+
if (strcmp(forceFirmwareFileName, fname) == 0)
69+
updateFromSD((char *)forceFirmwareFileName);
70+
71+
//Check for 'sfe_rtk' and 'bin' extension
72+
if (strcmp(BIN_EXT, &fname[strlen(fname) - strlen(BIN_EXT)]) == 0)
73+
{
74+
if (strstr(fname, BIN_HEADER) != NULL)
75+
{
76+
strcpy(binFileNames[binCount++], fname); //Add this to the array
77+
}
78+
else
79+
Serial.printf("Unknown: %s\n", fname);
80+
}
81+
}
82+
tempFile.close();
83+
}
84+
}
85+
}
86+
87+
//Look for firmware file on SD card and update as needed
88+
void updateFromSD(char *firmwareFileName)
89+
{
90+
Serial.printf("Loading %s\n", firmwareFileName);
91+
if (sd.exists(firmwareFileName))
92+
{
93+
SdFile firmwareFile;
94+
firmwareFile.open(firmwareFileName, O_READ);
95+
96+
size_t updateSize = firmwareFile.fileSize();
97+
if (updateSize == 0)
98+
{
99+
Serial.println(F("Error: Binary is empty"));
100+
firmwareFile.close();
101+
return;
102+
}
103+
104+
if (Update.begin(updateSize) == false)
105+
{
106+
Serial.println(F("Update begin failed. Not enough partition space available."));
107+
firmwareFile.close();
108+
return;
109+
}
110+
111+
Serial.print(F("Moving file to OTA section"));
112+
113+
const int pageSize = 512;
114+
byte dataArray[pageSize];
115+
int bytesWritten = 0;
116+
117+
//Bulk write from the SD file to the EEPROM
118+
while (firmwareFile.available())
119+
{
120+
int bytesToWrite = pageSize; //Max number of bytes to read
121+
if (firmwareFile.available() < bytesToWrite) bytesToWrite = firmwareFile.available(); //Trim this read size as needed
122+
123+
firmwareFile.read(dataArray, bytesToWrite); //Read the next set of bytes from file into our temp array
124+
125+
if (Update.write(dataArray, bytesToWrite) != bytesToWrite)
126+
Serial.println(F("Write failed"));
127+
else
128+
bytesWritten += bytesToWrite;
129+
130+
if (bytesWritten % (512 * 32) == 0) Serial.print("."); //Indicate progress
131+
}
132+
Serial.println(F("\nFile move complete"));
133+
134+
if (Update.end())
135+
{
136+
if (Update.isFinished())
137+
{
138+
Serial.println(F("Firmware updated successfully. Rebooting. Good bye!"));
139+
delay(1000);
140+
ESP.restart();
141+
}
142+
else
143+
Serial.println(F("Update not finished? Something went wrong!"));
144+
}
145+
else
146+
{
147+
Serial.print(F("Error Occurred. Error #: "));
148+
Serial.println(String(Update.getError()));
149+
}
150+
151+
firmwareFile.close();
152+
}
153+
else
154+
{
155+
Serial.println(F("No firmware file found"));
156+
}
157+
}

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void menuMain()
2323

2424
Serial.println(F("r) Reset all settings to default"));
2525

26+
if(binCount > 0)
27+
Serial.println(F("f) Firmware upgrade"));
28+
2629
Serial.println(F("t) Test menu"));
2730

2831
Serial.println(F("x) Exit"));
@@ -57,6 +60,8 @@ void menuMain()
5760
else
5861
Serial.println(F("Reset aborted"));
5962
}
63+
else if (incoming == 'f' && binCount > 0)
64+
menuFirmware();
6065
else if (incoming == 't')
6166
menuTest();
6267
else if (incoming == 'x')

0 commit comments

Comments
 (0)