1 /*
2  * Copyright 2020 - 2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _TIMER_STAMP_H_
10 #define _TIMER_STAMP_H_
11 
12 /************************************************************************************
13 *************************************************************************************
14 * Include
15 *************************************************************************************
16 ***********************************************************************************/
17 #include "fsl_common.h"
18 #if defined(SDK_OS_FREE_RTOS)
19 #include "FreeRTOS.h"
20 #endif
21 
22 /************************************************************************************
23 *************************************************************************************
24 * Public types
25 *************************************************************************************
26 ************************************************************************************/
27 /*! @brief HAL timer configuration structure for HAL time stamp setting. */
28 typedef struct _hal_time_stamp_config
29 {
30     uint32_t srcClock_Hz; /*!< Source clock of the timer */
31     uint8_t  instance;    /*!< Hardware timer module instance, for example: if you want use FTM0,then the instance is configured to 0, if
32                                you want use FTM2 hardware timer, then configure the instance to 2, detail information please refer to the
33                                SOC corresponding RM.Invalid instance value will cause initialization failure. */
34 
35     uint8_t clockSrcSelect;     /*!< Select clock source. It is for timer clock select, if the lptmr does not
36                                       want to use the default clock source*/
37 
38 } hal_time_stamp_config_t;
39 
40 /*! @brief Definition of time stamp adapter handle size. */
41 #define HAL_TIME_STAMP_HANDLE_SIZE                (8U)
42 
43 /*!
44  * @brief Defines the time stamp handle
45  *
46  * This macro is used to define a 4 byte aligned time stamp handle.
47  * Then use "(hal_time_stamp_handle_t)name" to get the time stamp handle.
48  *
49  * The macro should be global and could be optional. You could also define time stamp handle by yourself.
50  *
51  * This is an example,
52  * @code
53  * TIME_STAMP_HANDLE_DEFINE(timeStampHandle);
54  * @endcode
55  *
56  * @param name The name string of the time stamp handle.
57  */
58 #define TIME_STAMP_HANDLE_DEFINE(name) uint32_t name[((HAL_TIME_STAMP_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]
59 
60 typedef void* hal_time_stamp_handle_t;
61 /************************************************************************************
62 *************************************************************************************
63 * Public prototypes
64 *************************************************************************************
65 ************************************************************************************/
66 #if defined(__cplusplus)
67 extern "C" {
68 #endif /* _cplusplus */
69 
70 /*!
71  * @brief Initializes the timer Stamp adapter module for a timer basic operation.
72  *
73  * @note This API should be called at the beginning of the application using the time stamp adapter.
74  * For Initializes time stamp adapter,
75  *  @code
76  *   TIME_STAMP_HANDLE_DEFINE(halTimeStampHandle);
77  *   hal_time_stamp_config_t halTimeStampConfig;
78  *   halTimeStampConfig.srcClock_Hz = BOARD_GetTimeSrcClock();
79  *   halTimeStampConfig.instance = 0;
80  *   HAL_TimeStampInit(((hal_time_stamp_handle_t) halTimeStampHandle, &halTimerConfig);
81  *  @endcode
82  *
83  * @param halTimeStampHandle HAL time stamp adapter handle, the handle buffer with size #HAL_TIME_STAMP_HANDLE_SIZE
84  * should be allocated at upper level.
85  * The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices.
86  * You can define the handle in the following two ways:
87  * #TIME_STAMP_HANDLE_DEFINE(halTimeStampHandle);
88  * or
89  * uint32_t halTimeStampHandle[((HAL_TIME_STAMP_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
90  * @param halTimeStampConfig A pointer to the HAL time stamp configuration structure
91  */
92 void HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle, hal_time_stamp_config_t* halTimeStampConfig);
93 
94 /*!
95  * @brief Get the absolute time at the moment of the call.
96  *
97  * @param halTimerHandle     HAL timer adapter handle
98  * @retval the absolute time(microsecond) at the moment of the call
99  */
100 uint64_t HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle);
101 
102 /*!
103  * @brief Timer stamp adapter power up function.
104  *
105  * @note This API should be called by low power module when system exit from sleep mode.
106  *
107  * @param halTimerHandle     HAL timer adapter handle
108  */
109 void HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle);
110 
111 /*!
112  * @brief Timer stamp adapter power down function.
113  *
114  * @note This API should be called by low power module before system enter into sleep mode.
115  *
116  * @param halTimerHandle     HAL timer adapter handle
117  */
118 void HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle);
119 #if defined(__cplusplus)
120 }
121 #endif
122 /*! @}*/
123 #endif /* _TIMER_STAMP_H_ */
124