-
Notifications
You must be signed in to change notification settings - Fork 2
/
Catena4430_Sensor.ino
599 lines (518 loc) · 16.4 KB
/
Catena4430_Sensor.ino
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
/*
Module: Catena4430_Sensor.ino
Function:
Remote sensor example for the Catena 4430.
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation July 2019
*/
#include <Arduino.h>
#include <Wire.h>
#include <Catena.h>
#include "Catena4430_Sensor.h"
#include <arduino_lmic.h>
#include <Catena_Timer.h>
#include <Catena4430.h>
#include <Catena_FlashParam.h>
#include <Catena_Date.h>
#include <Catena4430_cPCA9570.h>
#include <Catena4430_c4430Gpios.h>
#include <Catena4430_cPIRdigital.h>
#include "Catena4430_cMeasurementLoop.h"
#include "Catena4430_cmd.h"
#include <Catena4430_cClockDriver_PCF8523.h>
#include <MCCI_Catena_SCD30.h>
#include <Catena_WatchdogTimer.h>
extern McciCatena::Catena gCatena;
using namespace McciCatena4430;
using namespace McciCatena;
using namespace McciCatenaScd30;
static_assert(
CATENA_ARDUINO_PLATFORM_VERSION_COMPARE_GE(
CATENA_ARDUINO_PLATFORM_VERSION,
CATENA_ARDUINO_PLATFORM_VERSION_CALC(0, 21, 0, 5)
),
"This sketch requires Catena-Arduino-Platform v0.21.0-5 or later"
);
constexpr std::uint32_t kAppVersion = McciCatena4430::makeVersion(2,5,0,0);
constexpr std::uint32_t kDoubleResetWaitMs = 3000;
constexpr std::uint32_t kTripleResetWaitMs = 4000;
constexpr std::uint32_t kSetDoubleResetMagic = 0xCA44301;
constexpr std::uint32_t kSetTripleResetMagic = 0xCA44302;
constexpr std::uint32_t kClearDoubleResetMagic = 0xCA44300;
/****************************************************************************\
|
| Variables.
|
\****************************************************************************/
// the global I2C GPIO object
cPCA9570 i2cgpio { &Wire };
// the global clock object
cClockDriver_PCF8523 gClock { &Wire };
c4430Gpios gpio { &i2cgpio };
Catena gCatena;
cDate gDate;
cTimer ledTimer;
Catena::LoRaWAN gLoRaWAN;
StatusLed gLed (Catena::PIN_STATUS_LED);
// allocate the buffer for cMeasurementLoop.
constexpr auto alignData = alignof(cMeasurementLoop);
constexpr size_t bufSize = sizeof(cMeasurementLoop);
static alignas(alignData) uint8_t buf[bufSize];
//
// The concrete instance for class `cMeasurementLoop` varies at runtime
// according to the hardware in use.
// This pointer is to the concrete
// instance viewed as an abstract object;
// virtual methods are provided by
// the concrete type to allow portable
// code to access the concrete hardware
// without knowing what type is in use.
//
cMeasurementLoop *gpMeasurementLoopConcrete = new(buf) cMeasurementLoop();
// concrete type for flash parameters
using Flash_t = McciCatena::FlashParamsStm32L0_t;
using ParamBoard_t = Flash_t::ParamBoard_t;
using PageEndSignature1_t = Flash_t::PageEndSignature1_t;
using ParamDescId = Flash_t::ParamDescId;
/* instantiate the bootloader API */
cBootloaderApi gBootloaderApi;
/* instantiate SPI */
SPIClass gSPI2(
Catena::PIN_SPI2_MOSI,
Catena::PIN_SPI2_MISO,
Catena::PIN_SPI2_SCK
);
/* instantiate the flash */
Catena_Mx25v8035f gFlash;
/* instantiate the downloader */
cDownload gDownload;
unsigned ledCount;
bool fAnalogPin1;
bool fAnalogPin2;
bool fCheckPinA1;
bool fCheckPinA2;
bool fToggle;
bool gfRejoin;
/****************************************************************************\
|
| User commands
|
\****************************************************************************/
// the individual commmands are put in this table
static const cCommandStream::cEntry sMyExtraCommmands[] =
{
{ "date", cmdDate },
{ "dir", cmdDir },
{ "log", cmdLog },
{ "tree", cmdDir },
{ "info", cmdInfo },
{ "interval", cmdInterval },
{ "hang", cmdHang },
// other commands go here....
};
/* a top-level structure wraps the above and connects to the system table */
/* it optionally includes a "first word" so you can for sure avoid name clashes */
static cCommandStream::cDispatch
sMyExtraCommands_top(
sMyExtraCommmands, /* this is the pointer to the table */
sizeof(sMyExtraCommmands), /* this is the size of the table */
nullptr /* this is no "first word" for all the commands in this table */
);
/****************************************************************************\
|
| Setup
|
\****************************************************************************/
void setup()
{
setup_double_reset();
setup_version();
setup_platform();
setup_printSignOn();
setup_radio();
setup_flash();
setup_download();
setup_measurement();
setup_gpio();
setup_rtc();
setup_commands();
setup_watchdog();
setup_start();
}
void setup_double_reset()
{
const uint32_t resetReason = READ_REG(RCC->CSR);
if (resetReason & RCC_CSR_PINRSTF)
{
if (RTC->BKP0R == kSetTripleResetMagic)
{
gfRejoin = true;
RTC->BKP0R = kClearDoubleResetMagic;
}
else if (RTC->BKP0R == kSetDoubleResetMagic)
{
RTC->BKP0R = kSetTripleResetMagic;
pinMode(D13, OUTPUT);
digitalWrite(D13, HIGH);
delay(kTripleResetWaitMs);
digitalWrite(D13, LOW);
fToggle = true;
RTC->BKP0R = kClearDoubleResetMagic;
}
else
{
RTC->BKP0R = kSetDoubleResetMagic;
pinMode(D13, OUTPUT);
digitalWrite(D13, HIGH);
delay(kDoubleResetWaitMs);
digitalWrite(D13, LOW);
RTC->BKP0R = kClearDoubleResetMagic;
}
}
}
void setup_platform()
{
const uint32_t setDisableLedFlag = 0x40000000;
const uint32_t clearDisableLedFlag = 0x0FFFFFFF;
uint32_t savedFlag;
gCatena.begin();
savedFlag = gCatena.GetOperatingFlags();
// if running unattended, don't wait for USB connect.
if (! (gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended)))
{
while (!Serial)
/* wait for USB attach */
yield();
}
// check for pin reset and second reset
if (fToggle)
{
// check if LEDs are disabled
if (savedFlag & static_cast<uint32_t>(gpMeasurementLoopConcrete->OPERATING_FLAGS::fDisableLed))
{
// clear flag to disable LEDs
savedFlag &= clearDisableLedFlag;
gCatena.getFram()->saveField(
cFramStorage::StandardKeys::kOperatingFlags,
savedFlag
);
}
// if LEDs are not disabled
else
{
// set flag to disable LEDs
savedFlag |= setDisableLedFlag;
gCatena.getFram()->saveField(
cFramStorage::StandardKeys::kOperatingFlags,
savedFlag
);
}
// update operatingFlag in the library
gCatena.SetOperatingFlags(savedFlag);
}
}
static constexpr const char *filebasename(const char *s)
{
const char *pName = s;
for (auto p = s; *p != '\0'; ++p)
{
if (*p == '/' || *p == '\\')
pName = p + 1;
}
return pName;
}
void setup_printSignOn()
{
static const char dashes[] = "------------------------------------";
gCatena.SafePrintf("\n%s%s\n", dashes, dashes);
// need username as other libraries has similar get version APIs
gCatena.SafePrintf("This is %s v%d.%d.%d-%d.\n",
filebasename(__FILE__),
McciCatena4430::getMajor(kAppVersion),
McciCatena4430::getMinor(kAppVersion),
McciCatena4430::getPatch(kAppVersion),
McciCatena4430::getLocal(kAppVersion)
);
do
{
char sRegion[16];
gCatena.SafePrintf("Target network: %s / %s\n",
gLoRaWAN.GetNetworkName(),
gLoRaWAN.GetRegionString(sRegion, sizeof(sRegion))
);
} while (0);
gCatena.SafePrintf("System clock rate is %u.%03u MHz\n",
((unsigned)gCatena.GetSystemClockRate() / (1000*1000)),
((unsigned)gCatena.GetSystemClockRate() / 1000 % 1000)
);
gCatena.SafePrintf("Enter 'help' for a list of commands.\n");
gCatena.SafePrintf("%s%s\n" "\n", dashes, dashes);
}
bool flashParam()
{
// fetch the signature
const PageEndSignature1_t * const pRomSig =
reinterpret_cast<const PageEndSignature1_t *>(Flash_t::kPageEndSignature1Address);
// get pointer to memory block */
uint32_t const descAddr = pRomSig->getParamPointer();
// find the serial number (must be first)
gpMeasurementLoopConcrete->m_pBoard = reinterpret_cast<const ParamBoard_t *>(descAddr);
const auto guid { Flash_t::kPageEndSignature1_Guid };
if (std::memcmp((const void *) &pRomSig->Guid, (const void *) &guid, sizeof(pRomSig->Guid)) != 0)
{
if (gpMeasurementLoopConcrete->isTraceEnabled(cMeasurementLoop::DebugFlags::kError))
gLog.printf(gLog.kError, "Guid value wrong\n");
return false;
}
if (! pRomSig->isValidParamPointer(descAddr))
{
if (gpMeasurementLoopConcrete->isTraceEnabled(cMeasurementLoop::DebugFlags::kError))
gLog.printf(gLog.kError, "invalid paramter pointer: %#08x\n", descAddr);
return false;
}
// check the ID and length
if (! (
gpMeasurementLoopConcrete->m_pBoard->uLen == sizeof(*gpMeasurementLoopConcrete->m_pBoard) &&
gpMeasurementLoopConcrete->m_pBoard->uType == unsigned(ParamDescId::Board)
))
{
if (gpMeasurementLoopConcrete->isTraceEnabled(cMeasurementLoop::DebugFlags::kError))
gLog.printf(
gLog.kError,
"invalid board length=%02x or type=%02x\n",
gpMeasurementLoopConcrete->m_pBoard->uLen,
gpMeasurementLoopConcrete->m_pBoard->uType
);
return false;
}
gpMeasurementLoopConcrete->setBoardRev(gpMeasurementLoopConcrete->m_pBoard->getRev());
gpMeasurementLoopConcrete->setBoard(gpMeasurementLoopConcrete->m_pBoard->getModel());
return true;
}
void printBoardInfo()
{
// print the s/n, model, rev
gLog.printf(gLog.kInfo, "serial-number:");
uint8_t serial[gpMeasurementLoopConcrete->m_pBoard->nSerial];
gpMeasurementLoopConcrete->m_pBoard->getSerialNumber(serial);
for (unsigned i = 0; i < sizeof(serial); ++i)
gCatena.SafePrintf("%c%02x", i == 0 ? ' ' : '-', serial[i]);
gLog.printf(
gLog.kInfo,
"\nAssembly-number: %u\nModel: %u\n",
gpMeasurementLoopConcrete->m_pBoard->getAssembly(),
gpMeasurementLoopConcrete->m_pBoard->getModel()
);
delay(1);
gLog.printf(
gLog.kInfo,
"ModNumber: %u\n",
gpMeasurementLoopConcrete->m_pBoard->getModNumber()
);
gLog.printf(
gLog.kInfo,
"RevNumber: %u\n",
gpMeasurementLoopConcrete->readBoardRev()
);
gLog.printf(
gLog.kInfo,
"Rev: %c\n",
gpMeasurementLoopConcrete->m_pBoard->getRevChar()
);
gLog.printf(
gLog.kInfo,
"Dash: %u\n",
gpMeasurementLoopConcrete->m_pBoard->getDash()
);
}
bool isVersion2()
{
if (gpMeasurementLoopConcrete->readBoardRev() < 3)
return false;
else
return true;
}
void setup_gpio()
{
if (! gpio.begin())
gCatena.SafePrintf("GPIO failed to initialize\n");
ledTimer.begin(400);
// set up the LED
gLed.begin();
gCatena.registerObject(&gLed);
gLed.Set(LedPattern::FastFlash);
if ((gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gpMeasurementLoopConcrete->OPERATING_FLAGS::fDisableLed)))
{
gpMeasurementLoopConcrete->fDisableLED = true;
gLed.Set(McciCatena::LedPattern::Off);
}
}
void setup_rtc()
{
if (! gClock.begin())
gCatena.SafePrintf("RTC failed to intialize\n");
cDate d;
if (! gClock.get(d))
{
if (!gpMeasurementLoopConcrete->fDisableLED)
{
uint8_t nBlink = 0;
while (nBlink < 5)
{
gIwdgTimer.refreshWatchdog();
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 is not running\n");
}
else
{
gCatena.SafePrintf("RTC is running. Date: %d-%02d-%02d %02d:%02d:%02dZ\n",
d.year(), d.month(), d.day(),
d.hour(), d.minute(), d.second()
);
}
}
void setup_flash(void)
{
gSPI2.begin();
if (gFlash.begin(&gSPI2, Catena::PIN_SPI2_FLASH_SS))
{
gpMeasurementLoopConcrete->registerSecondSpi(&gSPI2);
gFlash.powerDown();
gCatena.SafePrintf("FLASH found, put power down\n");
}
else
{
gFlash.end();
gSPI2.end();
gCatena.SafePrintf("No FLASH found: check hardware\n");
}
}
void setup_version(void)
{
if (!flashParam())
{
gCatena.SafePrintf(
"**Unable to fetch flash parameters, assuming 4610 version 1 (rev C or earlier)!\n"
);
gpMeasurementLoopConcrete->setBoardRev(0);
gpMeasurementLoopConcrete->setBoard(0);
}
else {
printBoardInfo();
}
auto board = gpMeasurementLoopConcrete->readBoard();
auto boardRev = gpMeasurementLoopConcrete->readBoardRev();
gpMeasurementLoopConcrete = gpMeasurementLoopConcrete->constructInstanceForHardware(isVersion2());
gpMeasurementLoopConcrete->setBoardRev(boardRev);
gpMeasurementLoopConcrete->setBoard(board);
}
void setup_download()
{
gDownload.begin(gFlash, gBootloaderApi);
}
void setup_radio()
{
gLoRaWAN.begin(&gCatena);
gCatena.registerObject(&gLoRaWAN);
LMIC_setClockError(10 * MAX_CLOCK_ERROR / 100);
if (gfRejoin)
LMIC_unjoinAndRejoin();
}
void setup_measurement()
{
gpMeasurementLoopConcrete->begin();
}
void setup_commands()
{
/* add our application-specific commands */
gCatena.addCommands(
/* name of app dispatch table, passed by reference */
sMyExtraCommands_top,
/*
|| optionally a context pointer using static_cast<void *>().
|| normally only libraries (needing to be reentrant) need
|| to use the context pointer.
*/
nullptr
);
}
void setup_watchdog()
{
gIwdgTimer.setupWatchdog();
}
void setup_start()
{
gpMeasurementLoopConcrete->requestActive(true);
}
/****************************************************************************\
|
| Loop
|
\****************************************************************************/
void loop()
{
gIwdgTimer.refreshWatchdog();
gCatena.poll();
if (gpMeasurementLoopConcrete->fDisableLED)
{
gpio.setGreen(false);
gpio.setBlue(false);
// set flags of Pin A1 and A2 to false.
// this used to check A1/A2 when disabling the flag fDisableLed
fAnalogPin1 = false;
fAnalogPin2 = false;
fCheckPinA1 = false;
fCheckPinA2 = false;
}
else
{
// copy current PIR state to the blue LED.
gpio.setRed(digitalRead(A0));
if (!fCheckPinA1)
{
// check the connection pin A1
if (digitalRead(A1) == 0)
{
fAnalogPin1 = true;
fCheckPinA1 = true;
}
}
if (fAnalogPin1)
{
// copy current state of Pin A1 to the Green LED.
gpio.setGreen(digitalRead(A1));
}
else
gpio.setGreen(false);
if (!fCheckPinA2)
{
// check the connection pin A2
if (digitalRead(A2) == 0)
{
fAnalogPin2 = true;
fCheckPinA2 = true;
}
}
if (fAnalogPin2)
{
// copy current state of Pin A2 to the Green LED.
gpio.setBlue(digitalRead(A2));
}
else
gpio.setBlue(false);
}
}