/* * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and * its subsidiaries and affiliates (collectly called MKSEMI). * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into an MKSEMI * integrated circuit in a product or a software update for such product, * must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of MKSEMI nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * 4. This software, with or without modification, must only be used with a * MKSEMI integrated circuit. * * 5. Any software provided in binary form under this license must not be * reverse engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef MK_DUAL_TIMER_H_ #define MK_DUAL_TIMER_H_ #include "mk_common.h" #ifndef TIMER2_INT_MODE_EN #define TIMER2_INT_MODE_EN (1) #endif #ifndef TIMER3_INT_MODE_EN #define TIMER3_INT_MODE_EN (1) #endif /** * @addtogroup MK8000_Dual_Timer * @{ */ /** * @brief DUAL TIMER device IDs enumeration */ enum DUAL_TIMER_DEV_T { DUAL_TIMER_ID0 = 0, DUAL_TIMER_ID1 = 1, DUAL_TIMER_MAX_NUM }; /** * @brief DUAL TIMER mode * */ enum DUAL_TIMER_TYPE_T { DUAL_TIMER_TYPE_FREERUNNING = 0, /*!< The counter operates continuously and wraps around to its maximum value each time that it reaches zero. */ DUAL_TIMER_TYPE_ONESHOT, /*!< The counter is loaded with a new value by writing to the Load Register. The counter decrements to zero and then halts until it is reprogrammed. */ DUAL_TIMER_TYPE_PERIODIC /*!< The counter operates continuously by reloading from the Load Register each time that the counter reaches zero. */ }; /** * @brief DUAL TIMER clock prescaler * */ enum DUAL_TIMER_PRESCALE_T { DUAL_TIMER_PRESCALE_DIV1 = 0, /*!< Clock is divided by 1 */ DUAL_TIMER_PRESCALE_DIV16, /*!< Clock is divided by 16 */ DUAL_TIMER_PRESCALE_DIV256, /*!< Clock is divided by 256 */ }; /** * @brief Setting the counter bit width. * */ enum DUAL_TIMER_SIZE_T { DUAL_TIMER_SIZE_16BIT = 0, /*!< 16bit width Free-running mode : Interval = (PerscleDiv/TimerClk(freq)) x 2^16 Periodic or One-shot mode : Interval = (PerscleDiv/TimerClk(freq)) x LoadValue */ DUAL_TIMER_SIZE_32BIT /*!< 32bit width Free-running mode : Interval = PerscleDiv/TimerClk(freq) x 2^32 Periodic or One-shot mode : Interval = (PerscleDiv/TimerClk(freq)) x LoadValue */ }; /** * @brief DUAL TIMER configure Structure * @note The minimum valid value for load is 1. * If load is set to 0 then an interrupt is generated immediately. */ struct DUAL_TIMER_CFG_T { enum DUAL_TIMER_TYPE_T type; /*!< Specifies the DUAL TIMER mode. This parameter can be a value of @ref DUAL_TIMER_TYPE_T */ enum DUAL_TIMER_PRESCALE_T prescale; /*!< Specifies the prescaler value used to divide the DUAL TIMER clock. This parameter can be a value of @ref DUAL_TIMER_PRESCALE_T */ enum DUAL_TIMER_SIZE_T width; /*!< Specifies the counter bit width. This parameter can be a value of @ref DUAL_TIMER_SIZE_T */ uint32_t load; /*!< Specifies the reload value, this is the value used to reload the counter. */ uint32_t int_en; /*!< Specifies whether the interrupt is enabled or disabled. This parameter will be one of the following values: @arg true is enable @arg false is disable */ drv_callback_t callback; /*!< Callback function provided by the user */ }; /** * @brief DUAL TIMER handle Structure * */ struct DUAL_TIMER_HANDLE_T { DUAL_TIMER_TypeDef *const base; /*!< DUAL TIMERx registers base address */ const IRQn_Type irq; /*!< DUAL TIMERx interrupt number */ uint32_t int_en; /*!< Specifies whether the interrupt is enabled or disabled This parameter will be one of the following values: @arg true is enable @arg false is disable */ drv_callback_t callback; /*!< Callback function provided by the user */ }; #ifdef __cplusplus extern "C" { #endif /** * @brief Function for initializing the DUAL TIMERx. * * @param[in] id Dual timer ID * @param[in] config Pointer to a DUAL_TIMER_CFG_T structure that contains the configuration information for DUAL TIMERx. * @return * @arg DRV_BUSY error id * @arg DEV_OK succeed */ int dual_timer_open(enum DUAL_TIMER_DEV_T id, struct DUAL_TIMER_CFG_T *config); /** * @brief Function for uninitializing the DUAL TIMERx. * * @param[in] id Dual timer ID * @return * @arg DRV_BUSY error id * @arg DEV_OK succeed */ int dual_timer_close(enum DUAL_TIMER_DEV_T id); /** * @brief Start the DUAL TIMERx. * * @param[in] id Dual timer ID * @param[in] start Start count value of the timer */ void dual_timer_start(enum DUAL_TIMER_DEV_T id, uint32_t start); /** * @brief Stop the DUAL TIMERx. * * @param[in] id Dual timer ID */ void dual_timer_stop(enum DUAL_TIMER_DEV_T id); /** * @brief Founction for resetting DUAL-TIMERS * */ void dual_timer_reset(void); /** * @brief Founction for updating periodic counter value. * @note This founction used to reload the counter when Periodic mode is enabled, and the current count reaches zero. * @param[in] id Dual timer ID * @param[in] count This parameter is used to set DUAL TIMERx background reload register. */ void dual_timer_set(enum DUAL_TIMER_DEV_T id, uint32_t count); /** * @brief Founction for getting the current value of the decrementing counter. * * @param[in] id Dual timer ID * @return Current value of the decrementing counter */ uint32_t dual_timer_get(enum DUAL_TIMER_DEV_T id); /** * @brief DUAL TIMERx work at One-shot mode, usually disable interrupt * * @param[in] id Dual timer ID * @param[in] count This parameter is used to set DUAL TIMERx reload register. */ void dual_timer_delay(enum DUAL_TIMER_DEV_T id, uint32_t count); /** * @brief Function for Interrupt handler for DUAL TIMER0. */ void TIMER2_IRQHandler(void); /** * @brief Function for Interrupt handler for DUAL TIMER1. */ void TIMER3_IRQHandler(void); #ifdef __cplusplus } #endif /** * @} */ #endif /* MK_DUAL_TIMER_H_ */