aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/system/src/hwtimer/fsl_hwtimer.c
blob: 72daf1aff83b7bed10953284bb04095641757fe5 (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
411
412
413
414
415
416
417
/*
 * Copyright (c) 2013 - 2014, 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.
 */

#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "fsl_hwtimer.h"

/*******************************************************************************
 * Internal type definition
 ******************************************************************************/
/*******************************************************************************
 * Internal Variables
 ******************************************************************************/
 /*******************************************************************************
 * Internal Code
 ******************************************************************************/
 /*******************************************************************************
 * Code
 ******************************************************************************/
/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_Init
 * Description   : The device interface pointer determines low layer driver to
 * be used. Device interface structure is exported by each low layer driver and
 * is opaque to the applications. Please refer to chapter concerning low layer
 * driver below for details. Meaning of the numerical identifier varies depending
 * on low layer driver used. Typically, it identifies particular timer channel to
 * initialize. The initialization function has to be called prior using any other
 * API function.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_Init(hwtimer_t *hwtimer, const hwtimer_devif_t * kDevif, uint32_t id, void *data)
 {
    /* Check input parameters */
    if ((hwtimer == NULL) || (kDevif == NULL))
    {
        return kHwtimerInvalidInput;
    }

    assert(NULL != kDevif->init);

    /* Initialize hwtimer structure */
    hwtimer->devif   = kDevif;
    hwtimer->ticks   = 0U;
    hwtimer->divider = 0U;
    hwtimer->modulo  = 0U;
    hwtimer->callbackFunc = NULL;
    hwtimer->callbackData = NULL;
    hwtimer->callbackPending = 0U;
    hwtimer->callbackBlocked = 0U;

    /* Call low level driver init function. */
    return hwtimer->devif->init(hwtimer, id, data);
 }

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_Deinit
 * Description   : Calls lower layer stop function to stop timer, then calls low
 * layer de-initialization function and afterwards invalidates hwtimer structure
 * by clearing it.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_Deinit(hwtimer_t *hwtimer)
{
    _hwtimer_error_code_t result;

    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }
    if (NULL == hwtimer->devif)
    {
        return kHwtimerInvalidPointer;
    }
    /* Stop timer if runs */
    assert(NULL != hwtimer->devif->stop);
    result = hwtimer->devif->stop(hwtimer);
    if (kHwtimerSuccess != result)
    {
        return result;
    }

    /* De-initialize timer. */
    assert(NULL != hwtimer->devif->deinit);
    result = hwtimer->devif->deinit(hwtimer);
    if (kHwtimerSuccess != result)
    {
        return result;
    }

    hwtimer->devif      = NULL;
    hwtimer->clockFreq  = 0U;
    hwtimer->ticks      = 0U;
    hwtimer->divider    = 0U;
    hwtimer->modulo     = 0U;
    hwtimer->callbackFunc = NULL;
    hwtimer->callbackData = NULL;
    hwtimer->callbackPending = 0U;
    hwtimer->callbackBlocked = 0U;

    return kHwtimerSuccess;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_SetPeriod
 * Description   : The function provides an alternate way to set up the timer to
 * desired period specified in microseconds rather than to frequency in Hz.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_SetPeriod(hwtimer_t *hwtimer, uint32_t period)
{
    /* Check input parameters */
    if ((NULL == hwtimer) || (0U == period))
    {
        return kHwtimerInvalidInput;
    }
    if (NULL == hwtimer->devif)
    {
        return kHwtimerInvalidPointer;
    }

    assert(NULL != hwtimer->devif->setDiv);
    return hwtimer->devif->setDiv(hwtimer, period);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_GetPeriod
 * Description   : The function returns current period of the timer in
 * microseconds calculated from the base frequency and actual divider settings
 * of the timer.
 *
 *END**************************************************************************/
uint32_t HWTIMER_SYS_GetPeriod(hwtimer_t *hwtimer)
{
    uint32_t period;

    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return 0U;
    }

    /* Obtain clock source clock frequency.*/
    if (hwtimer->clockFreq == 0U)
    {
        return 0U;
    }

    assert(hwtimer->divider <= hwtimer->clockFreq);
    /* Divider is always less than clockFreq */
    period = ((uint64_t)1000000U * hwtimer->divider) / hwtimer->clockFreq;

    return period;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_Start
 * Description   : Enables the timer and leaves it running. The timer starts
 * counting a new period generating interrupts every time the timer rolls over.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_Start(hwtimer_t *hwtimer)
{
    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }
    if (NULL == hwtimer->devif)
    {
        return kHwtimerInvalidPointer;
    }
    /* Start timer */
    assert(NULL != hwtimer->devif->start);
    return hwtimer->devif->start(hwtimer);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_Stop
 * Description   : The timer stops counting after this function is called.
 * Pending interrupts and callbacks are cancelled.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_Stop(hwtimer_t *hwtimer)
{
    _hwtimer_error_code_t result;

    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }
    if (NULL == hwtimer->devif)
    {
        return kHwtimerInvalidPointer;
    }

    assert(NULL != hwtimer->devif->stop);
    result = hwtimer->devif->stop(hwtimer);
    hwtimer->callbackPending = 0U;

    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_GetModulo
 * Description   : The function returns period of the timer in sub-ticks. It is
 * typically called after HWTIMER_SYS_SetPeriod() to obtain actual resolution
 * of the timer in the current configuration.
 *
 *END**************************************************************************/
uint32_t HWTIMER_SYS_GetModulo(hwtimer_t *hwtimer)
{
    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return 0U;
    }

    return hwtimer->modulo;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_GetTime
 * Description   : The function reads the current value of the hwtimer. Elapsed
 * periods(ticks) and current value of the timer counter (sub-ticks) are filled
 * into the Hwtimer_time structure. The sub-ticks number always counts up and is
 * reset to zero when the timer overflows regardless of the counting direction
 * of the underlying device.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_GetTime(hwtimer_t *hwtimer, hwtimer_time_t *time)
{
    /* Check input parameters */
    if ((NULL == hwtimer) || (NULL == time))
    {
        return kHwtimerInvalidInput;
    }
    if (NULL == hwtimer->devif)
    {
        return kHwtimerInvalidPointer;
    }

    assert(NULL != hwtimer->devif->getTime);
    return hwtimer->devif->getTime(hwtimer, time);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_GetTicks
 * Description   : The function reads the current value of the hwtimer.
 * The returned value corresponds with lower 32 bits of elapsed periods (ticks).
 * The value is guaranteed to be obtained atomically without necessity to mask
 * timer interrupt. Lower layer driver is not involved at all, thus call to this
 * function is considerably faster than HWTIMER_SYS_GetTime.
 *
 *END**************************************************************************/
uint32_t HWTIMER_SYS_GetTicks(hwtimer_t *hwtimer)
{
    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return 0U;
    }

    /* return lower 32b of 64 bit value */
    return (uint32_t)hwtimer->ticks;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_RegisterCallback
 * Description   : Registers function to be called when the timer expires.
 * The callback_data is arbitrary pointer passed as parameter to the callback
 * function.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_RegisterCallback(hwtimer_t *hwtimer, hwtimer_callback_t callbackFunc, void *callbackData)
{
    hwtimer_t volatile *hwtimerVol;

    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }

    hwtimerVol = hwtimer;
    /* Volatile used to prevent optimization of following lines. Interrupt may happen meanwhile. */
    hwtimerVol->callbackFunc = NULL; /* Prevent callback execution with old data */
    hwtimerVol->callbackPending = 0U;
    hwtimerVol->callbackData = callbackData;
    hwtimerVol->callbackFunc = callbackFunc;

    return kHwtimerSuccess;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_BlockCallback
 * Description   : The function is used to block callbacks in circumstances when
 * execution of the callback function is undesired. If the timer overflows when
 * callbacks are blocked the callback becomes pending.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_BlockCallback(hwtimer_t *hwtimer)
{
    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }

    hwtimer->callbackBlocked = 1U;

    return kHwtimerSuccess;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_UnblockCallback
 * Description   : The function is used to unblock previously blocked callbacks.
 * If there is a callback pending, it gets immediately executed. This function
 * must not be called from a callback routine (it does not make sense to do so
 * anyway as callback function never gets executed while callbacks are blocked).
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_UnblockCallback(hwtimer_t *hwtimer)
{
    hwtimer_callback_t callbackFunc;
    hwtimer_t volatile *hwtimerVol = hwtimer;

    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }

    /* Unblock callbacks in ISR. No more pending request could arrive after this. */
    hwtimerVol->callbackBlocked = 0U;
    /* Check for any previously set pending requests during blocked state */
    if (hwtimerVol->callbackPending)
    {
        callbackFunc = hwtimerVol->callbackFunc;
        if (NULL != callbackFunc)
        {
            /* Prevent invocation of callback from ISR (callback may not be re-entrant) */
            hwtimerVol->callbackFunc = NULL;
            callbackFunc(hwtimerVol->callbackData);
            /* Allow invocation of callback from ISR */
            hwtimerVol->callbackFunc = callbackFunc;
        }
        /* Clear pending flag, callback just serviced */
        hwtimerVol->callbackPending = 0U;
    }

    return kHwtimerSuccess;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : HWTIMER_SYS_CancelCallback
 * Description   : The function cancels pending callback, if any.
 *
 *END**************************************************************************/
_hwtimer_error_code_t HWTIMER_SYS_CancelCallback(hwtimer_t *hwtimer)
{
    /* Check input parameters */
    if (NULL == hwtimer)
    {
        return kHwtimerInvalidInput;
    }

    hwtimer->callbackPending = 0U;

    return kHwtimerSuccess;
}

/*******************************************************************************
 * EOF
 ******************************************************************************/