aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/system/src/hwtimer/fsl_hwtimer_systick.c
blob: fba842cbeae204396f51ae98f8df9cd0be86a3cd (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
/*
 * 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"
#include "fsl_hwtimer_systick.h"
#include "fsl_os_abstraction.h"
#include "fsl_interrupt_manager.h"
#include "fsl_clock_manager.h"

/*******************************************************************************
 * Internal type definition
 ******************************************************************************/
/*******************************************************************************
 * Internal Variables
 ******************************************************************************/
void HWTIMER_SYS_SystickIsrAction(void);
static _hwtimer_error_code_t HWTIMER_SYS_SystickInit(hwtimer_t *hwtimer, uint32_t systickId, void *data);
static _hwtimer_error_code_t HWTIMER_SYS_SystickDeinit(hwtimer_t *hwtimer);
static _hwtimer_error_code_t HWTIMER_SYS_SystickSetDiv(hwtimer_t *hwtimer, uint32_t period);
static _hwtimer_error_code_t HWTIMER_SYS_SystickStart(hwtimer_t *hwtimer);
static _hwtimer_error_code_t HWTIMER_SYS_SystickStop(hwtimer_t *hwtimer);
static _hwtimer_error_code_t HWTIMER_SYS_SystickGetTime(hwtimer_t *hwtimer, hwtimer_time_t *time);

const hwtimer_devif_t kSystickDevif =
{
    HWTIMER_SYS_SystickInit,
    HWTIMER_SYS_SystickDeinit,
    HWTIMER_SYS_SystickSetDiv,
    HWTIMER_SYS_SystickStart,
    HWTIMER_SYS_SystickStop,
    HWTIMER_SYS_SystickGetTime,
};

static hwtimer_t *g_hwtimersSystick = NULL;


 /*******************************************************************************
 * Internal Code
 ******************************************************************************/

/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Called from the Interrupt service routine.
 *
 * This ISR is used when SysTick counted to 0.
 * Checks whether callback_func is not NULL,
 * and unless callback is blocked by callback_blocked being non-zero it calls the callback function with callback_data as parameter,
 * otherwise callback_pending is set to non-zero value.
 *
 * @return void
 *
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickGet_time
 */
void HWTIMER_SYS_SystickIsrAction(void)
{
    hwtimer_t *hwtimer = g_hwtimersSystick;

    /* Check if interrupt is enabled for this systick. Cancel spurious interrupt */
    if (!(SysTick_CTRL_TICKINT_Msk & SysTick->CTRL))
    {
        return;
    }

    if (NULL != hwtimer)
    {

        /* Following part of function is typically the same for all low level hwtimer drivers */
        hwtimer->ticks++;

        if (NULL != hwtimer->callbackFunc)
        {
            if (hwtimer->callbackBlocked)
            {
                hwtimer->callbackPending = 1U;
            }
            else
            {
                /* Run user function*/
                hwtimer->callbackFunc(hwtimer->callbackData);
            }
        }
    }
}


/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief This function initializes caller allocated structure according to given
 * numerical identifier of the timer.
 *
 * Called by hwtimer_init().
 * Initializes the hwtimer_t structure.
 *
 * @param hwtimer[in]   Returns initialized hwtimer structure handle.
 * @param systickId[in] Determines Systick modul( Always 0).
 * @param isrPrior[in]  Interrupt priority for systick
 * @param data[in]      Specific data. Not used in this timer.
 *
 * @return kHwtimerSuccess              Success.
 * @return kHwtimerInvalidInput         When systick_id does not exist(When systick_id is not zero).
 * @return kHwtimerRegisterHandlerError When registration of the interrupt service routine failed.
 *
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickGet_time
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickInit(hwtimer_t *hwtimer, uint32_t systickId, void *data)
{

    assert(NULL != hwtimer);
    /* We count only with one systick module inside core */
    if ( 1U <= systickId)
    {
        return kHwtimerInvalidInput;
    }

    /* Disable timer and interrupt. Set clock source to processor clock */
    /* But mostly The CLKSOURCE bit in SysTick Control and Status register is always set to select the core clock. */
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;

    /* Reset reload value register. A start value of 0 is possible, but has no effect */
    SysTick->LOAD = 0U;
    /* A write of any value to current value register clears the field to 0, and also clears the SYST_CSR COUNTFLAG bit to 0. */
    SysTick->VAL = 0U;

    /* Store hwtimer in global variable */
    g_hwtimersSystick = hwtimer;

    return kHwtimerSuccess;
}

/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Initialization of systick timer module
 *
 * Called by hwtimer_deinit. Disables the peripheral. Unregisters ISR.
 *
 * @param hwtimer[in] Pointer to hwtimer structure.
 *
 * @return kHwtimerSuccess              Success.
 * @return kHwtimerRegisterHandlerError When registration of the interrupt service routine failed.
 *
 * @see HWTIMER_SYS_SystickInit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickGet_time
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickDeinit(hwtimer_t *hwtimer)
{
    _hwtimer_error_code_t retval = kHwtimerSuccess;
    assert(NULL != hwtimer);

    /* Disable timer and interrupt */
    SysTick->CTRL = 0U;
    /* Reset reload value register. A start value of 0 is possible, but has no effect */
    SysTick->LOAD = 0U;
    /* A write of any value to current value register clears the field to 0, and also clears the SYST_CSR COUNTFLAG bit to 0. */
    SysTick->VAL = 0U;

    g_hwtimersSystick = NULL;

    return retval;
}

/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Sets up timer with divider settings closest to the requested period in microseconds.
 *
 * The function gets the value of the base frequency of the timer via clock manager and calculates required
 * divider ratio. If the required period is a large value, then the code will try to use a low-frequency
 * external clock if available.
 *
 * Called by hwtimer_set_freq() and hwtimer_set_period().
 * Fills in the divider (actual total divider) and modulo (sub-tick resolution) members of the hwtimer_t structure.
 *
 * @param hwtimer[in] Pointer to hwtimer structure.
 * @param period[in] Required period of timer in micro seconds.
 *
 * @return kHwtimerSuccess      Success.
 * @return kHwtimerInvalidInput Divider is equal too big.
 *
 * @see HWTIMER_SYS_SystickInit
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickGet_time
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickSetDiv(hwtimer_t *hwtimer, uint32_t period)
{
    assert(NULL != hwtimer);
    uint64_t divider;

#if FSL_FEATURE_SYSTICK_HAS_EXT_REF
    /* Set the clock source back to core freq */
    CLOCK_SYS_SetSystickSrc(kClockSystickSrcCore);
#endif
    /* Get Core clock frequency */
    hwtimer->clockFreq = CLOCK_SYS_GetSystickFreq();

    divider = (((uint64_t)hwtimer->clockFreq * period)) / 1000000U ;

    /* if divider is greater than 24b value we return an error */
    if ((divider - 1U) & ~SysTick_LOAD_RELOAD_Msk)
    {
#if FSL_FEATURE_SYSTICK_HAS_EXT_REF
        /* Check if we can use a slower clock source to get required period */
        CLOCK_SYS_SetSystickSrc(kClockSystickSrcExtRef);
        hwtimer->clockFreq = CLOCK_SYS_GetSystickFreq();
        divider = (((uint64_t)hwtimer->clockFreq * period)) / 1000000U ;

        if ((divider - 1U) & ~SysTick_LOAD_RELOAD_Msk)
        {
            return kHwtimerInvalidInput;
        }
#else
        return kHwtimerInvalidInput;
#endif
    }

    /*
     * A start value of 0 (divider == 1) is possible, but has no effect because the
     * SysTick interrupt and COUNTFLAG are activated when counting from 1 to 0.
     */
    if (divider == 1U)
    {
        divider = 2U; /* Set smallest possible value for divider. */
    }

    SysTick->LOAD = divider - 1U;

    /* Store in struct. */
    hwtimer->divider    = divider;
    hwtimer->modulo     = divider;

    return kHwtimerSuccess;
}


/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Start systick timer module
 *
 * This function enables the timer and leaves it running, timer is
 * periodically generating interrupts.
 *
 * @param hwtimer[in] Pointer to hwtimer structure.
 *
 * @return kHwtimerSuccess Success.
 *
 * @see HWTIMER_SYS_SystickInit
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickGet_time
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickStart(hwtimer_t *hwtimer)
{
    assert(NULL != hwtimer);

    /* A write of any value to current value register clears the field to 0, and also clears the SYST_CSR COUNTFLAG bit to 0. */
    SysTick->VAL = 0U;

    /* Run timer and enable interrupt */
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk;

    return kHwtimerSuccess;
}

/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Stop systick timer module.
 *
 * Disable timer and interrupt.
 *
 * @param hwtimer[in] Pointer to hwtimer structure.
 *
 * @return kHwtimerSuccess Success.
 *
 * @see HWTIMER_SYS_SystickInit
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickGet_time
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickStop(hwtimer_t *hwtimer)
{
    assert(NULL != hwtimer);

    /* Disable timer and interrupt */
    SysTick->CTRL = 0U;

    return kHwtimerSuccess;
}

/*!
 * @cond DOXYGEN_PRIVATE
 *
 * @brief Atomically captures current time into Hwtimer_time structure.
 *
 * Corrects/normalizes the values if necessary (interrupt pending, etc.)
 *
 * @param hwtimer[in] Pointer to hwtimer structure.
 * @param time[out]   Pointer to time structure. This value is filled with current value of the timer.
 *
 * @return kHwtimerSuccess Success.
 *
 * @warning This function calls _int_enable and _int_disable functions
 *
 * @see HWTIMER_SYS_SystickInit
 * @see HWTIMER_SYS_SystickDeinit
 * @see HWTIMER_SYS_SystickSet_div
 * @see HWTIMER_SYS_SystickStart
 * @see HWTIMER_SYS_SystickStop
 * @see HWTIMER_SYS_SystickIsrAction
 */
static _hwtimer_error_code_t HWTIMER_SYS_SystickGetTime(hwtimer_t *hwtimer, hwtimer_time_t *time)
{
    uint32_t tempVal;

    assert(NULL != hwtimer);
    assert(NULL != time);

    /* Enter critical section to avoid disabling interrupt from pit for very long time */
    OSA_EnterCritical(kCriticalDisableInt);
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

    time->ticks = hwtimer->ticks;
    tempVal = SysTick->VAL;
    /* Check systick pending interrupt flag */
    if (SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)
    {
        SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
        OSA_ExitCritical(kCriticalDisableInt);
        time->subTicks = hwtimer->modulo - 1U;
    }
    else
    {
        SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
        OSA_ExitCritical(kCriticalDisableInt);
        /* interrupt flag is set upon 1->0 transition, not upon reload - wrap around */
        time->subTicks = SysTick->LOAD - tempVal + 1U;
    }

    return kHwtimerSuccess;
}
 /*******************************************************************************
 * Code
 ******************************************************************************/
/*******************************************************************************
 * EOF
 ******************************************************************************/