1 /*
2  * Copyright 2022 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_ctimer.h"
12 
13 typedef struct _hal_time_stamp_handle_struct_t
14 {
15     uint32_t timeStampClock_Hz;
16     uint8_t timeStampInstance;
17 } hal_time_stamp_handle_struct_t;
18 
19 /*******************************************************************************
20  * Variables
21  ******************************************************************************/
22 static CTIMER_Type *const s_CtimerBase[] = CTIMER_BASE_PTRS;
23 /*******************************************************************************
24  * Definitions
25  ******************************************************************************/
26 #define MAX_COUNT 0xffffffffUL
27 
28 /************************************************************************************
29 *************************************************************************************
30 * Public functions
31 *************************************************************************************
32 ************************************************************************************/
33 
34 /*!
35  * @brief Initializes the timer Stamp adapter module for a timer basic operation.
36  *
37  * @note This API should be called at the beginning of the application using the timer Stampadapter.
38  * For Initializes timer Stamp adapter,
39  *
40  * @param halTimerHandle     HAL timer adapter handle
41  * @param halTimeStampConfig A pointer to the HAL time stamp configuration structure
42  */
HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle,hal_time_stamp_config_t * halTimeStampConfig)43 void HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle, hal_time_stamp_config_t *halTimeStampConfig)
44 {
45     ctimer_config_t config;
46     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
47     ctimer_match_config_t mCtimerMatchConfig;
48     assert(halTimeStampHandle);
49 
50     CTIMER_GetDefaultConfig(&config);
51 
52     config.prescale = halTimeStampConfig->srcClock_Hz / 1000000U;
53 
54     config.prescale -= 1U;
55 
56     halTimeStampState->timeStampInstance = halTimeStampConfig->instance;
57     halTimeStampState->timeStampClock_Hz = halTimeStampConfig->srcClock_Hz / (uint32_t)(config.prescale + 1U);
58 
59     CTIMER_Init(s_CtimerBase[halTimeStampState->timeStampInstance], &config);
60     CTIMER_StopTimer(s_CtimerBase[halTimeStampState->timeStampInstance]);
61 
62     /* Configuration 0 */
63     mCtimerMatchConfig.enableCounterReset = true;
64     mCtimerMatchConfig.enableCounterStop  = false;
65     mCtimerMatchConfig.outControl         = kCTIMER_Output_NoAction;
66     mCtimerMatchConfig.outPinInitState    = false;
67     mCtimerMatchConfig.enableInterrupt    = false;
68     mCtimerMatchConfig.matchValue         = (uint32_t)USEC_TO_COUNT(MAX_COUNT, halTimeStampState->timeStampClock_Hz);
69 
70     CTIMER_SetupMatch(s_CtimerBase[halTimeStampState->timeStampInstance], (ctimer_match_t)kCTIMER_Match_0,
71                       &mCtimerMatchConfig);
72 
73     CTIMER_StartTimer(s_CtimerBase[halTimeStampState->timeStampInstance]);
74 }
75 
76 /*!
77  * @brief Get the absolute time at the moment of the call.
78  *
79  * @param halTimerHandle     HAL timer adapter handle
80  * @retval the absolute time(microsecond) at the moment of the call
81  */
HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)82 uint64_t HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)
83 {
84     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
85     assert(halTimeStampHandle);
86 
87     uint64_t count = CTIMER_GetTimerCountValue(s_CtimerBase[halTimeStampState->timeStampInstance]);
88 
89     return COUNT_TO_USEC(count, halTimeStampState->timeStampClock_Hz);
90 }
91 
HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)92 void HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)
93 {
94     assert(halTimeStampHandle);
95 }
96 
HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)97 void HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)
98 {
99 }
100