1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_TSTMR_H_
9 #define _FSL_TSTMR_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup tstmr_driver
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /* Component ID definition, used by tools. */
23 #ifndef FSL_COMPONENT_ID
24 #define FSL_COMPONENT_ID "platform.drivers.tstmr"
25 #endif
26 
27 /*! @name Driver version */
28 /*@{*/
29 #define FSL_TSTMR_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0 */
30                                                          /*@}*/
31 
32 /*******************************************************************************
33  * API
34  ******************************************************************************/
35 
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39 
40 /*!
41  * @brief Reads the time stamp.
42  *
43  * This function reads the low and high registers and returns the 56-bit free running
44  * counter value. This can be read by software at any time to determine the software ticks.
45  *
46  * @param base TSTMR peripheral base address.
47  *
48  * @return The 56-bit time stamp value.
49  */
TSTMR_ReadTimeStamp(TSTMR_Type * base)50 static inline uint64_t TSTMR_ReadTimeStamp(TSTMR_Type *base)
51 {
52     return *(volatile uint64_t *)(base);
53 }
54 
55 /*!
56  * @brief Delays for a specified number of microseconds.
57  *
58  * This function repeatedly reads the timestamp register and waits for the user-specified
59  * delay value.
60  *
61  * @param base      TSTMR peripheral base address.
62  * @param delayInUs Delay value in microseconds.
63  */
TSTMR_DelayUs(TSTMR_Type * base,uint32_t delayInUs)64 static inline void TSTMR_DelayUs(TSTMR_Type *base, uint32_t delayInUs)
65 {
66     uint64_t startTime = TSTMR_ReadTimeStamp(base);
67 #if defined(FSL_FEATURE_TSTMR_CLOCK_FREQUENCY_1MHZ) && FSL_FEATURE_TSTMR_CLOCK_FREQUENCY_1MHZ
68     while (TSTMR_ReadTimeStamp(base) - startTime < delayInUs)
69 #elif defined(FSL_FEATURE_TSTMR_CLOCK_FREQUENCY_8MHZ) && FSL_FEATURE_TSTMR_CLOCK_FREQUENCY_8MHZ
70     while (TSTMR_ReadTimeStamp(base) - startTime < 8 * delayInUs)
71 #else
72     assert(0);
73 #endif
74     {
75     }
76 }
77 
78 #if defined(__cplusplus)
79 }
80 #endif
81 
82 /*! @}*/
83 
84 #endif /* _FSL_TSTMR_H_ */
85