1 /*
2  * Copyright 2020 -2021 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_lptmr.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 LPTMR_Type *const s_LptmrBase[] = LPTMR_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     lptmr_config_t lptmrConfig;
45     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
46     assert(halTimeStampHandle);
47 
48     LPTMR_GetDefaultConfig(&lptmrConfig);
49     lptmrConfig.prescalerClockSource = (lptmr_prescaler_clock_select_t)halTimeStampState->timeStampClockSrcSelect;
50 
51     /* Initialize the LPTMR */
52     LPTMR_Init(s_LptmrBase[halTimeStampState->timeStampInstance], &lptmrConfig);
53 #if (defined(FSL_FEATURE_LPTMR_CNR_WIDTH_IS_32B) && (FSL_FEATURE_LPTMR_CNR_WIDTH_IS_32B > 0))
54     LPTMR_SetTimerPeriod(s_LptmrBase[halTimeStampState->timeStampInstance], 0xFFFFFFFFUL);
55 #else
56     LPTMR_SetTimerPeriod(s_LptmrBase[halTimeStampState->timeStampInstance], 0xFFFFUL);
57 #endif
58     /* Disables the selected LPTMR interrupts */
59     LPTMR_DisableInterrupts(s_LptmrBase[halTimeStampState->timeStampInstance], (uint32_t)kLPTMR_TimerInterruptEnable);
60     /* Start counting */
61     LPTMR_StartTimer(s_LptmrBase[halTimeStampState->timeStampInstance]);
62 }
63 /*!
64  * @brief Initializes the timer Stamp adapter module for a timer basic operation.
65  *
66  * @note This API should be called at the beginning of the application using the timer Stampadapter.
67  * For Initializes timer Stamp adapter,
68  *
69  * @param halTimerHandle     HAL timer adapter handle
70  * @param halTimeStampConfig A pointer to the HAL time stamp configuration structure
71  */
HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle,hal_time_stamp_config_t * halTimeStampConfig)72 void HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle, hal_time_stamp_config_t *halTimeStampConfig)
73 {
74     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
75     assert(halTimeStampHandle);
76 
77     halTimeStampState->timeStampInstance       = halTimeStampConfig->instance;
78     halTimeStampState->timeStampClock_Hz       = halTimeStampConfig->srcClock_Hz;
79     halTimeStampState->timeStampClockSrcSelect = halTimeStampConfig->clockSrcSelect;
80 
81     HAL_HWTimeStampInit(halTimeStampHandle);
82 }
83 
84 /*!
85  * @brief Get the absolute time at the moment of the call.
86  *
87  * @param halTimerHandle     HAL timer adapter handle
88  * @retval the absolute time(microsecond) at the moment of the call
89  */
HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)90 uint64_t HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)
91 {
92     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
93     assert(halTimeStampHandle);
94 
95     return COUNT_TO_USEC(LPTMR_GetCurrentTimerCount(s_LptmrBase[halTimeStampState->timeStampInstance]),
96                          halTimeStampState->timeStampClock_Hz);
97 }
98 
HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)99 void HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)
100 {
101 }
102 
HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)103 void HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)
104 {
105 }
106