Skip to content

Commit e73e9a9

Browse files
authored
Merge pull request #3 from sparkfun/v12
Fix low power SD logging bug
2 parents 0f9e52e + ac5c09b commit e73e9a9

File tree

12 files changed

+438
-40
lines changed

12 files changed

+438
-40
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Change Log
22
======================
33

4+
v1.2
5+
---------
6+
7+
* Fix bug in findNextAvailableLog() that was causing logging to fail during after a sleep/wakeup and sometimes corrupt the microSD card.
8+
* Fix calculation of actualHz when power sleeping >2s.
9+
* Log sensor configurations to config file. This allows users to configure an OpenLog Artemis and then deploy that configuration to multiple units as needed.
10+
411
v1.1
512
---------
613

Binary file not shown.

Firmware/OpenLog_Artemis/OpenLog_Artemis.ino

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
*/
1919

20-
const float FIRMWARE_VERSION = 1.1;
20+
const float FIRMWARE_VERSION = 1.2;
2121

2222
#include "settings.h"
2323

@@ -145,7 +145,7 @@ MS8607 pressureSensor_MS8607;
145145

146146
//Global variables
147147
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
148-
unsigned long measurementStartTime; //Used to calc the actual update rate.
148+
uint64_t measurementStartTime; //Used to calc the actual update rate. Max is ~80,000,000ms in a 24 hour period.
149149
unsigned long measurementCount = 0; //Used to calc the actual update rate.
150150
String outputData;
151151
String beginSensorOutput;
@@ -158,7 +158,9 @@ const uint32_t maxUsBeforeSleep = 2000000; //Number of us between readings befor
158158
const byte menuTimeout = 45; //Menus will exit/timeout after this number of seconds
159159
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
160160

161-
unsigned long startTime = 0;
161+
//unsigned long startTime = 0;
162+
163+
#define DUMP(varname) {Serial.printf("%s: %llu\n", #varname, varname);}
162164

163165
void setup() {
164166
//If 3.3V rail drops below 3V, system will power down and maintain RTC
@@ -210,16 +212,18 @@ void setup() {
210212
if (beginSensors() == true) Serial.println(beginSensorOutput); //159 - 865ms but varies based on number of devices attached
211213
else msg("No sensors detected");
212214

213-
measurementStartTime = millis();
215+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down. Used RTC instead.
216+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
217+
measurementStartTime = rtcMillis();
218+
else
219+
measurementStartTime = millis();
214220

215221
//Serial.printf("Setup time: %.02f ms\n", (micros() - startTime) / 1000.0);
216222

217223
//If we are immediately going to go to sleep after the first reading then
218224
//first present the user with the config menu in case they need to change something
219225
if (settings.usBetweenReadings >= maxUsBeforeSleep)
220-
{
221-
menuMain(); //Present user menu at startup to allow configuration even in LP mode
222-
}
226+
menuMain();
223227
}
224228

225229
void loop() {
-2.47 MB
Binary file not shown.
-106 KB
Binary file not shown.

Firmware/OpenLog_Artemis/Sensors.ino

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ bool beginSensors()
212212
{
213213
if (pressureSensor_MS8607.begin(qwiic) == true) //Wire port. This checks both 0x40 and 0x76 sensor addresses
214214
{
215-
if(settings.sensor_MS8607.enableHeater == true)
215+
if (settings.sensor_MS8607.enableHeater == true)
216216
pressureSensor_MS8607.enable_heater();
217217
else
218218
pressureSensor_MS8607.disable_heater();
219-
219+
220220
pressureSensor_MS8607.set_pressure_resolution(settings.sensor_MS8607.pressureResolution);
221221
pressureSensor_MS8607.set_humidity_resolution(settings.sensor_MS8607.humidityResolution);
222222

@@ -683,10 +683,22 @@ void getData()
683683

684684
if (settings.logHertz)
685685
{
686-
//Calculate the actual update rate based on the sketch start time and the
687-
//number of updates we've completed.
688-
float actualRate = measurementCount * 1000.0 / (millis() - measurementStartTime);
686+
uint64_t currentMillis;
687+
688+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down
689+
//Used RTC instead
690+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
691+
{
692+
currentMillis = rtcMillis();
693+
}
694+
else
695+
{
696+
//Calculate the actual update rate based on the sketch start time and the
697+
//number of updates we've completed.
698+
currentMillis = millis();
699+
}
689700

701+
float actualRate = measurementCount * 1000.0 / (currentMillis - measurementStartTime);
690702
outputData += (String)actualRate + ","; //Hz
691703
helperText += "output_Hz,";
692704
}
@@ -717,7 +729,7 @@ void determineMaxI2CSpeed()
717729
maxSpeed = 100000;
718730

719731
//If user wants to limit the I2C bus speed, do it here
720-
if(maxSpeed > settings.qwiicBusMaxSpeed)
732+
if (maxSpeed > settings.qwiicBusMaxSpeed)
721733
maxSpeed = settings.qwiicBusMaxSpeed;
722734

723735
qwiic.setClock(maxSpeed);

Firmware/OpenLog_Artemis/logging.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
void msg(const char * message)
33
{
44
Serial.println(message);
5-
if(online.microSD)
5+
if (online.microSD)
66
sensorDataFile.println(message);
77
}
88

@@ -17,15 +17,16 @@ char* findNextAvailableLog(int &newFileNumber, const char *fileLeader)
1717
newFileNumber--; //Check if last log file was empty
1818

1919
//Search for next available log spot
20-
static char newFileName[13];
20+
static char newFileName[40];
2121
while (1)
2222
{
2323
sprintf(newFileName, "%s%05u.TXT", fileLeader, newFileNumber); //Splice the new file number into this file name
2424

2525
if (sd.exists(newFileName) == false) break; //File name not found so we will use it.
2626

2727
//File exists so open and see if it is empty. If so, use it.
28-
newFile.open(newFileName, O_READ);
28+
//newFile = sd.open(newFileName, O_READ);
29+
newFile.open(newFileName, O_READ); //exFat
2930
if (newFile.size() == 0) break; // File is empty so we will use it.
3031

3132
newFile.close(); // Close this existing file we just opened.

Firmware/OpenLog_Artemis/lowerPower.ino

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ void powerDown()
99
detachInterrupt(digitalPinToInterrupt(PIN_POWER_LOSS)); //Prevent voltage supervisor from waking us from sleep
1010

1111
//Save files before going to sleep
12-
// if (online.dataLogging == true)
13-
// {
14-
// sensorDataFile.sync();
15-
// }
16-
// if (online.serialLogging == true)
17-
// {
18-
// serialDataFile.sync();
19-
// }
20-
12+
// if (online.dataLogging == true)
13+
// {
14+
// sensorDataFile.sync();
15+
// }
16+
// if (online.serialLogging == true)
17+
// {
18+
// serialDataFile.sync();
19+
// }
20+
2121
//Serial.flush(); //Don't waste time waiting for prints to finish
2222

2323
// Wire.end(); //Power down I2C
@@ -29,7 +29,7 @@ void powerDown()
2929

3030
Serial.end(); //Power down UART
3131
Serial1.end();
32-
32+
3333
//Force the peripherals off
3434
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0);
3535
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM1);
@@ -49,7 +49,7 @@ void powerDown()
4949
qwiicPowerOff();
5050
imuPowerOff();
5151
microSDPowerOff();
52-
52+
5353
//Power down Flash, SRAM, cache
5454
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_CACHE); //Turn off CACHE
5555
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_FLASH_512K); //Turn off everything but lower 512k
@@ -108,14 +108,16 @@ void goToSleep()
108108
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART0);
109109
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_UART1);
110110

