summaryrefslogtreecommitdiff
path: root/platform/drivers/inc/gpt.h
blob: 14c3b9e4c545b4216dad22cdfff561f08d8057c6 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form 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.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 __GPT_H__
#define __GPT_H__

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "device_imx.h"

/*!
 * @addtogroup gpt_driver
 * @{
 */

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

/*!
 * @brief Clock source
 */
enum _gpt_clock_source {
    gptClockSourceNone    = 0U,  /*!< No source selected.*/
    gptClockSourcePeriph  = 1U,  /*!< Use peripheral module clock.*/
    gptClockSourceLowFreq = 4U,  /*!< Use 32K clock.*/
    gptClockSourceOsc     = 5U   /*!< Use 24M OSC clock.*/
};

/*!
 * @brief Input capture channel number
 */
enum _gpt_input_capture_channel {
    gptInputCaptureChannel1 = 0U,
    gptInputCaptureChannel2 = 1U
};

/*!
 * @brief Input capture operation mode
 */
enum _gpt_input_operation_mode {
    gptInputOperationDisabled = 0U,  /*!< Don't capture.*/
    gptInputOperationRiseEdge = 1U,  /*!< Capture on rising edge of input pin.*/
    gptInputOperationFallEdge = 2U,  /*!< Capture on falling edge of input pin.*/
    gptInputOperationBothEdge = 3U   /*!< Capture on both edges of input pin.*/
};

/*!
 * @brief Output compare channel number
 */
enum _gpt_output_compare_channel {
    gptOutputCompareChannel1 = 0U,
    gptOutputCompareChannel2 = 1U,
    gptOutputCompareChannel3 = 2U
};

/*!
 * @brief Output compare operation mode
 */
enum _gpt_output_operation_mode {
    gptOutputOperationDisconnected = 0U,  /*!< Don't change output pin.*/
    gptOutputOperationToggle       = 1U,  /*!< Toggle output pin.*/
    gptOutputOperationClear        = 2U,  /*!< Set output pin low.*/
    gptOutputOperationSet          = 3U,  /*!< Set output pin high.*/
    gptOutputOperationActivelow    = 4U   /*!< Generate a active low pulse on output pin.*/
};

/*!
 * @brief Status flag
 */
enum _gpt_status_flag {
    gptStatusFlagOutputCompare1    = 1U << 0,  /*!< Output compare channel 1 evevnt.*/
    gptStatusFlagOutputCompare2    = 1U << 1,  /*!< Output compare channel 2 evevnt.*/
    gptStatusFlagOutputCompare3    = 1U << 2,  /*!< Output compare channel 3 evevnt.*/
    gptStatusFlagInputCapture1     = 1U << 3,  /*!< Capture channel 1 evevnt.*/
    gptStatusFlagInputCapture2     = 1U << 4,  /*!< Capture channel 2 evevnt.*/
    gptStatusFlagRollOver          = 1U << 5   /*!< Counter reaches maximum value and rolled over to 0 evevnt.*/
};

/*!
 * @brief Structure to configure the running mode.
 */
