This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoubleResetDetector_Generic.h
1571 lines (1189 loc) · 41.2 KB
/
DoubleResetDetector_Generic.h
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************************************************************************
DoubleResetDetector_Generic.h
Arduino AVR, Teensy, SAM-DUE, SAMD, STM32, nRF52, etc. boards
DoubleResetDetector_Generic is a library for the Arduino AVR, Teensy, SAM-DUE, SAMD, STM32, nRF52, etc. boards
to enable trigger configure mode by resetting the boards twice within configurable timeout seconds.
Based on and modified from DataCute https://github.com/datacute/DoubleResetDetector and
https://github.com/khoih-prog/ESP_DoubleResetDetector
Built by Khoi Hoang https://github.com/khoih-prog/DoubleResetDetector_Generic
Licensed under MIT license
Version: 1.8.1
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 14/04/2020 Initial coding for boards such as AVR, Teensy, SAM DUE, SAMD and STM32, etc.
1.0.1 K Hoang 01/05/2020 Add support to Adafruit nRF52 boards, such as Feather, Itsy-Bitsy nRF52840, NINA_W302_ublox.
1.0.2 K Hoang 04/05/2020 Fix not-detected DRD bug for SAMD boards.
1.0.3 K Hoang 28/12/2020 Suppress all possible compiler warnings
1.1.0 K Hoang 27/04/2021 Use new FlashStorage_STM32 library. Add support to new STM32 core v2.0.0 and STM32L5
1.2.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core
1.3.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core
1.4.0 K Hoang 05/06/2021 Permit more control over LittleFS for RP2040 Arduino mbed core
1.5.0 K Hoang 07/08/2021 Add support to RTL8720DN, etc. using AmebaD core
1.6.0 K Hoang 29/08/2021 Add support to MBED Nano_33_BLE, Nano_33_BLE_Sense, etc. using LittleFS
1.7.0 K Hoang 10/09/2021 Add support to MBED Portenta_H7 using LittleFS
1.7.1 K Hoang 13/09/2021 Select fix LittleFS size of 1024KB
1.7.2 K Hoang 14/09/2021 Back to using auto LittleFS to fix bug
1.7.3 K Hoang 10/10/2021 Update `platform.ini` and `library.json`
1.8.0 K Hoang 26/01/2022 Update to be compatible with new FlashStorage libraries. Add support to more SAMD/STM32 boards
1.8.1 K Hoang 05/03/2022 Add waitingForDRD() function to signal in DRD wating period
**********************************************************************************************************************************/
#pragma once
#ifndef DoubleResetDetector_Generic_H
#define DoubleResetDetector_Generic_H
#ifndef DRD_GENERIC_DEBUG
#define DRD_GENERIC_DEBUG false
#endif
#ifndef DOUBLERESETDETECTOR_GENERIC_VERSION
#define DOUBLERESETDETECTOR_GENERIC_VERSION "DoubleResetDetector_Generic v1.8.1"
#define DOUBLERESETDETECTOR_GENERIC_VERSION_MAJOR 1
#define DOUBLERESETDETECTOR_GENERIC_VERSION_MINOR 8
#define DOUBLERESETDETECTOR_GENERIC_VERSION_PATCH 1
#define DOUBLERESETDETECTOR_GENERIC_VERSION_INT 1008001
#endif
#if ( defined(ESP32) || defined(ESP8266) )
#error Please use ESP_DoubleResetDetector library (github.com/khoih-prog/ESP_DoubleResetDetector) for ESP8266 and ESP32!
#endif
// For AVR, Teensy, STM32 boards, use EEPROM
// For SAM DUE, use DueFlashStorage. For SAMD, use FlashStorage_SAMD
// For RTL8720, use FlashStorage_RTL8720
#define DRD_FILENAME "/drd.dat"
#define DRD_FLAG_OFFSET 0
#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#define DRD_GENERIC_USE_EEPROM true
/////////////////////////////
#define DRD_GENERIC_USE_SAM_DUE false
#define DRD_GENERIC_USE_SAMD false
#define DRD_GENERIC_USE_STM32 false
#define DRD_GENERIC_USE_NRF52 false
#define DRD_GENERIC_USE_RP2040 false
#define DRD_GENERIC_USE_MBED_RP2040 false
#define DRD_GENERIC_USE_NANO33BLE false
#define DRD_GENERIC_USE_RTL8720 false
#define DRD_GENERIC_USE_PORTENTA false
/////////////////////////////
#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
#if defined(DRD_GENERIC_USE_SAM_DUE)
#undef DRD_GENERIC_USE_SAM_DUE
#endif
#define DRD_GENERIC_USE_SAM_DUE true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use SAM-DUE and DueFlashStorage
#endif
/////////////////////////////
#elif ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
|| defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \
|| defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) \
|| defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD51__) || defined(__SAMD51J20A__) \
|| defined(__SAMD51J19A__) || defined(__SAMD51G19A__) || defined(__SAMD51P19A__) \
|| defined(__SAMD21E15A__) || defined(__SAMD21E16A__) || defined(__SAMD21E17A__) || defined(__SAMD21E18A__) \
|| defined(__SAMD21G15A__) || defined(__SAMD21G16A__) || defined(__SAMD21G17A__) || defined(__SAMD21G18A__) \
|| defined(__SAMD21J15A__) || defined(__SAMD21J16A__) || defined(__SAMD21J17A__) || defined(__SAMD21J18A__) )
#if defined(DRD_GENERIC_USE_SAMD)
#undef DRD_GENERIC_USE_SAMD
#endif
#define DRD_GENERIC_USE_SAMD true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use SAMD and FlashStorage
#endif
/////////////////////////////
#elif ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \
defined(NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \
defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) | defined(NINA_B302_ublox) ) && \
! ( defined(ARDUINO_ARCH_MBED) )
#if defined(DRD_GENERIC_USE_NRF52)
#undef DRD_GENERIC_USE_NRF52
#endif
#define DRD_GENERIC_USE_NRF52 true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use NRF52 and LittleFS / InternalFS
#endif
/////////////////////////////
#elif ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) )
#if defined(DRD_GENERIC_USE_RP2040)
#undef DRD_GENERIC_USE_RP2040
#endif
#define DRD_GENERIC_USE_RP2040 true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use RP2040 (such as RASPBERRY_PI_PICO) and LittleFS
#endif
/////////////////////////////
#elif ( defined(ARDUINO_ARCH_RP2040) && defined(ARDUINO_ARCH_MBED) )
// For Arduino' arduino-mbed core
// To check and determine if we need to init LittleFS here
#if MBED_RP2040_INITIALIZED
#define DRD_MBED_LITTLEFS_NEED_INIT false
#if (DRD_GENERIC_DEBUG)
#warning MBED_RP2040_INITIALIZED in another place
#endif
#else
// Better to delay until init done
#if defined(MBED_RP2040_INITIALIZED)
#undef MBED_RP2040_INITIALIZED
#endif
#define MBED_RP2040_INITIALIZED true
#define DRD_MBED_LITTLEFS_NEED_INIT true
#endif
#if defined(DRD_GENERIC_USE_MBED_RP2040)
#undef DRD_GENERIC_USE_MBED_RP2040
#endif
#define DRD_GENERIC_USE_MBED_RP2040 true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use MBED RP2040 (such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO) and LittleFS
#endif
/////////////////////////////
#elif ( ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) && defined(ARDUINO_ARCH_MBED) )
#if defined(BOARD_NAME)
#undef BOARD_NAME
#endif
#if defined(CORE_CM7)
#if (DRD_GENERIC_DEBUG)
#warning Using Portenta H7 M7 core
#endif
#define BOARD_NAME "PORTENTA_H7_M7"
#else
#if (DRD_GENERIC_DEBUG)
#warning Using Portenta H7 M4 core
#endif
#define BOARD_NAME "PORTENTA_H7_M4"
#endif
// For Arduino' arduino-mbed core
// To check and determine if we need to init LittleFS here
#if MBED_PORTENTA_H7_INITIALIZED
#define DRD_MBED_LITTLEFS_NEED_INIT false
#if (DRD_GENERIC_DEBUG)
#warning MBED_PORTENTA_H7_INITIALIZED in another place
#endif
#else
// Better to delay until init done
#if defined(MBED_PORTENTA_H7_INITIALIZED)
#undef MBED_PORTENTA_H7_INITIALIZED
#endif
#define MBED_PORTENTA_H7_INITIALIZED true
#define DRD_PORTENTA_LITTLEFS_NEED_INIT true
#endif
#if defined(DRD_GENERIC_USE_MBED_PORTENTA)
#undef DRD_GENERIC_USE_MBED_PORTENTA
#endif
#define DRD_GENERIC_USE_MBED_PORTENTA true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use MBED PORTENTA_H7 and LittleFS
#endif
/////////////////////////////
#elif ( defined(ARDUINO_ARCH_NRF52840) && defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARDUINO_NANO33BLE) )
// For Arduino' arduino-mbed core
// To check and determine if we need to init LittleFS here
#if NANO33BLE_INITIALIZED
#define DRD_NANO33BLE_NEED_INIT false
#if (DRD_GENERIC_DEBUG)
#warning NANO33BLE_INITIALIZED in another place
#endif
#else
// Better to delay until init done
#if defined(NANO33BLE_INITIALIZED)
#undef NANO33BLE_INITIALIZED
#endif
#define NANO33BLE_INITIALIZED true
#define DRD_NANO33BLE_NEED_INIT true
#endif
#if defined(DRD_GENERIC_USE_NANO33BLE)
#undef DRD_GENERIC_USE_NANO33BLE
#endif
#define DRD_GENERIC_USE_NANO33BLE true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use MBED nRF52840 (such as Nano_33_BLE, Nano_33_BLE_Sense) and LittleFS
#endif
/////////////////////////////
#elif defined(CONFIG_PLATFORM_8721D)
#if defined(DRD_GENERIC_USE_RTL8720)
#undef DRD_GENERIC_USE_RTL8720
#endif
#define DRD_GENERIC_USE_RTL8720 true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use RTL8720 and FlashStorage_RTL8720
#endif
#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
defined(STM32WB) || defined(STM32MP1) || defined(STM32L5) )
#if defined(DRD_GENERIC_USE_STM32)
#undef DRD_GENERIC_USE_STM32
#endif
#define DRD_GENERIC_USE_STM32 true
#if defined(DRD_GENERIC_USE_EEPROM)
#undef DRD_GENERIC_USE_EEPROM
#endif
#define DRD_GENERIC_USE_EEPROM false
#if (DRD_GENERIC_DEBUG)
#warning Use STM32 and FlashStorage_STM32
#endif
/////////////////////////////
#else
#if defined(CORE_TEENSY)
#if (DRD_GENERIC_DEBUG)
#warning Use TEENSY and EEPROM
#endif
#elif ( defined(ARDUINO_AVR_ADK) || defined(ARDUINO_AVR_BT) || defined(ARDUINO_AVR_DUEMILANOVE) || defined(ARDUINO_AVR_ESPLORA) \
|| defined(ARDUINO_AVR_ETHERNET) || defined(ARDUINO_AVR_FIO) || defined(ARDUINO_AVR_GEMMA) || defined(ARDUINO_AVR_LEONARDO) \
|| defined(ARDUINO_AVR_LILYPAD) || defined(ARDUINO_AVR_LILYPAD_USB) || defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) \
|| defined(ARDUINO_AVR_MICRO) || defined(ARDUINO_AVR_MINI) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_NG) \
|| defined(ARDUINO_AVR_PRO) || defined(ARDUINO_AVR_ROBOT_CONTROL) || defined(ARDUINO_AVR_ROBOT_MOTOR) || defined(ARDUINO_AVR_UNO) \
|| defined(ARDUINO_AVR_YUN) )
#if (DRD_GENERIC_DEBUG)
#warning Use AVR and EEPROM
#endif
#else
#if (DRD_GENERIC_DEBUG)
#warning Use Unknown board and EEPROM
#endif
#endif
#endif
/////////////////////////////
//default to use EEPROM, otherwise, use DueFlashStorage or FlashStorage_SAMD
/////////////////////////////
#if DRD_GENERIC_USE_EEPROM
#include <EEPROM.h>
#define FLAG_DATA_SIZE 4
#ifndef DRD_EEPROM_SIZE
// Please change according to your application to avoid overwriting or being overwritten
#define DRD_EEPROM_SIZE (E2END + 1)
#endif
/////////////////////////////
#elif DRD_GENERIC_USE_SAMD
// Include EEPROM-like API for FlashStorage
#include <FlashStorage_SAMD.h> //https://github.com/khoih-prog/FlashStorage_SAMD
/////////////////////////////
#elif DRD_GENERIC_USE_SAM_DUE
//Use DueFlashStorage to simulate EEPROM
#include <DueFlashStorage.h> //https://github.com/sebnil/DueFlashStorage
DueFlashStorage dueFlashStorage;
/////////////////////////////
#elif DRD_GENERIC_USE_NRF52
// Include LittleFS similar to SPIFFS
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
File DRD_file(InternalFS);
/////////////////////////////
#elif DRD_GENERIC_USE_RP2040
//Use LittleFS for RPI Pico
#include <FS.h>
#include <LittleFS.h>
FS* filesystem = &LittleFS;
#define FileFS LittleFS
/////////////////////////////
#elif (DRD_GENERIC_USE_MBED_RP2040 && DRD_MBED_LITTLEFS_NEED_INIT)
//Use LittleFS for MBED RPI Pico
#include "FlashIAPBlockDevice.h"
#include "LittleFileSystem.h"
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
#include "BlockDevice.h"
#if !defined(RP2040_FLASH_SIZE)
#define RP2040_FLASH_SIZE (2 * 1024 * 1024)
#endif
#if !defined(RP2040_FS_LOCATION_END)
#define RP2040_FS_LOCATION_END RP2040_FLASH_SIZE
#endif
#if !defined(RP2040_FS_SIZE_KB)
// Using default 64KB for LittleFS
#define RP2040_FS_SIZE_KB (64)
#endif
#if !defined(RP2040_FS_START)
#define RP2040_FS_START (RP2040_FLASH_SIZE - (RP2040_FS_SIZE_KB * 1024))
#endif
#if !defined(FORCE_REFORMAT)
#define FORCE_REFORMAT false
#elif FORCE_REFORMAT
#warning FORCE_REFORMAT enable. Are you sure ?
#endif
FlashIAPBlockDevice bd(XIP_BASE + RP2040_FS_START, (RP2040_FS_SIZE_KB * 1024));
mbed::LittleFileSystem fs("fs");
#if defined(DRD_FILENAME)
#undef DRD_FILENAME
#endif
#define DRD_FILENAME "/fs/drd.dat"
#if (DRD_GENERIC_DEBUG)
#warning DRD_MBED_LITTLEFS INITIALIZED locally in DoubleResetDetector_Generic
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_MBED_PORTENTA && DRD_PORTENTA_LITTLEFS_NEED_INIT)
//Use LittleFS for MBED RPI Pico
#include "FlashIAPBlockDevice.h"
#include "LittleFileSystem.h"
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
#include "BlockDevice.h"
#include "mbed_portenta/FlashIAPLimits.h"
#if !defined(FORCE_REFORMAT)
#define FORCE_REFORMAT false
#elif FORCE_REFORMAT
#warning FORCE_REFORMAT enable. Are you sure ?
#endif
static FlashIAPBlockDevice* blockDevicePtr;
mbed::LittleFileSystem fs("littlefs");
struct FlashIAPLimits _flashIAPLimits;
#if defined(DRD_FILENAME)
#undef DRD_FILENAME
#endif
#define DRD_FILENAME "/littlefs/drd.dat"
#if (DRD_GENERIC_DEBUG)
#warning DRD_PORTENTA_LITTLEFS INITIALIZED locally in DoubleResetDetector_Generic
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_NANO33BLE && DRD_NANO33BLE_NEED_INIT)
//Use LittleFS for MBED Nano33BLE
#include "FlashIAPBlockDevice.h"
#include "LittleFileSystem.h"
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
#include "BlockDevice.h"
#if !defined(NANO33BLE_FLASH_SIZE)
// Using max 512KB for FS
#define NANO33BLE_FLASH_SIZE (1 * 1024 * 1024)
#endif
#if !defined(NANO33BLE_FS_LOCATION_END)
#define NANO33BLE_FS_LOCATION_END NANO33BLE_FLASH_SIZE
#endif
#if !defined(NANO33BLE_FS_SIZE_KB)
// Using default 64KB for FS
#define NANO33BLE_FS_SIZE_KB (64)
#endif
#if !defined(NANO33BLE_FS_START)
#define NANO33BLE_FS_START (NANO33BLE_FLASH_SIZE - (NANO33BLE_FS_SIZE_KB * 1024))
#endif
#if !defined(FORCE_REFORMAT)
#define FORCE_REFORMAT false
#elif FORCE_REFORMAT
#warning FORCE_REFORMAT enable. Are you sure ?
#endif
// nRF52840 flash address from 0, length 1MB.
// Better to use max half of flash for LitleFS, must be 0x80000 (512KB)
// FLASH_BASE must be 0x80000, or crash !!!????
#define FLASH_BASE 0x80000
FlashIAPBlockDevice wholeBD(FLASH_BASE, 0x80000);
FlashIAPBlockDevice bd(FLASH_BASE, (NANO33BLE_FS_SIZE_KB * 1024));
mbed::LittleFileSystem fs("littlefs");
#if defined(DRD_FILENAME)
#undef DRD_FILENAME
#endif
#define DRD_FILENAME "/littlefs/drd.dat"
#if (DRD_GENERIC_DEBUG)
#warning DRD_NANO33BLE_LITTLEFS INITIALIZED locally in DoubleResetDetector_Generic
#endif
/////////////////////////////
#elif DRD_GENERIC_USE_STM32
/////////////////////////////////////////////
#if defined(DATA_EEPROM_BASE)
// For STM32 devices having integrated EEPROM.
#include <EEPROM.h>
#if (DRD_GENERIC_DEBUG)
#warning STM32 devices have integrated EEPROM. Not using buffered API.
#endif
#else
/**
Most STM32 devices don't have an integrated EEPROM. To emulate a EEPROM, the STM32 Arduino core emulated
the operation of an EEPROM with the help of the embedded flash.
Writing to a flash is very expensive operation, since a whole flash page needs to be written, even if you only
want to access the flash byte-wise.
The STM32 Arduino core provides a buffered access API to the emulated EEPROM. The library has allocated the
buffer even if you don't use the buffered API, so it's strongly suggested to use the buffered API anyhow.
*/
#if ( defined(STM32F1xx) || defined(STM32F3xx) )
#include <FlashStorage_STM32F1.h> // https://github.com/khoih-prog/FlashStorage_STM32
#if (DRD_GENERIC_DEBUG)
#warning STM32F1/F3 devices have no integrated EEPROM. Using buffered API with FlashStorage_STM32F1 library
#endif
#else
#include <FlashStorage_STM32.h> // https://github.com/khoih-prog/FlashStorage_STM32
#if (DRD_GENERIC_DEBUG)
#warning STM32 devices have no integrated EEPROM. Using buffered API with FlashStorage_STM32 library
#endif
#endif
#endif // #if defined(DATA_EEPROM_BASE)
//////////////////////////////////////////////
#elif DRD_GENERIC_USE_RTL8720
// Include FlashStorage API for FlashStorage_RTL8720
#include <FlashStorage_RTL8720.h> //https://github.com/khoih-prog/FlashStorage_RTL8720
/////////////////////////////
#endif //#if DRD_GENERIC_USE_EEPROM
/////////////////////////////////////////////
#define DOUBLERESETDETECTOR_GENERIC_FLAG_SET 0xD0D01234
#define DOUBLERESETDETECTOR_GENERIC_FLAG_CLEAR 0xD0D04321
/////////////////////////////////////////////
class DoubleResetDetector_Generic
{
public:
/////////////////////////////////////////////
DoubleResetDetector_Generic(unsigned long timeout, int address)
{
this->timeout = timeout * 1000;
this->DRD_EEPROM_START = address;
doubleResetDetected = false;
waitingForDoubleReset = false;
/////////////////////////////
#if (DRD_GENERIC_USE_EEPROM)
EEPROM.begin();
#if (DRD_GENERIC_DEBUG)
Serial.print("\nEEPROM size = ");
Serial.print(DRD_EEPROM_SIZE);
Serial.print(", start = ");
Serial.println(DRD_EEPROM_START);
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_STM32)
#if defined(DATA_EEPROM_BASE)
EEPROM.begin();
#endif
#if (DRD_GENERIC_DEBUG)
Serial.print("\n(Emulated-)EEPROM size = ");
Serial.print(EEPROM.length());
Serial.print(", start = ");
Serial.println(DRD_EEPROM_START);
#endif
/////////////////////////////
#elif DRD_GENERIC_USE_SAMD
// Do something to init FlashStorage
/////////////////////////////
#elif DRD_GENERIC_USE_SAM_DUE
// Do something to init DueFlashStorage
/////////////////////////////
#elif DRD_GENERIC_USE_NRF52
// Do something to init LittleFS / InternalFS
// Initialize Internal File System
InternalFS.begin();
/////////////////////////////
#elif DRD_GENERIC_USE_RP2040
bool beginOK = FileFS.begin();
#if (DRD_GENERIC_DEBUG)
if (!beginOK)
{
Serial.println("\nLittleFS error");
}
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_MBED_RP2040 && DRD_MBED_LITTLEFS_NEED_INIT)
Serial.print("LittleFS size (KB) = ");
Serial.println(RP2040_FS_SIZE_KB);
#if FORCE_REFORMAT
fs.reformat(&bd);
#endif
int err = fs.mount(&bd);
#if (DRD_GENERIC_DEBUG)
Serial.println(err ? "LittleFS Mount Fail" : "LittleFS Mount OK");
#endif
if (err)
{
#if (DRD_GENERIC_DEBUG)
// Reformat if we can't mount the filesystem
Serial.println("Formatting... ");
Serial.flush();
#endif
err = fs.reformat(&bd);
}
bool beginOK = (err == 0);
#if (DRD_GENERIC_DEBUG)
if (!beginOK)
{
Serial.println("\nLittleFS error");
}
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_MBED_PORTENTA && DRD_PORTENTA_LITTLEFS_NEED_INIT)
// Get limits of the the internal flash of the microcontroller
_flashIAPLimits = getFlashIAPLimits();
Serial.print("Flash Size: (KB) = ");
Serial.println(_flashIAPLimits.flash_size / 1024.0);
Serial.print("FlashIAP Start Address: = 0x");
Serial.println(_flashIAPLimits.start_address, HEX);
Serial.print("LittleFS size (KB) = ");
Serial.println(_flashIAPLimits.available_size / 1024.0);
blockDevicePtr = new FlashIAPBlockDevice(_flashIAPLimits.start_address, _flashIAPLimits.available_size);
if (!blockDevicePtr)
{
#if (DRD_GENERIC_DEBUG)
Serial.println("Error init FlashIAPBlockDevice");
#endif
return;
}
#if FORCE_REFORMAT
fs.reformat(blockDevicePtr);
#endif
int err = fs.mount(blockDevicePtr);
#if (DRD_GENERIC_DEBUG)
Serial.println(err ? "LittleFS Mount Fail" : "LittleFS Mount OK");
#endif
if (err)
{
#if (DRD_GENERIC_DEBUG)
// Reformat if we can't mount the filesystem
Serial.println("Formatting... ");
#endif
err = fs.reformat(blockDevicePtr);
}
bool beginOK = (err == 0);
#if (DRD_GENERIC_DEBUG)
if (!beginOK)
{
Serial.println("\nLittleFS error");
}
#endif
/////////////////////////////
#elif (DRD_GENERIC_USE_NANO33BLE && DRD_NANO33BLE_NEED_INIT)
Serial.print("LittleFS size (KB) = ");
Serial.println(NANO33BLE_FS_SIZE_KB);
#if FORCE_REFORMAT
mbed::LittleFileSystem::format(&bd);
#endif
int err = fs.mount(&bd);
#if (DRD_GENERIC_DEBUG)
Serial.println(err ? "LittleFS Mount Fail" : "LittleFS Mount OK");
#endif
if (err)
{
#if (DRD_GENERIC_DEBUG)
// Reformat if we can't mount the filesystem
Serial.println("Formatting... ");
#endif
err = mbed::LittleFileSystem::format(&bd);
}
bool beginOK = (err == 0);
#if (DRD_GENERIC_DEBUG)
if (!beginOK)
{
Serial.println("\nLittleFS error");
}
#endif
/////////////////////////////
#elif DRD_GENERIC_USE_RTL8720
// Do something to init FlashStorage_RTL8720
/////////////////////////////
#else
#error Un-identifiable board selected. Please check your Tools->Board setting.
#endif
};
/////////////////////////////////////////////
bool detectDoubleReset()
{
doubleResetDetected = detectRecentlyResetFlag();
if (doubleResetDetected)
{
#if (DRD_GENERIC_DEBUG)
Serial.println("doubleResetDetected");
#endif
clearRecentlyResetFlag();
}
else
{
#if (DRD_GENERIC_DEBUG)
Serial.println("No doubleResetDetected");
#endif
setRecentlyResetFlag();
waitingForDoubleReset = true;
}
return doubleResetDetected;
};
bool waitingForDRD()
{
return waitingForDoubleReset;
}
/////////////////////////////////////////////
void loop()
{
if ( waitingForDoubleReset && (millis() > timeout) )
{
#if (DRD_GENERIC_DEBUG)
Serial.println("Stop doubleResetDetecting");
#endif
stop();
}
};
/////////////////////////////////////////////
void stop()
{
clearRecentlyResetFlag();
waitingForDoubleReset = false;
};
/////////////////////////////////////////////
bool doubleResetDetected;
/////////////////////////////////////////////
private:
uint32_t DOUBLERESETDETECTOR_FLAG;
unsigned long timeout;
int DRD_EEPROM_START;
bool waitingForDoubleReset;
/////////////////////////////////////////////
#if (DRD_GENERIC_USE_SAMD)
/////////////////////////////////////////////
uint32_t readFlagSAMD()
{
uint16_t offset = DRD_EEPROM_START;
uint8_t* _pointer = (uint8_t *) &DOUBLERESETDETECTOR_FLAG;
for (unsigned int i = 0; i < sizeof(DOUBLERESETDETECTOR_FLAG); i++, _pointer++, offset++)
{
*_pointer = EEPROM.read(offset);
}
return DOUBLERESETDETECTOR_FLAG;
}
/////////////////////////////////////////////
#elif (DRD_GENERIC_USE_SAM_DUE)
/////////////////////////////////////////////
uint32_t readFlagSAM_DUE()
{
byte* dataPointer = (byte* ) dueFlashStorage.readAddress(DRD_EEPROM_START);
memcpy(&DOUBLERESETDETECTOR_FLAG, dataPointer, sizeof(DOUBLERESETDETECTOR_FLAG));
return DOUBLERESETDETECTOR_FLAG;
}
/////////////////////////////////////////////
#elif DRD_GENERIC_USE_NRF52
/////////////////////////////////////////////
uint32_t readFlagNRF52()
{
DRD_file.open(DRD_FILENAME, FILE_O_READ);
if (DRD_file)
{
DRD_file.seek(DRD_FLAG_OFFSET);
DRD_file.read((char *) &DOUBLERESETDETECTOR_FLAG, sizeof(DOUBLERESETDETECTOR_FLAG));
#if (DRD_GENERIC_DEBUG)
Serial.println("LittleFS Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
#endif
DRD_file.close();
}
else
{
#if (DRD_GENERIC_DEBUG)
Serial.println("Loading DRD file failed");
#endif
}
return DOUBLERESETDETECTOR_FLAG;
}
/////////////////////////////////////////////
#elif DRD_GENERIC_USE_RP2040
/////////////////////////////////////////////
uint32_t readFlagRP2040()
{
File file = FileFS.open(DRD_FILENAME, "r");
if (file)
{
file.seek(DRD_FLAG_OFFSET);
file.read((uint8_t *) &DOUBLERESETDETECTOR_FLAG, sizeof(DOUBLERESETDETECTOR_FLAG));
#if (DRD_GENERIC_DEBUG)
Serial.println("LittleFS Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
#endif
file.close();
}
else
{
#if (DRD_GENERIC_DEBUG)
Serial.println("Loading DRD file failed");
#endif
}
return DOUBLERESETDETECTOR_FLAG;
}
/////////////////////////////////////////////
#elif DRD_GENERIC_USE_MBED_RP2040
/////////////////////////////////////////////
uint32_t readFlagMbedRP2040()
{
FILE *file = fopen(DRD_FILENAME, "r");
if (file)
{
fseek(file, DRD_FLAG_OFFSET, SEEK_SET);
fread((uint8_t *) &DOUBLERESETDETECTOR_FLAG, 1, sizeof(DOUBLERESETDETECTOR_FLAG), file);
#if (DRD_GENERIC_DEBUG)
Serial.println("LittleFS Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
#endif
fclose(file);
}
else
{
#if (DRD_GENERIC_DEBUG)
Serial.println("Loading DRD file failed");
#endif
}
return DOUBLERESETDETECTOR_FLAG;
}
/////////////////////////////////////////////
#elif (DRD_GENERIC_USE_MBED_PORTENTA)
/////////////////////////////////////////////
uint32_t readFlagMbedPortenta()
{