-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclk.c
More file actions
3502 lines (2988 loc) · 138 KB
/
clk.c
File metadata and controls
3502 lines (2988 loc) · 138 KB
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
/*
*********************************************************************************************************
* uC/Clk
* Clock / Calendar
*
* Copyright 2005-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* CLOCK / CALENDAR
*
* Filename : clk.c
* Version : V3.10.00
*********************************************************************************************************
* Note(s) : (1) Assumes the following versions (or more recent) of software modules are included
* in the project build :
*
* (a) uC/CPU V1.25
* (b) uC/LIB V1.29
*
*
* (2) (a) Clock module is based on Coordinated Universal Time (UTC) and supports the
* following features :
*
* (1) Time zones
* (2) Leap years
* (3) Leap seconds
*
* (b) Clock module does NOT support Daylight (Savings) Time. If you want to handle
* Daylight Time in your application, set time zone offset accordingly.
*
* (c) Timestamp and Coordinated Universal Time (UTC) related links :
*
* (1) http://www.timeanddate.com/time/aboututc.html
* (2) http://www.allanstime.com/Publications/DWA/Science_Timekeeping/TheScienceOfTimekeeping.pdf
* (3) http://www.cl.cam.ac.uk/~mgk25/time/metrologia-leapsecond.pdf
* (4) http://www.navcen.uscg.gov/pdf/cgsicMeetings/45/29a%20UTCLeapSecond.ppt
*
*
* (3) (a) Clock module implements a software-maintained clock/calendar when 'CLK_CFG_EXT_EN'
* is disabled (see Note #4).
*
* (b) (1) Software-maintained clock/calendar is based on a periodic delay or timeout
* when 'CLK_CFG_SIGNAL_EN' is disabled.
*
* (2) Software-maintained clock/calendar is based on a periodic signal or timer
* when 'CLK_CFG_SIGNAL_EN' is enabled.
*
* (c) When software-maintained clock is enabled, Clock module's OS-dependent files and
* respective OS-application configuration MUST be included in the build.
*
*
* (4) (a) Clock module initializes, gets and sets its timestamp via an External timestamp
* when 'CLK_CFG_EXT_EN' is enabled.
*
* (b) (1) External timestamp can be maintained either in :
*
* (A) Hardware (e.g. via a hardware clock chip)
* (B) From another application (e.g. SNTPc)
*
* (2) External timestamp is accessed by application/BSP functions defined by the
* developer that MUST follow the functional requirements of the particular
* hardware/application(s).
*
* See also 'net.h Clk_ExtTS_Init()',
* 'net.h Clk_ExtTS_Get()',
* & 'net.h Clk_ExtTS_Set()'.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#define CLK_MODULE
#include "clk.h"
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
static const CLK_DAY Clk_DaysInYr[2u] = {
DEF_TIME_NBR_DAY_PER_YR, DEF_TIME_NBR_DAY_PER_YR_LEAP
};
static const CLK_DAY Clk_DaysInMonth[2u][CLK_MONTH_PER_YR] = {
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
{ 31u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u },
{ 31u, 29u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u }
};
#if (CLK_CFG_STR_CONV_EN == DEF_ENABLED)
static const CPU_CHAR * const Clk_StrMonth[CLK_MONTH_PER_YR] = {
/* 1 */
/* 01234567890 */
(const CPU_CHAR *)"January",
(const CPU_CHAR *)"February",
(const CPU_CHAR *)"March",
(const CPU_CHAR *)"April",
(const CPU_CHAR *)"May",
(const CPU_CHAR *)"June",
(const CPU_CHAR *)"July",
(const CPU_CHAR *)"August",
(const CPU_CHAR *)"September",
(const CPU_CHAR *)"October",
(const CPU_CHAR *)"November",
(const CPU_CHAR *)"December"
};
static const CPU_CHAR * const Clk_StrDayOfWk[DEF_TIME_NBR_DAY_PER_WK] = {
/* 1 */
/* 01234567890 */
(const CPU_CHAR *)"Sunday",
(const CPU_CHAR *)"Monday",
(const CPU_CHAR *)"Tuesday",
(const CPU_CHAR *)"Wednesday",
(const CPU_CHAR *)"Thursday",
(const CPU_CHAR *)"Friday",
(const CPU_CHAR *)"Saturday"
};
#endif
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
#if (CLK_CFG_EXT_EN != DEF_ENABLED)
static CLK_TS_SEC Clk_TS_UTC_sec;
#if (CLK_CFG_SIGNAL_EN == DEF_ENABLED)
static CLK_TICK_CTR Clk_TickCtr;
#endif
#endif
static CLK_TZ_SEC Clk_TZ_sec;
static CLK_YR Clk_CacheYr;
static CLK_MONTH Clk_CacheMonth;
static CLK_NBR_DAYS Clk_CacheYrDays;
static CLK_DAY Clk_CacheMonthDays;
static CPU_INT08U Clk_CacheLeapYr;
/*
*********************************************************************************************************
* LOCAL FUNCTIONS PROTOTYPES
*********************************************************************************************************
*/
static CPU_BOOLEAN Clk_IsLeapYr (CLK_YR yr);
static CPU_BOOLEAN Clk_IsDateValid (CLK_YR yr,
CLK_MONTH month,
CLK_DAY day,
CLK_YR yr_start,
CLK_YR yr_end);
static CPU_BOOLEAN Clk_IsDayOfYrValid (CLK_YR yr,
CLK_DAY day_of_yr);
static CPU_BOOLEAN Clk_IsDayOfWkValid (CLK_DAY day_of_wk);
static CPU_BOOLEAN Clk_IsTimeValid (CLK_HR hr,
CLK_MONTH min,
CLK_DAY sec);
static CPU_BOOLEAN Clk_IsTZValid (CLK_TZ_SEC tz_sec);
static CPU_BOOLEAN Clk_IsDateTimeValidHandler(CLK_DATE_TIME *p_date_time,
CLK_YR yr_start,
CLK_YR yr_end);
static CLK_DAY Clk_GetDayOfYrHandler (CLK_YR yr,
CLK_MONTH month,
CLK_DAY day);
static CLK_DAY Clk_GetDayOfWkHandler (CLK_YR yr,
CLK_MONTH month,
CLK_DAY day);
static void Clk_SetTZ_Handler (CLK_TZ_SEC tz_sec);
static CPU_BOOLEAN Clk_TS_ToDateTimeHandler (CLK_TS_SEC ts_sec,
CLK_TZ_SEC tz_sec,
CLK_DATE_TIME *p_date_time,
CLK_YR yr_start,
CLK_YR yr_end);
static CPU_BOOLEAN Clk_DateTimeToTS_Handler (CLK_TS_SEC *p_ts_sec,
CLK_DATE_TIME *p_date_time,
CLK_YR yr_start);
static CPU_BOOLEAN Clk_DateTimeMakeHandler (CLK_DATE_TIME *p_date_time,
CLK_YR yr,
CLK_MONTH month,
CLK_DAY day,
CLK_HR hr,
CLK_MIN min,
CLK_SEC sec,
CLK_TZ_SEC tz_sec,
CLK_YR yr_start,
CLK_YR yr_end);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* Clk_Init()
*
* Description : (1) Initialize Clock module :
*
* (a) Initialize clock :
* (1) External timestamp, if available
* (2) Clock/operating system initialization
*
* (b) Initialize Clock module variables
*
*
* Argument(s) : p_err Pointer to variable that will receive the return error code from this function :
*
* CLK_ERR_NONE Clock successfully initialized.
*
* -------- RETURNED BY Clk_OS_Init() : ---------
* CLK_OS_ERR_INIT_SIGNAL Clock OS signal NOT successfully initialized.
* CLK_OS_ERR_INIT_TASK Clock OS task NOT successfully initialized.
* CLK_OS_ERR_INIT_NAME Clock OS name(s) NOT successfully initialized.
*
* Return(s) : none.
*
* Note(s) : none.
*********************************************************************************************************
*/
void Clk_Init (CLK_ERR *p_err)
{
#if (CLK_CFG_ARG_CHK_EN == DEF_ENABLED) /* ----------------- VALIDATE ERR PTR ----------------- */
if (p_err == (CLK_ERR *)0) {
return;
}
#endif
#if (CLK_CFG_EXT_EN == DEF_ENABLED) /* ------------------- INIT EXT TS -------------------- */
Clk_ExtTS_Init();
#else /* ------------------- CLK/OS INIT -------------------- */
Clk_OS_Init(p_err);
if (*p_err != CLK_OS_ERR_NONE) {
return;
}
/* ---------------- INIT CLK VARIABLES ---------------- */
Clk_TS_UTC_sec = CLK_TS_SEC_NONE; /* Clk epoch = 2000-01-01 00:00:00 UTC */
#if (CLK_CFG_SIGNAL_EN == DEF_ENABLED)
Clk_TickCtr = CLK_TICK_NONE;
#endif
#endif
Clk_TZ_sec = CLK_CFG_TZ_DFLT_SEC; /* Clk TZ = UTC offset */
Clk_CacheMonth = CLK_MONTH_NONE;
Clk_CacheMonthDays = CLK_DAY_NONE;
Clk_CacheYr = CLK_YR_NONE;
Clk_CacheYrDays = CLK_DAY_NONE;
Clk_CacheLeapYr = 0u;
*p_err = CLK_ERR_NONE;
}
#ifdef PKG_USING_UC_CLK_AUTOINIT
static int rt_ucclk_auto_init(void)
{
CLK_ERR clk_err;
Clk_Init(&clk_err);
return 0;
}
INIT_DEVICE_EXPORT(rt_ucclk_auto_init);
#endif
/*
*********************************************************************************************************
* Clk_TaskHandler()
*
* Description : (1) Handle Clock Signal :
*
* (a) Wait for a signal
* (b) Update timestamp
*
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Note(s) : (1) 'Clk_TS_UTC_sec' MUST ALWAYS be accessed exclusively in critical sections.
*********************************************************************************************************
*/
#if (CLK_CFG_EXT_EN != DEF_ENABLED)
void Clk_TaskHandler (void)
{
CLK_ERR err;
CPU_SR_ALLOC();
while (DEF_ON) {
/* --------------- WAIT FOR CLK SIGNAL ---------------- */
do {
Clk_OS_Wait(&err);
} while (err != CLK_OS_ERR_NONE);
/* -------------------- UPDATE TS --------------------- */
CPU_CRITICAL_ENTER();
if (Clk_TS_UTC_sec < CLK_TS_SEC_MAX) {
Clk_TS_UTC_sec++;
}
CPU_CRITICAL_EXIT();
}
}
#endif
/*
*********************************************************************************************************
* Clk_SignalClk()
*
* Description : (1) Increment the clock tick counter :
*
* (a) Increment the clock tick counter
* (b) Signal the clock task when one second has elapsed
*
*
* Argument(s) : p_err Pointer to variable that will receive the return error code from this function :
*
* CLK_ERR_NONE Clock successfully signaled.
*
* - RETURNED BY Clk_OS_Signal() : -
* CLK_OS_ERR_SIGNAL Clock NOT successfully signaled.
*
* Return(s) : none.
*
* Note(s) : (1) CLK_CFG_SIGNAL_FREQ_HZ must be set to the number of times Clk_SignalClk() will be
* called every second.
*
* (2) 'Clk_TickCtr' MUST ALWAYS be accessed exclusively in critical sections.
*********************************************************************************************************
*/
#if ((CLK_CFG_EXT_EN != DEF_ENABLED) && \
(CLK_CFG_SIGNAL_EN == DEF_ENABLED))
void Clk_SignalClk (CLK_ERR *p_err)
{
CPU_BOOLEAN signal;
CPU_SR_ALLOC();
signal = DEF_NO;
CPU_CRITICAL_ENTER(); /* See Note #2. */
Clk_TickCtr++;
if (Clk_TickCtr >= CLK_CFG_SIGNAL_FREQ_HZ) {
Clk_TickCtr -= CLK_CFG_SIGNAL_FREQ_HZ; /* See Note #1. */
signal = DEF_YES;
}
CPU_CRITICAL_EXIT();
if (signal == DEF_YES) {
Clk_OS_Signal(p_err);
if (*p_err != CLK_OS_ERR_NONE) {
return;
}
}
*p_err = CLK_ERR_NONE;
}
#endif
/*
*********************************************************************************************************
* Clk_GetTS()
*
* Description : Get current Clock timestamp.
*
* Argument(s) : none.
*
* Return(s) : Current timestamp (in seconds, UTC+00).
*
* Note(s) : (1) Internal Clock timestamp ('Clk_TS_UTC_sec') SHOULD be set for UTC+00. Thus any
* local time zone offset MUST be applied after calling Clk_GetTS().
*
* (2) 'Clk_TS_UTC_sec' MUST ALWAYS be accessed exclusively in critical sections.
*********************************************************************************************************
*/
CLK_TS_SEC Clk_GetTS (void)
{
CLK_TS_SEC ts_sec;
#if (CLK_CFG_EXT_EN != DEF_ENABLED)
CPU_SR_ALLOC();
/* ---------------------- GET TS ---------------------- */
CPU_CRITICAL_ENTER();
ts_sec = Clk_TS_UTC_sec;
CPU_CRITICAL_EXIT();
#else
ts_sec = Clk_ExtTS_Get();
#endif
return (ts_sec);
}
/*
*********************************************************************************************************
* Clk_SetTS()
*
* Description : Set Clock timestamp.
*
* Argument(s) : ts_sec Current timestamp to set (in seconds, UTC+00).
*
* Return(s) : DEF_OK, if timestamp is successfully set.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) Internal Clock timestamp ('Clk_TS_UTC_sec') SHOULD be set for UTC+00 and should
* NOT include any local time zone offset.
*
* (2) 'Clk_TS_UTC_sec' MUST ALWAYS be accessed exclusively in critical sections.
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_SetTS (CLK_TS_SEC ts_sec)
{
CPU_BOOLEAN valid;
#if (CLK_CFG_EXT_EN != DEF_ENABLED)
CPU_SR_ALLOC();
/* ---------------------- SET TS ---------------------- */
CPU_CRITICAL_ENTER();
Clk_TS_UTC_sec = ts_sec;
CPU_CRITICAL_EXIT();
valid = DEF_OK;
#else
valid = Clk_ExtTS_Set(ts_sec);
#endif
return (valid);
}
/*
*********************************************************************************************************
* Clk_GetTZ()
*
* Description : Get Clock time zone offset.
*
* Argument(s) : none.
*
* Return(s) : Time zone offset (in seconds, +|- from UTC).
*
* Note(s) : (1) Time zone is based on Coordinated Universal Time (UTC) & has valid values :
*
* (a) Between +|- 12 hours (+|- 43200 seconds)
* (b) Multiples of 15 minutes
*
* (2) 'Clk_TZ_sec' MUST ALWAYS be accessed exclusively in critical sections.
*********************************************************************************************************
*/
CLK_TZ_SEC Clk_GetTZ (void)
{
CLK_TZ_SEC tz_sec;
CPU_SR_ALLOC();
/* ---------------------- GET TZ ---------------------- */
CPU_CRITICAL_ENTER();
tz_sec = Clk_TZ_sec;
CPU_CRITICAL_EXIT();
return (tz_sec);
}
/*
*********************************************************************************************************
* Clk_SetTZ()
*
* Description : Set Clock time zone offset.
*
* Argument(s) : tz_sec Time zone offset (in seconds, +|- from UTC).
*
* Return(s) : DEF_OK, if time zone is valid and set.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) Time zone is based on Coordinated Universal Time (UTC) & has valid values :
*
* (a) Between +|- 12 hours (+|- 43200 seconds)
* (b) Multiples of 15 minutes
*
* (2) Time zone is not applied on the current Clock timestamp.
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_SetTZ (CLK_TZ_SEC tz_sec)
{
CPU_BOOLEAN valid;
/* ------------------- VALIDATE TZ -------------------- */
valid = Clk_IsTZValid(tz_sec);
if (valid != DEF_YES) {
return (DEF_FAIL);
}
/* ---------------------- SET TZ ---------------------- */
Clk_SetTZ_Handler(tz_sec);
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_GetDateTime()
*
* Description : Get current Clock timestamp as a date/time structure.
*
* Argument(s) : p_date_time Pointer to variable that will receive the date/time structure.
*
* Return(s) : DEF_OK, if timestamp is valid & date/time structure successfully returned.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) Internal Clock time zone offset ('Clk_TZ_sec') is used to calculate the
* local date/time ('p_date_time') returned. Thus local date/time is returned
* at UTC+TZ, where Clock time zone offset (TZ) is returned as local time zone
* offset ('p_date_time->TZ_sec').
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_GetDateTime (CLK_DATE_TIME *p_date_time)
{
CLK_TS_SEC ts_sec;
CLK_TZ_SEC tz_sec;
CPU_BOOLEAN valid;
#if 0 /* Validated in Clk_TS_ToDateTime(). */
if (p_date_time == (CLK_DATE_TIME *)0) { /* -------------- VALIDATE DATE/TIME PTR -------------- */
return (DEF_FAIL);
}
#endif
ts_sec = Clk_GetTS(); /* ---------------------- GET TS ---------------------- */
tz_sec = Clk_GetTZ(); /* ---------------------- GET TZ ---------------------- */
/* ------------- CONV CLK TS TO DATE/TIME ------------- */
valid = Clk_TS_ToDateTime(ts_sec,
tz_sec,
p_date_time);
if (valid != DEF_OK) {
return (DEF_FAIL);
}
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_SetDateTime()
*
* Description : Set Clock timestamp from a date/time structure.
*
* Argument(s) : p_date_time Pointer to variable that contains the date/time structure.
*
* Return(s) : DEF_OK, if Clock date/time successfully set.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) (a) Date/time ('p_date_time') SHOULD be set to local time with correct time zone
* offset ('p_date_time->TZ_sec'). Clk_SetDateTime() removes the time zone offset
* from the date/time to calculate the Clock timestamp at UTC+00.
*
* (b) Internal Clock time zone offset ('Clk_TZ_sec') is set to the local time zone
* offset ('p_date_time->TZ_sec').
*
* (c) Time zone is based on Coordinated Universal Time (UTC) & has valid values :
*
* (1) Between +|- 12 hours (+|- 43200 seconds)
* (2) Multiples of 15 minutes
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_SetDateTime (CLK_DATE_TIME *p_date_time)
{
CLK_TS_SEC ts_sec;
CPU_BOOLEAN valid;
#if 0 /* Validated in Clk_DateTimeToTS(). */
if (p_date_time == (CLK_DATE_TIME *)0) { /* -------------- VALIDATE DATE/TIME PTR -------------- */
return (DEF_FAIL);
}
#endif
/* ------------- CONV DATE/TIME TO CLK TS ------------- */
valid = Clk_DateTimeToTS(&ts_sec, p_date_time); /* Validates date/time & TZ (see Note #1c). */
if (valid != DEF_OK) {
CLK_TRACE_DBG(("Date/time is not valid"));
return (DEF_FAIL);
}
/* ---------------------- SET TS ---------------------- */
valid = Clk_SetTS(ts_sec); /* See Note #1a. */
if (valid != DEF_OK) {
return (DEF_FAIL);
}
/* ---------------------- SET TZ ---------------------- */
Clk_SetTZ_Handler(p_date_time->TZ_sec); /* See Note #1b. */
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_TS_ToDateTime()
*
* Description : Convert Clock timestamp to date/time structure.
*
* Argument(s) : ts_sec Timestamp to convert (in seconds, UTC+00).
*
* tz_sec Time zone offset (in seconds, +|- from UTC).
*
* p_date_time Pointer to variable that will receive the date/time structure.
*
* Return(s) : DEF_OK, if date/time structure successfully returned.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) (a) Timestamp ('ts_sec') MUST be set for UTC+00 & SHOULD NOT include the time
* zone offset ('tz_sec') since Clk_TS_ToDateTime() includes the time zone
* offset in its date/time calculation. Thus the time zone offset SHOULD NOT
* be applied before or after calling Clk_TS_ToDateTime().
*
* (b) Time zone field of the date/time structure ('p_date_time->TZ_sec') is set
* to the value of the time zone argument ('tz_sec').
*
* (c) Time zone is based on Coordinated Universal Time (UTC) & has valid values :
*
* (1) Between +|- 12 hours (+|- 43200 seconds)
* (2) Multiples of 15 minutes
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_TS_ToDateTime (CLK_TS_SEC ts_sec,
CLK_TZ_SEC tz_sec,
CLK_DATE_TIME *p_date_time)
{
CPU_BOOLEAN valid;
/* ------------- CONV CLK TS TO DATE/TIME ------------- */
CLK_TRACE_DBG(("\n\rConvert TS to Date/time:\n\r"
" TS to convert= %u\n\n\r",
(unsigned int)ts_sec));
valid = Clk_TS_ToDateTimeHandler(ts_sec,
tz_sec,
p_date_time,
CLK_EPOCH_YR_START,
CLK_EPOCH_YR_END);
if (valid != DEF_OK) {
CLK_TRACE_DBG((" Date/time conversion has failed\n\r"));
return (DEF_FAIL);
}
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_DateTimeToTS()
*
* Description : Convert date/time structure to Clock timestamp.
*
* Argument(s) : p_ts_sec Pointer to variable that will receive the Clock timestamp :
*
* In seconds UTC+00, if NO error(s);
* CLK_TS_SEC_NONE, otherwise.
*
* p_date_time Pointer to variable that contains date/time structure to convert.
*
* Return(s) : DEF_OK, if timestamp successfully returned.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) Date/time structure ('p_date_time') MUST be representable in Clock timestamp.
* Thus date to convert MUST be :
*
* (a) >= CLK_EPOCH_YR_START
* (b) < CLK_EPOCH_YR_END
*
* (2) (a) Date/time ('p_date_time') SHOULD be set to local time with correct time zone
* offset ('p_date_time->TZ_sec'). Clk_DateTimeToTS() removes the time zone
* offset from the date/time to calculate & return a Clock timestamp at UTC+00.
*
* (b) Time zone is based on Coordinated Universal Time (UTC) & has valid values :
*
* (1) Between +|- 12 hours (+|- 43200 seconds)
* (2) Multiples of 15 minutes
*
* (3) Pointers to variables that return values MUST be initialized PRIOR to all other
* validation or function handling in case of any error(s).
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_DateTimeToTS (CLK_TS_SEC *p_ts_sec,
CLK_DATE_TIME *p_date_time)
{
CPU_BOOLEAN valid;
#if (CLK_CFG_ARG_CHK_EN == DEF_ENABLED) /* ----------------- VALIDATE TS PTR ------------------ */
if (p_ts_sec == (CLK_TS_SEC *)0) {
return (DEF_FAIL);
}
#endif
*p_ts_sec = CLK_TS_SEC_NONE; /* Init to ts none for err (see Note #3). */
#if 0 /* Validated in Clk_IsDateTimeValid(). */
if (p_date_time == (CLK_DATE_TIME *)0) { /* -------------- VALIDATE DATE/TIME PTR -------------- */
return (DEF_FAIL);
}
#endif
/* ---------------- VALIDATE DATE/TIME ---------------- */
valid = Clk_IsDateTimeValid(p_date_time);
if (valid != DEF_OK) {
return (DEF_FAIL);
}
/* ------------- CONV DATE/TIME TO CLK TS ------------- */
CLK_TRACE_DBG(("Convert Date/time to TS:\n\r"));
valid = Clk_DateTimeToTS_Handler(p_ts_sec,
p_date_time,
CLK_EPOCH_YR_START);
if (valid != DEF_OK) {
return (DEF_FAIL);
}
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_DateTimeMake()
*
* Description : Build a valid Clock epoch date/time structure.
*
* Argument(s) : p_date_time Pointer to variable that will receive the date/time structure.
*
* yr Year value [CLK_EPOCH_YR_START to CLK_EPOCH_YR_END) [see Note #1].
*
* month Month value [ CLK_MONTH_JAN to CLK_MONTH_DEC].
*
* day Day value [ 1 to 31].
*
* hr Hours value [ 0 to 23].
*
* min Minutes value [ 0 to 59].
*
* sec Seconds value [ 0 to 60] (see Note #3).
*
* tz_sec Time zone offset (in seconds, +|- from UTC) [-43200 to 43200].
*
* Return(s) : DEF_OK, if date/time structure successfully returned.
*
* DEF_FAIL, otherwise.
*
* Note(s) : (1) Date/time structure ('p_date_time') MUST be representable in Clock timestamp.
* Thus date to convert MUST be :
*
* (a) >= CLK_EPOCH_YR_START
* (b) < CLK_EPOCH_YR_END
*
* (2) Day of week ('p_date_time->DayOfWk') and Day of year ('p_date_time->DayOfYr')
* are internally calculated and set in the date/time structure if date is valid.
*
* (3) Seconds value of 60 is valid to be compatible with leap second adjustment and
* the atomic clock time structure.
*
* (4) Time zone is based ('tz_sec') on Coordinated Universal Time (UTC) & has valid
* values :
*
* (a) Between +|- 12 hours (+|- 43200 seconds)
* (b) Multiples of 15 minutes
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_DateTimeMake (CLK_DATE_TIME *p_date_time,
CLK_YR yr,
CLK_MONTH month,
CLK_DAY day,
CLK_HR hr,
CLK_MIN min,
CLK_SEC sec,
CLK_TZ_SEC tz_sec)
{
CPU_BOOLEAN valid;
/* ---------- VALIDATE & CONV CLK DATE/TIME ----------- */
valid = Clk_DateTimeMakeHandler(p_date_time,
yr,
month,
day,
hr,
min,
sec,
tz_sec,
CLK_EPOCH_YR_START,
CLK_EPOCH_YR_END);
if (valid != DEF_YES) {
return (DEF_FAIL);
}
return (DEF_OK);
}
/*
*********************************************************************************************************
* Clk_IsDateTimeValid()
*
* Description : Determine if date/time structure is valid in Clock epoch.
*
* Argument(s) : p_date_time Pointer to variable that contains the date/time structure to validate.
*
* Return(s) : DEF_YES, if date/time structure is valid.
*
* DEF_NO, otherwise.
*
* Note(s) : (1) Date/time structure ('p_date_time') MUST be representable in Clock epoch. Thus
* date to validate MUST be :
*
* (a) >= CLK_EPOCH_YR_START
* (b) < CLK_EPOCH_YR_END
*
* (2) Time zone is based ('p_date_time->TZ_sec') on Coordinated Universal Time (UTC)
* & has valid values :
*
* (a) Between +|- 12 hours (+|- 43200 seconds)
* (b) Multiples of 15 minutes
*********************************************************************************************************
*/
CPU_BOOLEAN Clk_IsDateTimeValid (CLK_DATE_TIME *p_date_time)
{
CPU_BOOLEAN valid;
/* -------------- VALIDATE CLK DATE/TIME -------------- */
CLK_TRACE_DBG(("Validate Clock date/time: "));
valid = Clk_IsDateTimeValidHandler(p_date_time,
CLK_EPOCH_YR_START,
CLK_EPOCH_YR_END);
if (valid != DEF_YES) {
CLK_TRACE_DBG(("Fail\n\r"));
return (DEF_NO);
}
CLK_TRACE_DBG(("Ok\n\r"));
return (DEF_YES);
}
/*
*********************************************************************************************************
* Clk_GetDayOfWk()
*
* Description : Get the day of week.
*
* Argument(s) : yr Year value [1900 to 2135] (see Note #1).
*
* month Month value [ 1 to 12] / [CLK_MONTH_JAN to CLK_MONTH_DEC].
*
* day Day value [ 1 to 31].
*
* Return(s) : Day of week [1 to 7] / [CLK_DAY_OF_WK_SUN to CLK_DAY_OF_WK_SAT], if NO error(s).
*
* CLK_DAY_OF_WK_NONE, otherwise.
*
* Note(s) : (1) It's only possible to get a day of week of an epoch supported by Clock :
*
* (a) Earliest year is the NTP epoch start year, thus Year ('yr') MUST be greater
* than or equal to 'CLK_NTP_EPOCH_YR_START'.
*
* (b) Latest year is the Clock epoch end year, thus Year ('yr') MUST be less
* than 'CLK_EPOCH_YR_END'.
*********************************************************************************************************
*/
CLK_DAY Clk_GetDayOfWk (CLK_YR yr,
CLK_MONTH month,
CLK_DAY day)
{
CLK_DAY day_of_wk;
CPU_BOOLEAN valid;
/* ------------------ VALIDATE DATE ------------------- */
valid = Clk_IsDateValid(yr,
month,
day,
CLK_NTP_EPOCH_YR_START,
CLK_EPOCH_YR_END);
if (valid != DEF_YES) {
return (CLK_DAY_OF_WK_NONE);
}
/* ------------------ GET DAY OF WK ------------------- */
day_of_wk = Clk_GetDayOfWkHandler(yr, month, day);
return (day_of_wk);
}
/*
*********************************************************************************************************
* Clk_GetDayOfYr()
*
* Description : Get the day of year.
*
* Argument(s) : yr Year value [1900 to 2135] (see Note #1).
*
* month Month value [ 1 to 12] / [CLK_MONTH_JAN to CLK_MONTH_DEC].
*
* day Day value [ 1 to 31].
*
* Return(s) : Day of year [1 to 366], if NO error(s).
*
* CLK_DAY_OF_WK_NONE, otherwise.
*
* Note(s) : (1) It's only possible to get a day of year of an epoch supported by Clock :
*