typedef struct GptModeConfig
{
    bool freeRun;     /*!< true: FreeRun mode, false: Restart mode */
    bool waitEnable;  /*!< GPT enabled in wait mode */
    bool stopEnable;  /*!< GPT enabled in stop mode */
    bool dozeEnable;  /*!< GPT enabled in doze mode */
    bool dbgEnable;   /*!< GPT enabled in debug mode */
    bool enableMode;  /*!< true: counter reset to 0 when enabled, false: counter retain its value when enabled */
} gpt_mode_config_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name GPT State Control
 * @{
 */

/*!
 * @brief Initialize GPT to reset state and initialize running mode
 *
 * @param base GPT base pointer.
 * @param config GPT mode setting configuration.
 */
void GPT_Init(GPT_Type * base, gpt_mode_config_t *config);

/*!
 * @brief Software reset of GPT module
 *
 * @param base GPT base pointer.
 */
static inline void GPT_SoftReset(GPT_Type * base)
{
    base->CR |= GPT_CR_SWR_MASK;
    /* Wait reset finished */
    while (base->CR & GPT_CR_SWR_MASK) { }
}

/*!
 * @brief Set clock source of GPT
 *
 * @param base GPT base pointer.
 * @param source clock source (see _gpt_clock_source)
 */
void GPT_SetClockSource(GPT_Type * base, uint32_t source);

/*!
 * @brief Get clock source of GPT
 *
 * @param base GPT base pointer.
 * @return clock source (see _gpt_clock_source)
 */
static inline uint32_t GPT_GetClockSource(GPT_Type * base)
{
    return (base->CR & GPT_CR_CLKSRC_MASK) >> GPT_CR_CLKSRC_SHIFT;
}

/*!
 * @brief Set pre scaler of GPT
 *
 * @param base GPT base pointer.
 * @param prescaler pre scaler of GPT (0-4095, divider=prescaler+1)
 */
static inline void GPT_SetPrescaler(GPT_Type * base, uint32_t prescaler)
{
    assert(prescaler <= GPT_PR_PRESCALER_MASK);
    base->PR = (base->PR & ~GPT_PR_PRESCALER_MASK) | GPT_PR_PRESCALER(prescaler);
}

/*!
 * @brief Get pre scaler of GPT
 *
 * @param base GPT base pointer.
 * @return pre scaler of GPT (0-4095)
 */
static inline uint32_t GPT_GetPrescaler(GPT_Type * base)
{
    return (base->PR & GPT_PR_PRESCALER_MASK) >> GPT_PR_PRESCALER_SHIFT;
}

/*!
 * @brief OSC 24M pre scaler before selected by clock source
 *
 * @param base GPT base pointer.
 * @param prescaler OSC pre scaler(0-15, divider=prescaler+1)
 */
static inline void GPT_SetOscPrescaler(GPT_Type * base, uint32_t prescaler)
{
    assert(prescaler <= (GPT_PR_PRESCALER24M_MASK >> GPT_PR_PRESCALER24M_SHIFT));
    base->PR = (base->PR & ~GPT_PR_PRESCALER24M_MASK) | GPT_PR_PRESCALER24M(prescaler);
}

/*!
 * @brief Get pre scaler of GPT
 *
 * @param base GPT base pointer.
 * @return OSC pre scaler of GPT (0-15)
 */
static inline uint32_t GPT_GetOscPrescaler(GPT_Type * base)
{
    return (base->PR & GPT_PR_PRESCALER24M_MASK) >> GPT_PR_PRESCALER24M_SHIFT;
}

/*!
 * @brief Enable GPT module
 *
 * @param base GPT base pointer.
 */
static inline void GPT_Enable(GPT_Type * base)
{
    base->CR |= GPT_CR_EN_MASK;
}

/*!
 * @brief Disable GPT module
 *
 * @param base GPT base pointer.
 */
static inline void GPT_Disable(GPT_Type * base)
{
    base->CR &= ~GPT_CR_EN_MASK;
}

/*!
 * @brief Get GPT counter value
 *
 * @param base GPT base pointer.
 * @return GPT counter value
 */
static inline uint32_t GPT_ReadCounter(GPT_Type * base)
{
    return base->CNT;
}

/*@}*/

/*!
 * @name GPT Input/Output Signal Control
 * @{
 */

/*!
 * @brief Set GPT operation mode of input capture channel
 *
 * @param base GPT base pointer.
 * @param channel GPT capture channel (see _gpt_input_capture_channel).
 * @param mode GPT input capture operation mode (see _gpt_input_operation_mode).
 */
static inline void GPT_SetInputOperationMode(GPT_Type * base, uint32_t channel, uint32_t mode)
{
    assert (channel <= gptInputCaptureChannel2);
    base->CR = (base->CR & ~(GPT_CR_IM1_MASK << (channel * 2))) | (GPT_CR_IM1(mode) << (channel * 2));
}

/*!
 * @brief Get GPT operation mode of input capture channel
 *
 * @param base GPT base pointer.
 * @param channel GPT capture channel (see _gpt_input_capture_channel).
 * @return GPT input capture operation mode (see _gpt_input_operation_mode).
 */
static inline uint32_t GPT_GetInputOperationMode(GPT_Type * base, uint32_t channel)
{
    assert (channel <= gptInputCaptureChannel2);
    return (base->CR >> (GPT_CR_IM1_SHIFT + channel * 2)) & (GPT_CR_IM1_MASK >> GPT_CR_IM1_SHIFT);
}

/*!
 * @brief Get GPT input capture value of certain channel
 *
 * @param base GPT base pointer.
 * @param channel GPT capture channel (see _gpt_input_capture_channel).
 * @return GPT input capture value
 */
static inline uint32_t GPT_GetInputCaptureValue(GPT_Type * base, uint32_t channel)
{
    assert (channel <= gptInputCaptureChannel2);
    return *(&base->ICR1 + channel);
}

/*!
 * @brief Set GPT operation mode of output compare channel
 *
 * @param base GPT base pointer.
 * @param channel GPT output compare channel (see _gpt_output_compare_channel).
 * @param mode GPT output operation mode (see _gpt_output_operation_mode).
 */
static inline void GPT_SetOutputOperationMode(GPT_Type * base, uint32_t channel, uint32_t mode)
{
    assert (channel <= gptOutputCompareChannel3);
    base->CR = (base->CR & ~(GPT_CR_OM1_MASK << (channel * 3))) | (GPT_CR_OM1(mode) << (channel * 3));
}

/*!
 * @brief Get GPT operation mode of output compare channel
 *
 * @param base GPT base pointer.
 * @param channel GPT output compare channel (see _gpt_output_compare_channel).
 * @return GPT output operation mode (see _gpt_output_operation_mode).
 */
static inline uint32_t GPT_GetOutputOperationMode(GPT_Type * base, uint32_t channel)
{
    assert (channel <= gptOutputCompareChannel3);
    return (base->CR >> (GPT_CR_OM1_SHIFT + channel * 3)) & (GPT_CR_OM1_MASK >> GPT_CR_OM1_SHIFT);
}

/*!
 * @brief Set GPT output compare value of output compare channel
 *
 * @param base GPT base pointer.
 * @param channel GPT output compare channel (see _gpt_output_compare_channel).
 * @param value GPT output compare value
 */
static inline void GPT_SetOutputCompareValue(GPT_Type * base, uint32_t channel, uint32_t value)
{
    assert (channel <= gptOutputCompareChannel3);
    *(&base->OCR1 + channel) = value;
}

/*!
 * @brief Get GPT output compare value of output compare channel
 *
 * @param base GPT base pointer.
 * @param channel GPT output compare channel (see _gpt_output_compare_channel).
 * @return GPT output compare value
 */
static inline uint32_t GPT_GetOutputCompareValue(GPT_Type * base, uint32_t channel)
{
    assert (channel <= gptOutputCompareChannel3);
    return *(&base->OCR1 + channel);
}

/*!
 * @brief Force GPT output action on output compare channel, ignoring comparator.
 *
 * @param base GPT base pointer.
 * @param channel GPT output compare channel (see _gpt_output_compare_channel).
 */
static inline void GPT_ForceOutput(GPT_Type * base, uint32_t channel)
{
    assert (channel <= gptOutputCompareChannel3);
    base->CR |= (GPT_CR_FO1_MASK << channel);
}

/*@}*/

/*!
 * @name GPT Interupt and Status Control
 * @{
 */

/*!
 * @brief Get GPT status flag.
 *
 * @param base GPT base pointer.
 * @param flags GPT status flag mask (see _gpt_status_flag for bit definition).
 * @return GPT status, each bit represents one status flag
 */
static inline uint32_t GPT_GetStatusFlag(GPT_Type * base, uint32_t flags)
{
    return base->SR & flags;
}

/*!
 * @brief Clear one or more GPT status flag.
 *
 * @param base GPT base pointer.
 * @param flags GPT status flag mask (see _gpt_status_flag for bit definition).
 */
static inline void GPT_ClearStatusFlag(GPT_Type * base, uint32_t flags)
{
    base->SR = flags;
}

/*!
 * @brief Enable or disable GPT interrupts.
 *
 * @param base GPT base pointer.
 * @param flags GPT status flag mask (see _gpt_status_flag for bit definition).
 * @param enable Interrupt enable (true: enable, false: disable).
 */
void GPT_SetIntCmd(GPT_Type * base, uint32_t flags, bool enable);

/*@}*/

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* __GPT_H__ */
/*******************************************************************************
 * EOF
 ******************************************************************************/