Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:00

0001 /*
0002  * Copyright (c) 2015, Freescale Semiconductor, Inc.
0003  * Copyright 2016-2020 NXP
0004  * All rights reserved.
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 #ifndef _FSL_PIT_H_
0009 #define _FSL_PIT_H_
0010 
0011 #include "fsl_common.h"
0012 
0013 /*!
0014  * @addtogroup pit
0015  * @{
0016  */
0017 
0018 /*******************************************************************************
0019  * Definitions
0020  ******************************************************************************/
0021 
0022 /*! @name Driver version */
0023 /*@{*/
0024 /*! @brief PIT Driver Version 2.0.4 */
0025 #define FSL_PIT_DRIVER_VERSION (MAKE_VERSION(2, 0, 4))
0026 /*@}*/
0027 
0028 /*!
0029  * @brief List of PIT channels
0030  * @note Actual number of available channels is SoC dependent
0031  */
0032 typedef enum _pit_chnl
0033 {
0034     kPIT_Chnl_0 = 0U, /*!< PIT channel number 0*/
0035     kPIT_Chnl_1,      /*!< PIT channel number 1 */
0036     kPIT_Chnl_2,      /*!< PIT channel number 2 */
0037     kPIT_Chnl_3,      /*!< PIT channel number 3 */
0038 } pit_chnl_t;
0039 
0040 /*! @brief List of PIT interrupts */
0041 typedef enum _pit_interrupt_enable
0042 {
0043     kPIT_TimerInterruptEnable = PIT_TCTRL_TIE_MASK, /*!< Timer interrupt enable*/
0044 } pit_interrupt_enable_t;
0045 
0046 /*! @brief List of PIT status flags */
0047 typedef enum _pit_status_flags
0048 {
0049     kPIT_TimerFlag = PIT_TFLG_TIF_MASK, /*!< Timer flag */
0050 } pit_status_flags_t;
0051 
0052 /*!
0053  * @brief PIT configuration structure
0054  *
0055  * This structure holds the configuration settings for the PIT peripheral. To initialize this
0056  * structure to reasonable defaults, call the PIT_GetDefaultConfig() function and pass a
0057  * pointer to your config structure instance.
0058  *
0059  * The configuration structure can be made constant so it resides in flash.
0060  */
0061 typedef struct _pit_config
0062 {
0063     bool enableRunInDebug; /*!< true: Timers run in debug mode; false: Timers stop in debug mode */
0064 } pit_config_t;
0065 
0066 /*******************************************************************************
0067  * API
0068  ******************************************************************************/
0069 
0070 #if defined(__cplusplus)
0071 extern "C" {
0072 #endif
0073 
0074 /*!
0075  * @name Initialization and deinitialization
0076  * @{
0077  */
0078 
0079 /*!
0080  * @brief Ungates the PIT clock, enables the PIT module, and configures the peripheral for basic operations.
0081  *
0082  * @note This API should be called at the beginning of the application using the PIT driver.
0083  *
0084  * @param base   PIT peripheral base address
0085  * @param config Pointer to the user's PIT config structure
0086  */
0087 void PIT_Init(PIT_Type *base, const pit_config_t *config);
0088 
0089 /*!
0090  * @brief Gates the PIT clock and disables the PIT module.
0091  *
0092  * @param base PIT peripheral base address
0093  */
0094 void PIT_Deinit(PIT_Type *base);
0095 
0096 /*!
0097  * @brief Fills in the PIT configuration structure with the default settings.
0098  *
0099  * The default values are as follows.
0100  * @code
0101  *     config->enableRunInDebug = false;
0102  * @endcode
0103  * @param config Pointer to the configuration structure.
0104  */
0105 static inline void PIT_GetDefaultConfig(pit_config_t *config)
0106 {
0107     assert(NULL != config);
0108 
0109     /* Timers are stopped in Debug mode */
0110     config->enableRunInDebug = false;
0111 }
0112 
0113 #if defined(FSL_FEATURE_PIT_HAS_CHAIN_MODE) && FSL_FEATURE_PIT_HAS_CHAIN_MODE
0114 
0115 /*!
0116  * @brief Enables or disables chaining a timer with the previous timer.
0117  *
0118  * When a timer has a chain mode enabled, it only counts after the previous
0119  * timer has expired. If the timer n-1 has counted down to 0, counter n
0120  * decrements the value by one. Each timer is 32-bits, which allows the developers
0121  * to chain timers together and form a longer timer (64-bits and larger). The first timer
0122  * (timer 0) can't be chained to any other timer.
0123  *
0124  * @param base    PIT peripheral base address
0125  * @param channel Timer channel number which is chained with the previous timer
0126  * @param enable  Enable or disable chain.
0127  *                true:  Current timer is chained with the previous timer.
0128  *                false: Timer doesn't chain with other timers.
0129  */
0130 static inline void PIT_SetTimerChainMode(PIT_Type *base, pit_chnl_t channel, bool enable)
0131 {
0132     if (enable)
0133     {
0134         base->CHANNEL[channel].TCTRL |= PIT_TCTRL_CHN_MASK;
0135     }
0136     else
0137     {
0138         base->CHANNEL[channel].TCTRL &= ~PIT_TCTRL_CHN_MASK;
0139     }
0140 }
0141 
0142 #endif /* FSL_FEATURE_PIT_HAS_CHAIN_MODE */
0143 
0144 /*! @}*/
0145 
0146 /*!
0147  * @name Interrupt Interface
0148  * @{
0149  */
0150 
0151 /*!
0152  * @brief Enables the selected PIT interrupts.
0153  *
0154  * @param base    PIT peripheral base address
0155  * @param channel Timer channel number
0156  * @param mask    The interrupts to enable. This is a logical OR of members of the
0157  *                enumeration ::pit_interrupt_enable_t
0158  */
0159 static inline void PIT_EnableInterrupts(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
0160 {
0161     base->CHANNEL[channel].TCTRL |= mask;
0162 }
0163 
0164 /*!
0165  * @brief Disables the selected PIT interrupts.
0166  *
0167  * @param base    PIT peripheral base address
0168  * @param channel Timer channel number
0169  * @param mask    The interrupts to disable. This is a logical OR of members of the
0170  *                enumeration ::pit_interrupt_enable_t
0171  */
0172 static inline void PIT_DisableInterrupts(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
0173 {
0174     base->CHANNEL[channel].TCTRL &= ~mask;
0175 }
0176 
0177 /*!
0178  * @brief Gets the enabled PIT interrupts.
0179  *
0180  * @param base    PIT peripheral base address
0181  * @param channel Timer channel number
0182  *
0183  * @return The enabled interrupts. This is the logical OR of members of the
0184  *         enumeration ::pit_interrupt_enable_t
0185  */
0186 static inline uint32_t PIT_GetEnabledInterrupts(PIT_Type *base, pit_chnl_t channel)
0187 {
0188     return (base->CHANNEL[channel].TCTRL & PIT_TCTRL_TIE_MASK);
0189 }
0190 
0191 /*! @}*/
0192 
0193 /*!
0194  * @name Status Interface
0195  * @{
0196  */
0197 
0198 /*!
0199  * @brief Gets the PIT status flags.
0200  *
0201  * @param base    PIT peripheral base address
0202  * @param channel Timer channel number
0203  *
0204  * @return The status flags. This is the logical OR of members of the
0205  *         enumeration ::pit_status_flags_t
0206  */
0207 static inline uint32_t PIT_GetStatusFlags(PIT_Type *base, pit_chnl_t channel)
0208 {
0209     return (base->CHANNEL[channel].TFLG & PIT_TFLG_TIF_MASK);
0210 }
0211 
0212 /*!
0213  * @brief  Clears the PIT status flags.
0214  *
0215  * @param base    PIT peripheral base address
0216  * @param channel Timer channel number
0217  * @param mask    The status flags to clear. This is a logical OR of members of the
0218  *                enumeration ::pit_status_flags_t
0219  */
0220 static inline void PIT_ClearStatusFlags(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
0221 {
0222     base->CHANNEL[channel].TFLG = mask;
0223 }
0224 
0225 /*! @}*/
0226 
0227 /*!
0228  * @name Read and Write the timer period
0229  * @{
0230  */
0231 
0232 /*!
0233  * @brief Sets the timer period in units of count.
0234  *
0235  * Timers begin counting from the value set by this function until it reaches 0,
0236  * then it generates an interrupt and load this register value again.
0237  * Writing a new value to this register does not restart the timer. Instead, the value
0238  * is loaded after the timer expires.
0239  *
0240  * @note Users can call the utility macros provided in fsl_common.h to convert to ticks.
0241  *
0242  * @param base    PIT peripheral base address
0243  * @param channel Timer channel number
0244  * @param count   Timer period in units of ticks
0245  */
0246 static inline void PIT_SetTimerPeriod(PIT_Type *base, pit_chnl_t channel, uint32_t count)
0247 {
0248     assert(count != 0U);
0249     /* According to RM, the LDVAL trigger = clock ticks -1 */
0250     base->CHANNEL[channel].LDVAL = count - 1U;
0251 }
0252 
0253 /*!
0254  * @brief Reads the current timer counting value.
0255  *
0256  * This function returns the real-time timer counting value, in a range from 0 to a
0257  * timer period.
0258  *
0259  * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec.
0260  *
0261  * @param base    PIT peripheral base address
0262  * @param channel Timer channel number
0263  *
0264  * @return Current timer counting value in ticks
0265  */
0266 static inline uint32_t PIT_GetCurrentTimerCount(PIT_Type *base, pit_chnl_t channel)
0267 {
0268     return base->CHANNEL[channel].CVAL;
0269 }
0270 
0271 /*! @}*/
0272 
0273 /*!
0274  * @name Timer Start and Stop
0275  * @{
0276  */
0277 
0278 /*!
0279  * @brief Starts the timer counting.
0280  *
0281  * After calling this function, timers load period value, count down to 0 and
0282  * then load the respective start value again. Each time a timer reaches 0,
0283  * it generates a trigger pulse and sets the timeout interrupt flag.
0284  *
0285  * @param base    PIT peripheral base address
0286  * @param channel Timer channel number.
0287  */
0288 static inline void PIT_StartTimer(PIT_Type *base, pit_chnl_t channel)
0289 {
0290     base->CHANNEL[channel].TCTRL |= PIT_TCTRL_TEN_MASK;
0291 }
0292 
0293 /*!
0294  * @brief Stops the timer counting.
0295  *
0296  * This function stops every timer counting. Timers reload their periods
0297  * respectively after the next time they call the PIT_DRV_StartTimer.
0298  *
0299  * @param base    PIT peripheral base address
0300  * @param channel Timer channel number.
0301  */
0302 static inline void PIT_StopTimer(PIT_Type *base, pit_chnl_t channel)
0303 {
0304     base->CHANNEL[channel].TCTRL &= ~PIT_TCTRL_TEN_MASK;
0305 }
0306 
0307 /*! @}*/
0308 
0309 #if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
0310 
0311 /*!
0312  * @brief Reads the current lifetime counter value.
0313  *
0314  * The lifetime timer is a 64-bit timer which chains timer 0 and timer 1 together.
0315  * Timer 0 and 1 are chained by calling the PIT_SetTimerChainMode before using this timer.
0316  * The period of lifetime timer is equal to the "period of timer 0 * period of timer 1".
0317  * For the 64-bit value, the higher 32-bit has the value of timer 1, and the lower 32-bit
0318  * has the value of timer 0.
0319  *
0320  * @param base PIT peripheral base address
0321  *
0322  * @return Current lifetime timer value
0323  */
0324 uint64_t PIT_GetLifetimeTimerCount(PIT_Type *base);
0325 
0326 #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER */
0327 
0328 #if defined(__cplusplus)
0329 }
0330 #endif
0331 
0332 /*! @}*/
0333 
0334 #endif /* _FSL_PIT_H_ */