1 /* -------------------------------------------------------------------------- */
2 /*                             Copyright 2023 NXP                             */
3 /*                            All rights reserved.                            */
4 /*                    SPDX-License-Identifier: BSD-3-Clause                   */
5 /* -------------------------------------------------------------------------- */
6 
7 /* -------------------------------------------------------------------------- */
8 /*                                  Includes                                  */
9 /* -------------------------------------------------------------------------- */
10 
11 #include "fwk_platform.h"
12 #include "fsl_adapter_time_stamp.h"
13 #include "fsl_clock.h"
14 
15 /* -------------------------------------------------------------------------- */
16 /*                               Private memory                               */
17 /* -------------------------------------------------------------------------- */
18 
19 static TIME_STAMP_HANDLE_DEFINE(timestampHandle);
20 static volatile bool timerManagerInitialized = false;
21 static volatile bool timestampInitialized    = false;
22 
23 /* -------------------------------------------------------------------------- */
24 /*                              Public functions                              */
25 /* -------------------------------------------------------------------------- */
26 
PLATFORM_InitTimerManager(void)27 timer_status_t PLATFORM_InitTimerManager(void)
28 {
29     /* Initialize timer manager */
30     timer_config_t timerConfig;
31     timer_status_t status;
32 
33     if (timerManagerInitialized == false)
34     {
35         timerConfig.instance    = PLATFORM_TM_INSTANCE;
36         timerConfig.srcClock_Hz = CLOCK_GetFreq(kCLOCK_OscClk);
37 
38         status = TM_Init(&timerConfig);
39         if (status == kStatus_TimerSuccess)
40         {
41             timerManagerInitialized = true;
42         }
43     }
44     return status;
45 }
46 
PLATFORM_DeinitTimerManager(void)47 void PLATFORM_DeinitTimerManager(void)
48 {
49     if (timerManagerInitialized == true)
50     {
51         TM_Deinit();
52         timerManagerInitialized = false;
53     }
54 }
55 
PLATFORM_InitTimeStamp(void)56 void PLATFORM_InitTimeStamp(void)
57 {
58     hal_time_stamp_config_t config;
59 
60     if (timestampInitialized == false)
61     {
62         config.instance    = 0U;
63         config.srcClock_Hz = CLOCK_GetPerClkFreq();
64 
65         HAL_TimeStampInit(timestampHandle, &config);
66 
67         timestampInitialized = true;
68     }
69 }
70 
PLATFORM_GetTimeStamp(void)71 uint64_t PLATFORM_GetTimeStamp(void)
72 {
73     return HAL_GetTimeStamp(timestampHandle);
74 }
75 
PLATFORM_GetMaxTimeStamp(void)76 uint64_t PLATFORM_GetMaxTimeStamp(void)
77 {
78     /* The timestamp module always converts the timer counter to microsec. As the GPT is a 32bits timer,
79      * and the calculations are 64 bit, no overflow is to be taken into account */
80     return (uint64_t)COUNT_TO_USEC(~0UL, CLOCK_GetPerClkFreq());
81 }
82