This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTimezone_Generic_Impl.h
1863 lines (1451 loc) · 54.2 KB
/
Timezone_Generic_Impl.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
/*********************************************************************************************************************************
Timezone_Generic_Impl.h
For AVR, ESP8266/ESP32, SAMD21/SAMD51, nRF52, STM32, WT32_ETH01 boards
Based on and modified from Arduino Timezone Library (https://github.com/JChristensen/Timezone)
to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Copyright (C) 2018 by Jack Christensen and licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
Built by Khoi Hoang https://github.com/khoih-prog/Timezone_Generic
Licensed under MIT license
Version: 1.10.1
Version Modified By Date Comments
------- ----------- ---------- -----------
1.2.4 K Hoang 17/10/2020 Initial porting to support SAM DUE, SAMD21, SAMD51, nRF52, ESP32/ESP8266, STM32, etc. boards
using SPIFFS, LittleFS, EEPROM, FlashStorage, DueFlashStorage.
1.2.5 K Hoang 28/10/2020 Add examples to use STM32 Built-In RTC.
1.2.6 K Hoang 01/11/2020 Allow un-initialized TZ then use begin() method to set the actual TZ (Credit of 6v6gt)
1.3.0 K Hoang 09/01/2021 Add support to ESP32/ESP8266 using LittleFS/SPIFFS, and to AVR, UNO WiFi Rev2, etc.
Fix compiler warnings.
1.4.0 K Hoang 04/06/2021 Add support to RP2040-based boards using RP2040 Arduino-mbed or arduino-pico core
1.5.0 K Hoang 13/06/2021 Add support to ESP32-S2 and ESP32-C3. Fix bug
1.6.0 K Hoang 16/07/2021 Add support to WT32_ETH01
1.7.0 K Hoang 10/08/2021 Add support to Ameba Realtek RTL8720DN, RTL8722DM and RTM8722CSM
1.7.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json`
1.7.2 K Hoang 02/11/2021 Fix crashing issue for new cleared flash
1.7.3 K Hoang 01/12/2021 Auto detect ESP32 core for LittleFS. Fix bug in examples for WT32_ETH01
1.8.0 K Hoang 31/12/2021 Fix `multiple-definitions` linker error
1.9.0 K Hoang 20/01/2022 Make compatible to old code
1.9.1 K Hoang 26/01/2022 Update to be compatible with new FlashStorage libraries. Add support to more SAMD/STM32 boards
1.10.0 K Hoang 06/04/2022 Use Ethernet_Generic library as default. Add support to Portenta_H7 Ethernet and WiFi
1.10.1 K Hoang 25/09/2022 Add support to `RP2040W` using `CYW43439 WiFi` with `arduino-pico` core
**********************************************************************************************************************************/
#pragma once
#ifndef TIMEZONE_GENERIC_IMPL_H
#define TIMEZONE_GENERIC_IMPL_H
#define TZ_FILENAME "/timezone.dat"
#define TZ_DATA_OFFSET 0
#define TZ_USE_EEPROM true
/////////////////////////////
// To eliminate warnings with [-Wundef]
#define TZ_USE_ESP32 false
#define TZ_USE_ESP8266 false
#define TZ_USE_SAMD false
#define TZ_USE_SAM_DUE false
#define TZ_USE_NRF52 false
#define TZ_USE_STM32 false
#define TZ_USE_RP2040 false
#define TZ_USE_MBED_RP2040 false
/////////////////////////////
#if defined(ESP32)
#if ( ARDUINO_ESP32C3_DEV )
// https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-gpio.c
#warning ESP32-C3 boards not fully supported yet. Only SPIFFS and EEPROM OK. Tempo esp32_adc2gpio to be replaced
const int8_t esp32_adc2gpio[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
#warning Using ESP32-C3
#endif
#if ( ARDUINO_ESP32C3_DEV )
// Currently, ESP32-C3 only supporting SPIFFS and EEPROM. Will fix to support LittleFS
#ifdef USE_LITTLEFS
#undef USE_LITTLEFS
#endif
#define USE_LITTLEFS false
#endif
#if defined(TZ_USE_ESP32)
#undef TZ_USE_ESP32
#endif
#define TZ_USE_ESP32 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#ifndef USE_LITTLEFS
#define USE_LITTLEFS true
#endif
#if USE_LITTLEFS
// Use LittleFS
#include "FS.h"
// Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h
//#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2)
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
#if (_TZ_LOGLEVEL_ > 2)
#warning Using ESP32 Core 1.0.6 or 2.0.0+
#endif
// The library has been merged into esp32 core from release 1.0.6
#include <LittleFS.h>
#define FileFS LittleFS
#else
#if (_TZ_LOGLEVEL_ > 2)
#warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library
#endif
// The library has been merged into esp32 core from release 1.0.6
#include <LITTLEFS.h> // https://github.com/lorol/LITTLEFS
#define FileFS LITTLEFS
#endif
#else
// Use SPIFFS
#include "FS.h"
#include <SPIFFS.h>
#define FileFS SPIFFS
#endif
//////////////////////////////////////////////////////////////
#elif defined(ESP8266)
#if defined(TZ_USE_ESP8266)
#undef TZ_USE_ESP8266
#endif
#define TZ_USE_ESP8266 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#ifndef USE_LITTLEFS
#define USE_LITTLEFS true
#endif
#include <FS.h>
#include <LittleFS.h>
#if USE_LITTLEFS
#define FileFS LittleFS
#else
#define FileFS SPIFFS
#endif
#elif ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
#if defined(TZ_USE_SAM_DUE)
#undef TZ_USE_SAM_DUE
#endif
#define TZ_USE_SAM_DUE true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use SAM-DUE and DueFlashStorage
//////////////////////////////////////////////////////////////
#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(TZ_USE_SAMD)
#undef TZ_USE_SAMD
#endif
#define TZ_USE_SAMD true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use SAMD and FlashStorage
//////////////////////////////////////////////////////////////
#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(NINA_B112_ublox) )
#if defined(TZ_USE_NRF52)
#undef TZ_USE_NRF52
#endif
#define TZ_USE_NRF52 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use NRF52 and LittleFS / InternalFS
//////////////////////////////////////////////////////////////
#elif ( ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) && defined(ARDUINO_ARCH_MBED) )
#if MBED_PORTENTA_H7_INITIALIZED
#define MBED_PORTENTA_H7_TO_BE_INITIALIZED false
#warning MBED_PORTENTA_H7_INITIALIZED in another place
#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 MBED_PORTENTA_H7_TO_BE_INITIALIZED true
//#warning MBED_PORTENTA_H7_TO_BE_INITIALIZED here
#endif
#if defined(TZ_USE_MBED_PORTENTA)
#undef TZ_USE_MBED_PORTENTA
#endif
#define TZ_USE_MBED_PORTENTA true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use MBED PORTENTA_H7 and LittleFS
//////////////////////////////////////////////////////////////
#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) )
#if defined(TZ_USE_STM32)
#undef TZ_USE_STM32
#endif
#define TZ_USE_STM32 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use STM32 and FlashStorage
//#define TZ_USE_EEPROM true
//#warning Use STM32 and EEPROM
//////////////////////////////////////////////////////////////
#elif ( defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) )
// For Earle' arduino-pico core
#if defined(TZ_USE_RP2040)
#undef TZ_USE_RP2040
#endif
#define TZ_USE_RP2040 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use RP2040 (such as RASPBERRY_PI_PICO) and LittleFS
//////////////////////////////////////////////////////////////
#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 MBED_RP2040_TO_BE_INITIALIZED false
#warning MBED_RP2040_INITIALIZED in another place
#else
// Better to delay until init done
#if defined(MBED_RP2040_INITIALIZED)
#undef MBED_RP2040_INITIALIZED
#endif
#define MBED_RP2040_INITIALIZED true
#define MBED_RP2040_TO_BE_INITIALIZED true
//#warning MBED_RP2040_TO_BE_INITIALIZED here
#endif
#if defined(TZ_USE_MBED_RP2040)
#undef TZ_USE_MBED_RP2040
#endif
#define TZ_USE_MBED_RP2040 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use MBED RP2040 (such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO) and LittleFS
//////////////////////////////////////////////////////////////
#elif ( defined(CONFIG_PLATFORM_8721D) || defined(BOARD_RTL8722D) || defined(BOARD_RTL8722DM_MINI) || defined(BOARD_RTL8720DN_BW16) )
#if defined(TZ_USE_RTL8720)
#undef TZ_USE_RTL8720
#endif
#define TZ_USE_RTL8720 true
#if defined(TZ_USE_EEPROM)
#undef TZ_USE_EEPROM
#endif
#define TZ_USE_EEPROM false
#warning Use TZ_USE_RTL8720 and FlashStorage_TZ_USE_RTL8720
//////////////////////////////////////////////////////////////
#else
#if defined(CORE_TEENSY)
#define TZ_USE_EENSY true
#warning Use TEENSY and EEPROM
#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) || defined(__AVR__) )
#warning Use AVR and EEPROM
#define TZ_USE_AVR true
//#include <avr/eeprom.h>
#else
#warning Use Unknown board and EEPROM
#endif
#endif
//default to use EEPROM, otherwise, use DueFlashStorage or FlashStorage_SAMD
#if TZ_USE_EEPROM
#include <EEPROM.h>
#ifndef TZ_EEPROM_SIZE
// Please change according to your application to avoid overwriting or being overwritten
#define TZ_EEPROM_SIZE (E2END + 1)
#endif
/////////////////////////////
#elif TZ_USE_SAMD
// Include EEPROM-like API for FlashStorage
#include <FlashStorage_SAMD.h> //https://github.com/khoih-prog/FlashStorage_SAMD
#elif TZ_USE_STM32
// Include EEPROM-like API for FlashStorage
//#include <FlashStorage_STM32.h> //https://github.com/khoih-prog/FlashStorage_STM32
/**
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_STM32F1
#warning STM32F1/F3 devices have no integrated EEPROM. Using buffered API with FlashStorage_STM32F1 library
#else
#include <FlashStorage_STM32.h> // https://github.com/khoih-prog/FlashStorage_STM32
#warning STM32 devices have no integrated EEPROM. Using buffered API with FlashStorage_STM32 library
#endif
/////////////////////////////
#elif TZ_USE_SAM_DUE
//Use DueFlashStorage to simulate EEPROM
#include <DueFlashStorage.h> //https://github.com/sebnil/DueFlashStorage
DueFlashStorage dueFlashStorage;
/////////////////////////////
#elif TZ_USE_NRF52
// Include LittleFS similar to SPIFFS
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
File TZ_file(InternalFS);
/////////////////////////////
#elif TZ_USE_RP2040
//Use LittleFS for RPI Pico
#include <FS.h>
#include <LittleFS.h>
FS* filesystem = &LittleFS;
#define FileFS LittleFS
/////////////////////////////
#elif (TZ_USE_MBED_RP2040 && MBED_RP2040_TO_BE_INITIALIZED)
//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
#endif
FlashIAPBlockDevice bd(XIP_BASE + RP2040_FS_START, (RP2040_FS_SIZE_KB * 1024));
mbed::LittleFileSystem fs("fs");
#if defined(TZ_FILENAME)
#undef TZ_FILENAME
#endif
#define TZ_FILENAME "/fs/timezone.dat"
#warning MBED_RP2040_TO_BE_INITIALIZED locally in Timezone_Generic
/////////////////////////////
#elif (TZ_USE_MBED_PORTENTA && MBED_PORTENTA_H7_TO_BE_INITIALIZED)
//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 = nullptr;
mbed::LittleFileSystem fs("littlefs");
struct FlashIAPLimits _flashIAPLimits;
#if defined(TZ_FILENAME)
#undef TZ_FILENAME
#endif
#define TZ_FILENAME "/littlefs/timezone.dat"
#warning MBED_PORTENTA_H7_TO_BE_INITIALIZED locally in Timezone_Generic
/////////////////////////////
#elif TZ_USE_RTL8720
// Include FlashStorage
#include <FlashStorage_RTL8720.h> //https://github.com/khoih-prog/FlashStorage_RTL8720
/////////////////////////////
#endif //#if TZ_USE_EEPROM
/////////////////////////////
#ifndef TZ_DEBUG
#define TZ_DEBUG false
#endif
/*----------------------------------------------------------------------*
Create a Timezone object from the given time change rules.
----------------------------------------------------------------------*/
Timezone::Timezone(const TimeChangeRule& dstStart, const TimeChangeRule& stdStart, uint32_t address)
: m_dst(dstStart), m_std(stdStart), TZ_DATA_START(address)
{
initStorage(address);
initTimeChanges();
}
/*----------------------------------------------------------------------*
Create a Timezone object for a zone that does not observe
daylight time.
----------------------------------------------------------------------*/
Timezone::Timezone(const TimeChangeRule& stdTime, uint32_t address)
: m_dst(stdTime), m_std(stdTime), TZ_DATA_START(address)
{
initStorage(address);
initTimeChanges();
}
//////
/*----------------------------------------------------------------------*
initialise (used where void constructor is called) 6v6gt
----------------------------------------------------------------------*/
void Timezone::init(const TimeChangeRule& dstStart, const TimeChangeRule& stdStart)
{
//m_dst = dstStart;
//m_std = stdStart;
memcpy(&m_dst, &dstStart, sizeof(m_dst));
memcpy(&m_std, &stdStart, sizeof(m_std));
}
//////
// Allow a "blank" TZ object then use begin() method to set the actual TZ.
// Feature added by 6v6gt (https://forum.arduino.cc/index.php?topic=711259)
/*----------------------------------------------------------------------*
Create a Timezone object from time change rules stored in EEPROM
at the given address.
----------------------------------------------------------------------*/
Timezone::Timezone(uint32_t address)
{
initStorage(address);
initTimeChanges();
readRules();
}
void Timezone::initStorage(uint32_t address)
{
this->TZ_DATA_START = address;
#if TZ_USE_EEPROM
EEPROM.begin();
TZ_LOGDEBUG3("Read from EEPROM, size = ", TZ_EEPROM_SIZE, ", offset = ", TZ_DATA_START);
/////////////////////////////
#elif TZ_USE_SAMD
// Do something to init FlashStorage
/////////////////////////////
#elif TZ_USE_STM32
// Do something to init FlashStorage
/////////////////////////////
#elif TZ_USE_SAM_DUE
// Do something to init DueFlashStorage
/////////////////////////////
#elif TZ_USE_NRF52
// Do something to init LittleFS / InternalFS
// Initialize Internal File System
InternalFS.begin();
/////////////////////////////
#elif TZ_USE_ESP32
// Do something to init LittleFS / InternalFS
// Initialize Internal File System
if (!FileFS.begin(true))
{
TZ_LOGDEBUG("SPIFFS/LittleFS failed! Already tried formatting.");
if (!FileFS.begin())
{
// prevents debug info from the library to hide err message.
delay(100);
TZ_LOGERROR("LittleFS failed!");
}
}
/////////////////////////////
#elif TZ_USE_ESP8266
// Do something to init LittleFS / InternalFS
// Initialize Internal File System
// Do something to init LittleFS / InternalFS
// Initialize Internal File System
FileFS.format();
if (!FileFS.begin())
{
TZ_LOGDEBUG("SPIFFS/LittleFS failed! Already tried formatting.");
if (!FileFS.begin())
{
// prevents debug info from the library to hide err message.
delay(100);
TZ_LOGERROR("LittleFS failed!");
}
}
/////////////////////////////
#elif TZ_USE_RP2040
bool beginOK = FileFS.begin();
if (!beginOK)
{
TZ_LOGERROR("LittleFS error");
}
/////////////////////////////
#elif TZ_USE_MBED_RP2040
TZ_LOGDEBUG1("LittleFS size (KB) = ", RP2040_FS_SIZE_KB);
int err = fs.mount(&bd);
if (err)
{
// Reformat if we can't mount the filesystem
TZ_LOGERROR("LittleFS Mount Fail. Formatting... ");
err = fs.reformat(&bd);
}
else
{
TZ_LOGDEBUG("LittleFS Mount OK");
}
if (err)
{
TZ_LOGERROR("LittleFS error");
}
/////////////////////////////
#elif TZ_USE_MBED_PORTENTA
if (blockDevicePtr != nullptr)
return;
// Get limits of the the internal flash of the microcontroller
_flashIAPLimits = getFlashIAPLimits();
TZ_LOGDEBUG1("Flash Size: (KB) = ", _flashIAPLimits.flash_size / 1024.0);
TZ_HEXLOGDEBUG1("FlashIAP Start Address: = 0x", _flashIAPLimits.start_address);
TZ_LOGDEBUG1("LittleFS size (KB) = ", _flashIAPLimits.available_size / 1024.0);
blockDevicePtr = new FlashIAPBlockDevice(_flashIAPLimits.start_address, _flashIAPLimits.available_size);
if (!blockDevicePtr)
{
TZ_LOGERROR("Error init FlashIAPBlockDevice");
return;
}
#if FORCE_REFORMAT
fs.reformat(blockDevicePtr);
#endif
int err = fs.mount(blockDevicePtr);
TZ_LOGDEBUG(err ? "LittleFS Mount Fail" : "LittleFS Mount OK");
if (err)
{
// Reformat if we can't mount the filesystem
TZ_LOGDEBUG("Formatting... ");
err = fs.reformat(blockDevicePtr);
}
bool beginOK = (err == 0);
if (!beginOK)
{
TZ_LOGERROR("\nLittleFS error");
}
/////////////////////////////
#elif TZ_USE_RTL8720
// Do something to init FlashStorage_RTL8720
/////////////////////////////
#else
#error Un-identifiable board selected. Please check your Tools->Board setting.
#endif
/////////////////////////////
storageSystemInit = true;
}
/*----------------------------------------------------------------------*
Convert the given UTC time to local time, standard or
daylight time, as appropriate.
----------------------------------------------------------------------*/
time_t Timezone::toLocal(const time_t& utc)
{
// recalculate the time change points if needed
if (year(utc) != year(m_dstUTC))
calcTimeChanges(year(utc));
if (utcIsDST(utc))
return utc + m_dst.offset * SECS_PER_MIN;
else
return utc + m_std.offset * SECS_PER_MIN;
}
/*----------------------------------------------------------------------*
Convert the given UTC time to local time, standard or
daylight time, as appropriate, and return a pointer to the time
change rule used to do the conversion. The caller must take care
not to alter this rule.
----------------------------------------------------------------------*/
time_t Timezone::toLocal(const time_t& utc, TimeChangeRule **tcr)
{
// recalculate the time change points if needed
if (year(utc) != year(m_dstUTC))
calcTimeChanges(year(utc));
if (utcIsDST(utc))
{
*tcr = &m_dst;
return utc + m_dst.offset * SECS_PER_MIN;
}
else
{
*tcr = &m_std;
return utc + m_std.offset * SECS_PER_MIN;
}
}
/*----------------------------------------------------------------------*
Convert the given local time to UTC time.
* *
WARNING:
This function is provided for completeness, but should seldom be
needed and should be used sparingly and carefully.
* *
Ambiguous situations occur after the Standard-to-DST and the
DST-to-Standard time transitions. When changing to DST, there is
one hour of local time that does not exist, since the clock moves
forward one hour. Similarly, when changing to standard time, there
is one hour of local times that occur twice since the clock moves
back one hour.
* *
This function does not test whether it is passed an erroneous time
value during the Local -> DST transition that does not exist.
If passed such a time, an incorrect UTC time value will be returned.
* *
If passed a local time value during the DST -> Local transition
that occurs twice, it will be treated as the earlier time, i.e.
the time that occurs before the transistion.
* *
Calling this function with local times during a transition interval
should be avoided!
----------------------------------------------------------------------*/
time_t Timezone::toUTC(const time_t& local)
{
// recalculate the time change points if needed
if (year(local) != year(m_dstLoc))
calcTimeChanges(year(local));
if (locIsDST(local))
return local - m_dst.offset * SECS_PER_MIN;
else
return local - m_std.offset * SECS_PER_MIN;
}
/*----------------------------------------------------------------------*
Determine whether the given UTC time_t is within the DST interval
or the Standard time interval.
----------------------------------------------------------------------*/
bool Timezone::utcIsDST(const time_t& utc)
{
// recalculate the time change points if needed
if (year(utc) != year(m_dstUTC))
calcTimeChanges(year(utc));
if (m_stdUTC == m_dstUTC) // daylight time not observed in this tz
return false;
else if (m_stdUTC > m_dstUTC) // northern hemisphere
return (utc >= m_dstUTC && utc < m_stdUTC);
else // southern hemisphere
return !(utc >= m_stdUTC && utc < m_dstUTC);
}
/*----------------------------------------------------------------------*
Determine whether the given Local time_t is within the DST interval
or the Standard time interval.
----------------------------------------------------------------------*/
bool Timezone::locIsDST(const time_t& local)
{
// recalculate the time change points if needed
if (year(local) != year(m_dstLoc))
calcTimeChanges(year(local));
if (m_stdUTC == m_dstUTC) // daylight time not observed in this tz
return false;
else if (m_stdLoc > m_dstLoc) // northern hemisphere
return (local >= m_dstLoc && local < m_stdLoc);
else // southern hemisphere
return !(local >= m_stdLoc && local < m_dstLoc);
}
/*----------------------------------------------------------------------*
Calculate the DST and standard time change points for the given
given year as local and UTC time_t values.
----------------------------------------------------------------------*/
void Timezone::calcTimeChanges(int yr)
{
m_dstLoc = toTime_t(m_dst, yr);
m_stdLoc = toTime_t(m_std, yr);
m_dstUTC = m_dstLoc - m_std.offset * SECS_PER_MIN;
m_stdUTC = m_stdLoc - m_dst.offset * SECS_PER_MIN;
}
/*----------------------------------------------------------------------*
Initialize the DST and standard time change points.
----------------------------------------------------------------------*/
void Timezone::initTimeChanges()
{
m_dstLoc = 0;
m_stdLoc = 0;
m_dstUTC = 0;
m_stdUTC = 0;
}
/*----------------------------------------------------------------------*
Convert the given time change rule to a time_t value
for the given year.
----------------------------------------------------------------------*/
time_t Timezone::toTime_t(const TimeChangeRule& r, int yr)
{
uint8_t m = r.month; // temp copies of r.month and r.week
uint8_t w = r.week;
if (w == 0) // is this a "Last week" rule?
{
if (++m > 12) // yes, for "Last", go to the next month
{
m = 1;
++yr;
}
w = 1; // and treat as first week of next month, subtract 7 days later
}
// calculate first day of the month, or for "Last" rules, first day of the next month
tmElements_t tm;
tm.Hour = r.hour;
tm.Minute = 0;
tm.Second = 0;
tm.Day = 1;
tm.Month = m;
tm.Year = yr - 1970;
time_t t = makeTime(tm);
// add offset from the first of the month to r.dow, and offset for the given week
t += ( (r.dow - weekday(t) + 7) % 7 + (w - 1) * 7 ) * SECS_PER_DAY;
// back up a week if this is a "Last" rule
if (r.week == 0)
t -= 7 * SECS_PER_DAY;
return t;
}
/*----------------------------------------------------------------------*
Read or update the daylight and standard time rules from RAM.
----------------------------------------------------------------------*/
void Timezone::setRules(const TimeChangeRule& dstStart, const TimeChangeRule& stdStart)
{
m_dst = dstStart;
m_std = stdStart;
initTimeChanges(); // force calcTimeChanges() at next conversion call
}
void Timezone::display_DST_Rule()
{
TZ_LOGERROR("DST rule");
TZ_LOGERROR3("abbrev :", m_dst.abbrev, ", week :", m_dst.week);
TZ_LOGERROR3("dow :", m_dst.dow, ", month :", m_dst.month);
TZ_LOGERROR3("hour :", m_dst.hour, ", offset :", m_dst.offset);
}
void Timezone::display_STD_Rule()
{
TZ_LOGERROR("DST rule");
TZ_LOGERROR3("abbrev :", m_std.abbrev, ", week :", m_std.week);
TZ_LOGERROR3("dow :", m_std.dow, ", month :", m_std.month);
TZ_LOGERROR3("hour :", m_std.hour, ", offset :", m_std.offset);
}
/*----------------------------------------------------------------------*
Read the daylight and standard time rules from EEPROM/storage at
the TZ_DATA_START offset.
----------------------------------------------------------------------*/
void Timezone::readRules()
{
readTZData();
initTimeChanges(); // force calcTimeChanges() at next conversion call
}
/*----------------------------------------------------------------------*
Write the daylight and standard time rules to EEPROM/storage at
the TZ_DATA_START offset.
----------------------------------------------------------------------*/
void Timezone::writeRules(int address)
{
this->TZ_DATA_START = address;
writeTZData(address);
initTimeChanges(); // force calcTimeChanges() at next conversion call
}
/////////////////////////////////////////////
#if (TZ_USE_ESP32)
#if USE_LITTLEFS
#warning Using ESP32 LittleFS in Timezone_Generic
#else
#warning Using ESP32 SPIFFS in Timezone_Generic
#endif
// ESP32 code
/*----------------------------------------------------------------------*
Read the daylight and standard time rules from LittleFS at
the given offset.
----------------------------------------------------------------------*/
void Timezone::readTZData()
{
if (!storageSystemInit)
{
// Format SPIFFS/LittleFS if not yet
if (!FileFS.begin(true))
{
TZ_LOGERROR(F("SPIFFS/LittleFS failed! Formatting."));
if (!FileFS.begin())
{
TZ_LOGERROR(F("SPIFFS/LittleFS failed! Pls use EEPROM."));
return;
}
}
storageSystemInit = true;
}
// ESP32 code
File file = FileFS.open(TZ_FILENAME, "r");
TZ_LOGDEBUG3(F("Reading m_dst & m_std from TZ_file :"), TZ_FILENAME, F(", data offset ="), TZ_DATA_OFFSET);
if (file)
{
memset(&m_dst, 0, TZ_DATA_SIZE);
memset(&m_std, 0, TZ_DATA_SIZE);
file.seek(TZ_DATA_OFFSET);
file.readBytes((char *) &m_dst, TZ_DATA_SIZE);
// Seek to be sure
file.seek(TZ_DATA_OFFSET + TZ_DATA_SIZE);
file.readBytes((char *) &m_std, TZ_DATA_SIZE);
TZ_LOGDEBUG(F("Reading from TZ_file OK"));
file.close();
}
else
{
TZ_LOGERROR(F("Reading from TZ_file failed"));
}
}
/*----------------------------------------------------------------------*
Write the daylight and standard time rules to LittleFS at