111-
112111
//Disable all pads
113112
for (int x = 0 ; x < 50 ; x++)
114113
am_hal_gpio_pinconfig(x, g_AM_HAL_GPIO_DISABLE);
115114

116115
//We can't leave these power control pins floating
117116
imuPowerOff();
118-
microSDPowerOff();
117+
//microSDPowerOff();
118+
119+
//Testing file record issues
120+
microSDPowerOn();
119121

120122
//Keep Qwiic bus powered on if user desires it
121123
if (settings.powerDownQwiicBusBetweenReads == true)
@@ -173,8 +175,6 @@ void wakeFromSleep()
173175
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
174176
am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ);
175177

176-
startTime = micros();
177-
178178
//Turn on ADC
179179
ap3_adc_setup();
180180

@@ -193,12 +193,8 @@ void wakeFromSleep()
193193

194194
beginSD(); //285 - 293ms
195195

196-
//loadSettings(); //50 - 250ms
197-
198196
beginQwiic();
199197

200-
//analogReadResolution(14); //Increase from default of 10
201-
202198
beginDataLogging(); //180ms
203199

204200
beginSerialLogging(); //20 - 99ms
@@ -207,8 +203,6 @@ void wakeFromSleep()
207203

208204
beginSensors(); //159 - 865ms but varies based on number of devices attached
209205

210-
measurementStartTime = millis();
211-
212206
//Serial.printf("Wake up time: %.02f ms\n", (micros() - startTime) / 1000.0);
213207

214208
//When we wake up micros has been reset to zero so we need to let the main loop know to take a reading
@@ -247,3 +241,17 @@ void imuPowerOff()
247241
pinMode(PIN_IMU_POWER, OUTPUT);
248242
digitalWrite(PIN_IMU_POWER, LOW);
249243
}
244+
245+
//Returns the number of milliseconds according to the RTC
246+
//Watch out for 24 hour roll over at 86,400,000ms
247+
uint32_t rtcMillis()
248+
{
249+
myRTC.getTime();
250+
uint32_t millisToday = 0;
251+
millisToday += (myRTC.hour * 3600000UL);
252+
millisToday += (myRTC.minute * 60000UL);
253+
millisToday += (myRTC.seconds * 1000UL);
254+
millisToday += (myRTC.hundredths * 10UL);
255+
256+
return(millisToday);
257+
}

