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_ostimer.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 OSTIMER_Type *const s_ostimerBase[] = OSTIMER_BASE_PTRS;
23 /*******************************************************************************
24  * Definitions
25  ******************************************************************************/
26 
27 /************************************************************************************
28 *************************************************************************************
29 * Public functions
30 *************************************************************************************
31 ************************************************************************************/
32 
33 /*!
34  * @brief Initializes the timer Stamp adapter module for a timer basic operation.
35  *
36  * @note This API should be called at the beginning of the application using the timer Stampadapter.
37  * For Initializes timer Stamp adapter,
38  *
39  * @param halTimerHandle     HAL timer adapter handle
40  * @param halTimeStampConfig A pointer to the HAL time stamp configuration structure
41  */
HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle,hal_time_stamp_config_t * halTimeStampConfig)42 void HAL_TimeStampInit(hal_time_stamp_handle_t halTimeStampHandle, hal_time_stamp_config_t *halTimeStampConfig)
43 {
44     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
45     assert(halTimeStampConfig->instance < (sizeof(s_ostimerBase) / sizeof(OSTIMER_Type *)));
46 
47     halTimeStampState->timeStampInstance = halTimeStampConfig->instance;
48     halTimeStampState->timeStampClock_Hz = halTimeStampConfig->srcClock_Hz;
49     OSTIMER_Init(s_ostimerBase[halTimeStampState->timeStampInstance]);
50     return;
51 }
52 
53 /*!
54  * @brief Get the absolute time at the moment of the call.
55  *
56  * @param halTimerHandle     HAL timer adapter handle
57  * @retval the absolute time(microsecond) at the moment of the call
58  */
HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)59 uint64_t HAL_GetTimeStamp(hal_time_stamp_handle_t halTimeStampHandle)
60 {
61     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
62     assert(halTimeStampHandle);
63 
64     uint64_t timerCount = OSTIMER_GetCurrentTimerValue(s_ostimerBase[halTimeStampState->timeStampInstance]);
65 
66     /* Discard the 20 MSBs of the timer to avoid 64bit overflow when converting to USEC */
67     return (uint64_t)COUNT_TO_USEC(timerCount & 0xFFFFFFFFFFFU, halTimeStampState->timeStampClock_Hz);
68 }
69 
HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)70 void HAL_TimeStampExitLowpower(hal_time_stamp_handle_t halTimeStampHandle)
71 {
72     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
73     assert(halTimeStampHandle);
74 
75     OSTIMER_Init(s_ostimerBase[halTimeStampState->timeStampInstance]);
76 }
77 
HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)78 void HAL_TimeStampEnterLowpower(hal_time_stamp_handle_t halTimeStampHandle)
79 {
80     hal_time_stamp_handle_struct_t *halTimeStampState = halTimeStampHandle;
81     assert(halTimeStampHandle);
82 
83     OSTIMER_Deinit(s_ostimerBase[halTimeStampState->timeStampInstance]);
84 }
85