-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCatena4430_cMeasurementLoop_SDcard.cpp
703 lines (588 loc) · 18.7 KB
/
Catena4430_cMeasurementLoop_SDcard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
Module: Catena4430_cMeasurementLoop_SDcard.cpp
Function:
Class for running the SD Card
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation November 2019
*/
#include "Catena4430_cMeasurementLoop.h"
#include "Catena4430_Sensor.h"
#include <Catena_Download.h>
#include <Catena_Fram.h>
#include <Arduino_LoRaWAN_lmic.h>
#include <SD.h>
#include <mcciadk_baselib.h>
using namespace McciCatena4430;
using namespace McciCatena;
/****************************************************************************\
|
| globals
|
\****************************************************************************/
SDClass gSD;
extern cMeasurementLoop *gpMeasurementLoopConcrete;
constexpr char gkMigrateFileName[] = "MIGRATE.V3";
constexpr char gkRejoinFileName[] = "REJOIN.NW";
/****************************************************************************\
|
| Some utilities
|
\****************************************************************************/
// turn on power to the SD card
void cMeasurementLoop::sdPowerUp(bool fOn)
{
gpio.setVsdcard(fOn);
}
void cMeasurementLoop::sdPrep()
{
digitalWrite(cMeasurementLoop::kSdCardCSpin, 1);
pinMode(cMeasurementLoop::kSdCardCSpin, OUTPUT);
if (! this->m_fSpi2Active)
{
this->m_pSPI2->begin();
this->m_fSpi2Active = true;
}
digitalWrite(cMeasurementLoop::kSdCardCSpin, 1);
this->sdPowerUp(true);
delay(100);
}
void cMeasurementLoop::sdFinish()
{
// gSD.end() calls card.forceIdle() which will
// (try to) put the card in the idle state.
if (! gSD.end())
{
gCatena.SafePrintf("gSD.end() timed out\n");
}
// turn off CS to avoid locking Vsdcard on.
this->m_pSPI2->end();
this->m_fSpi2Active = false;
pinMode(Catena::PIN_SPI2_MOSI, OUTPUT);
pinMode(Catena::PIN_SPI2_MISO, OUTPUT);
pinMode(Catena::PIN_SPI2_SCK, OUTPUT);
digitalWrite(Catena::PIN_SPI2_MOSI, 0);
digitalWrite(Catena::PIN_SPI2_MISO, 0);
digitalWrite(Catena::PIN_SPI2_SCK, 0);
digitalWrite(cMeasurementLoop::kSdCardCSpin, 0);
delay(1);
this->sdPowerUp(false);
}
/*
Name: McciCatena4430::cMeasurementLoop::fillTxBuffer()
Function:
Prepare a messages in a TxBuffer with data from current measurements.
Definition:
void McciCatena4430::cMeasurementLoop::fillTxBuffer(
cMeasurementLoop::TxBuffer_t& b
);
Description:
A format 0x22 message is prepared from the data in the cMeasurementLoop
object.
*/
bool
cMeasurementLoop::initSdCard(
)
{
bool fResult = this->checkSdCard();
sdFinish();
return fResult;
}
bool
cMeasurementLoop::checkSdCard()
{
sdPrep();
if (! gSD.begin(gSPI2, SPI_HALF_SPEED, kSdCardCSpin))
{
if (! this->fDisableLED)
{
uint8_t nBlink = 0;
while (nBlink < 5)
{
gpio.setGreen(true);
delay(100);
gpio.setGreen(false);
delay(100);
gpio.setGreen(true);
delay(100);
gpio.setGreen(false);
delay(500);
nBlink += 1;
}
}
return false;
}
else
return true;
}
static const char kHeader[] =
"Time,DevEUI,Raw,Port,Vbat,Version,BootCount,T,RH,P,Light,"
"CO2,P[0].delta,P[0].total,P[1].delta,P[1].total,"
"Act[7],Act[6],Act[5],Act[4],Act[3],Act[2],Act[1],Act[0]"
"\n";
bool
cMeasurementLoop::writeSdCard(
cMeasurementLoop::TxBuffer_t &b,
cMeasurementLoop::Measurement const & mData
)
{
cDate d;
bool fResult;
File dataFile;
if (! mData.DateTime.isValid())
{
if (! this->fDisableLED)
{
uint8_t nBlink = 0;
while (nBlink < 5)
{
gpio.setRed(true);
delay(100);
gpio.setRed(false);
delay(100);
gpio.setRed(true);
delay(100);
gpio.setRed(false);
delay(500);
nBlink += 1;
}
}
gCatena.SafePrintf("RTC not set, not storing data!\n");
return false;
}
fResult = this->checkSdCard();
if (! fResult)
gCatena.SafePrintf("** SD card not detected!\n");
if (fResult)
{
// make a directory
fResult = gSD.mkdir("Data");
if (! fResult)
gCatena.SafePrintf("mkdir failed\n");
}
if (fResult)
{
char fName[32];
bool fNew;
auto d = mData.DateTime;
McciAdkLib_Snprintf(fName, sizeof(fName), 0, "Data/%04u%02u%02u.dat", d.year(), d.month(), d.day());
fNew = !gSD.exists(fName);
if (fNew)
{
// gCatena.SafePrintf("%s not found, will create & write header\n", fName);
}
File dataFile = gSD.open(fName, FILE_WRITE);
if (dataFile)
{
if (fNew)
{
for (auto i : kHeader)
{
if (i == '\n')
{
dataFile.println();
}
else if (i == '\0')
{
break;
}
else
{
dataFile.print(i);
}
}
}
char buf[32];
McciAdkLib_Snprintf(
buf, sizeof(buf), 0,
"%04u-%02u-%02uT%02u:%02u:%02uZ,",
d.year(), d.month(), d.day(),
d.hour(), d.minute(), d.second()
);
dataFile.print(buf);
do {
CatenaBase::EUI64_buffer_t devEUI;
auto const pFram = gCatena.getFram();
// use devEUI.
if (pFram != nullptr &&
pFram->getField(cFramStorage::StandardKeys::kDevEUI, devEUI))
{
dataFile.print('"');
/* write the devEUI */
for (auto i = 0; i < sizeof(devEUI.b); ++i)
{
// the devEUI is stored in little-endian order.
McciAdkLib_Snprintf(
buf, sizeof(buf), 0,
"%02x", devEUI.b[sizeof(devEUI.b) - i - 1]
);
dataFile.print(buf);
}
dataFile.print('"');
}
} while (0);
dataFile.print(',');
dataFile.print('"');
for (unsigned i = 0; i < b.getn(); ++i)
{
McciAdkLib_Snprintf(
buf, sizeof(buf), 0,
"%02x",
b.getbase()[i]
);
dataFile.print(buf);
}
dataFile.print("\",");
dataFile.print(this->uplinkPort);
dataFile.print(',');
if (this->fData_Vbat)
dataFile.print(mData.Vbat);
dataFile.print(',');
if (this->fData_Version)
{
dataFile.print(mData.ver.Major);
dataFile.print('.');
dataFile.print(mData.ver.Minor);
dataFile.print('.');
dataFile.print(mData.ver.Patch);
dataFile.print('.');
dataFile.print(mData.ver.Local);
}
dataFile.print(',');
if (this->fData_BootCount)
dataFile.print(mData.BootCount);
dataFile.print(',');
gpMeasurementLoopConcrete->writeVersionData(dataFile);
if (this->fData_CO2)
{
dataFile.print(this->co2int);
dataFile.print('.');
dataFile.print(this->co2frac);
}
dataFile.print(',');
for (auto const & feeder : mData.pellets)
{
if (this->fData_Pellet)
dataFile.print(unsigned(feeder.Recent));
dataFile.print(',');
if (this->fData_Pellet)
dataFile.print(feeder.Total);
dataFile.print(',');
}
for (auto i = kMaxActivityEntries; i > 0; )
{
--i;
if (this->fData_Activity && i < mData.nActivity)
dataFile.print(mData.activity[i].Avg);
if (i > 0)
dataFile.print(',');
}
dataFile.println();
dataFile.close();
}
else
{
gCatena.SafePrintf("can't open: %s\n", fName);
}
}
sdFinish();
return fResult;
}
/*
Name: cMeasurementLoop::handleSdFirmwareUpdate()
Index: Name: cMeasurementLoop::handleSdFirmwareUpdateCardUp()
Function:
Check for firmware update request via SD card and handle it
Definition:
bool cMeasurementLoop::handleSdFirmwareUpdate(
void
);
bool cMeasurementLoop::handleSdFirmwareUpdateCardUp(
void
);
Description:
Check for a suitable file on the SD card. If found, copy to
flash. If successful, set the update flag, and rename the
update file so we won't consider it again. handleSdFirmwareUpdateCardUp()
is simply the inner method, to be called as a wrapper once power is
up on the card.
Returns:
True if an update was done and the system should be rebooted.
Notes:
*/
#define FUNCTION "cMeasurementLoop::handleSdFirmwareUpdate"
bool
cMeasurementLoop::handleSdFirmwareUpdate(
void
)
{
if (this->m_pSPI2 == nullptr)
gLog.printf(gLog.kBug, "SPI2 not registered, can't program flash\n");
bool fResult = this->checkSdCard();
if (fResult)
{
fResult = this->handleSdFirmwareUpdateCardUp();
}
this->sdFinish();
return fResult;
}
#undef FUNCTION
#define FUNCTION "cMeasurementLoop::handleSdFirmwareUpdateCardUp"
bool
cMeasurementLoop::handleSdFirmwareUpdateCardUp(
void
)
{
static const char * const sUpdate[] = { "update.bin", "fallback.bin" };
for (auto s : sUpdate)
{
if (! gSD.exists(s))
{
if (gLog.isEnabled(gLog.kTrace))
gLog.printf(gLog.kAlways, "%s: not found: %s\n", FUNCTION, s);
continue;
}
if (! this->fDisableLED)
{
gpio.setRed(true);
gpio.setGreen(true);
gpio.setBlue(true);
}
auto result = this->updateFromSd(
s,
s[0] == 'u' ? cDownload::DownloadRq_t::GetUpdate
: cDownload::DownloadRq_t::GetFallback
);
if (gLog.isEnabled(gLog.kTrace))
gLog.printf(gLog.kTrace, "%s: applied update from %s: %s\n", FUNCTION, s, result ? "true": "false");
return result;
}
return false;
}
bool
cMeasurementLoop::updateFromSd(
const char *sUpdate,
cDownload::DownloadRq_t rq
)
{
// launch a programming cycle. We'll stall the measurement FSM here while
// doing the operation, but poll the other FSMs.
struct context_t
{
cMeasurementLoop *pThis;
bool fWorking;
File firmwareFile;
cDownload::Status_t status;
cDownload::Request_t request;
};
context_t context { this, true };
this->m_fFwUpdate = true;
gLog.printf(gLog.kInfo, "Attempting to load firmware from %s\n", sUpdate);
// power management: typically the SPI2 is powered down by a sleep,
// and it's not powered back up when we wake up. The SPI flash is on
// SPI2, and so we must power it up. We also have to deal with the
// corner case where the SPI flash didn't probe correctly.
if (this->m_pSPI2)
{
// SPI was found, bring it up.
this->m_pSPI2->begin();
// and bring up the flash.
gFlash.begin(this->m_pSPI2, Catena::PIN_SPI2_FLASH_SS);
}
else
{
// something went wrong at boot time, we can't do anything
// with firwmare update...
gLog.printf(gLog.kError, "SPI2 pointer is null, give up\n");
return false;
}
// try to open the file
context.firmwareFile = gSD.open(sUpdate, FILE_READ);
if (! context.firmwareFile)
{
// hmm. it exists but we could not open it.
gLog.printf(gLog.kError, "%s: exists but can't open: %s\n", FUNCTION, sUpdate);
return false;
}
// the downloader requires a "request block" that tells it what to do.
// since we loop in this function, we can allocate it as a local variable,
// and keep it in the context object. Save some typing by defining an
// alias:
auto & request = context.request;
// The downloader is abstract; it doesn't know where data is coming from.
// It calls various callbacks to get data and orchestrate the collection
// of image data. So we (the client) must provide some callbacks. This
// is done in a consistent way:
//
// request.{callback}.init({pfn}, {pUserData});
//
// This initializes the specified callback with the specified function
// and user data.
//
// In the code below, we use C++ lambdas to save useless static functions,
// and also (nicely) to write code that is "inside" cMeasurementLoop and
// can access protected and private items.
//
// The four callbacks are: QueryAvailableData, PromptForData, ReadBytes,
// and Completion.
//
// initialize the "query available data" callback. We always say
// kTransferChunkBytes are available, becuase we're reading from
// a file. But this means we must fill buffer to max size in read
// when we hit end of file.
request.QueryAvailableData.init(
[](void *pUserData) -> int
{
return cDownload::kTransferChunkBytes;
},
nullptr
);
// initalize the "prompt for more data" callback; we don't need one
// when reading from a file.
request.PromptForData.init(nullptr, nullptr);
// initialize the read-byte callback
request.ReadBytes.init(
// this is called each time the downloader wants more data
[](void *pUserData, std::uint8_t *pBuffer, size_t nBuffer) -> size_t
{
context_t * const pCtx = (context_t *)pUserData;
gLog.printf(gLog.kInfo, ".");
gIwdgTimer.refreshWatchdog();
gCatena.poll();
auto n = pCtx->firmwareFile.readBytes(pBuffer, nBuffer);
if (n < nBuffer)
{
// at end of file we have spare bytes that are not
// used. Initialize to 0xFF because that's nice for
// SPI flash.
memset(pBuffer + n, 0xFF, nBuffer - n);
gLog.printf(gLog.kInfo, "\n");
}
return nBuffer;
},
(void *)&context
);
// initialize the "operation complete" callback. Just set
// a flag to get us out of the wait loop.
request.Completion.init(
[](void *pUserData, cDownload::Status_t status) -> void
{
context_t * const pCtx = (context_t *)pUserData;
pCtx->status = status;
pCtx->fWorking = false;
},
(void *)&context
);
// set the request code in the request.
request.rq = rq;
// launch the request.
if (! gDownload.evStart(request))
{
// It didn't launch. No callbacks will happen. Clean up.
context.firmwareFile.close();
// remove the file, so we don't get stuck in a loop.
gSD.remove(sUpdate);
// no need to reboot.
return false;
}
// it launched: wait for transfer to complete
while (context.fWorking)
{
gIwdgTimer.refreshWatchdog();
// give other clients a chance to look in.
// and allow the download to be coded asynchronously
// if necessary.
gCatena.poll();
}
// download operation is complete.
// close and remove the file
gIwdgTimer.refreshWatchdog();
context.firmwareFile.close();
gIwdgTimer.refreshWatchdog();
gSD.remove(sUpdate);
gIwdgTimer.refreshWatchdog();
// if it failed, display the error code.
if (context.status != cDownload::Status_t::kSuccessful)
{
gLog.printf(gLog.kError, "download failed, status %u\n", std::uint32_t(context.status));
// no need to reboot.
return false;
}
// if it succeeeded, say so, and tell caller to reboot.
// don't reboot here, because the outer app may need to shut things down
// in an orderly way.
else
{
gLog.printf(gLog.kInfo, "download succeded.\n");
return true;
}
}
void
cMeasurementLoop::handleSdTTNv3Migrate(
void
)
{
bool fMigrate = false;
bool fResult = this->checkSdCard();
if (fResult)
{
if (! gSD.exists(gkMigrateFileName))
fMigrate = false;
else
fMigrate = true;
}
if (fMigrate)
{
bool fFramUpdate = false;
auto const pFram = gCatena.getFram();
static const uint8_t AppEUI[] = { 1, 0, 0, 0, 0, 0, 0, 0 };
if (pFram == nullptr)
fFramUpdate = false;
else
{
pFram->saveField(cFramStorage::kAppEUI, AppEUI);
fFramUpdate = true;
}
if (fFramUpdate)
{
this->rejoinNetwork();
gSD.remove(gkMigrateFileName);
gLog.printf(gLog.kInfo, "cFramStorage::kAppEUI: update: success\n");
}
else
gLog.printf(gLog.kError, "cFramStorage::kAppEUI: not updated\n");
}
this->sdFinish();
}
void
cMeasurementLoop::handleSdNetworkRejoin(
void
)
{
bool fRejoin = false;
bool fResult = this->checkSdCard();
if (fResult)
{
if (! gSD.exists(gkRejoinFileName))
fRejoin = false;
else
fRejoin = true;
}
if (fRejoin)
{
this->rejoinNetwork();
gSD.remove(gkRejoinFileName);
}
this->sdFinish();
}
void
cMeasurementLoop::rejoinNetwork(
void
)
{
auto const pFram = gCatena.getFram();
pFram->saveField(cFramStorage::kDevAddr, 0);
LMIC_unjoinAndRejoin();
}
#undef FUNCTION