Skip to content

Commit a9decf9

Browse files
committed
Add system check sketch
1 parent 1a0d26c commit a9decf9

File tree

15 files changed

+1602
-0
lines changed

15 files changed

+1602
-0
lines changed
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
void beginBoard()
2+
{
3+
//Use ADC to check 50% resistor divider
4+
int pin_adc_rtk_facet = 35;
5+
uint16_t idValue = analogReadMilliVolts(pin_adc_rtk_facet);
6+
log_d("Board ADC ID: %d", idValue);
7+
8+
if (idValue > (3300 / 2 * 0.9) && idValue < (3300 / 2 * 1.1))
9+
{
10+
productVariant = RTK_FACET;
11+
}
12+
else if (idValue > (3300 * 2 / 3 * 0.9) && idValue < (3300 * 2 / 3 * 1.1))
13+
{
14+
productVariant = RTK_FACET_LBAND;
15+
}
16+
else if (idValue > (3300 * 3.3 / 13.3 * 0.9) && idValue < (3300 * 3.3 / 13.3 * 1.1))
17+
{
18+
productVariant = RTK_EXPRESS;
19+
}
20+
else if (idValue > (3300 * 10 / 13.3 * 0.9) && idValue < (3300 * 10 / 13.3 * 1.1))
21+
{
22+
productVariant = RTK_EXPRESS_PLUS;
23+
}
24+
else if (isConnected(0x19) == true) //Check for accelerometer
25+
{
26+
if (zedModuleType == PLATFORM_F9P) productVariant = RTK_EXPRESS;
27+
else if (zedModuleType == PLATFORM_F9R) productVariant = RTK_EXPRESS_PLUS;
28+
}
29+
else
30+
{
31+
productVariant = RTK_SURVEYOR;
32+
}
33+
34+
//Setup hardware pins
35+
if (productVariant == RTK_SURVEYOR)
36+
{
37+
pin_batteryLevelLED_Red = 32;
38+
pin_batteryLevelLED_Green = 33;
39+
pin_positionAccuracyLED_1cm = 2;
40+
pin_positionAccuracyLED_10cm = 15;
41+
pin_positionAccuracyLED_100cm = 13;
42+
pin_baseStatusLED = 4;
43+
pin_bluetoothStatusLED = 12;
44+
pin_setupButton = 5;
45+
pin_microSD_CS = 25;
46+
pin_zed_tx_ready = 26;
47+
pin_zed_reset = 27;
48+
pin_batteryLevel_alert = 36;
49+
//
50+
// //Bug in ZED-F9P v1.13 firmware causes RTK LED to not light when RTK Floating with SBAS on.
51+
// //The following changes the POR default but will be overwritten by settings in NVM or settings file
52+
// settings.ubxConstellations[1].enabled = false;
53+
54+
strcpy(platformFilePrefix, "SFE_Surveyor");
55+
strcpy(platformPrefix, "Surveyor");
56+
}
57+
else if (productVariant == RTK_EXPRESS || productVariant == RTK_EXPRESS_PLUS)
58+
{
59+
pin_muxA = 2;
60+
pin_muxB = 4;
61+
pin_powerSenseAndControl = 13;
62+
pin_setupButton = 14;
63+
pin_microSD_CS = 25;
64+
pin_dac26 = 26;
65+
pin_powerFastOff = 27;
66+
pin_adc39 = 39;
67+
//
68+
// pinMode(pin_powerSenseAndControl, INPUT_PULLUP);
69+
// pinMode(pin_powerFastOff, INPUT);
70+
//
71+
// if (esp_reset_reason() == ESP_RST_POWERON)
72+
// {
73+
// powerOnCheck(); //Only do check if we POR start
74+
// }
75+
//
76+
// pinMode(pin_setupButton, INPUT_PULLUP);
77+
//
78+
// setMuxport(settings.dataPortChannel); //Set mux to user's choice: NMEA, I2C, PPS, or DAC
79+
80+
if (productVariant == RTK_EXPRESS)
81+
{
82+
strcpy(platformFilePrefix, "SFE_Express");
83+
strcpy(platformPrefix, "Express");
84+
}
85+
else if (productVariant == RTK_EXPRESS_PLUS)
86+
{
87+
strcpy(platformFilePrefix, "SFE_Express_Plus");
88+
strcpy(platformPrefix, "Express Plus");
89+
}
90+
}
91+
else if (productVariant == RTK_FACET || productVariant == RTK_FACET_LBAND)
92+
{
93+
// //v11
94+
pin_muxA = 2;
95+
pin_muxB = 0;
96+
pin_powerSenseAndControl = 13;
97+
pin_peripheralPowerControl = 14;
98+
pin_microSD_CS = 25;
99+
pin_dac26 = 26;
100+
pin_powerFastOff = 27;
101+
pin_adc39 = 39;
102+
103+
pin_radio_rx = 33;
104+
pin_radio_tx = 32;
105+
pin_radio_rst = 15;
106+
pin_radio_pwr = 4;
107+
pin_radio_cts = 5;
108+
//pin_radio_rts = 255; //Not implemented
109+
//
110+
// pinMode(pin_powerSenseAndControl, INPUT_PULLUP);
111+
// pinMode(pin_powerFastOff, INPUT);
112+
//
113+
// if (esp_reset_reason() == ESP_RST_POWERON)
114+
// {
115+
// powerOnCheck(); //Only do check if we POR start
116+
// }
117+
//
118+
// pinMode(pin_peripheralPowerControl, OUTPUT);
119+
// digitalWrite(pin_peripheralPowerControl, HIGH); //Turn on SD, ZED, etc
120+
//
121+
// setMuxport(settings.dataPortChannel); //Set mux to user's choice: NMEA, I2C, PPS, or DAC
122+
//
123+
// //CTS is active low. ESP32 pin 5 has pullup at POR. We must drive it low.
124+
// pinMode(pin_radio_cts, OUTPUT);
125+
// digitalWrite(pin_radio_cts, LOW);
126+
127+
if (productVariant == RTK_FACET)
128+
{
129+
strcpy(platformFilePrefix, "SFE_Facet");
130+
strcpy(platformPrefix, "Facet");
131+
}
132+
else if (productVariant == RTK_FACET_LBAND)
133+
{
134+
strcpy(platformFilePrefix, "SFE_Facet_LBand");
135+
strcpy(platformPrefix, "Facet L-Band");
136+
}
137+
}
138+
139+
//Get unit MAC address
140+
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
141+
unitMACAddress[5] += 2; //Convert MAC address to Bluetooth MAC (add 2): https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system.html#mac-address
142+
143+
// //For all boards, check reset reason. If reset was due to wdt or panic, append last log
144+
// if (esp_reset_reason() == ESP_RST_POWERON)
145+
// {
146+
// reuseLastLog = false; //Start new log
147+
//
148+
// loadSettingsPartial();
149+
// if (settings.enableResetDisplay == true)
150+
// {
151+
// settings.resetCount = 0;
152+
// recordSystemSettings(); //Record to NVM
153+
// }
154+
// }
155+
// else
156+
// {
157+
// reuseLastLog = true; //Attempt to reuse previous log
158+
//
159+
// loadSettingsPartial();
160+
// if (settings.enableResetDisplay == true)
161+
// {
162+
// settings.resetCount++;
163+
// Serial.printf("resetCount: %d\n\r", settings.resetCount);
164+
// recordSystemSettings(); //Record to NVM
165+
// }
166+
//
167+
// Serial.print("Reset reason: ");
168+
// switch (esp_reset_reason())
169+
// {
170+
// case ESP_RST_UNKNOWN: Serial.println(F("ESP_RST_UNKNOWN")); break;
171+
// case ESP_RST_POWERON : Serial.println(F("ESP_RST_POWERON")); break;
172+
// case ESP_RST_SW : Serial.println(F("ESP_RST_SW")); break;
173+
// case ESP_RST_PANIC : Serial.println(F("ESP_RST_PANIC")); break;
174+
// case ESP_RST_INT_WDT : Serial.println(F("ESP_RST_INT_WDT")); break;
175+
// case ESP_RST_TASK_WDT : Serial.println(F("ESP_RST_TASK_WDT")); break;
176+
// case ESP_RST_WDT : Serial.println(F("ESP_RST_WDT")); break;
177+
// case ESP_RST_DEEPSLEEP : Serial.println(F("ESP_RST_DEEPSLEEP")); break;
178+
// case ESP_RST_BROWNOUT : Serial.println(F("ESP_RST_BROWNOUT")); break;
179+
// case ESP_RST_SDIO : Serial.println(F("ESP_RST_SDIO")); break;
180+
// default : Serial.println(F("Unknown"));
181+
// }
182+
// }
183+
}
184+
185+
186+
//Connect to ZED module and identify particulars
187+
void beginGNSS()
188+
{
189+
if (i2cGNSS.begin() == false)
190+
{
191+
log_d("GNSS Failed to begin. Trying again.");
192+
193+
//Try again with power on delay
194+
delay(1000); //Wait for ZED-F9P to power up before it can respond to ACK
195+
if (i2cGNSS.begin() == false)
196+
{
197+
//displayGNSSFail(1000);
198+
online.gnss = false;
199+
return;
200+
}
201+
}
202+
203+
//Increase transactions to reduce transfer time
204+
i2cGNSS.i2cTransactionSize = 128;
205+
206+
//Check the firmware version of the ZED-F9P. Based on Example21_ModuleInfo.
207+
if (i2cGNSS.getModuleInfo(1100) == true) // Try to get the module info
208+
{
209+
//i2cGNSS.minfo.extension[1] looks like 'FWVER=HPG 1.12'
210+
strcpy(zedFirmwareVersion, i2cGNSS.minfo.extension[1]);
211+
212+
//Remove 'FWVER='. It's extraneous and = causes settings file parsing issues
213+
char *ptr = strstr(zedFirmwareVersion, "FWVER=");
214+
if (ptr != NULL)
215+
strcpy(zedFirmwareVersion, ptr + strlen("FWVER="));
216+
217+
//Convert version to a uint8_t
218+
if (strstr(zedFirmwareVersion, "1.00") != NULL)
219+
zedFirmwareVersionInt = 100;
220+
else if (strstr(zedFirmwareVersion, "1.12") != NULL)
221+
zedFirmwareVersionInt = 112;
222+
else if (strstr(zedFirmwareVersion, "1.13") != NULL)
223+
zedFirmwareVersionInt = 113;
224+
else if (strstr(zedFirmwareVersion, "1.20") != NULL) //Mostly for F9R HPS 1.20, but also F9P HPG v1.20 Spartan future support
225+
zedFirmwareVersionInt = 120;
226+
else if (strstr(zedFirmwareVersion, "1.21") != NULL) //Future F9R HPS v1.21
227+
zedFirmwareVersionInt = 121;
228+
else if (strstr(zedFirmwareVersion, "1.30") != NULL) //ZED-F9P released Dec, 2021
229+
zedFirmwareVersionInt = 130;
230+
else if (strstr(zedFirmwareVersion, "1.32") != NULL) //ZED-F9P released May, 2022
231+
zedFirmwareVersionInt = 132;
232+
else
233+
{
234+
Serial.printf("Unknown firmware version: %s\n\r", zedFirmwareVersion);
235+
zedFirmwareVersionInt = 99; //0.99 invalid firmware version
236+
}
237+
238+
//Determine if we have a ZED-F9P (Express/Facet) or an ZED-F9R (Express Plus/Facet Plus)
239+
if (strstr(i2cGNSS.minfo.extension[3], "ZED-F9P") != NULL)
240+
zedModuleType = PLATFORM_F9P;
241+
else if (strstr(i2cGNSS.minfo.extension[3], "ZED-F9R") != NULL)
242+
zedModuleType = PLATFORM_F9R;
243+
else
244+
{
245+
Serial.printf("Unknown ZED module: %s\n\r", i2cGNSS.minfo.extension[3]);
246+
zedModuleType = PLATFORM_F9P;
247+
}
248+
249+
printZEDInfo(); //Print module type and firmware version
250+
}
251+
252+
online.gnss = true;
253+
}
254+
255+
//Configure the on board MAX17048 fuel gauge
256+
void beginFuelGauge()
257+
{
258+
// Set up the MAX17048 LiPo fuel gauge
259+
if (lipo.begin() == false)
260+
{
261+
Serial.println(F("MAX17048 not detected. Continuing."));
262+
return;
263+
}
264+
265+
//Always use hibernate mode
266+
if (lipo.getHIBRTActThr() < 0xFF) lipo.setHIBRTActThr((uint8_t)0xFF);
267+
if (lipo.getHIBRTHibThr() < 0xFF) lipo.setHIBRTHibThr((uint8_t)0xFF);
268+
269+
Serial.println(F("MAX17048 configuration complete"));
270+
271+
online.battery = true;
272+
}
273+
274+
void beginSD()
275+
{
276+
bool gotSemaphore;
277+
278+
online.microSD = false;
279+
gotSemaphore = false;
280+
while (settings.enableSD == true)
281+
{
282+
//Setup SD card access semaphore
283+
if (sdCardSemaphore == NULL)
284+
sdCardSemaphore = xSemaphoreCreateMutex();
285+
else if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) != pdPASS)
286+
{
287+
//This is OK since a retry will occur next loop
288+
log_d("sdCardSemaphore failed to yield, Begin.ino line %d\r\n", __LINE__);
289+
break;
290+
}
291+
gotSemaphore = true;
292+
293+
pinMode(pin_microSD_CS, OUTPUT);
294+
digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected
295+
296+
//Allocate the data structure that manages the microSD card
297+
if (!sd)
298+
{
299+
sd = new SdFat();
300+
if (!sd)
301+
{
302+
log_d("Failed to allocate the SdFat structure!");
303+
break;
304+
}
305+
}
306+
307+
//Do a quick test to see if a card is present
308+
int tries = 0;
309+
int maxTries = 5;
310+
while (tries < maxTries)
311+
{
312+
if (sdPresent() == true) break;
313+
log_d("SD present failed. Trying again %d out of %d", tries + 1, maxTries);
314+
315+
//Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf
316+
//Max current is 200mA average across 1s, peak 300mA
317+
delay(10);
318+
tries++;
319+
}
320+
if (tries == maxTries) break;
321+
322+
//If an SD card is present, allow SdFat to take over
323+
log_d("SD card detected");
324+
325+
if (settings.spiFrequency > 16)
326+
{
327+
Serial.println("Error: SPI Frequency out of range. Default to 16MHz");
328+
settings.spiFrequency = 16;
329+
}
330+
331+
if (sd->begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == false)
332+
{
333+
tries = 0;
334+
maxTries = 1;
335+
for ( ; tries < maxTries ; tries++)
336+
{
337+
log_d("SD init failed. Trying again %d out of %d", tries + 1, maxTries);
338+
339+
delay(250); //Give SD more time to power up, then try again
340+
if (sd->begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == true) break;
341+
}
342+
343+
if (tries == maxTries)
344+
{
345+
Serial.println(F("SD init failed. Is card present? Formatted?"));
346+
digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected
347+
break;
348+
}
349+
}
350+
351+
//Change to root directory. All new file creation will be in root.
352+
if (sd->chdir() == false)
353+
{
354+
Serial.println(F("SD change directory failed"));
355+
break;
356+
}
357+
358+
// if (createTestFile() == false)
359+
// {
360+
// Serial.println(F("Failed to create test file. Format SD card with 'SD Card Formatter'."));
361+
// displaySDFail(5000);
362+
// break;
363+
// }
364+
//
365+
// //Load firmware file from the microSD card if it is present
366+
// scanForFirmware();
367+
368+
Serial.println(F("microSD: Online"));
369+
online.microSD = true;
370+
break;
371+
}
372+
373+
//Free the semaphore
374+
if (sdCardSemaphore && gotSemaphore)
375+
xSemaphoreGive(sdCardSemaphore); //Make the file system available for use
376+
}

0 commit comments

Comments
 (0)