-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathcfe_time_utils.c
1089 lines (936 loc) · 34.7 KB
/
cfe_time_utils.c
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
/*
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
** File: cfe_time_utils.c
**
** Purpose: cFE Time Services (TIME) library utilities source file
**
** Author: S.Walling/Microtel
**
** Notes:
**
*/
/*
** Required header files...
*/
#include "cfe_time_utils.h"
#include "cfe_msgids.h"
#include "cfe_es_resetdata_typedef.h"
#include "cfe_es.h"
#include "cfe_msg.h"
#include "cfe_sb.h"
#include <string.h>
/*----------------------------------------------------------------
*
* Function: CFE_TIME_StartReferenceUpdate
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
volatile CFE_TIME_ReferenceState_t *CFE_TIME_StartReferenceUpdate(void)
{
uint32 Version = CFE_TIME_Global.LastVersionCounter;
volatile CFE_TIME_ReferenceState_t *CurrState;
volatile CFE_TIME_ReferenceState_t *NextState;
CurrState = &CFE_TIME_Global.ReferenceState[Version & CFE_TIME_REFERENCE_BUF_MASK];
++Version;
NextState = &CFE_TIME_Global.ReferenceState[Version & CFE_TIME_REFERENCE_BUF_MASK];
NextState->StateVersion = Version;
/* initially propagate all previous values to next values */
NextState->AtToneLeapSeconds = CurrState->AtToneLeapSeconds;
NextState->ClockSetState = CurrState->ClockSetState;
NextState->ClockFlyState = CurrState->ClockFlyState;
NextState->DelayDirection = CurrState->DelayDirection;
NextState->AtToneMET = CurrState->AtToneMET;
NextState->AtToneSTCF = CurrState->AtToneSTCF;
NextState->AtToneDelay = CurrState->AtToneDelay;
NextState->AtToneLatch = CurrState->AtToneLatch;
return NextState;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_LatchClock
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_TIME_SysTime_t CFE_TIME_LatchClock(void)
{
CFE_TIME_SysTime_t LatchTime;
OS_time_t LocalTime;
/*
** Get time in O/S format (seconds : microseconds)...
*/
CFE_PSP_GetTime(&LocalTime);
/*
** Convert time to cFE format (seconds : 1/2^32 subseconds)...
*/
LatchTime.Seconds = OS_TimeGetTotalSeconds(LocalTime);
LatchTime.Subseconds = OS_TimeGetSubsecondsPart(LocalTime);
return (LatchTime);
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_QueryResetVars
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_QueryResetVars(void)
{
CFE_TIME_ResetVars_t LocalResetVars;
uint32 DefSubsMET;
uint32 DefSubsSTCF;
int32 status;
volatile CFE_TIME_ReferenceState_t *RefState;
uint32 resetAreaSize;
cpuaddr resetAreaAddr;
CFE_ES_ResetData_t * CFE_TIME_ResetDataPtr;
RefState = CFE_TIME_StartReferenceUpdate();
/*
** Get the pointer to the Reset area from the BSP
*/
status = CFE_PSP_GetResetArea(&(resetAreaAddr), &(resetAreaSize));
if (status != CFE_PSP_SUCCESS)
{
/* There is something wrong with the Reset Area */
CFE_TIME_Global.DataStoreStatus = CFE_TIME_RESET_AREA_BAD;
}
else
{
CFE_TIME_ResetDataPtr = (CFE_ES_ResetData_t *)resetAreaAddr;
/* Get the structure from the Reset Area */
LocalResetVars = CFE_TIME_ResetDataPtr->TimeResetVars;
/*
** Verify TIME data signature and clock signal selection...
** (other data fields have no verifiable limits)
*/
if ((LocalResetVars.Signature == CFE_TIME_RESET_SIGNATURE) &&
((LocalResetVars.ClockSignal == CFE_TIME_ToneSignalSelect_PRIMARY) ||
(LocalResetVars.ClockSignal == CFE_TIME_ToneSignalSelect_REDUNDANT)))
{
/*
** Initialize TIME to valid Reset Area values...
*/
RefState->AtToneMET = LocalResetVars.CurrentMET;
RefState->AtToneSTCF = LocalResetVars.CurrentSTCF;
RefState->AtToneDelay = LocalResetVars.CurrentDelay;
RefState->AtToneLeapSeconds = LocalResetVars.LeapSeconds;
CFE_TIME_Global.ClockSignal = LocalResetVars.ClockSignal;
CFE_TIME_Global.DataStoreStatus = CFE_TIME_RESET_AREA_EXISTING;
}
else
{
/*
** We got a blank area from the reset variables
*/
CFE_TIME_Global.DataStoreStatus = CFE_TIME_RESET_AREA_NEW;
}
}
/*
** Initialize TIME to default values if no valid Reset data...
*/
if (CFE_TIME_Global.DataStoreStatus != CFE_TIME_RESET_AREA_EXISTING)
{
DefSubsMET = CFE_TIME_Micro2SubSecs(CFE_MISSION_TIME_DEF_MET_SUBS);
DefSubsSTCF = CFE_TIME_Micro2SubSecs(CFE_MISSION_TIME_DEF_STCF_SUBS);
RefState->AtToneMET.Seconds = CFE_MISSION_TIME_DEF_MET_SECS;
RefState->AtToneMET.Subseconds = DefSubsMET;
RefState->AtToneSTCF.Seconds = CFE_MISSION_TIME_DEF_STCF_SECS;
RefState->AtToneSTCF.Subseconds = DefSubsSTCF;
RefState->AtToneLeapSeconds = CFE_MISSION_TIME_DEF_LEAPS;
CFE_TIME_Global.ClockSignal = CFE_TIME_ToneSignalSelect_PRIMARY;
RefState->AtToneDelay.Seconds = 0;
RefState->AtToneDelay.Subseconds = 0;
}
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_UpdateResetVars
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_UpdateResetVars(const CFE_TIME_Reference_t *Reference)
{
CFE_TIME_ResetVars_t LocalResetVars;
uint32 resetAreaSize;
cpuaddr resetAreaAddr;
CFE_ES_ResetData_t * CFE_TIME_ResetDataPtr;
/*
** Update the data only if our Reset Area is valid...
*/
if (CFE_TIME_Global.DataStoreStatus != CFE_TIME_RESET_AREA_ERROR)
{
/* Store all of our critical variables to a ResetVars_t
* then copy that to the Reset Area */
LocalResetVars.Signature = CFE_TIME_RESET_SIGNATURE;
LocalResetVars.CurrentMET = Reference->CurrentMET;
LocalResetVars.CurrentSTCF = Reference->AtToneSTCF;
LocalResetVars.CurrentDelay = Reference->AtToneDelay;
LocalResetVars.LeapSeconds = Reference->AtToneLeapSeconds;
LocalResetVars.ClockSignal = CFE_TIME_Global.ClockSignal;
/*
** Get the pointer to the Reset area from the BSP
*/
if (CFE_PSP_GetResetArea(&(resetAreaAddr), &(resetAreaSize)) == CFE_PSP_SUCCESS)
{
CFE_TIME_ResetDataPtr = (CFE_ES_ResetData_t *)resetAreaAddr;
CFE_TIME_ResetDataPtr->TimeResetVars = LocalResetVars;
}
}
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_InitData
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_InitData(void)
{
uint32 i;
volatile CFE_TIME_ReferenceState_t *RefState;
/* Clear task global */
memset(&CFE_TIME_Global, 0, sizeof(CFE_TIME_Global));
/*
** Initialize task configuration data...
*/
for (i = 0; i < CFE_TIME_REFERENCE_BUF_DEPTH; ++i)
{
CFE_TIME_Global.ReferenceState[i].StateVersion = 0xFFFFFFFF;
}
/*
** Try to get values used to compute time from Reset Area...
*/
CFE_TIME_QueryResetVars();
RefState = CFE_TIME_StartReferenceUpdate();
/*
** Remaining data values used to compute time...
*/
RefState->AtToneLatch = CFE_TIME_LatchClock();
/*
** Data values used to define the current clock state...
*/
RefState->ClockSetState = CFE_TIME_SetState_NOT_SET;
RefState->ClockFlyState = CFE_TIME_FlywheelState_IS_FLY;
#if (CFE_PLATFORM_TIME_CFG_SOURCE == true)
CFE_TIME_Global.ClockSource = CFE_TIME_SourceSelect_EXTERNAL;
#else
CFE_TIME_Global.ClockSource = CFE_TIME_SourceSelect_INTERNAL;
#endif
CFE_TIME_Global.ServerFlyState = CFE_TIME_FlywheelState_IS_FLY;
/*
** Pending data values (from "time at tone" command data packet)...
*/
CFE_TIME_Global.PendingState = CFE_TIME_ClockState_INVALID;
/*
** Nonzero adjustment values...
*/
CFE_TIME_Global.OneTimeDirection = CFE_TIME_AdjustDirection_ADD;
CFE_TIME_Global.OneHzDirection = CFE_TIME_AdjustDirection_ADD;
RefState->DelayDirection = CFE_TIME_AdjustDirection_ADD;
/*
** Miscellaneous counters...
*/
CFE_TIME_Global.VirtualMET = RefState->AtToneMET.Seconds;
/*
** Time window verification values...
*/
CFE_TIME_Global.MinElapsed = CFE_TIME_Micro2SubSecs(CFE_MISSION_TIME_MIN_ELAPSED);
CFE_TIME_Global.MaxElapsed = CFE_TIME_Micro2SubSecs(CFE_MISSION_TIME_MAX_ELAPSED);
/*
** Range checking for external time source data...
*/
#if (CFE_PLATFORM_TIME_CFG_SOURCE == true)
CFE_TIME_Global.MaxDelta.Seconds = CFE_PLATFORM_TIME_MAX_DELTA_SECS;
CFE_TIME_Global.MaxDelta.Subseconds = CFE_TIME_Micro2SubSecs(CFE_PLATFORM_TIME_MAX_DELTA_SUBS);
#endif
/*
** Maximum local clock value (before roll-over)...
*/
CFE_TIME_Global.MaxLocalClock.Seconds = CFE_PLATFORM_TIME_MAX_LOCAL_SECS;
CFE_TIME_Global.MaxLocalClock.Subseconds = CFE_PLATFORM_TIME_MAX_LOCAL_SUBS;
/*
** Range limits for time between tone signal interrupts...
*/
CFE_TIME_Global.ToneOverLimit = CFE_TIME_Micro2SubSecs(CFE_PLATFORM_TIME_CFG_TONE_LIMIT);
CFE_TIME_Global.ToneUnderLimit = CFE_TIME_Micro2SubSecs((1000000 - CFE_PLATFORM_TIME_CFG_TONE_LIMIT));
CFE_TIME_FinishReferenceUpdate(RefState);
/*
** Initialize housekeeping packet (clear user data area)...
*/
CFE_MSG_Init(&CFE_TIME_Global.HkPacket.TlmHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_HK_TLM_MID),
sizeof(CFE_TIME_Global.HkPacket));
/*
** Initialize diagnostic packet (clear user data area)...
*/
CFE_MSG_Init(&CFE_TIME_Global.DiagPacket.TlmHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_DIAG_TLM_MID),
sizeof(CFE_TIME_Global.DiagPacket));
/*
** Initialize "time at the tone" signal command packet...
*/
CFE_MSG_Init(&CFE_TIME_Global.ToneSignalCmd.CmdHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_TONE_CMD_MID),
sizeof(CFE_TIME_Global.ToneSignalCmd));
/*
** Initialize "time at the tone" data command packet...
*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
CFE_MSG_Init(&CFE_TIME_Global.ToneDataCmd.CmdHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_DATA_CMD_MID),
sizeof(CFE_TIME_Global.ToneDataCmd));
#endif
/*
** Initialize simulated tone send message ("fake tone" mode only)...
*/
#if (CFE_MISSION_TIME_CFG_FAKE_TONE == true)
CFE_MSG_Init(&CFE_TIME_Global.ToneSendCmd.CmdHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_SEND_CMD_MID),
sizeof(CFE_TIME_Global.ToneSendCmd));
#endif
/*
** Initialize local 1Hz "wake-up" command packet (optional)...
*/
CFE_MSG_Init(&CFE_TIME_Global.Local1HzCmd.CmdHeader.Msg, CFE_SB_ValueToMsgId(CFE_TIME_1HZ_CMD_MID),
sizeof(CFE_TIME_Global.Local1HzCmd));
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_GetHkData
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_GetHkData(const CFE_TIME_Reference_t *Reference)
{
/*
** Get command execution counters...
*/
CFE_TIME_Global.HkPacket.Payload.CommandCounter = CFE_TIME_Global.CommandCounter;
CFE_TIME_Global.HkPacket.Payload.CommandErrorCounter = CFE_TIME_Global.CommandErrorCounter;
/*
** Current "as calculated" clock state...
*/
CFE_TIME_Global.HkPacket.Payload.ClockStateAPI = (int16)CFE_TIME_CalculateState(Reference);
/*
** Current clock state flags...
*/
CFE_TIME_Global.HkPacket.Payload.ClockStateFlags = CFE_TIME_GetClockInfo();
/*
** Leap Seconds...
*/
CFE_TIME_Global.HkPacket.Payload.LeapSeconds = Reference->AtToneLeapSeconds;
/*
** Current MET and STCF time values...
*/
CFE_TIME_Global.HkPacket.Payload.SecondsMET = Reference->CurrentMET.Seconds;
CFE_TIME_Global.HkPacket.Payload.SubsecsMET = Reference->CurrentMET.Subseconds;
CFE_TIME_Global.HkPacket.Payload.SecondsSTCF = Reference->AtToneSTCF.Seconds;
CFE_TIME_Global.HkPacket.Payload.SubsecsSTCF = Reference->AtToneSTCF.Subseconds;
/*
** 1Hz STCF adjustment values (server only)...
*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
CFE_TIME_Global.HkPacket.Payload.Seconds1HzAdj = CFE_TIME_Global.OneHzAdjust.Seconds;
CFE_TIME_Global.HkPacket.Payload.Subsecs1HzAdj = CFE_TIME_Global.OneHzAdjust.Subseconds;
#endif
/*
** Time at tone delay values (client only)...
*/
#if (CFE_PLATFORM_TIME_CFG_CLIENT == true)
CFE_TIME_Global.HkPacket.Payload.SecondsDelay = Reference->AtToneDelay.Seconds;
CFE_TIME_Global.HkPacket.Payload.SubsecsDelay = Reference->AtToneDelay.Subseconds;
#endif
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_GetDiagData
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_GetDiagData(void)
{
CFE_TIME_Reference_t Reference;
CFE_TIME_SysTime_t TempTime;
/*
** Get reference time values (local time, time at tone, etc.)...
*/
CFE_TIME_GetReference(&Reference);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.AtToneMET, &Reference.AtToneMET);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.AtToneSTCF, &Reference.AtToneSTCF);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.AtToneDelay, &Reference.AtToneDelay);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.AtToneLatch, &Reference.AtToneLatch);
CFE_TIME_Global.DiagPacket.Payload.AtToneLeapSeconds = Reference.AtToneLeapSeconds;
CFE_TIME_Global.DiagPacket.Payload.ClockStateAPI = CFE_TIME_CalculateState(&Reference);
/*
** Data values that reflect the time (right now)...
*/
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.TimeSinceTone, &Reference.TimeSinceTone);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.CurrentLatch, &Reference.CurrentLatch);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.CurrentMET, &Reference.CurrentMET);
TempTime = CFE_TIME_CalculateTAI(&Reference);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.CurrentTAI, &TempTime);
TempTime = CFE_TIME_CalculateUTC(&Reference);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.CurrentUTC, &TempTime);
/*
** Data values used to define the current clock state...
*/
CFE_TIME_Global.DiagPacket.Payload.ClockSetState = Reference.ClockSetState;
CFE_TIME_Global.DiagPacket.Payload.ClockFlyState = Reference.ClockFlyState;
CFE_TIME_Global.DiagPacket.Payload.ClockSource = CFE_TIME_Global.ClockSource;
CFE_TIME_Global.DiagPacket.Payload.ClockSignal = CFE_TIME_Global.ClockSignal;
CFE_TIME_Global.DiagPacket.Payload.ServerFlyState = CFE_TIME_Global.ServerFlyState;
CFE_TIME_Global.DiagPacket.Payload.Forced2Fly = (int16)CFE_TIME_Global.Forced2Fly;
/*
** Clock state flags...
*/
CFE_TIME_Global.DiagPacket.Payload.ClockStateFlags = CFE_TIME_GetClockInfo();
/*
** STCF adjustment direction values...
*/
CFE_TIME_Global.DiagPacket.Payload.OneTimeDirection = CFE_TIME_Global.OneTimeDirection;
CFE_TIME_Global.DiagPacket.Payload.OneHzDirection = CFE_TIME_Global.OneHzDirection;
CFE_TIME_Global.DiagPacket.Payload.DelayDirection = Reference.DelayDirection;
/*
** STCF adjustment values...
*/
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.OneTimeAdjust, &CFE_TIME_Global.OneTimeAdjust);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.OneHzAdjust, &CFE_TIME_Global.OneHzAdjust);
/*
** Most recent local clock latch values...
*/
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.ToneSignalLatch, &CFE_TIME_Global.ToneSignalLatch);
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.ToneDataLatch, &CFE_TIME_Global.ToneDataLatch);
/*
** Miscellaneous counters (subject to reset command)...
*/
CFE_TIME_Global.DiagPacket.Payload.ToneMatchCounter = CFE_TIME_Global.ToneMatchCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneMatchErrorCounter = CFE_TIME_Global.ToneMatchErrorCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneSignalCounter = CFE_TIME_Global.ToneSignalCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneDataCounter = CFE_TIME_Global.ToneDataCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneIntCounter = CFE_TIME_Global.ToneIntCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneIntErrorCounter = CFE_TIME_Global.ToneIntErrorCounter;
CFE_TIME_Global.DiagPacket.Payload.ToneTaskCounter = CFE_TIME_Global.ToneTaskCounter;
CFE_TIME_Global.DiagPacket.Payload.VersionCounter =
CFE_TIME_Global.LastVersionCounter - CFE_TIME_Global.ResetVersionCounter;
CFE_TIME_Global.DiagPacket.Payload.LocalIntCounter = CFE_TIME_Global.LocalIntCounter;
CFE_TIME_Global.DiagPacket.Payload.LocalTaskCounter = CFE_TIME_Global.LocalTaskCounter;
/*
** Miscellaneous counters (not subject to reset command)...
*/
CFE_TIME_Global.DiagPacket.Payload.VirtualMET = CFE_TIME_Global.VirtualMET;
/*
** Time window verification values (converted from micro-secs)...
**
** Regardless of whether the tone follows the time packet, or vice
** versa, these values define the acceptable window of time for
** the second event to follow the first. The minimum value may
** be as little as zero, and the maximum must be something less
** than a second.
*/
CFE_TIME_Global.DiagPacket.Payload.MinElapsed = CFE_TIME_Global.MinElapsed;
CFE_TIME_Global.DiagPacket.Payload.MaxElapsed = CFE_TIME_Global.MaxElapsed;
/*
** Maximum local clock value (before roll-over)...
*/
CFE_TIME_Copy(&CFE_TIME_Global.DiagPacket.Payload.MaxLocalClock, &CFE_TIME_Global.MaxLocalClock);
/*
** Tone signal tolerance limits...
*/
CFE_TIME_Global.DiagPacket.Payload.ToneOverLimit = CFE_TIME_Global.ToneOverLimit;
CFE_TIME_Global.DiagPacket.Payload.ToneUnderLimit = CFE_TIME_Global.ToneUnderLimit;
/*
** Reset Area access status...
*/
CFE_TIME_Global.DiagPacket.Payload.DataStoreStatus = CFE_TIME_Global.DataStoreStatus;
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_GetReference
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_GetReference(CFE_TIME_Reference_t *Reference)
{
CFE_TIME_SysTime_t TimeSinceTone;
CFE_TIME_SysTime_t CurrentMET;
uint32 VersionCounter;
uint32 RetryCount = 4;
volatile CFE_TIME_ReferenceState_t *RefState;
/*
** VersionCounter is incremented when reference data is modified...
*/
while (true)
{
VersionCounter = CFE_TIME_Global.LastVersionCounter;
RefState = &CFE_TIME_Global.ReferenceState[VersionCounter & CFE_TIME_REFERENCE_BUF_MASK];
Reference->CurrentLatch = CFE_TIME_LatchClock();
Reference->AtToneMET = RefState->AtToneMET;
Reference->AtToneSTCF = RefState->AtToneSTCF;
Reference->AtToneLeapSeconds = RefState->AtToneLeapSeconds;
Reference->AtToneDelay = RefState->AtToneDelay;
Reference->AtToneLatch = RefState->AtToneLatch;
Reference->ClockSetState = RefState->ClockSetState;
Reference->ClockFlyState = RefState->ClockFlyState;
Reference->DelayDirection = RefState->DelayDirection;
/*
* If the version counter inside the state record
* is the same value as the global _after_ copying the
* data, then the value is considered valid.
*/
if (VersionCounter == RefState->StateVersion)
{
/* successful read */
break;
}
/*
* The value was caught mid-update, so the reference data
* might not be consistent. Try again to read it.
*
* The number of retries is limited, to prevent getting
* stuck in this loop forever. There is currently no
* way to handle the inability to read the time reference.
*/
if (RetryCount == 0)
{
/* unsuccessful read */
break;
}
--RetryCount;
}
/*
** Compute the amount of time "since" the tone...
*/
if (CFE_TIME_Compare(Reference->CurrentLatch, Reference->AtToneLatch) == CFE_TIME_A_LT_B)
{
/*
** Local clock has rolled over since last tone...
*/
TimeSinceTone = CFE_TIME_Subtract(CFE_TIME_Global.MaxLocalClock, Reference->AtToneLatch);
TimeSinceTone = CFE_TIME_Add(TimeSinceTone, Reference->CurrentLatch);
}
else
{
/*
** Normal case -- local clock is greater than latch at tone...
*/
TimeSinceTone = CFE_TIME_Subtract(Reference->CurrentLatch, Reference->AtToneLatch);
}
Reference->TimeSinceTone = TimeSinceTone;
/*
** Add in the MET at the tone...
*/
CurrentMET = CFE_TIME_Add(TimeSinceTone, Reference->AtToneMET);
/*
** Synchronize "this" time client to the time server...
*/
#if (CFE_PLATFORM_TIME_CFG_CLIENT == true)
if (Reference->DelayDirection == CFE_TIME_AdjustDirection_ADD)
{
CurrentMET = CFE_TIME_Add(CurrentMET, Reference->AtToneDelay);
}
else
{
CurrentMET = CFE_TIME_Subtract(CurrentMET, Reference->AtToneDelay);
}
#endif
Reference->CurrentMET = CurrentMET;
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_CalculateTAI
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_TIME_SysTime_t CFE_TIME_CalculateTAI(const CFE_TIME_Reference_t *Reference)
{
CFE_TIME_SysTime_t TimeAsTAI;
TimeAsTAI = CFE_TIME_Add(Reference->CurrentMET, Reference->AtToneSTCF);
return (TimeAsTAI);
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_CalculateUTC
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
CFE_TIME_SysTime_t CFE_TIME_CalculateUTC(const CFE_TIME_Reference_t *Reference)
{
CFE_TIME_SysTime_t TimeAsUTC;
TimeAsUTC = CFE_TIME_Add(Reference->CurrentMET, Reference->AtToneSTCF);
TimeAsUTC.Seconds -= Reference->AtToneLeapSeconds;
return (TimeAsUTC);
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_CalculateState
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
int16 CFE_TIME_CalculateState(const CFE_TIME_Reference_t *Reference)
{
int16 ClockState;
/*
** Determine the current clock state...
*/
if (Reference->ClockSetState == CFE_TIME_SetState_WAS_SET)
{
if (Reference->ClockFlyState == CFE_TIME_FlywheelState_NO_FLY)
{
/*
** CFE_TIME_ClockState_VALID = clock set and not fly-wheeling...
*/
ClockState = CFE_TIME_ClockState_VALID;
/*
** If the server is fly-wheel then the client must also
** report fly-wheel (even if it is not)...
*/
#if (CFE_PLATFORM_TIME_CFG_CLIENT == true)
if (CFE_TIME_Global.ServerFlyState == CFE_TIME_FlywheelState_IS_FLY)
{
ClockState = CFE_TIME_ClockState_FLYWHEEL;
}
#endif
}
else
{
/*
** CFE_TIME_ClockState_FLYWHEEL = clock set and fly-wheeling...
*/
ClockState = CFE_TIME_ClockState_FLYWHEEL;
}
}
else
{
/*
** CFE_TIME_ClockState_INVALID = clock not set...
*/
ClockState = CFE_TIME_ClockState_INVALID;
}
return (ClockState);
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetState
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
void CFE_TIME_SetState(int16 NewState)
{
volatile CFE_TIME_ReferenceState_t *RefState;
RefState = CFE_TIME_StartReferenceUpdate();
/*
** If we get a command to set the clock to "flywheel" mode, then
** set a global flag so that we can choose to ignore time
** updates until we get another clock state command...
*/
if (NewState == CFE_TIME_ClockState_FLYWHEEL)
{
CFE_TIME_Global.Forced2Fly = true;
RefState->ClockFlyState = CFE_TIME_FlywheelState_IS_FLY;
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
CFE_TIME_Global.ServerFlyState = CFE_TIME_FlywheelState_IS_FLY;
#endif
}
else if (NewState == CFE_TIME_ClockState_VALID)
{
CFE_TIME_Global.Forced2Fly = false;
RefState->ClockSetState = CFE_TIME_SetState_WAS_SET;
}
else
{
CFE_TIME_Global.Forced2Fly = false;
RefState->ClockSetState = CFE_TIME_SetState_NOT_SET;
}
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetSource
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SOURCE == true)
void CFE_TIME_SetSource(int16 NewSource)
{
CFE_TIME_Global.ClockSource = NewSource;
}
#endif /* CFE_PLATFORM_TIME_CFG_SOURCE */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetSignal
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SIGNAL == true)
void CFE_TIME_SetSignal(int16 NewSignal)
{
/*
** Maintain current tone signal selection for telemetry...
*/
CFE_TIME_Global.ClockSignal = NewSignal;
}
#endif /* CFE_PLATFORM_TIME_CFG_SIGNAL */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetDelay
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_CLIENT == true)
void CFE_TIME_SetDelay(CFE_TIME_SysTime_t NewDelay, int16 Direction)
{
volatile CFE_TIME_ReferenceState_t *RefState;
RefState = CFE_TIME_StartReferenceUpdate();
RefState->AtToneDelay = NewDelay;
RefState->DelayDirection = Direction;
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
#endif /* CFE_PLATFORM_TIME_CFG_CLIENT */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetTime
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
void CFE_TIME_SetTime(CFE_TIME_SysTime_t NewTime)
{
volatile CFE_TIME_ReferenceState_t *RefState;
/*
** The input to this function is a time value that includes MET
** and STCF. If the default time format is UTC, the input
** time value has had leaps seconds removed from the total.
*/
CFE_TIME_Reference_t Reference;
CFE_TIME_SysTime_t NewSTCF;
/*
** Get reference time values (local time, time at tone, etc.)...
*/
CFE_TIME_GetReference(&Reference);
/*
** Remove current MET from the new time value (leaves STCF)...
*/
NewSTCF = CFE_TIME_Subtract(NewTime, Reference.CurrentMET);
/*
** Restore leap seconds if default time format is UTC...
*/
#if (CFE_MISSION_TIME_CFG_DEFAULT_UTC == true)
NewSTCF.Seconds += Reference.AtToneLeapSeconds;
#endif
RefState = CFE_TIME_StartReferenceUpdate();
RefState->AtToneSTCF = NewSTCF;
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
#endif /* CFE_PLATFORM_TIME_CFG_SERVER */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetMET
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
void CFE_TIME_SetMET(CFE_TIME_SysTime_t NewMET)
{
volatile CFE_TIME_ReferenceState_t *RefState;
RefState = CFE_TIME_StartReferenceUpdate();
/*
** Update reference values used to compute current time...
*/
RefState->AtToneMET = NewMET;
CFE_TIME_Global.VirtualMET = NewMET.Seconds;
RefState->AtToneLatch = CFE_TIME_LatchClock();
/*
** Update h/w MET register...
*/
#if (CFE_PLATFORM_TIME_CFG_VIRTUAL != true)
OS_SetLocalMET(NewMET.Seconds);
#endif
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
#endif /* CFE_PLATFORM_TIME_CFG_SERVER */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetSTCF
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
void CFE_TIME_SetSTCF(CFE_TIME_SysTime_t NewSTCF)
{
volatile CFE_TIME_ReferenceState_t *RefState;
RefState = CFE_TIME_StartReferenceUpdate();
RefState->AtToneSTCF = NewSTCF;
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}
#endif /* CFE_PLATFORM_TIME_CFG_SERVER */
/*----------------------------------------------------------------
*
* Function: CFE_TIME_SetLeapSeconds
*
* Application-scope internal function
* See description in header file for argument/return detail
*
*-----------------------------------------------------------------*/
#if (CFE_PLATFORM_TIME_CFG_SERVER == true)
void CFE_TIME_SetLeapSeconds(int16 NewLeaps)
{
volatile CFE_TIME_ReferenceState_t *RefState;
RefState = CFE_TIME_StartReferenceUpdate();
RefState->AtToneLeapSeconds = NewLeaps;
/*
** Time has changed, force anyone reading time to retry...
*/
CFE_TIME_FinishReferenceUpdate(RefState);
return;
}