ISSDK  1.7
IoT Sensing Software Development Kit
driver_systick.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file driver_systick.c
36  \brief Encapsulates the ARM sysTick counter, which is used for benchmarking
37 */
38 
39 #include "sensor_fusion.h"
40 #include "drivers.h"
41 
42 // SysTick register definitions based on CMSIS definitions for same
43 #define SYST_CSR SysTick->CTRL // SysTick Control & Status Register
44 #define SYST_RVR SysTick->LOAD // SysTick Reload Value Register
45 #define SYST_CVR SysTick->VAL // SysTick Current Value Register
46 
47 // ARM-core specific function that enables the ARM systick timer on Kinetis uCs.
48 // this is purely for algorithm benchmarking and can either be deleted or re-written for use on other uCs.
49 // the systick clock frequency for each uC are defined as CORE_SYSTICK_HZ in types.h.
50 
51 // the timer is 24 bit so allows measurement of intervals up to 2^24/CORE_SYSTICK_HZ secs=0.35s for a 48MHz uC.
53 {
54  SYST_CSR = 0x5u; // enable systick from internal clock
55  SYST_RVR = 0x00FFFFFFu; // set reload to maximum 24 bit value
56  return;
57 }
58 
59 // ARM-core specific function to store the current systick timer ticks for benchmarking
61 {
62  // store the 24 bit systick timer
63  *pstart = SYST_CVR & 0x00FFFFFF;
64 
65  return;
66 }
67 
68 // ARM-core specific function to compute the elapsed systick timer ticks for benchmarking
70 {
71  int32 elapsed_ticks;
72 
73  // subtract the stored start ticks and check for wraparound down through zero
74  elapsed_ticks = start_ticks - (SYST_CVR & 0x00FFFFFF);
75  if (elapsed_ticks < 0) elapsed_ticks += SYST_RVR;
76 
77  return elapsed_ticks;
78 }
79 
80 void ARM_systick_delay_ms(uint32 iSystemCoreClock, uint32 delay_ms)
81 {
82  int32 istart_ticks; // start ticks on entry
83  int32 ielapsed_ticks; // elapsed ticks
84  int16 i; // loop counter
85 
86  // loop for requested number of ms
87  for (i = 0; i < delay_ms; i++)
88  {
89  // loop until 1ms has elapsed
90  ARM_systick_start_ticks(&istart_ticks);
91  do
92  {
93  ielapsed_ticks = ARM_systick_elapsed_ticks(istart_ticks);
94  } while (ielapsed_ticks < iSystemCoreClock / 1000);
95  }
96 
97  return;
98 }
void ARM_systick_enable(void)
int32_t int32
Definition: sensor_fusion.h:67
uint32_t uint32
Definition: sensor_fusion.h:70
int32 ARM_systick_elapsed_ticks(int32 start_ticks)
The sensor_fusion.h file implements the top level programming interface.
Provides function prototypes for driver level interfaces.
#define SYST_RVR
#define SYST_CSR
#define SYST_CVR
void ARM_systick_delay_ms(uint32 iSystemCoreClock, uint32 delay_ms)
int16_t int16
Definition: sensor_fusion.h:66
void ARM_systick_start_ticks(int32 *pstart)