Firmware/OpenLog_Artemis/menuMain.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ void menuMain()
7676
while (Serial.available()) Serial.read(); //Empty buffer of any newline chars
7777

7878
//Reset measurements
79-
measurementStartTime = millis(); //Update time
8079
measurementCount = 0;
8180
totalCharactersPrinted = 0;
81+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down. Used RTC instead.
82+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
83+
measurementStartTime = rtcMillis();
84+
else
85+
measurementStartTime = millis();
8286

8387
//Edge case: after 10Hz reading, user sets the log rate above 2s mark. We never go to sleep because
8488
//takeReading is not true. And since we don't wake up, takeReading never gets set to true.

Firmware/OpenLog_Artemis/nvm.ino

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ void recordSettingsToFile()
4646
if (sd.exists("OLA_settings.cfg"))
4747
sd.remove("OLA_settings.cfg");
4848

49-
FsFile settingsFile;
49+
//File settingsFile; //FAT16/32
50+
FsFile settingsFile; //exFat
5051
if (settingsFile.open("OLA_settings.cfg", O_CREAT | O_APPEND | O_WRITE) == false)
5152
{
5253
Serial.println("Failed to create settings file");
@@ -94,6 +95,14 @@ void recordSettingsToFile()
9495
settingsFile.println("qwiicBusMaxSpeed=" + (String)settings.qwiicBusMaxSpeed);
9596
// settingsFile.println("=" + (String)settings.sensor_LPS25HB.);
9697

98+
settingsFile.println("sensor_MS8607.log=" + (String)settings.sensor_MS8607.log);
99+
settingsFile.println("sensor_MS8607.logHumidity=" + (String)settings.sensor_MS8607.logHumidity);
100+
settingsFile.println("sensor_MS8607.logPressure=" + (String)settings.sensor_MS8607.logPressure);
101+
settingsFile.println("sensor_MS8607.logTemperature=" + (String)settings.sensor_MS8607.logTemperature);
102+
settingsFile.println("sensor_MS8607.enableHeater=" + (String)settings.sensor_MS8607.enableHeater);
103+
settingsFile.println("sensor_MS8607.pressureResolution=" + (String)settings.sensor_MS8607.pressureResolution);
104+
settingsFile.println("sensor_MS8607.humidityResolution=" + (String)settings.sensor_MS8607.humidityResolution);
105+
97106
settingsFile.close();
98107
}
99108
}
@@ -108,7 +117,8 @@ bool loadSettingsFromFile()
108117
{
109118
if (sd.exists("OLA_settings.cfg"))
110119
{
111-
FsFile settingsFile;
120+
//File settingsFile; //FAT16/32
121+
FsFile settingsFile; //exFat
112122
if (settingsFile.open("OLA_settings.cfg", O_READ) == false)
113123
{
114124
Serial.println("Failed to open settings file");
@@ -284,6 +294,29 @@ bool parseLine(char* str) {
284294
settings.powerDownQwiicBusBetweenReads = d;
285295
else if (strcmp(settingName, "qwiicBusMaxSpeed") == 0)
286296
settings.qwiicBusMaxSpeed = d;
297+
298+
/*
299+
LPS25HB
300+
NAU7802
301+
MCP9600
302+
VCNL4040
303+
*/
304+
305+
else if (strcmp(settingName, "sensor_MS8607.log") == 0)
306+
settings.sensor_MS8607.log = d;
307+
else if (strcmp(settingName, "sensor_MS8607.logHumidity") == 0)
308+
settings.sensor_MS8607.logHumidity = d;
309+
else if (strcmp(settingName, "sensor_MS8607.logPressure") == 0)
310+
settings.sensor_MS8607.logPressure = d;
311+
else if (strcmp(settingName, "sensor_MS8607.logTemperature") == 0)
312+
settings.sensor_MS8607.logTemperature = d;
313+
else if (strcmp(settingName, "sensor_MS8607.enableHeater") == 0)
314+
settings.sensor_MS8607.enableHeater = d;
315+
else if (strcmp(settingName, "sensor_MS8607.pressureResolution") == 0)
316+
settings.sensor_MS8607.pressureResolution = (MS8607_pressure_resolution)d;
317+
else if (strcmp(settingName, "sensor_MS8607.humidityResolution") == 0)
318+
settings.sensor_MS8607.humidityResolution = (MS8607_humidity_resolution)d;
319+
287320
// else if (strcmp(settingName, "") == 0)
288321
// settings. = d;
289322
else

0 commit comments

Comments
 (0)