summaryrefslogtreecommitdiff
path: root/bsp/maxim/libraries/MAX32660PeriphDriver/Include/tmr.h
blob: c47fd121c19d3b547df630e0b2a0d2f5c363e3b9 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
 * @file    tmr.h
 * @brief   Timer (TMR) function prototypes and data types.
 */

/* ****************************************************************************
 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *
 * $Date: 2019-09-11 14:32:22 -0500 (Wed, 11 Sep 2019) $
 * $Revision: 46047 $
 *
 *************************************************************************** */

/* Define to prevent redundant inclusion */
#ifndef _TMR_H_
#define _TMR_H_

/* **** Includes **** */
#include "mxc_config.h"
#include "tmr_regs.h"
#include "mxc_sys.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup tmr Timer (TMR)
 * @ingroup periphlibs
 * @{
 */

/**
 * @brief Timer prescaler values
 */
typedef enum {
    TMR_PRES_1      = MXC_S_TMR_CN_PRES_DIV1,           /// Divide input clock by 1
    TMR_PRES_2      = MXC_S_TMR_CN_PRES_DIV2,           /// Divide input clock by 2
    TMR_PRES_4      = MXC_S_TMR_CN_PRES_DIV4,           /// Divide input clock by 4
    TMR_PRES_8      = MXC_S_TMR_CN_PRES_DIV8,           /// Divide input clock by 8
    TMR_PRES_16     = MXC_S_TMR_CN_PRES_DIV16,          /// Divide input clock by 16
    TMR_PRES_32     = MXC_S_TMR_CN_PRES_DIV32,          /// Divide input clock by 32
    TMR_PRES_64     = MXC_S_TMR_CN_PRES_DIV64,          /// Divide input clock by 64
    TMR_PRES_128    = MXC_S_TMR_CN_PRES_DIV128,         /// Divide input clock by 128
    TMR_PRES_256    = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV1,  /// Divide input clock by 256
    TMR_PRES_512    = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV2,  /// Divide input clock by 512
    TMR_PRES_1024   = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV4,  /// Divide input clock by 1024
    TMR_PRES_2048   = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV8,  /// Divide input clock by 2048
    TMR_PRES_4096   = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV16  /// Divide input clock by 4096
} tmr_pres_t;

/**
 * @brief Timer modes
 */
typedef enum {
    TMR_MODE_ONESHOT          = MXC_V_TMR_CN_TMODE_ONESHOT,       /// Timer Mode ONESHOT
    TMR_MODE_CONTINUOUS       = MXC_V_TMR_CN_TMODE_CONTINUOUS,    /// Timer Mode CONTINUOUS
    TMR_MODE_COUNTER          = MXC_V_TMR_CN_TMODE_COUNTER,       /// Timer Mode COUNTER
    TMR_MODE_PWM              = MXC_V_TMR_CN_TMODE_PWM,           /// Timer Mode PWM
    TMR_MODE_CAPTURE          = MXC_V_TMR_CN_TMODE_CAPTURE,       /// Timer Mode CAPTURE
    TMR_MODE_COMPARE          = MXC_V_TMR_CN_TMODE_COMPARE,       /// Timer Mode COMPARE
    TMR_MODE_GATED            = MXC_V_TMR_CN_TMODE_GATED,         /// Timer Mode GATED
    TMR_MODE_CAPTURE_COMPARE  = MXC_V_TMR_CN_TMODE_CAPTURECOMPARE /// Timer Mode CAPTURECOMPARE
} tmr_mode_t;

/**
 * @brief Timer units of time enumeration
 */
typedef enum {
    TMR_UNIT_NANOSEC = 0,       /**< Nanosecond Unit Indicator. */
    TMR_UNIT_MICROSEC,          /**< Microsecond Unit Indicator. */
    TMR_UNIT_MILLISEC,          /**< Millisecond Unit Indicator. */
    TMR_UNIT_SEC,               /**< Second Unit Indicator. */
} tmr_unit_t;

/**
 * @brief Timer Configuration
 */
typedef struct {
    tmr_mode_t mode;    /// Desired timer mode
    uint32_t cmp_cnt;   /// Compare register value in timer ticks
    unsigned pol;       /// Polarity (0 or 1)
} tmr_cfg_t;

/**
 * @brief Timer PWM Configuration
 */
typedef struct {
    unsigned pol;       /// PWM polarity (0 or 1)
    uint32_t per_cnt;   /// PWM period in timer ticks
    uint32_t duty_cnt;  /// PWM duty in timer ticks
} tmr_pwm_cfg_t;

/* **** Definitions **** */

/* **** Function Prototypes **** */

/**
 * @brief      Initialize timer module clock.
 * @param      tmr        Pointer to timer module to initialize.
 * @param      pres       Prescaler value.
 * @param      sys_cfg    System configuration object
 * @return     #E_NO_ERROR if successful, error code otherwise.
 */
int TMR_Init(mxc_tmr_regs_t *tmr, tmr_pres_t pres, const sys_cfg_tmr_t* sys_cfg);

/**
 * @brief      Shutdown timer module clock.
 * @param      tmr  Pointer to timer module to initialize.
 * @return     #E_NO_ERROR if successful, error code otherwise.
 */
int TMR_Shutdown(mxc_tmr_regs_t *tmr);

/**
 * @brief      Enable the timer.
 * @param      tmr  Pointer to timer module to initialize.
 */
void TMR_Enable(mxc_tmr_regs_t* tmr);

/**
 * @brief      Disable the timer.
 * @param      tmr  Pointer to timer module to initialize.
 */
void TMR_Disable(mxc_tmr_regs_t* tmr);

/**
 * @brief      Configure the timer.
 * @param      tmr  Pointer to timer module to initialize.
 * @param      cfg  Pointer to timer configuration struct.
 * @return     #E_NO_ERROR if successful.
 */
int TMR_Config(mxc_tmr_regs_t *tmr, const tmr_cfg_t *cfg);

/**
 * @brief   Configure the timer for PWM operation.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   cfg     Pointer to timer PWM configuration struct.
 * @note    Can cause a glitch if the Timer is currently running.
 * @return  #E_BAD_PARAM if duty_cnt > per_cnt.
 */
int TMR_PWMConfig(mxc_tmr_regs_t *tmr, const tmr_pwm_cfg_t *cfg);

/**
 * @brief   Set the timer duty cycle.
 * @param   tmr     Pointer to timer module to initialize
 * @param   duty    New duty cycle count
 * @note    Will block until safe to change the duty count.
 * @return  #E_BAD_PARAM if duty_cnt > per_cnt.
 */
int TMR_PWMSetDuty(mxc_tmr_regs_t *tmr, uint32_t duty);

/**
 * @brief   Set the timer period.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   per     New period count.
 * @note    Will block until safe to change the period count.
 * @return  #E_BAD_PARAM if duty_cnt > per_cnt.
 */
int TMR_PWMSetPeriod(mxc_tmr_regs_t* tmr, uint32_t per);

/**
 * @brief   Get the timer compare count.
 * @param   tmr     Pointer to timer module to initialize.
 * @return  Returns the current compare count.
 */
uint32_t TMR_GetCompare(mxc_tmr_regs_t* tmr);

/**
 * @brief   Get the timer capture count.
 * @param   tmr     Pointer to timer module to initialize.
 * @return  Returns the most recent capture count.
 */
uint32_t TMR_GetCapture(mxc_tmr_regs_t* tmr);

/**
 * @brief   Get the timer count.
 * @param   tmr     Pointer to timer module to initialize.
 * @return  Returns the current count.
 */
uint32_t TMR_GetCount(mxc_tmr_regs_t* tmr);

/**
 * @brief   Clear the timer interrupt.
 * @param   tmr     Pointer to timer module to initialize.
 */
void TMR_IntClear(mxc_tmr_regs_t* tmr);

/**
 * @brief   Get the timer interrupt status.
 * @param   tmr     Pointer to timer module to initialize.
 * @return  Returns the interrupt status. 1 if interrupt has occurred.
 */
uint32_t TMR_IntStatus(mxc_tmr_regs_t* tmr);

/**
 * @brief   Set the timer compare count.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   cmp_cnt New compare count.
 * @note    This function does not protect against output glitches in PWM mode.
 *          Use TMR_PWMSetPeriod when in PWM mode.
 */
void TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);

/**
 * @brief   Set the timer count.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   cnt     New count.
 */
void TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);

/**
 * @brief   Convert real time to timer ticks.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   time    Number of units of time.
 * @param   units   Which units of time you want to convert.
 * @param   ticks   Pointer to store the number of ticks calculated.
 * @return  #E_NO_ERROR if successful, error code otherwise.
 */
int TMR_GetTicks(mxc_tmr_regs_t *tmr, uint32_t time, tmr_unit_t units, uint32_t *ticks);

/**
 * @brief   Convert timer ticks to real time.
 * @param   tmr     Pointer to timer module to initialize.
 * @param   ticks   Number of ticks.
 * @param   time    Pointer to store number of units of time.
 * @param   units   Pointer to store the units that time represents.
 * @return  #E_NO_ERROR if successful, error code otherwise.
 */
int TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, tmr_unit_t *units);

/**@} end of group tmr */

#ifdef __cplusplus
}
#endif

#endif /* _TMR_H_ */