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_gpt.h"
14 
15 /* -------------------------------------------------------------------------- */
16 /*                               Private definitions                          */
17 /* -------------------------------------------------------------------------- */
18 #define GPT_INDEX(x) ((x)-1u)
19 
20 #ifndef TIMESTAMP_GPT_INST
21 /* Unless otherwise defined, allocate GPT6 arbitrarily among the 6 GPT instances
22  * for the CM7 core timestamp.
23  */
24 #define TIMESTAMP_GPT_INST GPT_INDEX(6)
25 #endif
26 
27 /* -------------------------------------------------------------------------- */
28 /*                               Private memory                               */
29 /* -------------------------------------------------------------------------- */
30 
31 static TIME_STAMP_HANDLE_DEFINE(timestampHandle);
32 static bool timestampInitialized = false;
33 
34 /* -------------------------------------------------------------------------- */
35 /*                              Public functions                              */
36 /* -------------------------------------------------------------------------- */
37 
PLATFORM_InitTimeStamp(void)38 void PLATFORM_InitTimeStamp(void)
39 {
40     hal_time_stamp_config_t config;
41 
42     if (timestampInitialized == false)
43     {
44         config.instance       = TIMESTAMP_GPT_INST;
45         config.srcClock_Hz    = 32768U; /* 32kHz */
46         config.clockSrcSelect = kGPT_ClockSource_LowFreq;
47 
48         HAL_TimeStampInit(timestampHandle, &config);
49 
50         timestampInitialized = true;
51     }
52 }
53 
PLATFORM_GetTimeStamp(void)54 uint64_t PLATFORM_GetTimeStamp(void)
55 {
56     return HAL_GetTimeStamp(timestampHandle);
57 }
58 
PLATFORM_GetMaxTimeStamp(void)59 uint64_t PLATFORM_GetMaxTimeStamp(void)
60 {
61     /* The timestamp module always converts the timer counter to microsec. As the GPT is a 32bits timer,
62      * and the calculations are 64 bit, no overflow is to be taken into account */
63     return (uint64_t)COUNT_TO_USEC(~0UL, 32768U);
64 }