WXK
2025-02-11 e328ebef585cea2351b37117b2d5ac4978ecd3c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * 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
 */
#if defined(__ICCARM__)
uint32_t dual_timer_get(enum DUAL_TIMER_DEV_T id);
#else
uint32_t RAM_FUNC dual_timer_get(enum DUAL_TIMER_DEV_T id);
#endif
 
/**
 * @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_ */