1 /*
2  * Copyright 2023 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_device_registers.h"
10 #include "fsl_adapter_time_stamp.h"
11 #include "fsl_gpt.h"
12 
13 typedef struct _hal_time_stamp_handle_struct_t
14 {
15     uint32_t timeStampClock_Hz;
16     uint8_t timeStampInstance;
17     uint8_t timeStampClockSrcSelect;
18 
19 } hal_time_stamp_handle_struct_t;
20 
21 /*******************************************************************************
22  * Variables
23  ******************************************************************************/
24 static GPT_Type *const s_GptBase[] = GPT_BASE_PTRS;
25 /************************************************************************************
26 *************************************************************************************
27 * Private prototypes
28 *************************************************************************************
29 ************************************************************************************/
30 
31 /************************************************************************************
32 *************************************************************************************
33 * Private memory declarations
34 *************************************************************************************
35 ************************************************************************************/
36 
37 /************************************************************************************
38 *************************************************************************************
39 * Public functions
40 *************************************************************************************
41 ************************************************************************************/
HAL_HWTimeStampInit(hal_time_stamp_handle_t halTimeStampHandle)42 static void HAL_HWTimeStampInit(hal_time_stamp_handle_t halTimeStampHandle)
43 {
44     gpt_config_t gptConfig;
45     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
46     assert(halTimeStampHandle);
47 
48     GPT_Type *gpt = s_GptBase[halTimeStampState->timeStampInstance];
49 
50     gptConfig.clockSource     = halTimeStampState->timeStampClockSrcSelect;
51     gptConfig.divider         = 1u;
52     gptConfig.enableFreeRun   = true;
53     gptConfig.enableRunInWait = true;
54     gptConfig.enableRunInStop = true;
55     gptConfig.enableRunInDoze = true;
56     gptConfig.enableRunInDbg  = false;
57     gptConfig.enableMode      = true;
58 
59     /* Initialize the LPTMR */
60     GPT_Init(gpt, &gptConfig);
61     GPT_SetOutputCompareValue(gpt, kGPT_OutputCompare_Channel1, ~0UL);
62     /* Disables the selected GPT channel interrupts */
63     GPT_DisableInterrupts(gpt, (uint32_t)kGPT_OutputCompare1InterruptEnable);
64     /* Start counting */
65     GPT_StartTimer(gpt);
66 }
67 /*!
68  * @brief Initializes the timer Stamp adapter module for a timer basic operation.
69  *
70  * @note This API should be called at the beginning of the application using the timer Stampadapter.
71  * For Initializes timer Stamp adapter,
72  *
73  * @param halTimerHandle     HAL timer adapter handle
74  * @param halTimeStampConfig A pointer to the HAL time stamp configuration structure
75  */
HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle,hal_time_stamp_config_t * halTimeStampConfig)76 void HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle, hal_time_stamp_config_t *halTimeStampConfig)
77 {
78     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
79     assert(halTimeStampHandle);
80 
81     halTimeStampState->timeStampInstance       = halTimeStampConfig->instance;
82     halTimeStampState->timeStampClock_Hz       = halTimeStampConfig->srcClock_Hz;
83     halTimeStampState->timeStampClockSrcSelect = halTimeStampConfig->clockSrcSelect;
84 
85     HAL_HWTimeStampInit(halTimeStampHandle);
86 }
87 
88 /*!
89  * @brief Get the absolute time at the moment of the call.
90  *
91  * @param halTimerHandle     HAL timer adapter handle
92  * @retval the absolute time(microsecond) at the moment of the call
93  */
HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)94 uint64_t HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)
95 {
96     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
97     assert(halTimeStampHandle);
98     GPT_Type *gpt = s_GptBase[halTimeStampState->timeStampInstance];
99 
100     return COUNT_TO_USEC(GPT_GetCurrentTimerCount(gpt), halTimeStampState->timeStampClock_Hz);
101 }
102 
HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)103 void HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)
104 {
105 }
106 
HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)107 void HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)
108 {
109 }
110